UNPKG

@real-estate/core

Version:

A simple library to maintain state in JavaScript

94 lines (93 loc) 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MultiWatcher = void 0; var StateIdCounterCls_1 = require("./StateIdCounterCls"); var RState = /** @class */ (function () { /** * Creates a new State that can be watched * @param initialValue Sets the initial value of the state * @param description An optional description of the state and what is does in the app */ function RState(initialValue) { var _this = this; this.watcherIdIncrement = 1; /** * Gets you the current state in correct data type * @returns The most updated state of type specified when state is created */ this.get = function () { return _this.stateContainer.data; }; /** * Sets the new state. This updates the state and calls all the watchers. * @param value sets the value of the state. This is of type specified on creation */ this.set = function (value) { _this.stateContainer.data = value; _this.stateContainer.watchers.forEach(function (watcher) { watcher.caller(value); }); }; /** * Sets new functions to be callbacks for when the state changes. It watches the state * @param callback Sets the callback function to be called when states changes. The callback function is also called immediately, to ensure updated UI * @param initCall Calls the function on creation in watch if true. Default false * @returns The watcher id */ this.watch = function (callback, initCall) { if (initCall === void 0) { initCall = false; } var watcherId = _this.getNewWatcherId(); _this.stateContainer.watchers.push({ caller: callback, id: watcherId }); if (initCall) { callback(_this.stateContainer.data); } return watcherId; }; /** * Delete a watcher with the id from THIS CURRENT STATE! NOT ANY OTHER STATE! * @param watcherId Watcher Id you can get when calling the watcher. Remember that watcher ids are only valid within the same state object */ this.deleteWatcher = function (watcherId) { var filtered = _this.stateContainer.watchers.filter(function (w) { return w.id !== watcherId; }); _this.stateContainer.watchers = filtered; }; this.getNewWatcherId = function () { var watcherId = _this.watcherIdIncrement; _this.watcherIdIncrement = watcherId + 1; return watcherId; }; var container = { data: initialValue, watchers: [], }; this.stateContainer = container; this.privateId = StateIdCounterCls_1.default.getInstance().getNewStateid(); } /** * Each State instance has it's own unique id * @returns Unique Id for each state object in the system */ RState.prototype.getStateId = function () { return this.privateId; }; return RState; }()); /** * Watches multiple states for changes * @param stateArr An array of all state objects to watch * @param callback Function to call if any of the states are changed * @param initCall Calls the function only once, on creation in watch if true. Default false */ function MultiWatcher(stateArr, callback, initCall) { if (initCall === void 0) { initCall = false; } stateArr.forEach(function (states) { states.watch(function () { callback(); }, false); }); if (initCall) { callback(); } } exports.MultiWatcher = MultiWatcher; exports.default = RState;