state-jet 1.3.1 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -49,10 +49,49 @@ import { useStateGlobal } from "state-jet";
49
49
  const counter = useStateGlobal("counter", 0);
50
50
 
51
51
  function Counter() {
52
- const count = counter.useStore();
52
+ const count = counter.useState();
53
53
  return <button onClick={() => counter.set(count + 1)}>Count: {count}</button>;
54
54
  }
55
55
  ```
56
+ ## ⚡ Why state-jet Is More Advanced Than Zustand
57
+
58
+ - **No Proxies Needed** → Zustand uses proxies for state updates, but state-jet uses signals, making it even faster.
59
+ - **Derived State Is Automatic** → No need for selectors; state updates only trigger where necessary.
60
+ - **Optimistic Updates & Rollback** → Unlike Zustand, state-jet has built-in support for instant UI updates and auto-revert on failures.
61
+ - **Multi-Tab Sync** → global state persists across browser tabs and devices.
62
+ - **CRDT Support** → Automatic conflict resolution for real-time apps, something even Zustand lacks.
63
+
64
+ ### ✅Conclusion
65
+
66
+ If you need the simplest, fastest, and most advanced state management solution for React, state-jet beats Redux, Recoil, MobX, Jotai, and even Zustand in performance, reactivity, and developer experience. 🚀
67
+
68
+ ## Create Slice
69
+
70
+ ```bash
71
+ import { useSlice } from "state-jet";
72
+
73
+ export const useProductSlice = () => useSlice("products")("list", []);
74
+
75
+ export const useCartSlice = () =>
76
+ useSlice("cart")("items", []);
77
+
78
+ export const useUserSlice = () => useSlice("user")("info", null);
79
+ ```
80
+
81
+ ## Create Store
82
+
83
+ ```bash
84
+ import { useStore } from "state-jet";
85
+ import { useProductSlice, useCartSlice, useUserSlice } from "./slices";
86
+
87
+ const initializer: any = () => ({
88
+ products: useProductSlice(),
89
+ cart: useCartSlice(),
90
+ user: useUserSlice()
91
+ });
92
+
93
+ export const useEcommerceStore = () => useStore(initializer);
94
+ ```
56
95
 
57
96
  ## ⚡ Comparison Table
58
97
  | Feature | Redux | Recoil | MobX | Jotai | Zustand | state-jet |
@@ -67,54 +106,6 @@ function Counter() {
67
106
  | **CRDT Conflict Resolution** | ❌ No | ❌ No | ❌ No | ❌ No | ❌ No | ✅ Yes |
68
107
 
69
108
 
70
- ## ⚡ Why state-jet Is More Advanced Than Zustand
71
-
72
- - **No Proxies Needed** → Zustand uses proxies for state updates, but state-jet uses signals, making it even faster.
73
- - **Derived State Is Automatic** → No need for selectors; state updates only trigger where necessary.
74
- - **Optimistic Updates & Rollback** → Unlike Zustand, state-jet has built-in support for instant UI updates and auto-revert on failures.
75
- - **Multi-Tab Sync** → global state persists across browser tabs and devices.
76
- - **CRDT Support** → Automatic conflict resolution for real-time apps, something even Zustand lacks.
77
-
78
- ✅ Conclusion
79
-
80
- If you need the simplest, fastest, and most advanced state management solution for React, state-jet beats Redux, Recoil, MobX, Jotai, and even Zustand in performance, reactivity, and developer experience. 🚀
81
-
82
- ## 🎯 Why Use `optimisticUpdate`?
83
-
84
- | Feature | Without `optimisticUpdate` | With `optimisticUpdate` |
85
- | ----------------------- | -------------------------- | --------------------------- |
86
- | **UI Responsiveness** | Delayed (Waits for API) | Instant update (Optimistic) |
87
- | **User Experience** | Slow & Janky | Fast & Smooth |
88
- | **Rollback on Failure** | Manual Handling | Automatic |
89
- | **Code Complexity** | High | Low |
90
-
91
- ## 🎯 Why Use `syncCRDT`?
92
-
93
- | Feature | Without `syncCRDT` | With `syncCRDT` |
94
- | ---------------------- | ------------------ | -------------------------- |
95
- | **Multi-User Sync** | Possible Conflicts | ✅ Automatic Merging |
96
- | **Real-Time Updates** | Needs Manual Fixes | ✅ No Data Loss |
97
- | **Handles Conflicts** | Can Lose Changes | ✅ Merges Automatically |
98
- | **Scalable for Teams** | Hard to Maintain | ✅ Ideal for Collaboration |
99
-
100
- ## 🎯 Why Use `derivedState`?
101
-
102
- | Feature | Without `derivedState` | With `derivedState` |
103
- | ------------------------- | --------------------------- | ------------------------------ |
104
- | **Manual Recalculations** | ❌ Yes (Recompute manually) | ✅ Automatic |
105
- | **Reactivity** | ❌ Requires `useEffect` | ✅ Updates only when needed |
106
- | **Performance** | ❌ Unoptimized | ✅ Only recalculates on change |
107
- | **Code Complexity** | ❌ High | ✅ Minimal |
108
-
109
- ## 🎯 Why Use `undo & redo`?
110
-
111
- | Feature | Without Undo/Redo | With Undo/Redo |
112
- | ---------------------- | ------------------------ | -------------------------- |
113
- | **Accidental Changes** | ❌ Lost forever | ✅ Easily undone |
114
- | **User Experience** | ❌ Frustrating | ✅ Smooth & intuitive |
115
- | **Multi-Step Editing** | ❌ Hard to track | ✅ Easy to restore history |
116
- | **Performance** | ❌ Needs manual tracking | ✅ Automatic |
117
-
118
109
  ## Contributing
119
110
 
120
111
  Development of State-jet happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving State-jet.
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var e=require("react"),t=Symbol.for("immer-nothing"),r=Symbol.for("immer-draftable"),n=Symbol.for("immer-state"),o="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 a(e,...t){if("production"!==process.env.NODE_ENV){const r=o[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 s=Object.getPrototypeOf;function i(e){return!!e&&!!e[n]}function c(e){return!!e&&(f(e)||Array.isArray(e)||!!e[r]||!!e.constructor?.[r]||y(e)||h(e))}var u=Object.prototype.constructor.toString();function f(e){if(!e||"object"!=typeof e)return!1;const t=s(e);if(null===t)return!0;const r=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return r===Object||"function"==typeof r&&Function.toString.call(r)===u}function l(e,t){0===p(e)?Reflect.ownKeys(e).forEach((r=>{t(r,e[r],e)})):e.forEach(((r,n)=>t(n,r,e)))}function p(e){const t=e[n];return t?t.type_:Array.isArray(e)?1:y(e)?2:h(e)?3:0}function d(e,t){return 2===p(e)?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function _(e,t,r){const n=p(e);2===n?e.set(t,r):3===n?e.add(r):e[t]=r}function y(e){return e instanceof Map}function h(e){return e instanceof Set}function b(e){return e.copy_||e.base_}function m(e,t){if(y(e))return new Map(e);if(h(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);const r=f(e);if(!0===t||"class_only"===t&&!r){const t=Object.getOwnPropertyDescriptors(e);delete t[n];let r=Reflect.ownKeys(t);for(let n=0;n<r.length;n++){const o=r[n],a=t[o];!1===a.writable&&(a.writable=!0,a.configurable=!0),(a.get||a.set)&&(t[o]={configurable:!0,writable:!0,enumerable:a.enumerable,value:e[o]})}return Object.create(s(e),t)}{const t=s(e);if(null!==t&&r)return{...e};const n=Object.create(t);return Object.assign(n,e)}}function S(e,t=!1){return v(e)||i(e)||!c(e)||(p(e)>1&&(e.set=e.add=e.clear=e.delete=g),Object.freeze(e),t&&Object.entries(e).forEach((([e,t])=>S(t,!0)))),e}function g(){a(2)}function v(e){return Object.isFrozen(e)}var O,w={};function E(e){const t=w[e];return t||a(0,e),t}function P(){return O}function j(e,t){t&&(E("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function D(e){A(e),e.drafts_.forEach(T),e.drafts_=null}function A(e){e===O&&(O=e.parent_)}function x(e){return O=function(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}(O,e)}function T(e){const t=e[n];0===t.type_||1===t.type_?t.revoke_():t.revoked_=!0}function N(e,r){r.unfinalizedDrafts_=r.drafts_.length;const o=r.drafts_[0];return void 0!==e&&e!==o?(o[n].modified_&&(D(r),a(4)),c(e)&&(e=z(r,e),r.parent_||F(r,e)),r.patches_&&E("Patches").generateReplacementPatches_(o[n].base_,e,r.patches_,r.inversePatches_)):e=z(r,o,[]),D(r),r.patches_&&r.patchListener_(r.patches_,r.inversePatches_),e!==t?e:void 0}function z(e,t,r){if(v(t))return t;const o=t[n];if(!o)return l(t,((n,a)=>L(e,o,t,n,a,r))),t;if(o.scope_!==e)return t;if(!o.modified_)return F(e,o.base_,!0),o.base_;if(!o.finalized_){o.finalized_=!0,o.scope_.unfinalizedDrafts_--;const t=o.copy_;let n=t,a=!1;3===o.type_&&(n=new Set(t),t.clear(),a=!0),l(n,((n,s)=>L(e,o,t,n,s,r,a))),F(e,t,!1),r&&e.patches_&&E("Patches").generatePatches_(o,r,e.patches_,e.inversePatches_)}return o.copy_}function L(e,t,r,n,o,s,u){if("production"!==process.env.NODE_ENV&&o===r&&a(5),i(o)){const a=z(e,o,s&&t&&3!==t.type_&&!d(t.assigned_,n)?s.concat(n):void 0);if(_(r,n,a),!i(a))return;e.canAutoFreeze_=!1}else u&&r.add(o);if(c(o)&&!v(o)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;z(e,o),(!t||!t.scope_.parent_)&&"symbol"!=typeof n&&Object.prototype.propertyIsEnumerable.call(r,n)&&F(e,o)}}function F(e,t,r=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&S(t,r)}var I={get(e,t){if(t===n)return e;const r=b(e);if(!d(r,t))return function(e,t,r){const n=R(t,r);return n?"value"in n?n.value:n.get?.call(e.draft_):void 0}(e,r,t);const o=r[t];return e.finalized_||!c(o)?o:o===M(e.base_,t)?(k(e),e.copy_[t]=K(o,e)):o},has:(e,t)=>t in b(e),ownKeys:e=>Reflect.ownKeys(b(e)),set(e,t,r){const o=R(b(e),t);if(o?.set)return o.set.call(e.draft_,r),!0;if(!e.modified_){const o=M(b(e),t),a=o?.[n];if(a&&a.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,o)&&(void 0!==r||d(e.base_,t)))return!0;k(e),V(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!==M(e.base_,t)||t in e.base_?(e.assigned_[t]=!1,k(e),V(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0),getOwnPropertyDescriptor(e,t){const r=b(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n&&{writable:!0,configurable:1!==e.type_||"length"!==t,enumerable:n.enumerable,value:r[t]}},defineProperty(){a(11)},getPrototypeOf:e=>s(e.base_),setPrototypeOf(){a(12)}},C={};function M(e,t){const r=e[n];return(r?b(r):e)[t]}function R(e,t){if(!(t in e))return;let r=s(e);for(;r;){const e=Object.getOwnPropertyDescriptor(r,t);if(e)return e;r=s(r)}}function V(e){e.modified_||(e.modified_=!0,e.parent_&&V(e.parent_))}function k(e){e.copy_||(e.copy_=m(e.base_,e.scope_.immer_.useStrictShallowCopy_))}l(I,((e,t)=>{C[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}})),C.deleteProperty=function(e,t){return"production"!==process.env.NODE_ENV&&isNaN(parseInt(t))&&a(13),C.set.call(this,e,t,void 0)},C.set=function(e,t,r){return"production"!==process.env.NODE_ENV&&"length"!==t&&isNaN(parseInt(t))&&a(14),I.set.call(this,e[0],t,r,e[0])};function K(e,t){const r=y(e)?E("MapSet").proxyMap_(e,t):h(e)?E("MapSet").proxySet_(e,t):function(e,t){const r=Array.isArray(e),n={type_:r?1:0,scope_:t?t.scope_:P(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1};let o=n,a=I;r&&(o=[n],a=C);const{revoke:s,proxy:i}=Proxy.revocable(o,a);return n.draft_=i,n.revoke_=s,i}(e,t);return(t?t.scope_:P()).drafts_.push(r),r}function $(e){if(!c(e)||v(e))return e;const t=e[n];let r;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,r=m(e,t.scope_.immer_.useStrictShallowCopy_)}else r=m(e,!0);return l(r,((e,t)=>{_(r,e,$(t))})),t&&(t.finalized_=!1),r}var G=new class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(e,r,n)=>{if("function"==typeof e&&"function"!=typeof r){const t=r;r=e;const n=this;return function(e=t,...o){return n.produce(e,(e=>r.call(this,e,...o)))}}let o;if("function"!=typeof r&&a(6),void 0!==n&&"function"!=typeof n&&a(7),c(e)){const t=x(this),a=K(e,void 0);let s=!0;try{o=r(a),s=!1}finally{s?D(t):A(t)}return j(t,n),N(o,t)}if(!e||"object"!=typeof e){if(o=r(e),void 0===o&&(o=e),o===t&&(o=void 0),this.autoFreeze_&&S(o,!0),n){const t=[],r=[];E("Patches").generateReplacementPatches_(e,o,t,r),n(t,r)}return o}a(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)}createDraft(e){c(e)||a(8),i(e)&&(e=function(e){return i(e)||a(10,e),$(e)}(e));const t=x(this),r=K(e,void 0);return r[n].isManual_=!0,A(t),r}finishDraft(e,t){const r=e&&e[n];(!r||!r.isManual_)&&a(9);const{scope_:o}=r;return j(o,t),N(void 0,o)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}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=E("Patches").applyPatches_;return i(e)?n(e,t):this.produce(e,(e=>n(e,t)))}},U=G.produce;G.produceWithPatches.bind(G),G.setAutoFreeze.bind(G),G.setUseStrictShallowCopy.bind(G),G.applyPatches.bind(G),G.createDraft.bind(G),G.finishDraft.bind(G);const B=(()=>{if("object"==typeof globalThis&&globalThis)return globalThis;if("object"==typeof self&&self)return self;if("object"==typeof window&&window)return window;if(typeof global<"u"&&"object"==typeof global&&global)return global;throw new Error("Unable to locate global `this`")})(),H=(e,t)=>{typeof B?.localStorage<"u"&&B.localStorage.setItem(e,JSON.stringify(t))},J=(e,t)=>{if(typeof B?.localStorage<"u"){const r=B.localStorage?.getItem(e);return r?JSON.parse(r):t}return t},q=(e,t)=>{typeof B?.localStorage<"u"&&B.localStorage.setItem(e,(e=>btoa(JSON.stringify(e)))(t))},W=(e,t)=>{if(typeof B?.localStorage<"u"){const r=B.localStorage?.getItem(e);return r?(e=>JSON.parse(atob(e)))(r):t}return t};var X,Y={exports:{}},Q={};
1
+ "use strict";var e=require("react"),t=Symbol.for("immer-nothing"),r=Symbol.for("immer-draftable"),n=Symbol.for("immer-state"),o="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 a(e,...t){if("production"!==process.env.NODE_ENV){const r=o[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 s=Object.getPrototypeOf;function i(e){return!!e&&!!e[n]}function c(e){return!!e&&(l(e)||Array.isArray(e)||!!e[r]||!!e.constructor?.[r]||y(e)||h(e))}var u=Object.prototype.constructor.toString();function l(e){if(!e||"object"!=typeof e)return!1;const t=s(e);if(null===t)return!0;const r=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return r===Object||"function"==typeof r&&Function.toString.call(r)===u}function f(e,t){0===p(e)?Reflect.ownKeys(e).forEach((r=>{t(r,e[r],e)})):e.forEach(((r,n)=>t(n,r,e)))}function p(e){const t=e[n];return t?t.type_:Array.isArray(e)?1:y(e)?2:h(e)?3:0}function d(e,t){return 2===p(e)?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function _(e,t,r){const n=p(e);2===n?e.set(t,r):3===n?e.add(r):e[t]=r}function y(e){return e instanceof Map}function h(e){return e instanceof Set}function b(e){return e.copy_||e.base_}function m(e,t){if(y(e))return new Map(e);if(h(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);const r=l(e);if(!0===t||"class_only"===t&&!r){const t=Object.getOwnPropertyDescriptors(e);delete t[n];let r=Reflect.ownKeys(t);for(let n=0;n<r.length;n++){const o=r[n],a=t[o];!1===a.writable&&(a.writable=!0,a.configurable=!0),(a.get||a.set)&&(t[o]={configurable:!0,writable:!0,enumerable:a.enumerable,value:e[o]})}return Object.create(s(e),t)}{const t=s(e);if(null!==t&&r)return{...e};const n=Object.create(t);return Object.assign(n,e)}}function S(e,t=!1){return v(e)||i(e)||!c(e)||(p(e)>1&&(e.set=e.add=e.clear=e.delete=g),Object.freeze(e),t&&Object.entries(e).forEach((([e,t])=>S(t,!0)))),e}function g(){a(2)}function v(e){return Object.isFrozen(e)}var O,w={};function E(e){const t=w[e];return t||a(0,e),t}function P(){return O}function j(e,t){t&&(E("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function D(e){A(e),e.drafts_.forEach(T),e.drafts_=null}function A(e){e===O&&(O=e.parent_)}function x(e){return O=function(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}(O,e)}function T(e){const t=e[n];0===t.type_||1===t.type_?t.revoke_():t.revoked_=!0}function N(e,r){r.unfinalizedDrafts_=r.drafts_.length;const o=r.drafts_[0];return void 0!==e&&e!==o?(o[n].modified_&&(D(r),a(4)),c(e)&&(e=z(r,e),r.parent_||$(r,e)),r.patches_&&E("Patches").generateReplacementPatches_(o[n].base_,e,r.patches_,r.inversePatches_)):e=z(r,o,[]),D(r),r.patches_&&r.patchListener_(r.patches_,r.inversePatches_),e!==t?e:void 0}function z(e,t,r){if(v(t))return t;const o=t[n];if(!o)return f(t,((n,a)=>L(e,o,t,n,a,r))),t;if(o.scope_!==e)return t;if(!o.modified_)return $(e,o.base_,!0),o.base_;if(!o.finalized_){o.finalized_=!0,o.scope_.unfinalizedDrafts_--;const t=o.copy_;let n=t,a=!1;3===o.type_&&(n=new Set(t),t.clear(),a=!0),f(n,((n,s)=>L(e,o,t,n,s,r,a))),$(e,t,!1),r&&e.patches_&&E("Patches").generatePatches_(o,r,e.patches_,e.inversePatches_)}return o.copy_}function L(e,t,r,n,o,s,u){if("production"!==process.env.NODE_ENV&&o===r&&a(5),i(o)){const a=z(e,o,s&&t&&3!==t.type_&&!d(t.assigned_,n)?s.concat(n):void 0);if(_(r,n,a),!i(a))return;e.canAutoFreeze_=!1}else u&&r.add(o);if(c(o)&&!v(o)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;z(e,o),(!t||!t.scope_.parent_)&&"symbol"!=typeof n&&Object.prototype.propertyIsEnumerable.call(r,n)&&$(e,o)}}function $(e,t,r=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&S(t,r)}var F={get(e,t){if(t===n)return e;const r=b(e);if(!d(r,t))return function(e,t,r){const n=R(t,r);return n?"value"in n?n.value:n.get?.call(e.draft_):void 0}(e,r,t);const o=r[t];return e.finalized_||!c(o)?o:o===C(e.base_,t)?(V(e),e.copy_[t]=k(o,e)):o},has:(e,t)=>t in b(e),ownKeys:e=>Reflect.ownKeys(b(e)),set(e,t,r){const o=R(b(e),t);if(o?.set)return o.set.call(e.draft_,r),!0;if(!e.modified_){const o=C(b(e),t),a=o?.[n];if(a&&a.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,o)&&(void 0!==r||d(e.base_,t)))return!0;V(e),M(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!==C(e.base_,t)||t in e.base_?(e.assigned_[t]=!1,V(e),M(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0),getOwnPropertyDescriptor(e,t){const r=b(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n&&{writable:!0,configurable:1!==e.type_||"length"!==t,enumerable:n.enumerable,value:r[t]}},defineProperty(){a(11)},getPrototypeOf:e=>s(e.base_),setPrototypeOf(){a(12)}},I={};function C(e,t){const r=e[n];return(r?b(r):e)[t]}function R(e,t){if(!(t in e))return;let r=s(e);for(;r;){const e=Object.getOwnPropertyDescriptor(r,t);if(e)return e;r=s(r)}}function M(e){e.modified_||(e.modified_=!0,e.parent_&&M(e.parent_))}function V(e){e.copy_||(e.copy_=m(e.base_,e.scope_.immer_.useStrictShallowCopy_))}f(F,((e,t)=>{I[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}})),I.deleteProperty=function(e,t){return"production"!==process.env.NODE_ENV&&isNaN(parseInt(t))&&a(13),I.set.call(this,e,t,void 0)},I.set=function(e,t,r){return"production"!==process.env.NODE_ENV&&"length"!==t&&isNaN(parseInt(t))&&a(14),F.set.call(this,e[0],t,r,e[0])};function k(e,t){const r=y(e)?E("MapSet").proxyMap_(e,t):h(e)?E("MapSet").proxySet_(e,t):function(e,t){const r=Array.isArray(e),n={type_:r?1:0,scope_:t?t.scope_:P(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1};let o=n,a=F;r&&(o=[n],a=I);const{revoke:s,proxy:i}=Proxy.revocable(o,a);return n.draft_=i,n.revoke_=s,i}(e,t);return(t?t.scope_:P()).drafts_.push(r),r}function K(e){if(!c(e)||v(e))return e;const t=e[n];let r;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,r=m(e,t.scope_.immer_.useStrictShallowCopy_)}else r=m(e,!0);return f(r,((e,t)=>{_(r,e,K(t))})),t&&(t.finalized_=!1),r}var G=new class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(e,r,n)=>{if("function"==typeof e&&"function"!=typeof r){const t=r;r=e;const n=this;return function(e=t,...o){return n.produce(e,(e=>r.call(this,e,...o)))}}let o;if("function"!=typeof r&&a(6),void 0!==n&&"function"!=typeof n&&a(7),c(e)){const t=x(this),a=k(e,void 0);let s=!0;try{o=r(a),s=!1}finally{s?D(t):A(t)}return j(t,n),N(o,t)}if(!e||"object"!=typeof e){if(o=r(e),void 0===o&&(o=e),o===t&&(o=void 0),this.autoFreeze_&&S(o,!0),n){const t=[],r=[];E("Patches").generateReplacementPatches_(e,o,t,r),n(t,r)}return o}a(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)}createDraft(e){c(e)||a(8),i(e)&&(e=function(e){return i(e)||a(10,e),K(e)}(e));const t=x(this),r=k(e,void 0);return r[n].isManual_=!0,A(t),r}finishDraft(e,t){const r=e&&e[n];(!r||!r.isManual_)&&a(9);const{scope_:o}=r;return j(o,t),N(void 0,o)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}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=E("Patches").applyPatches_;return i(e)?n(e,t):this.produce(e,(e=>n(e,t)))}},U=G.produce;G.produceWithPatches.bind(G),G.setAutoFreeze.bind(G),G.setUseStrictShallowCopy.bind(G),G.applyPatches.bind(G),G.createDraft.bind(G),G.finishDraft.bind(G);const B=(()=>{if("object"==typeof globalThis&&globalThis)return globalThis;if("object"==typeof self&&self)return self;if("object"==typeof window&&window)return window;if(typeof global<"u"&&"object"==typeof global&&global)return global;throw new Error("Unable to locate global `this`")})(),H=(e,t)=>{typeof B?.localStorage<"u"&&B.localStorage.setItem(e,JSON.stringify(t))},J=(e,t)=>{if(typeof B?.localStorage<"u"){const r=B.localStorage?.getItem(e);return r?JSON.parse(r):t}return t},q=(e,t)=>{typeof B?.localStorage<"u"&&B.localStorage.setItem(e,(e=>btoa(JSON.stringify(e)))(t))},W=(e,t)=>{if(typeof B?.localStorage<"u"){const r=B.localStorage?.getItem(e);return r?(e=>JSON.parse(atob(e)))(r):t}return t};var X,Y={exports:{}},Q={};
2
2
  /**
3
3
  * @license React
4
4
  * use-sync-external-store-shim.production.js
@@ -16,4 +16,4 @@
16
16
  *
17
17
  * This source code is licensed under the MIT license found in the
18
18
  * LICENSE file in the root directory of this source tree.
19
- */var re=(ee||(ee=1,"production"===process.env.NODE_ENV?Y.exports=function(){if(X)return Q;X=1;var t=e,r="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},n=t.useState,o=t.useEffect,a=t.useLayoutEffect,s=t.useDebugValue;function i(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!r(e,n)}catch{return!0}}var c=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?function(e,t){return t()}:function(e,t){var r=t(),c=n({inst:{value:r,getSnapshot:t}}),u=c[0].inst,f=c[1];return a((function(){u.value=r,u.getSnapshot=t,i(u)&&f({inst:u})}),[e,r,t]),o((function(){return i(u)&&f({inst:u}),e((function(){i(u)&&f({inst:u})}))}),[e]),s(r),r};return Q.useSyncExternalStore=void 0!==t.useSyncExternalStore?t.useSyncExternalStore:c,Q}():Y.exports=(Z||(Z=1,"production"!==process.env.NODE_ENV&&function(){function t(e){var t=e.getSnapshot;e=e.value;try{var r=t();return!n(e,r)}catch{return!0}}typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var r=e,n="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},o=r.useState,a=r.useEffect,s=r.useLayoutEffect,i=r.useDebugValue,c=!1,u=!1,f=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?function(e,t){return t()}:function(e,f){c||void 0===r.startTransition||(c=!0,console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var l=f();if(!u){var p=f();n(l,p)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),u=!0)}var d=(p=o({inst:{value:l,getSnapshot:f}}))[0].inst,_=p[1];return s((function(){d.value=l,d.getSnapshot=f,t(d)&&_({inst:d})}),[e,l,f]),a((function(){return t(d)&&_({inst:d}),e((function(){t(d)&&_({inst:d})}))}),[e]),i(l),l};te.useSyncExternalStore=void 0!==r.useSyncExternalStore?r.useSyncExternalStore:f,typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()),te)),Y.exports);const ne={},oe={},ae={},se=B.__STATE_JET_DEVTOOLS__,ie=(e,t)=>{ne[e]||(ne[e]=[]),void 0===oe[e]&&(oe[e]=-1),ne[e]=ne[e].slice(0,oe[e]+1),ne[e].push(t),oe[e]++,se?.updateState&&se.updateState(e,t,[...ne[e]])},ce=(e,t)=>{ae[e]||(ae[e]=[]);const r=performance.now();t();const n=performance.now()-r;ae[e].push(n),se?.updatePerformance&&se.updatePerformance(e,n)},ue=new Set,fe=new Map,le=new Map,pe={};let de=!1;const _e=(e,t)=>({...e,...t,lastUpdated:Date.now()});exports.derivedState=(e,t)=>()=>{const r=e.map((e=>e.useStore()));return t(...r)},exports.mergeCRDT=_e,exports.optimisticUpdate=async(e,t,r,n)=>{const o=e.useStore(),a=t(o);e.set(a);try{await r()}catch(t){console.error("API request failed:",t),e.set(n?n(o):o)}},exports.restoreEncryptedState=W,exports.restoreState=J,exports.saveEncryptedState=q,exports.saveState=H,exports.syncCRDT=(e,t)=>{const r=t.useStore(),n=_e(r,e);t.set(n)},exports.useStateGlobal=(e,t,r)=>{if(!fe.has(e)){let n=t;r?.persist&&(n=r?.encrypt?W(e,t):J(e,t)),fe.set(e,{value:n,listeners:new Set,...r})}const n=fe.get(e),o=async()=>{de=!1;const e=Array.from(le.entries());le.clear();for(const[t,r]of e){const e=fe.get(t);if(ue.has(t)){console.warn(`[state-jet] Skipping recursive middleware call for: ${t}`);continue}let n=r;const o=e.value;if(pe[t]||(pe[t]={past:[],present:o,future:[]}),pe[t].past.push(o),pe[t].present=n,pe[t].future=[],ue.add(t),e?.middleware)for(const r of e.middleware)try{console.log(`[state-jet] Running middleware for ${t}`);const e=r(t,o,n,(e=>{n=e}));if(void 0!==e)if(e instanceof Promise){const t=await e;void 0!==t&&(n=t)}else n=e}catch(e){console.error(`[state-jet] Middleware error in ${t}:`,e)}ue.delete(t),"function"==typeof n?e.value=U(e.value,n):o!==n&&(e.value=n,e.listeners.forEach((e=>e())),ie(t,n),ce(t,(()=>{})),e?.persist&&(e?.encrypt?q(t,n):H(t,n)))}le.clear()};return{useStore:()=>re.useSyncExternalStore((e=>(n.listeners.add(e),()=>n.listeners.delete(e))),(()=>n.value)),set:(t,r=!1)=>{const a=n.value,s="function"==typeof t?t(a):t;if(r)return n.value=s,void n.listeners.forEach((e=>e()));le.set(e,s),de||(de=!0,B?.requestAnimationFrame?B.requestAnimationFrame(o):o())},undo:()=>{pe[e]?.past.length&&(pe[e].future.unshift(pe[e].present),pe[e].present=pe[e].past.pop(),n.value=pe[e].present,n.listeners.forEach((e=>e())),(e=>{if(oe[e]>0)oe[e]--,ne[e][oe[e]]})(e))},redo:()=>{pe[e]?.future.length&&(pe[e].past.push(pe[e].present),pe[e].present=pe[e].future.shift(),n.value=pe[e].present,n.listeners.forEach((e=>e())),(e=>{if(oe[e]<ne[e]?.length-1)oe[e]++,ne[e][oe[e]]})(e))},clear:()=>{n.value=t,fe.clear(),Object.keys(pe).forEach((e=>delete pe[e]))}}};
19
+ */var re=(ee||(ee=1,"production"===process.env.NODE_ENV?Y.exports=function(){if(X)return Q;X=1;var t=e,r="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},n=t.useState,o=t.useEffect,a=t.useLayoutEffect,s=t.useDebugValue;function i(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!r(e,n)}catch{return!0}}var c=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?function(e,t){return t()}:function(e,t){var r=t(),c=n({inst:{value:r,getSnapshot:t}}),u=c[0].inst,l=c[1];return a((function(){u.value=r,u.getSnapshot=t,i(u)&&l({inst:u})}),[e,r,t]),o((function(){return i(u)&&l({inst:u}),e((function(){i(u)&&l({inst:u})}))}),[e]),s(r),r};return Q.useSyncExternalStore=void 0!==t.useSyncExternalStore?t.useSyncExternalStore:c,Q}():Y.exports=(Z||(Z=1,"production"!==process.env.NODE_ENV&&function(){function t(e){var t=e.getSnapshot;e=e.value;try{var r=t();return!n(e,r)}catch{return!0}}typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var r=e,n="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},o=r.useState,a=r.useEffect,s=r.useLayoutEffect,i=r.useDebugValue,c=!1,u=!1,l=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?function(e,t){return t()}:function(e,l){c||void 0===r.startTransition||(c=!0,console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var f=l();if(!u){var p=l();n(f,p)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),u=!0)}var d=(p=o({inst:{value:f,getSnapshot:l}}))[0].inst,_=p[1];return s((function(){d.value=f,d.getSnapshot=l,t(d)&&_({inst:d})}),[e,f,l]),a((function(){return t(d)&&_({inst:d}),e((function(){t(d)&&_({inst:d})}))}),[e]),i(f),f};te.useSyncExternalStore=void 0!==r.useSyncExternalStore?r.useSyncExternalStore:l,typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()),te)),Y.exports);const ne={},oe={},ae={},se=B.__STATE_JET_DEVTOOLS__,ie=(e,t)=>{ne[e]||(ne[e]=[]),void 0===oe[e]&&(oe[e]=-1),ne[e]=ne[e].slice(0,oe[e]+1),ne[e].push(t),oe[e]++,se?.updateState&&se.updateState(e,t,[...ne[e]])},ce=(e,t)=>{ae[e]||(ae[e]=[]);const r=performance.now();t();const n=performance.now()-r;ae[e].push(n),se?.updatePerformance&&se.updatePerformance(e,n)},ue=new Set,le=new Map,fe=new Map,pe={};let de=!1;const _e=e=>(le.has(e)||le.set(e,{}),(t,r,n)=>{const o=le.get(e);if(!o[t]){let a=r;n?.persist&&(a=n?.encrypt?W(`${e}:${t}`,r):J(`${e}:${t}`,r)),o[t]={value:a,listeners:new Set,...n}}const a=o[t],s=async()=>{de=!1;const r=Array.from(fe.entries());fe.clear();for(const[n,a]of r){const r=o[t];if(ue.has(n)){console.warn(`[state-jet] Skipping recursive middleware call for: ${n}`);continue}let s=a;const i=r.value;if(pe[n]||(pe[n]={past:[],present:i,future:[]}),pe[n].past.push(i),pe[n].present=s,pe[n].future=[],ue.add(n),r?.middleware)for(const e of r.middleware)try{console.log(`[state-jet] Running middleware for ${n}`);const t=e(n,i,s,(e=>{s=e}));if(void 0!==t)if(t instanceof Promise){const e=await t;void 0!==e&&(s=e)}else s=t}catch(e){console.error(`[state-jet] Middleware error in ${n}:`,e)}ue.delete(n),"function"==typeof s?r.value=U(r.value,s):i!==s&&(r.value=s,r.listeners.forEach((e=>e())),ie(`${e}.${n}`,s),ce(`${e}.${n}`,(()=>{})),r?.persist&&(r?.encrypt?q(`${e}:${n}`,s):H(`${e}:${n}`,s)))}fe.clear()};return{useState:()=>re.useSyncExternalStore((e=>(a.listeners.add(e),()=>a.listeners.delete(e))),(()=>a.value)),set:(e,r=!1)=>{const n=a.value,o="function"==typeof e?e(n):e;if(r)return a.value=o,void a.listeners.forEach((e=>e()));fe.set(t,o),de||(de=!0,B?.requestAnimationFrame?B.requestAnimationFrame(s):s())},undo:()=>{pe[t]?.past.length&&(pe[t].future.unshift(pe[t].present),pe[t].present=pe[t].past.pop(),a.value=pe[t].present,a.listeners.forEach((e=>e())),(e=>{if(oe[e]>0)oe[e]--,ne[e][oe[e]]})(t))},redo:()=>{pe[t]?.future.length&&(pe[t].past.push(pe[t].present),pe[t].present=pe[t].future.shift(),a.value=pe[t].present,a.listeners.forEach((e=>e())),(e=>{if(oe[e]<ne[e]?.length-1)oe[e]++,ne[e][oe[e]]})(t))},clear:()=>{a.value=r,le.clear(),Object.keys(pe).forEach((e=>delete pe[e]))}}}),ye=(e,t)=>({...e,...t,lastUpdated:Date.now()});exports.derivedState=(e,t)=>()=>{const r=e.map((e=>e.useState()));return t(...r)},exports.mergeCRDT=ye,exports.optimisticUpdate=async(e,t,r,n)=>{const o=e.useState(),a=t(o);e.set(a);try{await r()}catch(t){console.error("API request failed:",t),e.set(n?n(o):o)}},exports.restoreEncryptedState=W,exports.restoreState=J,exports.saveEncryptedState=q,exports.saveState=H,exports.syncCRDT=(e,t)=>{const r=t.useState(),n=ye(r,e);t.set(n)},exports.useSlice=_e,exports.useStateGlobal=(e,t,r)=>_e("global")(e,t,r),exports.useStore=t=>{const r=e.useRef(null);return r.current||(r.current=t()),r.current};
package/dist/index.d.ts CHANGED
@@ -1,4 +1,9 @@
1
- type Middleware<T> = (key: string, prev: T, next: T | Action<T>, set?: (value: T) => void) => T | void | Promise<void>;
1
+ type Middleware<T> = (key: string, prev: T | undefined, next: T | Action<T>, set?: (value: T) => void) => T | void | Promise<void>;
2
+ type Options<T> = {
3
+ middleware?: Middleware<T | undefined>[];
4
+ persist?: boolean;
5
+ encrypt?: boolean;
6
+ };
2
7
  type Action<T> = {
3
8
  type: string;
4
9
  payload?: T;
@@ -6,24 +11,34 @@ type Action<T> = {
6
11
  /**
7
12
  * Creates a global reactive state with middleware, persistence, and batch updates.
8
13
  */
9
- declare const useStateGlobal: <T>(key: string, initialValue?: T, options?: {
10
- middleware?: Middleware<T>[];
11
- persist?: boolean;
12
- encrypt?: boolean;
13
- }) => {
14
- useStore: () => T;
14
+ declare const useStateGlobal: <T>(key: string, initialValue?: T, options?: Options<T>) => {
15
+ useState: () => T | undefined;
16
+ set: (newValue: T | Action<T | undefined> | ((prev: T | undefined) => T | undefined) | undefined, immediate?: boolean) => void;
17
+ undo: () => void;
18
+ redo: () => void;
19
+ clear: () => void;
20
+ };
21
+ /**
22
+ * Creates a slice that can hold multiple state values.
23
+ */
24
+ declare const useSlice: (sliceName: string) => <T>(key: string, initialValue: T, options?: Options<T | undefined>) => {
25
+ useState: () => T;
15
26
  set: (newValue: T | ((prev: T) => T) | Action<T>, immediate?: boolean) => void;
16
27
  undo: () => void;
17
28
  redo: () => void;
18
29
  clear: () => void;
19
30
  };
31
+ /**
32
+ * Creates a store with multiple slices.
33
+ */
34
+ declare const useStore: <T extends Record<string, ReturnType<typeof useSlice>>>(initializer: () => T) => T;
20
35
 
21
36
  declare const saveState: <T>(key: string, value: T) => void;
22
37
  declare const restoreState: <T>(key: string, defaultValue?: T) => any;
23
38
 
24
39
  declare const optimisticUpdate: <T>(setState: {
25
40
  set: (value: T) => void;
26
- useStore: () => T;
41
+ useState: () => T;
27
42
  }, updateFn: (prevState: T) => T, apiCall: () => Promise<T>, rollbackFn?: (prevState: T) => T) => Promise<void>;
28
43
 
29
44
  declare const saveEncryptedState: <T>(key: string, value: T) => void;
@@ -31,12 +46,12 @@ declare const restoreEncryptedState: <T>(key: string, defaultValue?: T) => unkno
31
46
 
32
47
  declare const mergeCRDT: <T>(localState: T, remoteState: T) => T;
33
48
  declare const syncCRDT: <T>(remoteState: T, setState: {
34
- useStore: () => T;
49
+ useState: () => T;
35
50
  set: (state: T) => void;
36
51
  }) => void;
37
52
 
38
53
  declare const derivedState: <T, D extends {
39
- useStore: () => unknown;
40
- }[]>(dependencies: D, computeFn: (...values: { [K in keyof D]: ReturnType<D[K]["useStore"]>; }) => T) => () => T;
54
+ useState: () => unknown;
55
+ }[]>(dependencies: D, computeFn: (...values: { [K in keyof D]: ReturnType<D[K]["useState"]>; }) => T) => () => T;
41
56
 
42
- export { derivedState, mergeCRDT, optimisticUpdate, restoreEncryptedState, restoreState, saveEncryptedState, saveState, syncCRDT, useStateGlobal };
57
+ export { derivedState, mergeCRDT, optimisticUpdate, restoreEncryptedState, restoreState, saveEncryptedState, saveState, syncCRDT, useSlice, useStateGlobal, useStore };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import e from"react";var t=Symbol.for("immer-nothing"),r=Symbol.for("immer-draftable"),n=Symbol.for("immer-state"),o="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 a(e,...t){if("production"!==process.env.NODE_ENV){const r=o[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 s=Object.getPrototypeOf;function i(e){return!!e&&!!e[n]}function c(e){return!!e&&(f(e)||Array.isArray(e)||!!e[r]||!!e.constructor?.[r]||y(e)||h(e))}var u=Object.prototype.constructor.toString();function f(e){if(!e||"object"!=typeof e)return!1;const t=s(e);if(null===t)return!0;const r=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return r===Object||"function"==typeof r&&Function.toString.call(r)===u}function l(e,t){0===p(e)?Reflect.ownKeys(e).forEach((r=>{t(r,e[r],e)})):e.forEach(((r,n)=>t(n,r,e)))}function p(e){const t=e[n];return t?t.type_:Array.isArray(e)?1:y(e)?2:h(e)?3:0}function d(e,t){return 2===p(e)?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function _(e,t,r){const n=p(e);2===n?e.set(t,r):3===n?e.add(r):e[t]=r}function y(e){return e instanceof Map}function h(e){return e instanceof Set}function b(e){return e.copy_||e.base_}function m(e,t){if(y(e))return new Map(e);if(h(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);const r=f(e);if(!0===t||"class_only"===t&&!r){const t=Object.getOwnPropertyDescriptors(e);delete t[n];let r=Reflect.ownKeys(t);for(let n=0;n<r.length;n++){const o=r[n],a=t[o];!1===a.writable&&(a.writable=!0,a.configurable=!0),(a.get||a.set)&&(t[o]={configurable:!0,writable:!0,enumerable:a.enumerable,value:e[o]})}return Object.create(s(e),t)}{const t=s(e);if(null!==t&&r)return{...e};const n=Object.create(t);return Object.assign(n,e)}}function S(e,t=!1){return v(e)||i(e)||!c(e)||(p(e)>1&&(e.set=e.add=e.clear=e.delete=g),Object.freeze(e),t&&Object.entries(e).forEach((([e,t])=>S(t,!0)))),e}function g(){a(2)}function v(e){return Object.isFrozen(e)}var O,w={};function E(e){const t=w[e];return t||a(0,e),t}function P(){return O}function j(e,t){t&&(E("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function D(e){A(e),e.drafts_.forEach(N),e.drafts_=null}function A(e){e===O&&(O=e.parent_)}function T(e){return O=function(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}(O,e)}function N(e){const t=e[n];0===t.type_||1===t.type_?t.revoke_():t.revoked_=!0}function z(e,r){r.unfinalizedDrafts_=r.drafts_.length;const o=r.drafts_[0];return void 0!==e&&e!==o?(o[n].modified_&&(D(r),a(4)),c(e)&&(e=L(r,e),r.parent_||F(r,e)),r.patches_&&E("Patches").generateReplacementPatches_(o[n].base_,e,r.patches_,r.inversePatches_)):e=L(r,o,[]),D(r),r.patches_&&r.patchListener_(r.patches_,r.inversePatches_),e!==t?e:void 0}function L(e,t,r){if(v(t))return t;const o=t[n];if(!o)return l(t,((n,a)=>x(e,o,t,n,a,r))),t;if(o.scope_!==e)return t;if(!o.modified_)return F(e,o.base_,!0),o.base_;if(!o.finalized_){o.finalized_=!0,o.scope_.unfinalizedDrafts_--;const t=o.copy_;let n=t,a=!1;3===o.type_&&(n=new Set(t),t.clear(),a=!0),l(n,((n,s)=>x(e,o,t,n,s,r,a))),F(e,t,!1),r&&e.patches_&&E("Patches").generatePatches_(o,r,e.patches_,e.inversePatches_)}return o.copy_}function x(e,t,r,n,o,s,u){if("production"!==process.env.NODE_ENV&&o===r&&a(5),i(o)){const a=L(e,o,s&&t&&3!==t.type_&&!d(t.assigned_,n)?s.concat(n):void 0);if(_(r,n,a),!i(a))return;e.canAutoFreeze_=!1}else u&&r.add(o);if(c(o)&&!v(o)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;L(e,o),(!t||!t.scope_.parent_)&&"symbol"!=typeof n&&Object.prototype.propertyIsEnumerable.call(r,n)&&F(e,o)}}function F(e,t,r=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&S(t,r)}var I={get(e,t){if(t===n)return e;const r=b(e);if(!d(r,t))return function(e,t,r){const n=R(t,r);return n?"value"in n?n.value:n.get?.call(e.draft_):void 0}(e,r,t);const o=r[t];return e.finalized_||!c(o)?o:o===M(e.base_,t)?(k(e),e.copy_[t]=K(o,e)):o},has:(e,t)=>t in b(e),ownKeys:e=>Reflect.ownKeys(b(e)),set(e,t,r){const o=R(b(e),t);if(o?.set)return o.set.call(e.draft_,r),!0;if(!e.modified_){const o=M(b(e),t),a=o?.[n];if(a&&a.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,o)&&(void 0!==r||d(e.base_,t)))return!0;k(e),V(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!==M(e.base_,t)||t in e.base_?(e.assigned_[t]=!1,k(e),V(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0),getOwnPropertyDescriptor(e,t){const r=b(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n&&{writable:!0,configurable:1!==e.type_||"length"!==t,enumerable:n.enumerable,value:r[t]}},defineProperty(){a(11)},getPrototypeOf:e=>s(e.base_),setPrototypeOf(){a(12)}},C={};function M(e,t){const r=e[n];return(r?b(r):e)[t]}function R(e,t){if(!(t in e))return;let r=s(e);for(;r;){const e=Object.getOwnPropertyDescriptor(r,t);if(e)return e;r=s(r)}}function V(e){e.modified_||(e.modified_=!0,e.parent_&&V(e.parent_))}function k(e){e.copy_||(e.copy_=m(e.base_,e.scope_.immer_.useStrictShallowCopy_))}l(I,((e,t)=>{C[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}})),C.deleteProperty=function(e,t){return"production"!==process.env.NODE_ENV&&isNaN(parseInt(t))&&a(13),C.set.call(this,e,t,void 0)},C.set=function(e,t,r){return"production"!==process.env.NODE_ENV&&"length"!==t&&isNaN(parseInt(t))&&a(14),I.set.call(this,e[0],t,r,e[0])};function K(e,t){const r=y(e)?E("MapSet").proxyMap_(e,t):h(e)?E("MapSet").proxySet_(e,t):function(e,t){const r=Array.isArray(e),n={type_:r?1:0,scope_:t?t.scope_:P(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1};let o=n,a=I;r&&(o=[n],a=C);const{revoke:s,proxy:i}=Proxy.revocable(o,a);return n.draft_=i,n.revoke_=s,i}(e,t);return(t?t.scope_:P()).drafts_.push(r),r}function $(e){if(!c(e)||v(e))return e;const t=e[n];let r;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,r=m(e,t.scope_.immer_.useStrictShallowCopy_)}else r=m(e,!0);return l(r,((e,t)=>{_(r,e,$(t))})),t&&(t.finalized_=!1),r}var G=new class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(e,r,n)=>{if("function"==typeof e&&"function"!=typeof r){const t=r;r=e;const n=this;return function(e=t,...o){return n.produce(e,(e=>r.call(this,e,...o)))}}let o;if("function"!=typeof r&&a(6),void 0!==n&&"function"!=typeof n&&a(7),c(e)){const t=T(this),a=K(e,void 0);let s=!0;try{o=r(a),s=!1}finally{s?D(t):A(t)}return j(t,n),z(o,t)}if(!e||"object"!=typeof e){if(o=r(e),void 0===o&&(o=e),o===t&&(o=void 0),this.autoFreeze_&&S(o,!0),n){const t=[],r=[];E("Patches").generateReplacementPatches_(e,o,t,r),n(t,r)}return o}a(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)}createDraft(e){c(e)||a(8),i(e)&&(e=function(e){return i(e)||a(10,e),$(e)}(e));const t=T(this),r=K(e,void 0);return r[n].isManual_=!0,A(t),r}finishDraft(e,t){const r=e&&e[n];(!r||!r.isManual_)&&a(9);const{scope_:o}=r;return j(o,t),z(void 0,o)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}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=E("Patches").applyPatches_;return i(e)?n(e,t):this.produce(e,(e=>n(e,t)))}},U=G.produce;G.produceWithPatches.bind(G),G.setAutoFreeze.bind(G),G.setUseStrictShallowCopy.bind(G),G.applyPatches.bind(G),G.createDraft.bind(G),G.finishDraft.bind(G);const B=(()=>{if("object"==typeof globalThis&&globalThis)return globalThis;if("object"==typeof self&&self)return self;if("object"==typeof window&&window)return window;if(typeof global<"u"&&"object"==typeof global&&global)return global;throw new Error("Unable to locate global `this`")})(),H=(e,t)=>{typeof B?.localStorage<"u"&&B.localStorage.setItem(e,JSON.stringify(t))},J=(e,t)=>{if(typeof B?.localStorage<"u"){const r=B.localStorage?.getItem(e);return r?JSON.parse(r):t}return t},W=(e,t)=>{typeof B?.localStorage<"u"&&B.localStorage.setItem(e,(e=>btoa(JSON.stringify(e)))(t))},q=(e,t)=>{if(typeof B?.localStorage<"u"){const r=B.localStorage?.getItem(e);return r?(e=>JSON.parse(atob(e)))(r):t}return t};var X,Y={exports:{}},Q={};
1
+ import e,{useRef as t}from"react";var r=Symbol.for("immer-nothing"),n=Symbol.for("immer-draftable"),o=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 s(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 i=Object.getPrototypeOf;function c(e){return!!e&&!!e[o]}function u(e){return!!e&&(l(e)||Array.isArray(e)||!!e[n]||!!e.constructor?.[n]||h(e)||b(e))}var f=Object.prototype.constructor.toString();function l(e){if(!e||"object"!=typeof e)return!1;const t=i(e);if(null===t)return!0;const r=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return r===Object||"function"==typeof r&&Function.toString.call(r)===f}function p(e,t){0===d(e)?Reflect.ownKeys(e).forEach((r=>{t(r,e[r],e)})):e.forEach(((r,n)=>t(n,r,e)))}function d(e){const t=e[o];return t?t.type_:Array.isArray(e)?1:h(e)?2:b(e)?3:0}function _(e,t){return 2===d(e)?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function y(e,t,r){const n=d(e);2===n?e.set(t,r):3===n?e.add(r):e[t]=r}function h(e){return e instanceof Map}function b(e){return e instanceof Set}function m(e){return e.copy_||e.base_}function S(e,t){if(h(e))return new Map(e);if(b(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);const r=l(e);if(!0===t||"class_only"===t&&!r){const t=Object.getOwnPropertyDescriptors(e);delete t[o];let r=Reflect.ownKeys(t);for(let n=0;n<r.length;n++){const o=r[n],a=t[o];!1===a.writable&&(a.writable=!0,a.configurable=!0),(a.get||a.set)&&(t[o]={configurable:!0,writable:!0,enumerable:a.enumerable,value:e[o]})}return Object.create(i(e),t)}{const t=i(e);if(null!==t&&r)return{...e};const n=Object.create(t);return Object.assign(n,e)}}function g(e,t=!1){return O(e)||c(e)||!u(e)||(d(e)>1&&(e.set=e.add=e.clear=e.delete=v),Object.freeze(e),t&&Object.entries(e).forEach((([e,t])=>g(t,!0)))),e}function v(){s(2)}function O(e){return Object.isFrozen(e)}var w,E={};function P(e){const t=E[e];return t||s(0,e),t}function j(){return w}function D(e,t){t&&(P("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function A(e){T(e),e.drafts_.forEach(z),e.drafts_=null}function T(e){e===w&&(w=e.parent_)}function N(e){return w=function(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}(w,e)}function z(e){const t=e[o];0===t.type_||1===t.type_?t.revoke_():t.revoked_=!0}function L(e,t){t.unfinalizedDrafts_=t.drafts_.length;const n=t.drafts_[0];return void 0!==e&&e!==n?(n[o].modified_&&(A(t),s(4)),u(e)&&(e=$(t,e),t.parent_||F(t,e)),t.patches_&&P("Patches").generateReplacementPatches_(n[o].base_,e,t.patches_,t.inversePatches_)):e=$(t,n,[]),A(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==r?e:void 0}function $(e,t,r){if(O(t))return t;const n=t[o];if(!n)return p(t,((o,a)=>x(e,n,t,o,a,r))),t;if(n.scope_!==e)return t;if(!n.modified_)return F(e,n.base_,!0),n.base_;if(!n.finalized_){n.finalized_=!0,n.scope_.unfinalizedDrafts_--;const t=n.copy_;let o=t,a=!1;3===n.type_&&(o=new Set(t),t.clear(),a=!0),p(o,((o,s)=>x(e,n,t,o,s,r,a))),F(e,t,!1),r&&e.patches_&&P("Patches").generatePatches_(n,r,e.patches_,e.inversePatches_)}return n.copy_}function x(e,t,r,n,o,a,i){if("production"!==process.env.NODE_ENV&&o===r&&s(5),c(o)){const s=$(e,o,a&&t&&3!==t.type_&&!_(t.assigned_,n)?a.concat(n):void 0);if(y(r,n,s),!c(s))return;e.canAutoFreeze_=!1}else i&&r.add(o);if(u(o)&&!O(o)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;$(e,o),(!t||!t.scope_.parent_)&&"symbol"!=typeof n&&Object.prototype.propertyIsEnumerable.call(r,n)&&F(e,o)}}function F(e,t,r=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&g(t,r)}var I={get(e,t){if(t===o)return e;const r=m(e);if(!_(r,t))return function(e,t,r){const n=M(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_||!u(n)?n:n===R(e.base_,t)?(k(e),e.copy_[t]=K(n,e)):n},has:(e,t)=>t in m(e),ownKeys:e=>Reflect.ownKeys(m(e)),set(e,t,r){const n=M(m(e),t);if(n?.set)return n.set.call(e.draft_,r),!0;if(!e.modified_){const n=R(m(e),t),a=n?.[o];if(a&&a.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||_(e.base_,t)))return!0;k(e),V(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!==R(e.base_,t)||t in e.base_?(e.assigned_[t]=!1,k(e),V(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0),getOwnPropertyDescriptor(e,t){const r=m(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n&&{writable:!0,configurable:1!==e.type_||"length"!==t,enumerable:n.enumerable,value:r[t]}},defineProperty(){s(11)},getPrototypeOf:e=>i(e.base_),setPrototypeOf(){s(12)}},C={};function R(e,t){const r=e[o];return(r?m(r):e)[t]}function M(e,t){if(!(t in e))return;let r=i(e);for(;r;){const e=Object.getOwnPropertyDescriptor(r,t);if(e)return e;r=i(r)}}function V(e){e.modified_||(e.modified_=!0,e.parent_&&V(e.parent_))}function k(e){e.copy_||(e.copy_=S(e.base_,e.scope_.immer_.useStrictShallowCopy_))}p(I,((e,t)=>{C[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}})),C.deleteProperty=function(e,t){return"production"!==process.env.NODE_ENV&&isNaN(parseInt(t))&&s(13),C.set.call(this,e,t,void 0)},C.set=function(e,t,r){return"production"!==process.env.NODE_ENV&&"length"!==t&&isNaN(parseInt(t))&&s(14),I.set.call(this,e[0],t,r,e[0])};function K(e,t){const r=h(e)?P("MapSet").proxyMap_(e,t):b(e)?P("MapSet").proxySet_(e,t):function(e,t){const r=Array.isArray(e),n={type_:r?1:0,scope_:t?t.scope_:j(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1};let o=n,a=I;r&&(o=[n],a=C);const{revoke:s,proxy:i}=Proxy.revocable(o,a);return n.draft_=i,n.revoke_=s,i}(e,t);return(t?t.scope_:j()).drafts_.push(r),r}function G(e){if(!u(e)||O(e))return e;const t=e[o];let r;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,r=S(e,t.scope_.immer_.useStrictShallowCopy_)}else r=S(e,!0);return p(r,((e,t)=>{y(r,e,G(t))})),t&&(t.finalized_=!1),r}var U=new class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(e,t,n)=>{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&&s(6),void 0!==n&&"function"!=typeof n&&s(7),u(e)){const r=N(this),a=K(e,void 0);let s=!0;try{o=t(a),s=!1}finally{s?A(r):T(r)}return D(r,n),L(o,r)}if(!e||"object"!=typeof e){if(o=t(e),void 0===o&&(o=e),o===r&&(o=void 0),this.autoFreeze_&&g(o,!0),n){const t=[],r=[];P("Patches").generateReplacementPatches_(e,o,t,r),n(t,r)}return o}s(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)}createDraft(e){u(e)||s(8),c(e)&&(e=function(e){return c(e)||s(10,e),G(e)}(e));const t=N(this),r=K(e,void 0);return r[o].isManual_=!0,T(t),r}finishDraft(e,t){const r=e&&e[o];(!r||!r.isManual_)&&s(9);const{scope_:n}=r;return D(n,t),L(void 0,n)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}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=P("Patches").applyPatches_;return c(e)?n(e,t):this.produce(e,(e=>n(e,t)))}},B=U.produce;U.produceWithPatches.bind(U),U.setAutoFreeze.bind(U),U.setUseStrictShallowCopy.bind(U),U.applyPatches.bind(U),U.createDraft.bind(U),U.finishDraft.bind(U);const H=(()=>{if("object"==typeof globalThis&&globalThis)return globalThis;if("object"==typeof self&&self)return self;if("object"==typeof window&&window)return window;if(typeof global<"u"&&"object"==typeof global&&global)return global;throw new Error("Unable to locate global `this`")})(),J=(e,t)=>{typeof H?.localStorage<"u"&&H.localStorage.setItem(e,JSON.stringify(t))},W=(e,t)=>{if(typeof H?.localStorage<"u"){const r=H.localStorage?.getItem(e);return r?JSON.parse(r):t}return t},q=(e,t)=>{typeof H?.localStorage<"u"&&H.localStorage.setItem(e,(e=>btoa(JSON.stringify(e)))(t))},X=(e,t)=>{if(typeof H?.localStorage<"u"){const r=H.localStorage?.getItem(e);return r?(e=>JSON.parse(atob(e)))(r):t}return t};var Y,Q={exports:{}},Z={};
2
2
  /**
3
3
  * @license React
4
4
  * use-sync-external-store-shim.production.js
@@ -7,7 +7,7 @@ import e from"react";var t=Symbol.for("immer-nothing"),r=Symbol.for("immer-draft
7
7
  *
8
8
  * This source code is licensed under the MIT license found in the
9
9
  * LICENSE file in the root directory of this source tree.
10
- */var Z,ee,te={};
10
+ */var ee,te,re={};
11
11
  /**
12
12
  * @license React
13
13
  * use-sync-external-store-shim.development.js
@@ -16,4 +16,4 @@ import e from"react";var t=Symbol.for("immer-nothing"),r=Symbol.for("immer-draft
16
16
  *
17
17
  * This source code is licensed under the MIT license found in the
18
18
  * LICENSE file in the root directory of this source tree.
19
- */var re=(ee||(ee=1,"production"===process.env.NODE_ENV?Y.exports=function(){if(X)return Q;X=1;var t=e,r="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},n=t.useState,o=t.useEffect,a=t.useLayoutEffect,s=t.useDebugValue;function i(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!r(e,n)}catch{return!0}}var c=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?function(e,t){return t()}:function(e,t){var r=t(),c=n({inst:{value:r,getSnapshot:t}}),u=c[0].inst,f=c[1];return a((function(){u.value=r,u.getSnapshot=t,i(u)&&f({inst:u})}),[e,r,t]),o((function(){return i(u)&&f({inst:u}),e((function(){i(u)&&f({inst:u})}))}),[e]),s(r),r};return Q.useSyncExternalStore=void 0!==t.useSyncExternalStore?t.useSyncExternalStore:c,Q}():Y.exports=(Z||(Z=1,"production"!==process.env.NODE_ENV&&function(){function t(e){var t=e.getSnapshot;e=e.value;try{var r=t();return!n(e,r)}catch{return!0}}typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var r=e,n="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},o=r.useState,a=r.useEffect,s=r.useLayoutEffect,i=r.useDebugValue,c=!1,u=!1,f=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?function(e,t){return t()}:function(e,f){c||void 0===r.startTransition||(c=!0,console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var l=f();if(!u){var p=f();n(l,p)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),u=!0)}var d=(p=o({inst:{value:l,getSnapshot:f}}))[0].inst,_=p[1];return s((function(){d.value=l,d.getSnapshot=f,t(d)&&_({inst:d})}),[e,l,f]),a((function(){return t(d)&&_({inst:d}),e((function(){t(d)&&_({inst:d})}))}),[e]),i(l),l};te.useSyncExternalStore=void 0!==r.useSyncExternalStore?r.useSyncExternalStore:f,typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()),te)),Y.exports);const ne={},oe={},ae={},se=B.__STATE_JET_DEVTOOLS__,ie=(e,t)=>{ne[e]||(ne[e]=[]),void 0===oe[e]&&(oe[e]=-1),ne[e]=ne[e].slice(0,oe[e]+1),ne[e].push(t),oe[e]++,se?.updateState&&se.updateState(e,t,[...ne[e]])},ce=(e,t)=>{ae[e]||(ae[e]=[]);const r=performance.now();t();const n=performance.now()-r;ae[e].push(n),se?.updatePerformance&&se.updatePerformance(e,n)},ue=new Set,fe=new Map,le=new Map,pe={};let de=!1;const _e=(e,t,r)=>{if(!fe.has(e)){let n=t;r?.persist&&(n=r?.encrypt?q(e,t):J(e,t)),fe.set(e,{value:n,listeners:new Set,...r})}const n=fe.get(e),o=async()=>{de=!1;const e=Array.from(le.entries());le.clear();for(const[t,r]of e){const e=fe.get(t);if(ue.has(t)){console.warn(`[state-jet] Skipping recursive middleware call for: ${t}`);continue}let n=r;const o=e.value;if(pe[t]||(pe[t]={past:[],present:o,future:[]}),pe[t].past.push(o),pe[t].present=n,pe[t].future=[],ue.add(t),e?.middleware)for(const r of e.middleware)try{console.log(`[state-jet] Running middleware for ${t}`);const e=r(t,o,n,(e=>{n=e}));if(void 0!==e)if(e instanceof Promise){const t=await e;void 0!==t&&(n=t)}else n=e}catch(e){console.error(`[state-jet] Middleware error in ${t}:`,e)}ue.delete(t),"function"==typeof n?e.value=U(e.value,n):o!==n&&(e.value=n,e.listeners.forEach((e=>e())),ie(t,n),ce(t,(()=>{})),e?.persist&&(e?.encrypt?W(t,n):H(t,n)))}le.clear()};return{useStore:()=>re.useSyncExternalStore((e=>(n.listeners.add(e),()=>n.listeners.delete(e))),(()=>n.value)),set:(t,r=!1)=>{const a=n.value,s="function"==typeof t?t(a):t;if(r)return n.value=s,void n.listeners.forEach((e=>e()));le.set(e,s),de||(de=!0,B?.requestAnimationFrame?B.requestAnimationFrame(o):o())},undo:()=>{pe[e]?.past.length&&(pe[e].future.unshift(pe[e].present),pe[e].present=pe[e].past.pop(),n.value=pe[e].present,n.listeners.forEach((e=>e())),(e=>{if(oe[e]>0)oe[e]--,ne[e][oe[e]]})(e))},redo:()=>{pe[e]?.future.length&&(pe[e].past.push(pe[e].present),pe[e].present=pe[e].future.shift(),n.value=pe[e].present,n.listeners.forEach((e=>e())),(e=>{if(oe[e]<ne[e]?.length-1)oe[e]++,ne[e][oe[e]]})(e))},clear:()=>{n.value=t,fe.clear(),Object.keys(pe).forEach((e=>delete pe[e]))}}},ye=async(e,t,r,n)=>{const o=e.useStore(),a=t(o);e.set(a);try{await r()}catch(t){console.error("API request failed:",t),e.set(n?n(o):o)}},he=(e,t)=>({...e,...t,lastUpdated:Date.now()}),be=(e,t)=>{const r=t.useStore(),n=he(r,e);t.set(n)},me=(e,t)=>()=>{const r=e.map((e=>e.useStore()));return t(...r)};export{me as derivedState,he as mergeCRDT,ye as optimisticUpdate,q as restoreEncryptedState,J as restoreState,W as saveEncryptedState,H as saveState,be as syncCRDT,_e as useStateGlobal};
19
+ */var ne=(te||(te=1,"production"===process.env.NODE_ENV?Q.exports=function(){if(Y)return Z;Y=1;var t=e,r="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},n=t.useState,o=t.useEffect,a=t.useLayoutEffect,s=t.useDebugValue;function i(e){var t=e.getSnapshot;e=e.value;try{var n=t();return!r(e,n)}catch{return!0}}var c=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?function(e,t){return t()}:function(e,t){var r=t(),c=n({inst:{value:r,getSnapshot:t}}),u=c[0].inst,f=c[1];return a((function(){u.value=r,u.getSnapshot=t,i(u)&&f({inst:u})}),[e,r,t]),o((function(){return i(u)&&f({inst:u}),e((function(){i(u)&&f({inst:u})}))}),[e]),s(r),r};return Z.useSyncExternalStore=void 0!==t.useSyncExternalStore?t.useSyncExternalStore:c,Z}():Q.exports=(ee||(ee=1,"production"!==process.env.NODE_ENV&&function(){function t(e){var t=e.getSnapshot;e=e.value;try{var r=t();return!n(e,r)}catch{return!0}}typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());var r=e,n="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},o=r.useState,a=r.useEffect,s=r.useLayoutEffect,i=r.useDebugValue,c=!1,u=!1,f=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?function(e,t){return t()}:function(e,f){c||void 0===r.startTransition||(c=!0,console.error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var l=f();if(!u){var p=f();n(l,p)||(console.error("The result of getSnapshot should be cached to avoid an infinite loop"),u=!0)}var d=(p=o({inst:{value:l,getSnapshot:f}}))[0].inst,_=p[1];return s((function(){d.value=l,d.getSnapshot=f,t(d)&&_({inst:d})}),[e,l,f]),a((function(){return t(d)&&_({inst:d}),e((function(){t(d)&&_({inst:d})}))}),[e]),i(l),l};re.useSyncExternalStore=void 0!==r.useSyncExternalStore?r.useSyncExternalStore:f,typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&"function"==typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error())}()),re)),Q.exports);const oe={},ae={},se={},ie=H.__STATE_JET_DEVTOOLS__,ce=(e,t)=>{oe[e]||(oe[e]=[]),void 0===ae[e]&&(ae[e]=-1),oe[e]=oe[e].slice(0,ae[e]+1),oe[e].push(t),ae[e]++,ie?.updateState&&ie.updateState(e,t,[...oe[e]])},ue=(e,t)=>{se[e]||(se[e]=[]);const r=performance.now();t();const n=performance.now()-r;se[e].push(n),ie?.updatePerformance&&ie.updatePerformance(e,n)},fe=new Set,le=new Map,pe=new Map,de={};let _e=!1;const ye=(e,t,r)=>he("global")(e,t,r),he=e=>(le.has(e)||le.set(e,{}),(t,r,n)=>{const o=le.get(e);if(!o[t]){let a=r;n?.persist&&(a=n?.encrypt?X(`${e}:${t}`,r):W(`${e}:${t}`,r)),o[t]={value:a,listeners:new Set,...n}}const a=o[t],s=async()=>{_e=!1;const r=Array.from(pe.entries());pe.clear();for(const[n,a]of r){const r=o[t];if(fe.has(n)){console.warn(`[state-jet] Skipping recursive middleware call for: ${n}`);continue}let s=a;const i=r.value;if(de[n]||(de[n]={past:[],present:i,future:[]}),de[n].past.push(i),de[n].present=s,de[n].future=[],fe.add(n),r?.middleware)for(const e of r.middleware)try{console.log(`[state-jet] Running middleware for ${n}`);const t=e(n,i,s,(e=>{s=e}));if(void 0!==t)if(t instanceof Promise){const e=await t;void 0!==e&&(s=e)}else s=t}catch(e){console.error(`[state-jet] Middleware error in ${n}:`,e)}fe.delete(n),"function"==typeof s?r.value=B(r.value,s):i!==s&&(r.value=s,r.listeners.forEach((e=>e())),ce(`${e}.${n}`,s),ue(`${e}.${n}`,(()=>{})),r?.persist&&(r?.encrypt?q(`${e}:${n}`,s):J(`${e}:${n}`,s)))}pe.clear()};return{useState:()=>ne.useSyncExternalStore((e=>(a.listeners.add(e),()=>a.listeners.delete(e))),(()=>a.value)),set:(e,r=!1)=>{const n=a.value,o="function"==typeof e?e(n):e;if(r)return a.value=o,void a.listeners.forEach((e=>e()));pe.set(t,o),_e||(_e=!0,H?.requestAnimationFrame?H.requestAnimationFrame(s):s())},undo:()=>{de[t]?.past.length&&(de[t].future.unshift(de[t].present),de[t].present=de[t].past.pop(),a.value=de[t].present,a.listeners.forEach((e=>e())),(e=>{if(ae[e]>0)ae[e]--,oe[e][ae[e]]})(t))},redo:()=>{de[t]?.future.length&&(de[t].past.push(de[t].present),de[t].present=de[t].future.shift(),a.value=de[t].present,a.listeners.forEach((e=>e())),(e=>{if(ae[e]<oe[e]?.length-1)ae[e]++,oe[e][ae[e]]})(t))},clear:()=>{a.value=r,le.clear(),Object.keys(de).forEach((e=>delete de[e]))}}}),be=e=>{const r=t(null);return r.current||(r.current=e()),r.current},me=async(e,t,r,n)=>{const o=e.useState(),a=t(o);e.set(a);try{await r()}catch(t){console.error("API request failed:",t),e.set(n?n(o):o)}},Se=(e,t)=>({...e,...t,lastUpdated:Date.now()}),ge=(e,t)=>{const r=t.useState(),n=Se(r,e);t.set(n)},ve=(e,t)=>()=>{const r=e.map((e=>e.useState()));return t(...r)};export{ve as derivedState,Se as mergeCRDT,me as optimisticUpdate,X as restoreEncryptedState,W as restoreState,q as saveEncryptedState,J as saveState,ge as syncCRDT,he as useSlice,ye as useStateGlobal,be as useStore};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-jet",
3
- "version": "1.3.1",
3
+ "version": "2.0.6",
4
4
  "description": "Ultra-lightweight global state management for React",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",