state-jet 2.2.4 → 2.4.1
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/dist/index.cjs +1 -19
- package/dist/index.d.ts +12 -27
- package/dist/index.mjs +1 -19
- package/package.json +31 -39
package/dist/index.cjs
CHANGED
|
@@ -1,19 +1 @@
|
|
|
1
|
-
"use strict";var e=require("react"),t=
|
|
2
|
-
/**
|
|
3
|
-
* @license React
|
|
4
|
-
* use-sync-external-store-shim.production.js
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
7
|
-
*
|
|
8
|
-
* This source code is licensed under the MIT license found in the
|
|
9
|
-
* LICENSE file in the root directory of this source tree.
|
|
10
|
-
*/var Z,ee,te={};
|
|
11
|
-
/**
|
|
12
|
-
* @license React
|
|
13
|
-
* use-sync-external-store-shim.development.js
|
|
14
|
-
*
|
|
15
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
16
|
-
*
|
|
17
|
-
* This source code is licensed under the MIT license found in the
|
|
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=>(fe.has(e)||fe.set(e,{}),(t,r,n)=>{const o=fe.get(e),a=`${e}:${t}`;if(!o[t]){let e=r;n?.persist&&(e=n?.encrypt?W(a,r):J(a,r)),o[t]={value:e,listeners:new Set,...n}}const s=o[t],i=async()=>{de=!1;const e=Array.from(le.entries());le.clear();for(const[t,r]of e){const[e,n]=t.split(":"),o=fe.get(e);if(!o)continue;const a=o[n];if(!a)continue;if(ue.has(t)){console.warn(`[state-jet] Skipping recursive middleware for: ${t}`);continue}let s=r;const i=a.value;if(pe[t]||(pe[t]={past:[],present:i,future:[]}),pe[t].past.push(i),pe[t].present=s,pe[t].future=[],ue.add(t),a?.middleware)for(const e of a.middleware)try{console.log(`[state-jet] Running middleware for ${t}`);const r=e(t,i,s,(e=>{s=e}));if(void 0!==r)if(r instanceof Promise){const e=await r;void 0!==e&&(s=e)}else s=r}catch(e){console.error(`[state-jet] Middleware error in ${t}:`,e)}ue.delete(t),"function"==typeof s?a.value=U(a.value,s):i!==s&&(a.value=s,a.listeners.forEach((e=>e())),ie(`${e}.${n}`,s),ce(`${e}.${n}`,(()=>{})),a?.persist&&(a?.encrypt?q(t,s):H(t,s)))}};return{useState:()=>re.useSyncExternalStore((e=>(s.listeners.add(e),()=>s.listeners.delete(e))),(()=>s.value)),set:async(r,n=!1)=>{const o=s.value,c="function"==typeof r?r(o):r;if(n)return s.value=c,s.listeners.forEach((e=>e())),void ie(`${e}.${t}`,c);le.set(a,c),de||(de=!0,s.frameSync&&B?.requestAnimationFrame?await new Promise((e=>B.requestAnimationFrame((()=>e(i()))))):await i())},get:()=>s.value,undo:()=>{pe[a]?.past.length&&(pe[a].future.unshift(pe[a].present),pe[a].present=pe[a].past.pop(),s.value=pe[a].present,s.listeners.forEach((e=>e())),(e=>{if(oe[e]>0)oe[e]--,ne[e][oe[e]]})(a),ie(`${e}.undo`,pe[a].present))},redo:()=>{pe[a]?.future.length&&(pe[a].past.push(pe[a].present),pe[a].present=pe[a].future.shift(),s.value=pe[a].present,s.listeners.forEach((e=>e())),(e=>{if(oe[e]<ne[e]?.length-1)oe[e]++,ne[e][oe[e]]})(a),ie(`${e}.redo`,pe[a].present))},clear:()=>{s.value=r,fe.clear(),Object.keys(pe).forEach((e=>delete pe[e])),ie(`${e}.clear`,r)}}}),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};
|
|
1
|
+
"use strict";var e=require("react"),t=require("immer");const r=(e=>{if(!1===e||null===e)throw new Error("Unable to locate global `this`");return e||globalThis})(),s=(e,t)=>{typeof r?.localStorage<"u"&&r.localStorage.setItem(e,JSON.stringify(t))},o=(e,t)=>{if(typeof r?.localStorage<"u"){const s=r.localStorage?.getItem(e);return s?JSON.parse(s):t}return t},n=(e,t)=>{typeof r?.localStorage<"u"&&r.localStorage.setItem(e,(e=>btoa(JSON.stringify(e)))(t))},a=(e,t)=>{if(typeof r?.localStorage<"u"){const s=r.localStorage?.getItem(e);return s?(e=>JSON.parse(atob(e)))(s):t}return t},u={},c={},l={},i=r.__STATE_JET_DEVTOOLS__,f=(e,t)=>{u[e]||(u[e]=[]),void 0===c[e]&&(c[e]=-1),u[e]=u[e].slice(0,c[e]+1),u[e].push(t),c[e]++,i?.updateState&&i.updateState(e,t,[...u[e]])},p=(e,t)=>{l[e]||(l[e]=[]);const r=performance.now();t();const s=performance.now()-r;l[e].push(s),i?.updatePerformance&&i.updatePerformance(e,s)},S=new Map,d=new Map,v={},g=new Map;let y=!1;function h(r){return S.has(r)||S.set(r,{}),function(l,i,h){const m=S.get(r),w=`${r}:${l}`;if(g.has(w))return g.get(w);m[l]||(m[l]={value:i,listeners:new Set,...h});const x=m[l],E={useState:()=>(e.useEffect((()=>{if(x.persist&&!x.restored){x.restored=!0;const e=x.encrypt?a(w,i):o(w,i);if(e!==i){x.value=e;for(const e of x.listeners)e()}}}),[i]),e.useSyncExternalStore((e=>(x.listeners.add(e),()=>x.listeners.delete(e))),(()=>x.value),(()=>i))),set:async(e,r=!1)=>{if(r){v[w]||(v[w]={past:[],present:x.value,future:[]}),v[w].past.push(x.value),x.value="function"==typeof e?t.produce(x.value,e):e,v[w].present=x.value,v[w].future=[];for(const e of x.listeners)e();f(w,x.value)}else d.set(w,e),y||(y=!0,await async function(){for(;d.size>0;){const e=Array.from(d.entries());d.clear();for(const[r,o]of e){const[e,a]=r.split(":"),u=S.get(e);if(!u)continue;const c=u[a];if(!c)continue;let l=o;const i=c.value;if(v[r]||(v[r]={past:[],present:i,future:[]}),v[r].past.push(i),v[r].present=l,v[r].future=[],c.middleware)for(const e of c.middleware)try{const t=e(r,i,l,(e=>{l=e}));if(t instanceof Promise){const e=await t;void 0!==e&&(l=e)}else void 0!==t&&(l=t)}catch(e){console.error(e)}c.value="function"==typeof l?t.produce(c.value,l):l;for(const e of c.listeners)e();f(r,c.value),p(r,(()=>{})),c.persist&&(c.encrypt?n(r,c.value):s(r,c.value))}}y=!1}())},get:()=>x.value,undo:()=>{const e=v[w];if(e&&e.past.length){e.future.unshift(e.present),e.present=e.past.pop(),x.value=e.present;for(const e of x.listeners)e();(e=>{if(c[e]>0)c[e]--,u[e][c[e]]})(w),f(`${r}.undo`,x.value)}},redo:()=>{const e=v[w];if(e&&e.future.length){e.past.push(e.present),e.present=e.future.shift(),x.value=e.present;for(const e of x.listeners)e();(e=>{if(c[e]<u[e]?.length-1)c[e]++,u[e][c[e]]})(w),f(`${r}.redo`,x.value)}},clear:()=>{x.value=i;for(const e of x.listeners)e();f(`${r}.clear`,i)}};return g.set(w,E),E}}const m=(e,t)=>({...e,...t,lastUpdated:Date.now()});exports.VERSION="2.4.0",exports.derivedState=(e,t)=>()=>{const r=e.map((e=>e.useState()));return t(...r)},exports.mergeCRDT=m,exports.optimisticUpdate=async(e,t,r,s)=>{const o=e.useState(),n=t(o);e.set(n);try{await r()}catch(t){console.error("API request failed:",t),e.set(s?s(o):o)}},exports.restoreEncryptedState=a,exports.restoreState=o,exports.saveEncryptedState=n,exports.saveState=s,exports.syncCRDT=(e,t,r)=>{const s=t();r(m(s,e))},exports.useSlice=h,exports.useStateGlobal=function(e,t,r){return h("global")(e,t,r)},exports.useStore=function(t){const r=e.useRef(null);return r.current||(r.current=t()),r.current};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type Middleware<T> = (
|
|
1
|
+
type Middleware<T> = (_key: string, _prev: T | undefined, _next: T | Action<T>, _set?: (_value: T) => void) => T | void | Promise<T | void>;
|
|
2
2
|
type Options<T> = {
|
|
3
3
|
middleware?: Middleware<T | undefined>[];
|
|
4
4
|
persist?: boolean;
|
|
@@ -9,32 +9,18 @@ type Action<T> = {
|
|
|
9
9
|
type: string;
|
|
10
10
|
payload?: T;
|
|
11
11
|
};
|
|
12
|
-
|
|
13
|
-
* Creates a global reactive state with middleware, persistence, and batch updates.
|
|
14
|
-
*/
|
|
15
|
-
declare const useStateGlobal: <T>(key: string, initialValue?: T, options?: Options<T>) => {
|
|
16
|
-
useState: () => T | undefined;
|
|
17
|
-
set: (newValue: T | Action<T | undefined> | ((prev: T | undefined) => T | undefined) | undefined, immediate?: boolean) => Promise<void>;
|
|
18
|
-
get: () => T | undefined;
|
|
19
|
-
undo: () => void;
|
|
20
|
-
redo: () => void;
|
|
21
|
-
clear: () => void;
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Creates a slice that can hold multiple state values.
|
|
25
|
-
*/
|
|
26
|
-
declare const useSlice: (sliceName: string) => <T>(key: string, initialValue: T, options?: Options<T | undefined>) => {
|
|
12
|
+
interface StoreHook<T> {
|
|
27
13
|
useState: () => T;
|
|
28
|
-
set: (
|
|
14
|
+
set: (val: T | ((prev: T) => T) | Action<T>, immediate?: boolean) => Promise<void>;
|
|
29
15
|
get: () => T;
|
|
30
16
|
undo: () => void;
|
|
31
17
|
redo: () => void;
|
|
32
18
|
clear: () => void;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
declare
|
|
19
|
+
}
|
|
20
|
+
declare function useSlice(sliceName: string): <T>(key: string, initialValue: T, options?: Options<T | undefined>) => StoreHook<T>;
|
|
21
|
+
declare function useStateGlobal<T>(key: string, initialValue: T, options?: Options<T>): StoreHook<T>;
|
|
22
|
+
declare function useStateGlobal<T>(key: string, initialValue?: T, options?: Options<T>): StoreHook<T | undefined>;
|
|
23
|
+
declare function useStore<T extends Record<string, unknown>>(initializer: () => T): T;
|
|
38
24
|
|
|
39
25
|
declare const saveState: <T>(key: string, value: T) => void;
|
|
40
26
|
declare const restoreState: <T>(key: string, defaultValue?: T) => any;
|
|
@@ -48,13 +34,12 @@ declare const saveEncryptedState: <T>(key: string, value: T) => void;
|
|
|
48
34
|
declare const restoreEncryptedState: <T>(key: string, defaultValue?: T) => unknown;
|
|
49
35
|
|
|
50
36
|
declare const mergeCRDT: <T>(localState: T, remoteState: T) => T;
|
|
51
|
-
declare const syncCRDT: <T>(remoteState: T,
|
|
52
|
-
useState: () => T;
|
|
53
|
-
set: (state: T) => void;
|
|
54
|
-
}) => void;
|
|
37
|
+
declare const syncCRDT: <T>(remoteState: T, getStateValue: () => T, set: (state: T) => void) => void;
|
|
55
38
|
|
|
56
39
|
declare const derivedState: <T, D extends {
|
|
57
40
|
useState: () => unknown;
|
|
58
41
|
}[]>(dependencies: D, computeFn: (...values: { [K in keyof D]: ReturnType<D[K]["useState"]>; }) => T) => () => T;
|
|
59
42
|
|
|
60
|
-
|
|
43
|
+
declare const VERSION = "2.4.0";
|
|
44
|
+
|
|
45
|
+
export { VERSION, derivedState, mergeCRDT, optimisticUpdate, restoreEncryptedState, restoreState, saveEncryptedState, saveState, syncCRDT, useSlice, useStateGlobal, useStore };
|
package/dist/index.mjs
CHANGED
|
@@ -1,19 +1 @@
|
|
|
1
|
-
import e,
|
|
2
|
-
/**
|
|
3
|
-
* @license React
|
|
4
|
-
* use-sync-external-store-shim.production.js
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
7
|
-
*
|
|
8
|
-
* This source code is licensed under the MIT license found in the
|
|
9
|
-
* LICENSE file in the root directory of this source tree.
|
|
10
|
-
*/var ee,te,re={};
|
|
11
|
-
/**
|
|
12
|
-
* @license React
|
|
13
|
-
* use-sync-external-store-shim.development.js
|
|
14
|
-
*
|
|
15
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
16
|
-
*
|
|
17
|
-
* This source code is licensed under the MIT license found in the
|
|
18
|
-
* LICENSE file in the root directory of this source tree.
|
|
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),a=`${e}:${t}`;if(!o[t]){let e=r;n?.persist&&(e=n?.encrypt?X(a,r):W(a,r)),o[t]={value:e,listeners:new Set,...n}}const s=o[t],i=async()=>{_e=!1;const e=Array.from(pe.entries());pe.clear();for(const[t,r]of e){const[e,n]=t.split(":"),o=le.get(e);if(!o)continue;const a=o[n];if(!a)continue;if(fe.has(t)){console.warn(`[state-jet] Skipping recursive middleware for: ${t}`);continue}let s=r;const i=a.value;if(de[t]||(de[t]={past:[],present:i,future:[]}),de[t].past.push(i),de[t].present=s,de[t].future=[],fe.add(t),a?.middleware)for(const e of a.middleware)try{console.log(`[state-jet] Running middleware for ${t}`);const r=e(t,i,s,(e=>{s=e}));if(void 0!==r)if(r instanceof Promise){const e=await r;void 0!==e&&(s=e)}else s=r}catch(e){console.error(`[state-jet] Middleware error in ${t}:`,e)}fe.delete(t),"function"==typeof s?a.value=B(a.value,s):i!==s&&(a.value=s,a.listeners.forEach((e=>e())),ce(`${e}.${n}`,s),ue(`${e}.${n}`,(()=>{})),a?.persist&&(a?.encrypt?q(t,s):J(t,s)))}};return{useState:()=>ne.useSyncExternalStore((e=>(s.listeners.add(e),()=>s.listeners.delete(e))),(()=>s.value)),set:async(r,n=!1)=>{const o=s.value,c="function"==typeof r?r(o):r;if(n)return s.value=c,s.listeners.forEach((e=>e())),void ce(`${e}.${t}`,c);pe.set(a,c),_e||(_e=!0,s.frameSync&&H?.requestAnimationFrame?await new Promise((e=>H.requestAnimationFrame((()=>e(i()))))):await i())},get:()=>s.value,undo:()=>{de[a]?.past.length&&(de[a].future.unshift(de[a].present),de[a].present=de[a].past.pop(),s.value=de[a].present,s.listeners.forEach((e=>e())),(e=>{if(ae[e]>0)ae[e]--,oe[e][ae[e]]})(a),ce(`${e}.undo`,de[a].present))},redo:()=>{de[a]?.future.length&&(de[a].past.push(de[a].present),de[a].present=de[a].future.shift(),s.value=de[a].present,s.listeners.forEach((e=>e())),(e=>{if(ae[e]<oe[e]?.length-1)ae[e]++,oe[e][ae[e]]})(a),ce(`${e}.redo`,de[a].present))},clear:()=>{s.value=r,le.clear(),Object.keys(de).forEach((e=>delete de[e])),ce(`${e}.clear`,r)}}}),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};
|
|
1
|
+
import{useRef as e,useEffect as t,useSyncExternalStore as s}from"react";import{produce as r}from"immer";const o=(e=>{if(!1===e||null===e)throw new Error("Unable to locate global `this`");return e||globalThis})(),a=(e,t)=>{typeof o?.localStorage<"u"&&o.localStorage.setItem(e,JSON.stringify(t))},n=(e,t)=>{if(typeof o?.localStorage<"u"){const s=o.localStorage?.getItem(e);return s?JSON.parse(s):t}return t},u=(e,t)=>{typeof o?.localStorage<"u"&&o.localStorage.setItem(e,(e=>btoa(JSON.stringify(e)))(t))},l=(e,t)=>{if(typeof o?.localStorage<"u"){const s=o.localStorage?.getItem(e);return s?(e=>JSON.parse(atob(e)))(s):t}return t},c={},f={},i={},p=o.__STATE_JET_DEVTOOLS__,S=(e,t)=>{c[e]||(c[e]=[]),void 0===f[e]&&(f[e]=-1),c[e]=c[e].slice(0,f[e]+1),c[e].push(t),f[e]++,p?.updateState&&p.updateState(e,t,[...c[e]])},d=(e,t)=>{i[e]||(i[e]=[]);const s=performance.now();t();const r=performance.now()-s;i[e].push(r),p?.updatePerformance&&p.updatePerformance(e,r)},v=new Map,g=new Map,m={},y=new Map;let h=!1;function w(e){return v.has(e)||v.set(e,{}),function(o,i,p){const w=v.get(e),E=`${e}:${o}`;if(y.has(E))return y.get(E);w[o]||(w[o]={value:i,listeners:new Set,...p});const b=w[o],O={useState:()=>(t((()=>{if(b.persist&&!b.restored){b.restored=!0;const e=b.encrypt?l(E,i):n(E,i);if(e!==i){b.value=e;for(const e of b.listeners)e()}}}),[i]),s((e=>(b.listeners.add(e),()=>b.listeners.delete(e))),(()=>b.value),(()=>i))),set:async(e,t=!1)=>{if(t){m[E]||(m[E]={past:[],present:b.value,future:[]}),m[E].past.push(b.value),b.value="function"==typeof e?r(b.value,e):e,m[E].present=b.value,m[E].future=[];for(const e of b.listeners)e();S(E,b.value)}else g.set(E,e),h||(h=!0,await async function(){for(;g.size>0;){const e=Array.from(g.entries());g.clear();for(const[t,s]of e){const[e,o]=t.split(":"),n=v.get(e);if(!n)continue;const l=n[o];if(!l)continue;let c=s;const f=l.value;if(m[t]||(m[t]={past:[],present:f,future:[]}),m[t].past.push(f),m[t].present=c,m[t].future=[],l.middleware)for(const e of l.middleware)try{const s=e(t,f,c,(e=>{c=e}));if(s instanceof Promise){const e=await s;void 0!==e&&(c=e)}else void 0!==s&&(c=s)}catch(e){console.error(e)}l.value="function"==typeof c?r(l.value,c):c;for(const e of l.listeners)e();S(t,l.value),d(t,(()=>{})),l.persist&&(l.encrypt?u(t,l.value):a(t,l.value))}}h=!1}())},get:()=>b.value,undo:()=>{const t=m[E];if(t&&t.past.length){t.future.unshift(t.present),t.present=t.past.pop(),b.value=t.present;for(const e of b.listeners)e();(e=>{if(f[e]>0)f[e]--,c[e][f[e]]})(E),S(`${e}.undo`,b.value)}},redo:()=>{const t=m[E];if(t&&t.future.length){t.past.push(t.present),t.present=t.future.shift(),b.value=t.present;for(const e of b.listeners)e();(e=>{if(f[e]<c[e]?.length-1)f[e]++,c[e][f[e]]})(E),S(`${e}.redo`,b.value)}},clear:()=>{b.value=i;for(const e of b.listeners)e();S(`${e}.clear`,i)}};return y.set(E,O),O}}function E(e,t,s){return w("global")(e,t,s)}function b(t){const s=e(null);return s.current||(s.current=t()),s.current}const O=async(e,t,s,r)=>{const o=e.useState(),a=t(o);e.set(a);try{await s()}catch(t){console.error("API request failed:",t),e.set(r?r(o):o)}},T=(e,t)=>({...e,...t,lastUpdated:Date.now()}),I=(e,t,s)=>{const r=t();s(T(r,e))},_=(e,t)=>()=>{const s=e.map((e=>e.useState()));return t(...s)},J="2.4.0";export{J as VERSION,_ as derivedState,T as mergeCRDT,O as optimisticUpdate,l as restoreEncryptedState,n as restoreState,u as saveEncryptedState,a as saveState,I as syncCRDT,w as useSlice,E as useStateGlobal,b as useStore};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "state-jet",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"description": "Ultra-lightweight global state management for React",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -19,25 +19,19 @@
|
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
22
|
-
"url": "https://github.com/venkateshsundaram/state-jet.git"
|
|
22
|
+
"url": "git+https://github.com/venkateshsundaram/state-jet.git"
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://statejet.org",
|
|
25
25
|
"keywords": [
|
|
26
26
|
"react",
|
|
27
27
|
"state",
|
|
28
|
-
"redux",
|
|
29
28
|
"state-management",
|
|
30
29
|
"global-state",
|
|
31
30
|
"global-state-management",
|
|
32
|
-
"signals",
|
|
33
31
|
"state-jet",
|
|
34
32
|
"statejet",
|
|
35
33
|
"state-jet-react",
|
|
36
|
-
"jet",
|
|
37
|
-
"react-jet",
|
|
38
|
-
"jetstate",
|
|
39
|
-
"jet-state",
|
|
40
|
-
"react-jetstate",
|
|
34
|
+
"react-state-jet",
|
|
41
35
|
"react-hooks",
|
|
42
36
|
"store",
|
|
43
37
|
"hooks",
|
|
@@ -45,7 +39,6 @@
|
|
|
45
39
|
"typescript",
|
|
46
40
|
"global",
|
|
47
41
|
"react-state-management",
|
|
48
|
-
"state-manager",
|
|
49
42
|
"global-state-hooks",
|
|
50
43
|
"state-hooks",
|
|
51
44
|
"use-store",
|
|
@@ -60,20 +53,6 @@
|
|
|
60
53
|
"bugs": {
|
|
61
54
|
"url": "https://github.com/venkateshsundaram/state-jet/issues"
|
|
62
55
|
},
|
|
63
|
-
"scripts": {
|
|
64
|
-
"build": "rimraf dist && rollup -c",
|
|
65
|
-
"test": "vitest",
|
|
66
|
-
"test:coverage": "vitest run --coverage",
|
|
67
|
-
"clean": "rimraf dist",
|
|
68
|
-
"publish:npm": "npm publish --access public",
|
|
69
|
-
"lint": "eslint src --ext .ts,.tsx",
|
|
70
|
-
"format": "prettier --write src",
|
|
71
|
-
"format:check": "prettier --check src",
|
|
72
|
-
"release": "standard-version",
|
|
73
|
-
"postrelease": "git push --follow-tags",
|
|
74
|
-
"prepare": "npm run build",
|
|
75
|
-
"prepublishOnly": "npm run build"
|
|
76
|
-
},
|
|
77
56
|
"husky": {
|
|
78
57
|
"hooks": {
|
|
79
58
|
"pre-commit": "lint-staged"
|
|
@@ -81,7 +60,7 @@
|
|
|
81
60
|
},
|
|
82
61
|
"lint-staged": {
|
|
83
62
|
"*.ts": [
|
|
84
|
-
"eslint --fix",
|
|
63
|
+
"eslint --fix --no-warn-ignored",
|
|
85
64
|
"prettier --write"
|
|
86
65
|
]
|
|
87
66
|
},
|
|
@@ -91,21 +70,22 @@
|
|
|
91
70
|
]
|
|
92
71
|
},
|
|
93
72
|
"devDependencies": {
|
|
73
|
+
"@eslint/js": "^9.39.4",
|
|
94
74
|
"@commitlint/cli": "^19.8.0",
|
|
95
75
|
"@commitlint/config-conventional": "^19.8.0",
|
|
96
76
|
"@rollup/plugin-commonjs": "^28.0.2",
|
|
97
77
|
"@rollup/plugin-node-resolve": "^15.0.2",
|
|
98
|
-
"@rollup/plugin-terser": "^0.
|
|
78
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
99
79
|
"@testing-library/react": "^16.1.0",
|
|
100
80
|
"@types/react": "^19.0.10",
|
|
101
81
|
"@types/react-dom": "^19.0.4",
|
|
102
|
-
"@types/use-sync-external-store": "^0.0.6",
|
|
103
82
|
"@typescript-eslint/eslint-plugin": "^8.26.0",
|
|
104
83
|
"@typescript-eslint/parser": "^8.26.0",
|
|
105
|
-
"@vitest/coverage-v8": "^
|
|
106
|
-
"eslint": "^
|
|
84
|
+
"@vitest/coverage-v8": "^4.1.2",
|
|
85
|
+
"eslint": "^9.39.4",
|
|
107
86
|
"eslint-plugin-react": "^7.37.4",
|
|
108
87
|
"eslint-plugin-react-hooks": "^5.2.0",
|
|
88
|
+
"globals": "^17.4.0",
|
|
109
89
|
"husky": "^9.1.7",
|
|
110
90
|
"immer": "^10.1.1",
|
|
111
91
|
"jsdom": "^26.0.0",
|
|
@@ -113,20 +93,19 @@
|
|
|
113
93
|
"prettier": "^3.5.3",
|
|
114
94
|
"react": "19.0.0",
|
|
115
95
|
"react-dom": "19.0.0",
|
|
116
|
-
"rimraf": "^
|
|
96
|
+
"rimraf": "^6.1.3",
|
|
117
97
|
"rollup": "^3.29.4",
|
|
118
98
|
"rollup-plugin-dts": "^5.3.0",
|
|
119
99
|
"rollup-plugin-esbuild": "^5.0.0",
|
|
120
100
|
"standard-version": "^9.5.0",
|
|
121
101
|
"typescript": "^5.7.3",
|
|
122
|
-
"
|
|
123
|
-
"vitest": "^
|
|
102
|
+
"typescript-eslint": "^8.57.2",
|
|
103
|
+
"vitest": "^4.1.2"
|
|
124
104
|
},
|
|
125
105
|
"peerDependencies": {
|
|
126
106
|
"@types/react": ">=18.0.0",
|
|
127
107
|
"immer": ">=9.0.6",
|
|
128
|
-
"react": ">=18.0.0"
|
|
129
|
-
"use-sync-external-store": ">=1.2.0"
|
|
108
|
+
"react": ">=18.0.0"
|
|
130
109
|
},
|
|
131
110
|
"peerDependenciesMeta": {
|
|
132
111
|
"@types/react": {
|
|
@@ -137,10 +116,23 @@
|
|
|
137
116
|
},
|
|
138
117
|
"react": {
|
|
139
118
|
"optional": true
|
|
140
|
-
},
|
|
141
|
-
"use-sync-external-store": {
|
|
142
|
-
"optional": true
|
|
143
119
|
}
|
|
144
120
|
},
|
|
145
|
-
"
|
|
146
|
-
|
|
121
|
+
"overrides": {
|
|
122
|
+
"minimatch": "^10.0.0",
|
|
123
|
+
"yargs-parser": "^21.0.0"
|
|
124
|
+
},
|
|
125
|
+
"type": "module",
|
|
126
|
+
"scripts": {
|
|
127
|
+
"build": "rimraf dist && rollup -c",
|
|
128
|
+
"test": "vitest",
|
|
129
|
+
"test:coverage": "vitest run --coverage",
|
|
130
|
+
"clean": "rimraf dist",
|
|
131
|
+
"publish:npm": "npm publish --access public",
|
|
132
|
+
"lint": "eslint src --max-warnings 0",
|
|
133
|
+
"format": "prettier --write src",
|
|
134
|
+
"format:check": "prettier --check src",
|
|
135
|
+
"release": "standard-version",
|
|
136
|
+
"postrelease": "git push --follow-tags"
|
|
137
|
+
}
|
|
138
|
+
}
|