UNPKG

simple-object-state

Version:

An experiemental object oriented state mangment lib

66 lines 2.19 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var SimpleObjectState_1 = require("./SimpleObjectState"); var ShallowEqual_1 = require("./ShallowEqual"); var Store = /** @class */ (function () { function Store() { this.state = {}; this.actions = {}; } Store.prototype.destructor = function () { return; }; Store.prototype.getActions = function () { return this.actions; }; Store.prototype.getState = function () { return this.state; }; Store.prototype.callAction = function (action, args) { var actions = this.actions; if (this.actions && action && actions[action]) { var func = actions[action]; func.apply(void 0, args); this.storeDidCallAction(action); } }; Store.prototype.storeDidCallAction = function (action) { return; }; Store.prototype.storeDidUpdate = function (prevState) { return; }; Store.prototype.shouldStoreUpdate = function (nextState) { if (nextState) { var state = this.state; var isEqual = ShallowEqual_1.shallowEqual({ nextState: nextState, state: state }); return !isEqual; } return false; }; Store.prototype.setState = function (nextState) { var prevState = this.state; var combinedState = __assign(__assign({}, this.state), nextState); // forgo state update if they are equal if (!this.shouldStoreUpdate(combinedState)) { return; } this.state = combinedState; SimpleObjectState_1.SimpleObjectState.onSetState(this); this.storeDidUpdate(prevState); }; return Store; }()); exports.Store = Store; //# sourceMappingURL=Store.js.map