state-jet
Version:
Ultra-lightweight global state management for React
2 lines (1 loc) • 13.2 kB
JavaScript
import{useRef as e,useEffect as t,useSyncExternalStore as r}from"react";var n=Symbol.for("immer-nothing"),o=Symbol.for("immer-draftable"),s=Symbol.for("immer-state"),a="production"!==process.env.NODE_ENV?[function(e){return`The plugin for '${e}' has not been loaded into Immer. To enable the plugin, import and call \`enable${e}()\` when initializing your application.`},function(e){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 '${e}'`},"This object has been frozen and should not be mutated",function(e){return"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+e},"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.","Immer forbids circular references","The first or second argument to `produce` must be a function","The third argument to `produce` must be a function or undefined","First argument to `createDraft` must be a plain object, an array, or an immerable object","First argument to `finishDraft` must be a draft returned by `createDraft`",function(e){return`'current' expects a draft, got: ${e}`},"Object.defineProperty() cannot be used on an Immer draft","Object.setPrototypeOf() cannot be used on an Immer draft","Immer only supports deleting array indices","Immer only supports setting array indices and the 'length' property",function(e){return`'original' expects a draft, got: ${e}`}]:[];function i(e,...t){if("production"!==process.env.NODE_ENV){const r=a[e],n="function"==typeof r?r.apply(null,t):r;throw new Error(`[Immer] ${n}`)}throw new Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var c=Object.getPrototypeOf;function u(e){return!!e&&!!e[s]}function f(e){return!!e&&(d(e)||Array.isArray(e)||!!e[o]||!!e.constructor?.[o]||b(e)||v(e))}var l=Object.prototype.constructor.toString(),p=new WeakMap;function d(e){if(!e||"object"!=typeof e)return!1;const t=Object.getPrototypeOf(e);if(null===t||t===Object.prototype)return!0;const r=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;if(r===Object)return!0;if("function"!=typeof r)return!1;let n=p.get(r);return void 0===n&&(n=Function.toString.call(r),p.set(r,n)),n===l}function _(e,t,r=!0){0===y(e)?(r?Reflect.ownKeys(e):Object.keys(e)).forEach(r=>{t(r,e[r],e)}):e.forEach((r,n)=>t(n,r,e))}function y(e){const t=e[s];return t?t.type_:Array.isArray(e)?1:b(e)?2:v(e)?3:0}function h(e,t){return 2===y(e)?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function m(e,t,r){const n=y(e);2===n?e.set(t,r):3===n?e.add(r):e[t]=r}function b(e){return e instanceof Map}function v(e){return e instanceof Set}function g(e){return e.copy_||e.base_}function S(e,t){if(b(e))return new Map(e);if(v(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);const r=d(e);if(!0===t||"class_only"===t&&!r){const t=Object.getOwnPropertyDescriptors(e);delete t[s];let r=Reflect.ownKeys(t);for(let n=0;n<r.length;n++){const o=r[n],s=t[o];!1===s.writable&&(s.writable=!0,s.configurable=!0),(s.get||s.set)&&(t[o]={configurable:!0,writable:!0,enumerable:s.enumerable,value:e[o]})}return Object.create(c(e),t)}{const t=c(e);if(null!==t&&r)return{...e};const n=Object.create(t);return Object.assign(n,e)}}function w(e,t=!1){return P(e)||u(e)||!f(e)||(y(e)>1&&Object.defineProperties(e,{set:O,add:O,clear:O,delete:O}),Object.freeze(e),t&&Object.values(e).forEach(e=>w(e,!0))),e}var O={value:function(){i(2)}};function P(e){return null===e||"object"!=typeof e||Object.isFrozen(e)}var j,E={};function z(e){const t=E[e];return t||i(0,e),t}function I(){return j}function N(e,t){t&&(z("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function D(e){A(e),e.drafts_.forEach(M),e.drafts_=null}function A(e){e===j&&(j=e.parent_)}function F(e){return j=function(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}(j,e)}function M(e){const t=e[s];0===t.type_||1===t.type_?t.revoke_():t.revoked_=!0}function T(e,t){t.unfinalizedDrafts_=t.drafts_.length;const r=t.drafts_[0];return void 0!==e&&e!==r?(r[s].modified_&&(D(t),i(4)),f(e)&&(e=$(t,e),t.parent_||k(t,e)),t.patches_&&z("Patches").generateReplacementPatches_(r[s].base_,e,t.patches_,t.inversePatches_)):e=$(t,r,[]),D(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==n?e:void 0}function $(e,t,r){if(P(t))return t;const n=e.immer_.shouldUseStrictIteration(),o=t[s];if(!o)return _(t,(n,s)=>C(e,o,t,n,s,r),n),t;if(o.scope_!==e)return t;if(!o.modified_)return k(e,o.base_,!0),o.base_;if(!o.finalized_){o.finalized_=!0,o.scope_.unfinalizedDrafts_--;const t=o.copy_;let s=t,a=!1;3===o.type_&&(s=new Set(t),t.clear(),a=!0),_(s,(n,s)=>C(e,o,t,n,s,r,a),n),k(e,t,!1),r&&e.patches_&&z("Patches").generatePatches_(o,r,e.patches_,e.inversePatches_)}return o.copy_}function C(e,t,r,n,o,s,a){if(null==o||"object"!=typeof o&&!a)return;const c=P(o);if(!c||a){if("production"!==process.env.NODE_ENV&&o===r&&i(5),u(o)){const a=$(e,o,s&&t&&3!==t.type_&&!h(t.assigned_,n)?s.concat(n):void 0);if(m(r,n,a),!u(a))return;e.canAutoFreeze_=!1}else a&&r.add(o);if(f(o)&&!c){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1||t&&t.base_&&t.base_[n]===o&&c)return;$(e,o),(!t||!t.scope_.parent_)&&"symbol"!=typeof n&&(b(r)?r.has(n):Object.prototype.propertyIsEnumerable.call(r,n))&&k(e,o)}}}function k(e,t,r=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&w(t,r)}var R={get(e,t){if(t===s)return e;const r=g(e);if(!h(r,t))return function(e,t,r){const n=V(t,r);return n?"value"in n?n.value:n.get?.call(e.draft_):void 0}(e,r,t);const n=r[t];return e.finalized_||!f(n)?n:n===x(e.base_,t)?(K(e),e.copy_[t]=W(n,e)):n},has:(e,t)=>t in g(e),ownKeys:e=>Reflect.ownKeys(g(e)),set(e,t,r){const n=V(g(e),t);if(n?.set)return n.set.call(e.draft_,r),!0;if(!e.modified_){const n=x(g(e),t),o=n?.[s];if(o&&o.base_===r)return e.copy_[t]=r,e.assigned_[t]=!1,!0;if(function(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}(r,n)&&(void 0!==r||h(e.base_,t)))return!0;K(e),J(e)}return e.copy_[t]===r&&(void 0!==r||t in e.copy_)||Number.isNaN(r)&&Number.isNaN(e.copy_[t])||(e.copy_[t]=r,e.assigned_[t]=!0),!0},deleteProperty:(e,t)=>(void 0!==x(e.base_,t)||t in e.base_?(e.assigned_[t]=!1,K(e),J(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0),getOwnPropertyDescriptor(e,t){const r=g(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n&&{writable:!0,configurable:1!==e.type_||"length"!==t,enumerable:n.enumerable,value:r[t]}},defineProperty(){i(11)},getPrototypeOf:e=>c(e.base_),setPrototypeOf(){i(12)}},U={};function x(e,t){const r=e[s];return(r?g(r):e)[t]}function V(e,t){if(!(t in e))return;let r=c(e);for(;r;){const e=Object.getOwnPropertyDescriptor(r,t);if(e)return e;r=c(r)}}function J(e){e.modified_||(e.modified_=!0,e.parent_&&J(e.parent_))}function K(e){e.copy_||(e.copy_=S(e.base_,e.scope_.immer_.useStrictShallowCopy_))}_(R,(e,t)=>{U[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}}),U.deleteProperty=function(e,t){return"production"!==process.env.NODE_ENV&&isNaN(parseInt(t))&&i(13),U.set.call(this,e,t,void 0)},U.set=function(e,t,r){return"production"!==process.env.NODE_ENV&&"length"!==t&&isNaN(parseInt(t))&&i(14),R.set.call(this,e[0],t,r,e[0])};function W(e,t){const r=b(e)?z("MapSet").proxyMap_(e,t):v(e)?z("MapSet").proxySet_(e,t):function(e,t){const r=Array.isArray(e),n={type_:r?1:0,scope_:t?t.scope_:I(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1};let o=n,s=R;r&&(o=[n],s=U);const{revoke:a,proxy:i}=Proxy.revocable(o,s);return n.draft_=i,n.revoke_=a,i}(e,t);return(t?t.scope_:I()).drafts_.push(r),r}function L(e){if(!f(e)||P(e))return e;const t=e[s];let r,n=!0;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,r=S(e,t.scope_.immer_.useStrictShallowCopy_),n=t.scope_.immer_.shouldUseStrictIteration()}else r=S(e,!0);return _(r,(e,t)=>{m(r,e,L(t))},n),t&&(t.finalized_=!1),r}var G=(new class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.useStrictIteration_=!0,this.produce=(e,t,r)=>{if("function"==typeof e&&"function"!=typeof t){const r=t;t=e;const n=this;return function(e=r,...o){return n.produce(e,e=>t.call(this,e,...o))}}let o;if("function"!=typeof t&&i(6),void 0!==r&&"function"!=typeof r&&i(7),f(e)){const n=F(this),s=W(e,void 0);let a=!0;try{o=t(s),a=!1}finally{a?D(n):A(n)}return N(n,r),T(o,n)}if(!e||"object"!=typeof e){if(o=t(e),void 0===o&&(o=e),o===n&&(o=void 0),this.autoFreeze_&&w(o,!0),r){const t=[],n=[];z("Patches").generateReplacementPatches_(e,o,t,n),r(t,n)}return o}i(1,e)},this.produceWithPatches=(e,t)=>{if("function"==typeof e)return(t,...r)=>this.produceWithPatches(t,t=>e(t,...r));let r,n;return[this.produce(e,t,(e,t)=>{r=e,n=t}),r,n]},"boolean"==typeof e?.autoFreeze&&this.setAutoFreeze(e.autoFreeze),"boolean"==typeof e?.useStrictShallowCopy&&this.setUseStrictShallowCopy(e.useStrictShallowCopy),"boolean"==typeof e?.useStrictIteration&&this.setUseStrictIteration(e.useStrictIteration)}createDraft(e){f(e)||i(8),u(e)&&(e=function(e){return u(e)||i(10,e),L(e)}(e));const t=F(this),r=W(e,void 0);return r[s].isManual_=!0,A(t),r}finishDraft(e,t){const r=e&&e[s];(!r||!r.isManual_)&&i(9);const{scope_:n}=r;return N(n,t),T(void 0,n)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}setUseStrictIteration(e){this.useStrictIteration_=e}shouldUseStrictIteration(){return this.useStrictIteration_}applyPatches(e,t){let r;for(r=t.length-1;r>=0;r--){const n=t[r];if(0===n.path.length&&"replace"===n.op){e=n.value;break}}r>-1&&(t=t.slice(r+1));const n=z("Patches").applyPatches_;return u(e)?n(e,t):this.produce(e,e=>n(e,t))}}).produce;const q=(e=>{if(!1===e||null===e)throw new Error("Unable to locate global `this`");return e||globalThis})(),X=(e,t)=>{typeof q?.localStorage<"u"&&q.localStorage.setItem(e,JSON.stringify(t))},B=(e,t)=>{if(typeof q?.localStorage<"u"){const r=q.localStorage?.getItem(e);return r?JSON.parse(r):t}return t},H=(e,t)=>{typeof q?.localStorage<"u"&&q.localStorage.setItem(e,(e=>btoa(JSON.stringify(e)))(t))},Q=(e,t)=>{if(typeof q?.localStorage<"u"){const r=q.localStorage?.getItem(e);return r?(e=>JSON.parse(atob(e)))(r):t}return t},Y={},Z={},ee={},te=q.__STATE_JET_DEVTOOLS__,re=(e,t)=>{Y[e]||(Y[e]=[]),void 0===Z[e]&&(Z[e]=-1),Y[e]=Y[e].slice(0,Z[e]+1),Y[e].push(t),Z[e]++,te?.updateState&&te.updateState(e,t,[...Y[e]])},ne=(e,t)=>{ee[e]||(ee[e]=[]);const r=performance.now();t();const n=performance.now()-r;ee[e].push(n),te?.updatePerformance&&te.updatePerformance(e,n)},oe=new Map,se=new Map,ae={},ie=new Map;let ce=!1;function ue(e){return oe.has(e)||oe.set(e,{}),function(n,o,s){const a=oe.get(e),i=`${e}:${n}`;if(ie.has(i))return ie.get(i);a[n]||(a[n]={value:o,listeners:new Set,...s});const c=a[n],u={useState:()=>(t(()=>{if(c.persist&&!c.restored){c.restored=!0;const e=c.encrypt?Q(i,o):B(i,o);if(e!==o){c.value=e;for(const e of c.listeners)e()}}},[o]),r(e=>(c.listeners.add(e),()=>c.listeners.delete(e)),()=>c.value,()=>o)),set:async(e,t=!1)=>{if(t){ae[i]||(ae[i]={past:[],present:c.value,future:[]}),ae[i].past.push(c.value),c.value="function"==typeof e?G(c.value,e):e,ae[i].present=c.value,ae[i].future=[];for(const e of c.listeners)e();re(i,c.value)}else se.set(i,e),ce||(ce=!0,await async function(){for(;se.size>0;){const e=Array.from(se.entries());se.clear();for(const[t,r]of e){const[e,n]=t.split(":"),o=oe.get(e);if(!o)continue;const s=o[n];if(!s)continue;let a=r;const i=s.value;if(ae[t]||(ae[t]={past:[],present:i,future:[]}),ae[t].past.push(i),ae[t].present=a,ae[t].future=[],s.middleware)for(const e of s.middleware)try{const r=e(t,i,a,e=>{a=e});if(r instanceof Promise){const e=await r;void 0!==e&&(a=e)}else void 0!==r&&(a=r)}catch(e){console.error(e)}s.value="function"==typeof a?G(s.value,a):a;for(const e of s.listeners)e();re(t,s.value),ne(t,()=>{}),s.persist&&(s.encrypt?H(t,s.value):X(t,s.value))}}ce=!1}())},get:()=>c.value,undo:()=>{const t=ae[i];if(t&&t.past.length){t.future.unshift(t.present),t.present=t.past.pop(),c.value=t.present;for(const e of c.listeners)e();(e=>{if(Z[e]>0)Z[e]--,Y[e][Z[e]]})(i),re(`${e}.undo`,c.value)}},redo:()=>{const t=ae[i];if(t&&t.future.length){t.past.push(t.present),t.present=t.future.shift(),c.value=t.present;for(const e of c.listeners)e();(e=>{if(Z[e]<Y[e]?.length-1)Z[e]++,Y[e][Z[e]]})(i),re(`${e}.redo`,c.value)}},clear:()=>{c.value=o;for(const e of c.listeners)e();re(`${e}.clear`,o)}};return ie.set(i,u),u}}function fe(e,t,r){return ue("global")(e,t,r)}function le(t){const r=e(null);return r.current||(r.current=t()),r.current}const pe=async(e,t,r,n)=>{const o=e.useState(),s=t(o);e.set(s);try{await r()}catch(t){console.error("API request failed:",t),e.set(n?n(o):o)}},de=(e,t)=>({...e,...t,lastUpdated:Date.now()}),_e=(e,t,r)=>{const n=t();r(de(n,e))},ye=(e,t)=>()=>{const r=e.map(e=>e.useState());return t(...r)},he="2.4.0";export{he as VERSION,ye as derivedState,de as mergeCRDT,pe as optimisticUpdate,Q as restoreEncryptedState,B as restoreState,H as saveEncryptedState,X as saveState,_e as syncCRDT,ue as useSlice,fe as useStateGlobal,le as useStore};