easy-peasy-classes
Version:
easy-peasy enhanced with classes, full typescript type safety, nested store stuctures and model inheritance
3 lines (2 loc) • 7.67 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("easy-peasy"),o=require("lodash");function n(){return(n=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(t[n]=o[n])}return t}).apply(this,arguments)}function r(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,(Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}var i=function(){function t(){this.def={},this.properties=[],this.children=[],this.actions=[],this.computed=[],this.listeners=[],this.thunks=[],this.instances=[],this._isHoldingChildren=!1}var r=t.prototype;return r.addModelMetadata=function(t){this.def={proto:t.ctor.prototype},this.instances.push({proto:t.ctor.prototype,instance:new t.ctor})},r.addPropertyMetadata=function(t){this.properties.push(t)},r.addChildMetadata=function(t){this.children.push(t)},r.addActionMetadata=function(t){this.actions.push(t)},r.addComputedMetadata=function(t){this.computed.push(t)},r.addListenerMetadata=function(t){this.listeners.push(t)},r.addThunkMetadata=function(t){this.thunks.push(t)},r.buildModel=function(){var t=this,r={},i=this.def.proto;return this.properties.filter((function(t){return t.proto===i})).forEach((function(e){var o=t.instances.find((function(t){return t.proto===e.proto}));r[e.fieldName]=null==o?void 0:o.instance[e.fieldName]})),this.children.filter((function(t){return t.proto===i})).forEach((function(e){t._isHoldingChildren=!0;var o=t.instances.find((function(t){return t.proto===e.proto}));r[e.fieldName]=null==o?void 0:o.instance[e.fieldName]})),this.actions.filter((function(t){return t.proto===i})).forEach((function(t){r[t.fieldName]=e.action((function(e,o){try{t.handler.call(e,o)}catch(t){if(t instanceof TypeError&&"this.getStoreActions is not a function"===t.message)throw new Error("this.getStoreActions() can only be used in a Thunk.\nIf you used an Action, you should probably add a separate Thunk to do your business logic and keep your actions granular.");if(t instanceof TypeError&&"this.getStoreState is not a function"===t.message)throw new Error("this.getStoreState() can only be used in a Thunk.\nIf you used an Action, you should probably add a separate Thunk to do your business logic and keep your actions granular.");if(t instanceof TypeError&&"this.getStoreStateClone is not a function"===t.message)throw new Error("this.getStoreStateClone() can only be used in a Thunk.\nIf you used an Action, you should probably add a separate Thunk to do your business logic and keep your actions granular.");throw t instanceof TypeError&&"this.getStateClone is not a function"===t.message?new Error("this.getStateClone() can only be used in a Thunk.\nIf you used an Action, you should probably add a separate Thunk to do your business logic and keep your actions granular."):t}}))})),this.computed.filter((function(t){return t.proto===i})).forEach((function(t){r[t.fieldName]=e.computed((function(e){return t.handler.call(e)}))})),this.listeners.filter((function(t){return t.proto===i})).forEach((function(t){r[t.fieldName]=e.thunkOn(t.actionFn,(function(e,r,i){var a=i.getState,u=i.getStoreActions,s=i.getStoreState;return t.handler.call(n({},o.merge({},e,a()),{getStoreActions:u,getStoreState:s,getStateClone:function(){var t=a();return o.cloneDeep(t)},getStoreStateClone:function(t){var e=t(s());return o.cloneDeep(e)}}),r)}))})),this.thunks.filter((function(t){return t.proto===i})).forEach((function(t){r[t.fieldName]=e.thunk((function(e,r,i){var a=i.getState,u=i.getStoreActions,s=i.getStoreState;return t.handler.call(n({},o.merge({},e,a()),{getStoreActions:u,getStoreState:s,getStateClone:function(){var t=a();return o.cloneDeep(t)},getStoreStateClone:function(t){var e=t(s());return o.cloneDeep(e)}}),r)}))})),r},t}(),a=new(function(){function t(){this.pool=[],this.currentIndex=0,this.pool[0]=new i}var e=t.prototype;return e.next=function(){this.currentIndex++,this.pool[this.currentIndex]=new i,console.log("waiting for new decorators (storageIndex : "+this.currentIndex+")")},e.getCurrentIndex=function(){return this.currentIndex},e.getCurrentStorage=function(){return this.pool[this.currentIndex]},e.getStorageByIndex=function(t){return this.pool[t]},t}()),u=function(t){var e=a.getCurrentStorage();e.addModelMetadata({ctor:t}),Object.defineProperty(t,"obj",{get:function(){return n=e.buildModel(),r=(null===(o=Object.getPrototypeOf(t))||void 0===o?void 0:o.obj)||{},(i=Object.assign({},r,n))._isHoldingChildren=e._isHoldingChildren,i;var o,n,r,i}}),console.log("@Model "+t.name),a.next()},s=function(t,e){console.log("@Property ",t.constructor.name+"."+e.toString()),a.getCurrentStorage().addPropertyMetadata({proto:t,fieldName:e})},c=Symbol("modelSym"),d=function(){function t(){}var e;return(e=[{key:"obj",get:function(){return{}}}])&&function(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}(t,e),t}();exports.ModelBase=function(t){function e(){var e;return(e=t.apply(this,arguments)||this)._isHoldingChildren=!1,e}return r(e,t),e}(d),t.__decorate([s],exports.ModelBase.prototype,"_isHoldingChildren",void 0),exports.ModelBase=t.__decorate([u],exports.ModelBase);var l=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).getStateClone=void 0,e.getStoreStateClone=void 0,e.getStoreActions=void 0,e.getStoreState=void 0,e}return r(e,t),e}(d),p=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).getStateClone=void 0,e.getStoreStateClone=void 0,e.getStoreActions=void 0,e.getStoreState=void 0,e}return r(e,t),e}(exports.ModelBase);exports.Action=function(t,e,o){console.log("@Action ",t.constructor.name+"."+e.toString()),a.getCurrentStorage().addActionMetadata({proto:t,fieldName:e,handler:o.value})},exports.Child=function(t,e){console.log("@Child ",t.constructor.name+"."+e.toString()),a.getCurrentStorage().addChildMetadata({proto:t,fieldName:e})},exports.Computed=function(t,e,o){console.log("@Computed ",t.constructor.name+"."+e.toString()),a.getCurrentStorage().addComputedMetadata({proto:t,fieldName:e,handler:o.value||o.get})},exports.Listener=function(t){return function(e,o,n){console.log("@Listener ",e.constructor.name+"."+o.toString()),a.getCurrentStorage().addListenerMetadata({proto:e,fieldName:o,handler:n.value,actionFn:t})}},exports.Model=u,exports.ModelBasePure=d,exports.ModelBasePureTyped=l,exports.ModelBaseTyped=p,exports.Property=s,exports.Thunk=function(t,e,o){console.log("@Thunk ",t.constructor.name+"."+e.toString()),a.getCurrentStorage().addThunkMetadata({proto:t,fieldName:e,handler:o.value})},exports.createLocalHooksFactory=function(t){return function(e){return{useLocalActions:function(o){return void 0===o&&(o=function(t){return t}),t.useStoreActions((function(t){return o(e(t))}))},useLocalState:function(o){return void 0===o&&(o=function(t){return t}),t.useStoreState((function(t){return o(e(t))}))}}}},exports.createStore=function(t,o){return e.createStore(t,o)},exports.createTypedHooks=function(){var t=e.createTypedHooks();return{useStoreActions:t.useStoreActions,useStoreState:function(e){var o=t.useStoreState(e);if(null!=o&&o._isHoldingChildren)throw new Error("The selected state located at "+e.toString()+" contains one or more @Child() properties and it shouldn't be accessed. Its properties should be accessed individually.");return o},useStoreDispatch:t.useStoreDispatch}},exports.getMapperFactory=function(){return{useMapper:function(t){return t}}},exports.modelSym=c;
//# sourceMappingURL=easy-peasy-classes.cjs.production.min.js.map