UNPKG

event-storm

Version:
2 lines (1 loc) 14.1 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const t={},e=({options:e={},...r})=>({lastState:"lastState"in r?r.lastState:t,options:e,subscribers:[]}),r=()=>{},n=t=>"function"==typeof t,o=t=>Array.isArray(t),i=t=>void 0===t,a=t=>"object"==typeof t&&t,u={},c=t=>u[t],s=(t,r,n)=>!(t=>!!u[t])(t)&&((t,r,n)=>(u[t]=e({lastState:r,options:n}),c(t)))(t,r,n),f=async(t,e,{force:r,...o})=>{const i=c(t),a=n(e)?e(i.lastState):e,u=(s=a)&&n(s.then)?await a:a;var s;const{lastState:f}=i;i.lastState=u;const l=o.fireDuplicates||i.options.fireDuplicates;i.subscribers.forEach((({callback:t})=>l||r||u!==f?t(u,o):void 0))};let l=0;function p(t){for(var e=arguments.length,r=Array(e>1?e-1:0),n=1;n<e;n++)r[n-1]=arguments[n];if("production"!==process.env.NODE_ENV){var o=Z[t],i=o?"function"==typeof o?o.apply(null,r):o:"unknown error nr: "+t;throw Error("[Immer] "+i)}throw Error("[Immer] minified error nr: "+t+(r.length?" "+r.map((function(t){return"'"+t+"'"})).join(","):"")+". Find the full error at: https://bit.ly/3cXEKWf")}function d(t){return!!t&&!!t[Y]}function b(t){return!!t&&(function(t){if(!t||"object"!=typeof t)return!1;var e=Object.getPrototypeOf(t);if(null===e)return!0;var r=Object.hasOwnProperty.call(e,"constructor")&&e.constructor;return r===Object||"function"==typeof r&&Function.toString.call(r)===tt}(t)||Array.isArray(t)||!!t[Q]||!!t.constructor[Q]||g(t)||P(t))}function h(t,e,r){void 0===r&&(r=!1),0===y(t)?(r?Object.keys:et)(t).forEach((function(n){r&&"symbol"==typeof n||e(n,t[n],t)})):t.forEach((function(r,n){return e(n,r,t)}))}function y(t){var e=t[Y];return e?e.i>3?e.i-4:e.i:Array.isArray(t)?1:g(t)?2:P(t)?3:0}function v(t,e){return 2===y(t)?t.has(e):Object.prototype.hasOwnProperty.call(t,e)}function m(t,e,r){var n=y(t);2===n?t.set(e,r):3===n?(t.delete(e),t.add(r)):t[e]=r}function g(t){return X&&t instanceof Map}function P(t){return q&&t instanceof Set}function O(t){return t.o||t.t}function S(t){if(Array.isArray(t))return Array.prototype.slice.call(t);var e=rt(t);delete e[Y];for(var r=et(e),n=0;n<r.length;n++){var o=r[n],i=e[o];!1===i.writable&&(i.writable=!0,i.configurable=!0),(i.get||i.set)&&(e[o]={configurable:!0,writable:!0,enumerable:i.enumerable,value:t[o]})}return Object.create(Object.getPrototypeOf(t),e)}function w(t,e){return void 0===e&&(e=!1),E(t)||d(t)||!b(t)||(y(t)>1&&(t.set=t.add=t.clear=t.delete=j),Object.freeze(t),e&&h(t,(function(t,e){return w(e,!0)}),!0)),t}function j(){p(2)}function E(t){return null==t||"object"!=typeof t||Object.isFrozen(t)}function D(t){var e=nt[t];return e||p(18,t),e}function $(){return"production"===process.env.NODE_ENV||G||p(0),G}function A(t,e){e&&(D("Patches"),t.u=[],t.s=[],t.v=e)}function x(t){_(t),t.p.forEach(N),t.p=null}function _(t){t===G&&(G=t.l)}function I(t){return G={p:[],l:G,h:t,m:!0,_:0}}function N(t){var e=t[Y];0===e.i||1===e.i?e.j():e.O=!0}function k(t,e){e._=e.p.length;var r=e.p[0],n=void 0!==t&&t!==r;return e.h.g||D("ES5").S(e,t,n),n?(r[Y].P&&(x(e),p(4)),b(t)&&(t=F(e,t),e.l||z(e,t)),e.u&&D("Patches").M(r[Y].t,t,e.u,e.s)):t=F(e,r,[]),x(e),e.u&&e.v(e.u,e.s),t!==H?t:void 0}function F(t,e,r){if(E(e))return e;var n=e[Y];if(!n)return h(e,(function(o,i){return M(t,n,e,o,i,r)}),!0),e;if(n.A!==t)return e;if(!n.P)return z(t,n.t,!0),n.t;if(!n.I){n.I=!0,n.A._--;var o=4===n.i||5===n.i?n.o=S(n.k):n.o;h(3===n.i?new Set(o):o,(function(e,i){return M(t,n,o,e,i,r)})),z(t,o,!1),r&&t.u&&D("Patches").R(n,r,t.u,t.s)}return n.o}function M(t,e,r,n,o,i){if("production"!==process.env.NODE_ENV&&o===r&&p(5),d(o)){var a=F(t,o,i&&e&&3!==e.i&&!v(e.D,n)?i.concat(n):void 0);if(m(r,n,a),!d(a))return;t.m=!1}if(b(o)&&!E(o)){if(!t.h.F&&t._<1)return;F(t,o),e&&e.A.l||z(t,o)}}function z(t,e,r){void 0===r&&(r=!1),t.h.F&&t.m&&w(e,r)}function R(t,e){var r=t[Y];return(r?O(r):t)[e]}function K(t,e){if(e in t)for(var r=Object.getPrototypeOf(t);r;){var n=Object.getOwnPropertyDescriptor(r,e);if(n)return n;r=Object.getPrototypeOf(r)}}function T(t){t.P||(t.P=!0,t.l&&T(t.l))}function V(t){t.o||(t.o=S(t.t))}function C(t,e,r){var n=g(e)?D("MapSet").N(e,r):P(e)?D("MapSet").T(e,r):t.g?function(t,e){var r=Array.isArray(t),n={i:r?1:0,A:e?e.A:$(),P:!1,I:!1,D:{},l:e,t:t,k:null,o:null,j:null,C:!1},o=n,i=ot;r&&(o=[n],i=it);var a=Proxy.revocable(o,i),u=a.revoke,c=a.proxy;return n.k=c,n.j=u,c}(e,r):D("ES5").J(e,r);return(r?r.A:$()).p.push(n),n}function U(t){return d(t)||p(22,t),function t(e){if(!b(e))return e;var r,n=e[Y],o=y(e);if(n){if(!n.P&&(n.i<4||!D("ES5").K(n)))return n.t;n.I=!0,r=W(e,o),n.I=!1}else r=W(e,o);return h(r,(function(e,o){n&&function(t,e){return 2===y(t)?t.get(e):t[e]}(n.t,e)===o||m(r,e,t(o))})),3===o?new Set(r):r}(t)}function W(t,e){switch(e){case 2:return new Map(t);case 3:return Array.from(t)}return S(t)}var J,G,L="undefined"!=typeof Symbol&&"symbol"==typeof Symbol("x"),X="undefined"!=typeof Map,q="undefined"!=typeof Set,B="undefined"!=typeof Proxy&&void 0!==Proxy.revocable&&"undefined"!=typeof Reflect,H=L?Symbol.for("immer-nothing"):((J={})["immer-nothing"]=!0,J),Q=L?Symbol.for("immer-draftable"):"__$immer_draftable",Y=L?Symbol.for("immer-state"):"__$immer_state",Z={0:"Illegal state",1:"Immer drafts cannot have computed properties",2:"This object has been frozen and should not be mutated",3:function(t){return"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+t},4:"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",5:"Immer forbids circular references",6:"The first or second argument to `produce` must be a function",7:"The third argument to `produce` must be a function or undefined",8:"First argument to `createDraft` must be a plain object, an array, or an immerable object",9:"First argument to `finishDraft` must be a draft returned by `createDraft`",10:"The given draft is already finalized",11:"Object.defineProperty() cannot be used on an Immer draft",12:"Object.setPrototypeOf() cannot be used on an Immer draft",13:"Immer only supports deleting array indices",14:"Immer only supports setting array indices and the 'length' property",15:function(t){return"Cannot apply patch, path doesn't resolve: "+t},16:'Sets cannot have "replace" patches.',17:function(t){return"Unsupported patch operation: "+t},18:function(t){return"The plugin for '"+t+"' has not been loaded into Immer. To enable the plugin, import and call `enable"+t+"()` when initializing your application."},20:"Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available",21:function(t){return"produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '"+t+"'"},22:function(t){return"'current' expects a draft, got: "+t},23:function(t){return"'original' expects a draft, got: "+t},24:"Patching reserved attributes like __proto__, prototype and constructor is not allowed"},tt=""+Object.prototype.constructor,et="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(t){return Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t))}:Object.getOwnPropertyNames,rt=Object.getOwnPropertyDescriptors||function(t){var e={};return et(t).forEach((function(r){e[r]=Object.getOwnPropertyDescriptor(t,r)})),e},nt={},ot={get:function(t,e){if(e===Y)return t;var r=O(t);if(!v(r,e))return function(t,e,r){var n,o=K(e,r);return o?"value"in o?o.value:null===(n=o.get)||void 0===n?void 0:n.call(t.k):void 0}(t,r,e);var n=r[e];return t.I||!b(n)?n:n===R(t.t,e)?(V(t),t.o[e]=C(t.A.h,n,t)):n},has:function(t,e){return e in O(t)},ownKeys:function(t){return Reflect.ownKeys(O(t))},set:function(t,e,r){var n=K(O(t),e);if(null==n?void 0:n.set)return n.set.call(t.k,r),!0;if(!t.P){var o=R(O(t),e),i=null==o?void 0:o[Y];if(i&&i.t===r)return t.o[e]=r,t.D[e]=!1,!0;if(function(t,e){return t===e?0!==t||1/t==1/e:t!=t&&e!=e}(r,o)&&(void 0!==r||v(t.t,e)))return!0;V(t),T(t)}return t.o[e]===r&&"number"!=typeof r&&(void 0!==r||e in t.o)||(t.o[e]=r,t.D[e]=!0,!0)},deleteProperty:function(t,e){return void 0!==R(t.t,e)||e in t.t?(t.D[e]=!1,V(t),T(t)):delete t.D[e],t.o&&delete t.o[e],!0},getOwnPropertyDescriptor:function(t,e){var r=O(t),n=Reflect.getOwnPropertyDescriptor(r,e);return n?{writable:!0,configurable:1!==t.i||"length"!==e,enumerable:n.enumerable,value:r[e]}:n},defineProperty:function(){p(11)},getPrototypeOf:function(t){return Object.getPrototypeOf(t.t)},setPrototypeOf:function(){p(12)}},it={};h(ot,(function(t,e){it[t]=function(){return arguments[0]=arguments[0][0],e.apply(this,arguments)}})),it.deleteProperty=function(t,e){return"production"!==process.env.NODE_ENV&&isNaN(parseInt(e))&&p(13),it.set.call(this,t,e,void 0)},it.set=function(t,e,r){return"production"!==process.env.NODE_ENV&&"length"!==e&&isNaN(parseInt(e))&&p(14),ot.set.call(this,t[0],e,r,t[0])};var at=function(){function t(t){var e=this;this.g=B,this.F=!0,this.produce=function(t,r,n){if("function"==typeof t&&"function"!=typeof r){var o=r;r=t;var i=e;return function(t){var e=this;void 0===t&&(t=o);for(var n=arguments.length,a=Array(n>1?n-1:0),u=1;u<n;u++)a[u-1]=arguments[u];return i.produce(t,(function(t){var n;return(n=r).call.apply(n,[e,t].concat(a))}))}}var a;if("function"!=typeof r&&p(6),void 0!==n&&"function"!=typeof n&&p(7),b(t)){var u=I(e),c=C(e,t,void 0),s=!0;try{a=r(c),s=!1}finally{s?x(u):_(u)}return"undefined"!=typeof Promise&&a instanceof Promise?a.then((function(t){return A(u,n),k(t,u)}),(function(t){throw x(u),t})):(A(u,n),k(a,u))}if(!t||"object"!=typeof t){if(void 0===(a=r(t))&&(a=t),a===H&&(a=void 0),e.F&&w(a,!0),n){var f=[],l=[];D("Patches").M(t,a,f,l),n(f,l)}return a}p(21,t)},this.produceWithPatches=function(t,r){if("function"==typeof t)return function(r){for(var n=arguments.length,o=Array(n>1?n-1:0),i=1;i<n;i++)o[i-1]=arguments[i];return e.produceWithPatches(r,(function(e){return t.apply(void 0,[e].concat(o))}))};var n,o,i=e.produce(t,r,(function(t,e){n=t,o=e}));return"undefined"!=typeof Promise&&i instanceof Promise?i.then((function(t){return[t,n,o]})):[i,n,o]},"boolean"==typeof(null==t?void 0:t.useProxies)&&this.setUseProxies(t.useProxies),"boolean"==typeof(null==t?void 0:t.autoFreeze)&&this.setAutoFreeze(t.autoFreeze)}var e=t.prototype;return e.createDraft=function(t){b(t)||p(8),d(t)&&(t=U(t));var e=I(this),r=C(this,t,void 0);return r[Y].C=!0,_(e),r},e.finishDraft=function(t,e){var r=t&&t[Y];"production"!==process.env.NODE_ENV&&(r&&r.C||p(9),r.I&&p(10));var n=r.A;return A(n,e),k(void 0,n)},e.setAutoFreeze=function(t){this.F=t},e.setUseProxies=function(t){t&&!B&&p(20),this.g=t},e.applyPatches=function(t,e){var r;for(r=e.length-1;r>=0;r--){var n=e[r];if(0===n.path.length&&"replace"===n.op){t=n.value;break}}r>-1&&(e=e.slice(r+1));var o=D("Patches").$;return d(t)?o(t,e):this.produce(t,(function(t){return o(t,e)}))},t}(),ut=new at,ct=ut.produce;ut.produceWithPatches.bind(ut);var st=ut.setAutoFreeze.bind(ut);ut.setUseProxies.bind(ut),ut.applyPatches.bind(ut),ut.createDraft.bind(ut),ut.finishDraft.bind(ut);const ft=t=>t,lt=Symbol("subscribe"),pt=t=>{const e=t?.[lt];return e||t};st(!1);const dt=(t,e,r="")=>new Proxy(t,{get:(n,i)=>i===lt?(e(r),t):t[i]&&a(t[i])?dt(t[i],e,`${r}${o(n)?`[${i}]`:`${r?".":""}${i}`}`):(e(`${r}${o(n)?`[${i}]`:`${r?".":""}${i}`}`),t[i])}),bt=(t,e,r,n=[],u="")=>{if(o(t)&&o(e))return e.forEach(((o,a)=>{if(n.push(`${u}[${a}]`),i(t[a]))t[a]=o;else{if(e[a])return bt(t[a],e[a],r,n,`${u}[${a}]`);t[a]=e[a]}})),t.length=e.length,n;if(a(e)){for(let o in e)if(o in t)if(a(e[o])&&e[o]&&(r.fireDuplicates||t[o]!==e[o])){if(n.push(`${u}${u?".":""}${o}`),e[o]&&t[o])return bt(t[o],e[o],r,n,`${u}${u?".":""}${o}`);t[o]=e[o]}else(r.fireDuplicates||t[o]!==e[o])&&(t[o]=e[o],n.push(`${u}${u?".":""}${o}`));else n.push(`${u}${u?".":""}${o}`),t[o]=e[o];return n}return(r.fireDuplicates||t!==e)&&(t=e),n};exports.createModel=(t,e)=>{const r=String(l++),n=s(r,t,e);return{getState:()=>n.lastState,dispatch:(t,e)=>{f(r,t,{...e})},setOptions:t=>((t,e)=>{c(t).options=e})(r,t),subscribe:(t,e)=>((t,e,r={})=>{const n=c(t);return r.needPrevious&&e(n.lastState),!n.subscribers.some((t=>t.callback===e))&&n.subscribers.push({callback:e}),()=>{n.subscribers=n.subscribers.filter((t=>t.callback!==e))}})(r,t,e)}},exports.createStorm=(t,e)=>{let r=t,o={},a=[];return{getState:()=>r,subscribe:t=>{let e=new Set;const n=dt(r,(t=>{t&&e.add(t)}));return t(n,pt),!e.size&&e.add("default",t),e.forEach((e=>{o[e]=o[e]||[],o[e].push(t)})),()=>{e.forEach((e=>{o[e]=o[e].filter((e=>e!==t))}))}},addMiddleware:t=>(a.push(t),()=>{a=a.filter((e=>e!==t))}),dispatch:(t,u)=>{let c;const s=n(t)?t(r):t,f=ct(r,(t=>{c=bt(t,s,{...e,...u||{}})})),l=[f,r];!i(u)&&l.push(u),a.forEach((t=>t(...l))),r=f,c.forEach((t=>{o[t]&&o[t].forEach((t=>{t(r,ft)}))})),o.default?.forEach((t=>t(r,ft)))}}},exports.createVirtualModel=({models:r=[],handler:n,...o}={})=>{const i=e({options:o});i.options.handler=n,i.options.models=r;const a=()=>i.options.models.map((t=>t.getState())),u=t=>{const e=i.options.handler(...a()),{lastState:r}=i;i.lastState=e,i.subscribers.forEach((({callback:n})=>i.options.fireDuplicates||e!==r?n(e,t):void 0))};let c=r.map((t=>t.subscribe(((t,e)=>u(e))))),s=!1;return{getState:()=>((s||i.lastState===t)&&(i.lastState=i.options.handler(...a())),i.lastState),subscribe:function(t,e={}){return e.needPrevious&&t(this.getState()),!i.subscribers.some((e=>e.callback===t))&&i.subscribers.push({callback:t}),()=>{i.subscribers=i.subscribers.filter((e=>e.callback!==t))}},setOptions:({models:t,handler:e=i.options.handler,...r})=>{i.options={...i.options,...r,handler:e},t&&(c.map((t=>t())),i.options.models=t,c=t.map((t=>t.subscribe(((t,e)=>u(e))))),s=!0),i.options.models.forEach((t=>{t.setOptions(r)}))}}},exports.persisted=t=>({storageKey:e,beforeunload:n=r,permanent:o=!1})=>r=>{let i;try{const t=o?window.localStorage:window.sessionStorage;i=JSON.parse(t.getItem(e))}catch{i={}}const a=t({...r,...i});return window.addEventListener("beforeunload",(()=>{try{(o?window.localStorage:window.sessionStorage).setItem(e,JSON.stringify(n(a.getState())))}catch{}})),a};