simple-object-state
Version:
An experiemental object oriented state mangment lib
1 lines • 6.78 kB
JavaScript
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var n in r)("object"==typeof exports?exports:t)[n]=r[n]}}(window,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=1)}([function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(2);function o(t){return t.name}function i(t){return t.constructor.name}var s=new(function(){function t(){var e=this;if(this.Stores={},this.destroyStore=function(t){var r=e.getWrapper(t);r?r.destructor():console.warn("Store ",t," was not available for creating!")},this.getState=function(t){var r=e.getStore(t);if(r)return r.getState()},this.createStore=function(t){var r=e.getWrapper(t);r?r.create():console.warn("Store ",t," was not available for creating!")},this.callAction=function(t,r){for(var n=[],o=2;o<arguments.length;o++)n[o-2]=arguments[o];var i=e.getWrapper(t);i?i.callAction(r,n):console.warn("Store ",t," was not available for creating!")},this.getClass=function(t){var r=e.getWrapper(t);if(r)return r.Class},this.getInitialState=function(t){var r=e.getStore(t);if(r)return r.InitialState},this.register=function(t){return e.addStoreByClass(t)},this.unregister=function(t){e.Stores[o(t)]&&(e.Stores[o(t)].destructor(),delete e.Stores[o(t)])},this.subscribe=function(t,r){var n=e.getWrapper(t);n?n.subscribe(r):console.warn("Store ",t," was not available for subscribe!")},this.unsubscribe=function(t,r){var n=e.getWrapper(t);n?n.unsubscribe(r):console.warn("Store ",t," was not available for unsubscribe!")},this.onSetState=function(t){e.debug&&(console.groupCollapsed("SimpleObjectState State Update - "+t.constructor.name),console.log("Store -",t),console.log("New State -",t.getState()),console.groupCollapsed("Stack Trace"),console.trace(),console.groupEnd(),console.groupEnd()),e.Stores[i(t)].onSetState()},this.getStore=function(t){var r=e.getWrapper(t);if(r)return r.getInstance()},this.getString=function(t){return function(t){return"string"==typeof t}(t)?t:function(t){return"function"==typeof t}(t)?o(t):function(t){return"object"==typeof t&&t.constructor}(t)?i(t):void 0},this.getWrapper=function(t){var r=e.getString(t);if(r)return e.Stores[r];console.warn("Store has not been registered")},this.debug=!1,window.SimpleObjectState)return t.Instance=window.SimpleObjectState,t.Instance;t.Instance||(t.Instance=this,window.SimpleObjectState=this)}return t.prototype.setDebug=function(t){this.debug=t},t.prototype.addStoreByClass=function(t){return this.Stores[o(t)]||(this.Stores[o(t)]=new n.SimpleObjectStateStoreWrapper(t)),this.Stores[o(t)]},t}());e.SimpleObjectState=s},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(0),o=r(3);e.Store=o.Store,e.subscribe=n.SimpleObjectState.subscribe,e.unsubscribe=n.SimpleObjectState.unsubscribe,e.register=n.SimpleObjectState.register,e.unregister=n.SimpleObjectState.unregister,e.getInitialState=n.SimpleObjectState.getInitialState,e.callAction=n.SimpleObjectState.callAction,e.createStore=n.SimpleObjectState.createStore,e.destroyStore=n.SimpleObjectState.destroyStore,e.getState=n.SimpleObjectState.getState},function(t,e,r){"use strict";var n=this&&this.__spreadArrays||function(){for(var t=0,e=0,r=arguments.length;e<r;e++)t+=arguments[e].length;var n=Array(t),o=0;for(e=0;e<r;e++)for(var i=arguments[e],s=0,a=i.length;s<a;s++,o++)n[o]=i[s];return n};Object.defineProperty(e,"__esModule",{value:!0});var o=function(){function t(t){this.Class=t,this.Listeners=[],this.isCreatedThroughSub=!1}return t.prototype.destructor=function(){this.Instance&&(this.Instance.destructor(),this.Instance=void 0,this.Listeners=[],this.isCreatedThroughSub=!1)},t.prototype.create=function(){return this.Instance||(this.Instance=new this.Class),this.isCreatedThroughSub=!1,this.Instance},t.prototype.getInstance=function(){return this.Instance},t.prototype.callAction=function(t,e){this.Instance&&this.Instance.callAction(t,e)},t.prototype.onSetState=function(){var t=this;this.Instance&&this.Listeners.forEach((function(e){e?e(t.getState()):t.unsubscribe(e)}))},t.prototype.getState=function(){var t=this.getInstance();return t?t.getState():(console.warn("no instance setup yet!"),{})},t.prototype.subscribe=function(t){this.Listeners.push(t);var e=Boolean(this.Instance);this.create(),e||(this.isCreatedThroughSub=!0)},t.prototype.unsubscribe=function(t){if(this.Instance){for(var e=n(this.Listeners),r=[],o=e.pop();o&&o!==t;)r.push(o),o=e.pop();r=n(r,e),this.Listeners=r,this.isCreatedThroughSub&&!this.Listeners.length&&this.destructor()}},t}();e.SimpleObjectStateStoreWrapper=o},function(t,e,r){"use strict";var n=this&&this.__assign||function(){return(n=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0});var o=r(0),i=r(4),s=function(){function t(){this.state={},this.actions={}}return t.prototype.destructor=function(){},t.prototype.getActions=function(){return this.actions},t.prototype.getState=function(){return this.state},t.prototype.callAction=function(t,e){var r=this.actions;this.actions&&t&&r[t]&&(r[t].apply(void 0,e),this.storeDidCallAction(t))},t.prototype.storeDidCallAction=function(t){},t.prototype.storeDidUpdate=function(t){},t.prototype.shouldStoreUpdate=function(t){if(t){var e=this.state;return!i.shallowEqual({nextState:t,state:e})}return!1},t.prototype.setState=function(t){var e=this.state,r=n(n({},this.state),t);this.shouldStoreUpdate(r)&&(this.state=r,o.SimpleObjectState.onSetState(this),this.storeDidUpdate(e))},t}();e.Store=s},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.shallowEqual=function(t){var e=t.nextState,r=t.state;for(var n in e)if(e[n]!==r[n])return!1;return!0}}])}));