UNPKG

dreamstate

Version:

Store management library based on react context and observers

1 lines 12.8 kB
"use strict";var e,t=require("tslib"),n=require("react"),r=require("shallow-equal");exports.E=void 0,(e=exports.E||(exports.E={})).UNEXPECTED_ERROR="UNEXPECTED_ERROR",e.RESTRICTED_OPERATION="RESTRICTED_OPERATION",e.INSTANCE_DISPOSED_LIFECYCLE="INSTANCE_DISPOSED_LIFECYCLE",e.INSTANCE_DISPOSED_SCOPE="INSTANCE_DISPOSED_SCOPE",e.OUT_OF_SCOPE="OUT_OF_SCOPE",e.INCORRECT_PARAMETER="INCORRECT_PARAMETER",e.INCORRECT_SIGNAL_LISTENER="INCORRECT_SIGNAL_LISTENER",e.INCORRECT_SIGNAL_TYPE="INCORRECT_SIGNAL_TYPE",e.INCORRECT_QUERY_PROVIDER="INCORRECT_QUERY_PROVIDER",e.INCORRECT_QUERY_TYPE="INCORRECT_QUERY_TYPE",e.TARGET_CONTEXT_MANAGER_EXPECTED="TARGET_CONTEXT_MANAGER_EXPECTED";var o=function(e){function n(t,n){void 0===t&&(t=exports.E.UNEXPECTED_ERROR);var r=e.call(this)||this;return r.name="DreamstateError",r.code=t,r.message=function(e,t){switch(e){case exports.E.INSTANCE_DISPOSED_LIFECYCLE:return"Disposed manager instances are not supposed to access lifecycle.";case exports.E.INSTANCE_DISPOSED_SCOPE:return"Disposed manager instances are not supposed to access scope.";case exports.E.OUT_OF_SCOPE:return"Instance is out of scope, make sure it is created or mocked correctly.";case exports.E.INCORRECT_PARAMETER:return"Incorrect parameter supplied.".concat(t?" ".concat(t):"");case exports.E.INCORRECT_SIGNAL_TYPE:return"Unexpected signal type provided, expected symbol, string or number.".concat(t?" Got: '".concat(t,"' instead."):"");case exports.E.INCORRECT_SIGNAL_LISTENER:return"Signal listener must be function, '".concat(t,"' provided.");case exports.E.INCORRECT_QUERY_TYPE:return"Unexpected query type provided, expected symbol, string or number. Got: '".concat(t,"' instead.");case exports.E.INCORRECT_QUERY_PROVIDER:return"Query provider must be factory function, '".concat(t,"' provided.");case exports.E.TARGET_CONTEXT_MANAGER_EXPECTED:return"Cannot perform action, class extending ContextManager is expected.".concat(t?" ".concat(t):"");case exports.E.RESTRICTED_OPERATION:return"Operation is restricted.".concat(t?" ".concat(t):"");case exports.E.UNEXPECTED_ERROR:default:return"Unexpected dreamstate error.".concat(t?" ".concat(t):"")}}(t,n),r}return t.__extends(n,e),n}(Error);function i(){throw new o(exports.E.INSTANCE_DISPOSED_SCOPE)}function a(){throw new o(exports.E.OUT_OF_SCOPE)}var E=Symbol("SIGNAL_METADATA"),s=Symbol("QUERY_METADATA"),c=Symbol("SIGNALING_HANDLER"),u=Symbol("SCOPE"),R=new WeakMap,f=new WeakMap,T=new WeakMap;var _=function(e){Object.assign(this,e)},S=function(){function e(e,t){this.__selector__=e,this.__memo__=t}return e.prototype.process=function(e){if(this.__memo__){var t=this.__memo__(e);this.__diff__&&!this.__diff__.some((function(e,n){return e!==t[n]}))||(this.__diff__=t,Object.assign(this,this.__selector__(e)))}else Object.assign(this,this.__selector__(e))},e}(),l=function(){function e(){}return e.prototype.asMerged=function(t){return void 0===t&&(t={}),Object.assign(new e,this,t)},e}();function p(e){return"string"==typeof e}function C(e){return"number"==typeof e}function I(e){return"object"==typeof e&&null!==e}function N(e){return"symbol"==typeof e}function d(e){return"function"==typeof e}function O(e){return p(e)||C(e)||N(e)}function y(e){return p(e)||C(e)||N(e)}function h(e){for(var t in e){var n=e[t];n instanceof S&&n.process(e)}return e}var v=function(){function e(e){this.IS_DISPOSED=!1,this.context={},h(this.context)}return e.getDefaultContext=function(){return null},Object.defineProperty(e,"REACT_CONTEXT",{get:function(){if(this===e)throw new o(exports.E.RESTRICTED_OPERATION,"Direct references to ContextManager statics forbidden.");return function(e){var t=R.get(e);if(t)return t;var r=n.createContext(e.getDefaultContext());return R.set(e,r),r}(this)},enumerable:!1,configurable:!0}),e.prototype.onProvisionStarted=function(){},e.prototype.onProvisionEnded=function(){},e.prototype.getScope=function(){if(this[u])return this[u];a()},e.prototype.forceUpdate=function(){this.context=h(Object.assign({},this.context)),this[u]&&this[u].INTERNAL.notifyObservers(this)},e.prototype.setContext=function(e){var t=Object.assign({},this.context,d(e)?e(this.context):e);this.context=h(t),this[u]&&function(e,t){if(!I(t))throw new o(exports.E.INCORRECT_PARAMETER,"Context should be non-nullable object, supplied '".concat(typeof t,"' type instead."));return!e||Object.keys(t).some((function(n){return!(t[n]instanceof S||t[n]instanceof _||(t[n]instanceof l?r.shallowEqualObjects(t[n],e[n]):t[n]===e[n]))}))}(this[u].INTERNAL.REGISTRY.CONTEXT_STATES_REGISTRY.get(this.constructor),t)&&this[u].INTERNAL.notifyObservers(this)},e.prototype.emitSignal=function(e){if(this[u])return this[u].emitSignal(e,this.constructor);a()},e.prototype.queryDataAsync=function(e){if(this[u])return this[u].queryDataAsync(e);a()},e.prototype.queryDataSync=function(e){if(this[u])return this[u].queryDataSync(e);a()},e}();function A(){}function g(e,t,n){return new Promise((function(r,o){try{var i=Date.now(),a=e(t);return a instanceof Promise?a.then((function(o){r({answerer:n||e,type:t.type,data:o,timestamp:i})})).catch(o):r({answerer:n||e,type:t.type,data:a,timestamp:i})}catch(e){o(e)}}))}function x(e,t,n){return{answerer:n||e,type:t.type,data:e(t),timestamp:Date.now()}}function P(e,t,n){var r=n.QUERY_PROVIDERS_REGISTRY;if(!d(t))throw new o(exports.E.INCORRECT_QUERY_PROVIDER,typeof t);if(!O(e))throw new o(exports.E.INCORRECT_QUERY_TYPE,typeof e);if(r.has(e)){var i=r.get(e).filter((function(e){return e!==t}));i.length?r.set(e,i):r.delete(e)}}function w(){return{QUERY_PROVIDERS_REGISTRY:new Map,SIGNAL_LISTENERS_REGISTRY:new Set,CONTEXT_INSTANCES_REGISTRY:new Map,CONTEXT_STATES_REGISTRY:new Map,CONTEXT_SERVICES_REFERENCES:new Map,CONTEXT_OBSERVERS_REGISTRY:new Map,CONTEXT_SUBSCRIBERS_REGISTRY:new Map}}function D(e){var n,r;try{for(var o=t.__values(this[E]),i=o.next();!i.done;i=o.next()){var a=t.__read(i.value,2),s=a[0],c=a[1];(Array.isArray(c)?c.includes(e.type):e.type===c)&&this[s](e)}}catch(e){n={error:e}}finally{try{i&&!i.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}}function m(){return this.canceled=!0,this}function b(e,t){if(e.prototype instanceof v){for(var n=[],r=e;r!==v;)n.push(t.get(r)),r=Object.getPrototypeOf(r);return n.reduce((function(e,t){return t?e.concat(t):e}),[])}throw new o(exports.E.INCORRECT_PARAMETER,"Failed to collect metadata of class that is not extending ContextManager.")}var Y=n.createContext(null);function G(e,t,r,o){return void 0===e&&(e=null),void 0===o&&(o=0),o>=t.length?e:n.createElement(t[o].REACT_CONTEXT.Provider,{value:r.get(t[o])},G(e,t,r,o+1))}function M(){return{}}function L(){return n.useReducer(M,null)[1]}function U(e){return function(t){var r=function(e,t){var r=n.useContext(Y),o=L();return n.useMemo((function(){for(var n=0;n<e.length;n++)r.INTERNAL.registerManager(e[n],t)}),e),n.useEffect((function(){for(var n=!1,i=e.length-1;i>=0;i--){var a=r.INTERNAL.registerManager(e[i],t);r.INTERNAL.addServiceObserver(e[i],o),n=a||n}return n&&o(),function(){for(var t=0;t<e.length;t++)r.INTERNAL.removeServiceObserver(e[t],o)}}),e),r.INTERNAL.REGISTRY.CONTEXT_STATES_REGISTRY}(e,t.initialState);return G(t.children,e,r)}}function X(e){var t=e.children,r=e.initialState,o=e.dependencies,i=e.ManagerClass,a=e.scope,E=L();return n.useMemo((function(){a.INTERNAL.registerManager(i,r)}),o),n.useEffect((function(){var e=a.INTERNAL.registerManager(i,r);return a.INTERNAL.addServiceObserver(i,E),e&&E(),function(){a.INTERNAL.removeServiceObserver(i,E)}}),o),n.createElement(i.REACT_CONTEXT.Provider,{value:a.INTERNAL.REGISTRY.CONTEXT_STATES_REGISTRY.get(i)},t)}function Q(e,t,r,o){return void 0===o&&(o=0),o>=e.length?t.children:n.createElement(X,{ManagerClass:e[o],initialState:t.initialState,dependencies:e,scope:r},Q(e,t,r,o+1))}function j(e){return function(t){var r=n.useContext(Y);return Q(e,t,r)}}exports.A=_,exports.C=v,exports.D=o,exports.N=l,exports.Q=T,exports.S=f,exports.a=y,exports.b=Y,exports.c=function(e){void 0===e&&(e=w());var n=e.SIGNAL_LISTENERS_REGISTRY,r=e.CONTEXT_STATES_REGISTRY,a=e.CONTEXT_OBSERVERS_REGISTRY;e.QUERY_PROVIDERS_REGISTRY;var R=e.CONTEXT_SERVICES_REFERENCES,_=e.CONTEXT_INSTANCES_REGISTRY,S=e.CONTEXT_SUBSCRIBERS_REGISTRY,l={INTERNAL:{REGISTRY:e,registerManager:function(e,t,o){if(_.has(e))return!1;var i=new e(t);return o&&Object.assign(i.context,o),h(i.context),i[u]=l,i[E]=b(e,f),i[s]=b(e,T),i[c]=D.bind(i),r.set(e,i.context),R.set(e,0),a.set(e,new Set),n.add(i[c]),_.set(e,i),S.has(e)?S.get(e).forEach((function(e){e(i.context)})):S.set(e,new Set),!0},unRegisterManager:function(e){if(_.has(e)){var t=_.get(e);return t[u]=null,t.setContext=A,t.forceUpdate=A,t.emitSignal=i,t.queryDataSync=i,t.queryDataAsync=i,t.IS_DISPOSED=!0,n.delete(t[c]),_.delete(e),r.delete(e),!0}return _.delete(e),r.delete(e),!1},addServiceObserver:function(e,t,n){void 0===n&&(n=R.get(e)+1),a.get(e).add(t),R.set(e,n),1===n&&_.get(e).onProvisionStarted()},removeServiceObserver:function(e,t,n){void 0===n&&(n=R.get(e)-1),a.get(e).delete(t),R.set(e,n),0===n&&(_.get(e).onProvisionEnded(),this.unRegisterManager(e))},notifyObservers:function(e){var t=e.context;r.set(e.constructor,t),a.get(e.constructor).forEach((function(e){e()})),S.get(e.constructor).forEach((function(e){e(t)}))},subscribeToManager:function(e,t){if(!(e.prototype instanceof v))throw new o(exports.E.TARGET_CONTEXT_MANAGER_EXPECTED,"Cannot subscribe to '".concat(null==e?void 0:e.name,"'."));return S.get(e).add(t),function(){S.get(e).delete(t)}},unsubscribeFromManager:function(t,n){if(!(t.prototype instanceof v))throw new o(exports.E.TARGET_CONTEXT_MANAGER_EXPECTED,"Cannot unsubscribe from '".concat(null==t?void 0:t.name,"'."));e.CONTEXT_SUBSCRIBERS_REGISTRY.get(t).delete(n)}},getContextOf:function(e){var t=r.get(e);return t?Object.assign({},t):null},getInstanceOf:function(e){return _.get(e)||null},emitSignal:function(t,n){return void 0===n&&(n=null),function(e,t,n){if(void 0===t&&(t=null),!e||!y(e.type))throw new o(exports.E.INCORRECT_SIGNAL_TYPE);var r={type:e.type,data:e.data,emitter:t,timestamp:Date.now(),cancel:m};return n.SIGNAL_LISTENERS_REGISTRY.forEach((function(t){try{r.canceled||t(r)}catch(t){console.error("[DS]","Failed to proceed emitted signal (".concat(String(e.type),"):"),t)}})),r}(t,n,e)},subscribeToSignals:function(e){if(!d(e))throw new o(exports.E.INCORRECT_SIGNAL_LISTENER,typeof e);return n.add(e),function(){n.delete(e)}},unsubscribeFromSignals:function(e){n.delete(e)},registerQueryProvider:function(t,n){return function(e,t,n){if("function"!=typeof t)throw new o(exports.E.INCORRECT_QUERY_PROVIDER,typeof t);if(!O(e))throw new o(exports.E.INCORRECT_QUERY_TYPE,typeof e);if(n.QUERY_PROVIDERS_REGISTRY.has(e)){var r=n.QUERY_PROVIDERS_REGISTRY.get(e);r.includes(t)||r.unshift(t)}else n.QUERY_PROVIDERS_REGISTRY.set(e,[t]);return function(){P(e,t,n)}}(t,n,e)},unRegisterQueryProvider:function(t,n){return P(t,n,e)},queryDataSync:function(n){return function(e,n){var r,i,a,E,c=n.CONTEXT_INSTANCES_REGISTRY,u=n.QUERY_PROVIDERS_REGISTRY;if(!e||!e.type)throw new o(exports.E.INCORRECT_PARAMETER,"Query must be an object with declared type or array of objects with type.");try{for(var R=t.__values(c.values()),f=R.next();!f.done;f=R.next()){var T=f.value;try{for(var _=(a=void 0,t.__values(T[s])),S=_.next();!S.done;S=_.next()){var l=S.value;if(e.type===l[1])return x(T[l[0]].bind(T),e,T.constructor)}}catch(e){a={error:e}}finally{try{S&&!S.done&&(E=_.return)&&E.call(_)}finally{if(a)throw a.error}}}}catch(e){r={error:e}}finally{try{f&&!f.done&&(i=R.return)&&i.call(R)}finally{if(r)throw r.error}}return u.has(e.type)?x(u.get(e.type)[0],e,null):null}(n,e)},queryDataAsync:function(n){return function(e,n){var r,i,a,E,c=n.CONTEXT_INSTANCES_REGISTRY,u=n.QUERY_PROVIDERS_REGISTRY;if(!e||!e.type)throw new o(exports.E.INCORRECT_PARAMETER,"Query must be an object with declared type or array of objects with type.");try{for(var R=t.__values(c.values()),f=R.next();!f.done;f=R.next()){var T=f.value;try{for(var _=(a=void 0,t.__values(T[s])),S=_.next();!S.done;S=_.next()){var l=S.value;if(e.type===l[1])return g(T[l[0]].bind(T),e,T.constructor)}}catch(e){a={error:e}}finally{try{S&&!S.done&&(E=_.return)&&E.call(_)}finally{if(a)throw a.error}}}}catch(e){r={error:e}}finally{try{f&&!f.done&&(i=R.return)&&i.call(R)}finally{if(r)throw r.error}}return u.has(e.type)?g(u.get(e.type)[0],e,null):Promise.resolve(null)}(n,e)}};return l},exports.d=I,exports.e=d,exports.f=function(e){return void 0===e},exports.g=S,exports.h=function(e,t){if(void 0===t&&(t={}),!Array.isArray(e)||!e.length)throw new o(exports.E.INCORRECT_PARAMETER,"Only array of context managers is acceptable.");for(var n=0;n<e.length;n++)if(!(e[n]&&e[n].prototype instanceof v))throw new o(exports.E.TARGET_CONTEXT_MANAGER_EXPECTED,"'".concat(String(e[n]),"' is in sources array."));return t.isCombined?U(e):j(e)},exports.i=O,exports.j=w;