UNPKG

dreamstate

Version:

Store management library based on react context and observers

1 lines 3.57 kB
"use strict";var e=require("./lib.js"),t=require("react");function n(t,n){var r=!1;return{configurable:!0,get:function(){if(r)return t.value;var e=t.value.bind(this);return r=!0,Object.defineProperty(this,n,{configurable:!0,writable:!1,value:e}),r=!1,e},set:function(){throw new e.D(e.E.RESTRICTED_OPERATION,"Direct runtime modification of bound method is not allowed.")}}}function r(e){return function(t,n){return t&&n?(e(n,t.constructor),t):(t.finisher=function(n){e(t.key,n)},t)}}function o(){return{value:e.c()}}require("tslib"),require("shallow-equal");var i=function(){function e(e,t,n){this.value=e,this.isLoading=t,this.error=n}return e.prototype.asLoading=function(t){return new e(arguments.length>0?t:this.value,!0,null)},e.prototype.asFailed=function(t,n){return new e(arguments.length>1?n:this.value,!1,t)},e.prototype.asReady=function(t){return new e(arguments.length>0?t:this.value,!1,null)},e.prototype.asUpdated=function(t,n,r){return new e(t,arguments.length>1?n:this.isLoading,arguments.length>2?r:this.error)},e}();exports.ContextManager=e.C,exports.DreamstateError=e.D,Object.defineProperty(exports,"DreamstateErrorCode",{enumerable:!0,get:function(){return e.E}}),exports.createProvider=e.h,exports.createScope=e.c,exports.Bind=function(){return function(e,t,r){if(t&&r)return n(r,t);e.descriptor=n(e.descriptor,e.key)}},exports.OnQuery=function(t){if(!e.i(t))throw new e.D(e.E.INCORRECT_QUERY_TYPE,typeof t);return r((function(n,r){if(!(r.prototype instanceof e.C))throw new e.D(e.E.TARGET_CONTEXT_MANAGER_EXPECTED,"Only ContextManager extending classes methods can be decorated as query handlers.");e.Q.has(r)?e.Q.get(r).push([n,t]):e.Q.set(r,[[n,t]])}))},exports.OnSignal=function(t){if(Array.isArray(t)?0===t.length||t.some((function(t){return!e.a(t)})):!e.a(t))throw new e.D(e.E.INCORRECT_PARAMETER,"Unexpected signal type provided, expected symbol, string, number or array of it. Got: ".concat(typeof t,"."));return r((function(n,r){if(!(r.prototype instanceof e.C))throw new e.D(e.E.INCORRECT_PARAMETER,"Only ContextManager extending classes methods can be decorated as handlers.");e.S.has(r)?e.S.get(r).push([n,t]):e.S.set(r,[[n,t]])}))},exports.ScopeProvider=function(n){var r=t.useState(o);return t.createElement(e.b.Provider,r[0],n.children)},exports.createActions=function(t){if(e.d(t))return new e.A(t);throw new e.D(e.E.INCORRECT_PARAMETER,"Actions store should be initialized with an object, got '".concat(typeof t,"' instead."))},exports.createComputed=function(t,n){if(e.e(t)&&(e.f(n)||e.e(n)))return new e.g(t,n);throw new e.D(e.E.INCORRECT_PARAMETER,"Computed value should be initialized with functional selector and optional memo function.")},exports.createLoadable=function(e,t,n){return void 0===e&&(e=null),void 0===t&&(t=!1),void 0===n&&(n=null),new i(e,t,n)},exports.createNested=function(t){if(e.d(t))return Object.assign(new e.N,t);throw new e.D(e.E.INCORRECT_PARAMETER,"Nested stores should be initialized with an object, got '".concat(typeof t,"' instead."))},exports.useManager=function(n,r){return r?function(n,r){var o=t.useContext(e.b),i=t.useState((function(){return o.INTERNAL.REGISTRY.CONTEXT_STATES_REGISTRY.get(n)}));return t.useEffect((function(){var e=i[0],t=i[1],u=o.INTERNAL.REGISTRY.CONTEXT_STATES_REGISTRY.get(n)||null,a=u?r(u):null;return e!==u&&t(u),o.INTERNAL.subscribeToManager(n,(function(e){if(!a)return a=r(e),t(e);for(var n=r(e),o=0;o<n.length;o++)if(a[o]!==n[o])return a=n,t(e)}))}),[n,o.INTERNAL]),i[0]}(n,r):t.useContext(n.REACT_CONTEXT)},exports.useScope=function(){return t.useContext(e.b)};