react-shared-states 2.0.1 → 2.1.0
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/hooks/use-shared-state.d.ts +2 -2
- package/dist/main.esm.js +2 -2
- package/dist/main.min.js +2 -2
- package/package.json +1 -1
|
@@ -5,8 +5,8 @@ export interface SharedStateCreated<T> extends SharedCreated {
|
|
|
5
5
|
initialValue: T;
|
|
6
6
|
}
|
|
7
7
|
export declare const createSharedState: <T>(initialValue: T, scopeName?: Prefix) => SharedStateCreated<T>;
|
|
8
|
-
export declare function useSharedState<T
|
|
8
|
+
export declare function useSharedState<T>(key: string, initialValue: T, scopeName?: Prefix): readonly [T, (v: T | ((prev: T) => T)) => void];
|
|
9
9
|
export declare function useSharedState<T>(sharedStateCreated: SharedStateCreated<T>): readonly [T, (v: T | ((prev: T) => T)) => void];
|
|
10
10
|
export type SharedStateSelector<S, T = S> = (original: S) => T;
|
|
11
|
-
export declare function useSharedStateSelector<T,
|
|
11
|
+
export declare function useSharedStateSelector<T, R = T>(key: string, selector: SharedStateSelector<T, R>, scopeName?: Prefix): Readonly<R>;
|
|
12
12
|
export declare function useSharedStateSelector<T, R>(sharedStateCreated: SharedStateCreated<T>, selector: SharedStateSelector<T, R>): Readonly<R>;
|
package/dist/main.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* react-shared-states v2.0
|
|
2
|
+
* react-shared-states v2.1.0
|
|
3
3
|
* (c) Hichem Taboukouyout
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
* Github: github.com/HichemTab-tech/react-shared-states
|
|
@@ -276,7 +276,7 @@ function se(o, t, n) {
|
|
|
276
276
|
e = u, i = f;
|
|
277
277
|
} else
|
|
278
278
|
e = E(o);
|
|
279
|
-
const { prefix: r } = j(i), s = k(void 0), a = b(() => (u) => (h.addListener(e, r, u), () => {
|
|
279
|
+
const { prefix: r } = j(i), s = k(void 0), a = b(() => (u) => (h.init(e, r, void 0), h.addListener(e, r, u), () => {
|
|
280
280
|
h.removeListener(e, r, u);
|
|
281
281
|
}), [e, r]), c = b(() => () => {
|
|
282
282
|
const u = h.get(e, r)?.value, f = t(u);
|
package/dist/main.min.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* react-shared-states v2.0
|
|
2
|
+
* react-shared-states v2.1.0
|
|
3
3
|
* (c) Hichem Taboukouyout
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
* Github: github.com/HichemTab-tech/react-shared-states
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
(function(c,A){typeof exports=="object"&&typeof module<"u"?A(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],A):(c=typeof globalThis<"u"?globalThis:c||self,A(c.ReactSharedStates={},c.jsxRuntime,c.React))})(this,(function(c,A,d){"use strict";c.isDevMode=!1;const G=o=>{c.isDevMode=o},P=(...o)=>{c.isDevMode&&console.log("%c[react-shared-states]","color: #007acc; font-weight: bold",...o)},D=o=>{if(!o)throw new Error("Value is empty");return o},K=()=>Math.random().toString(36).substring(2,15),$=d.createContext(void 0),I=({children:o,scopeName:t})=>{if(t&&t.includes("//"))throw new Error("scopeName cannot contain '//'");return t||(t=d.useMemo(()=>K(),[])),A.jsx($.Provider,{value:{scopeName:t},children:o})},V=()=>d.useContext($),B=[];class h{constructor(t=()=>null){this.defaultValue=t}data=new Map;addListener(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&r.listeners.push(e)}removeListener(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&(r.listeners=r.listeners.filter(s=>s!==e))}callListeners(t,n){const e=h.prefix(t,n),i=this.data.get(e);i&&i.listeners.forEach(r=>r())}init(t,n,e,i=!1){const r=h.prefix(t,n);this.data.has(r)||this.data.set(r,{value:e,isStatic:i||void 0,listeners:[]})}createStatic(t,n,e){const i=e??"_global",r={key:K(),prefix:i,...t};return B.push(r),this.init(r.key,r.prefix,n,!0),this.defaultValue=()=>n,r}initStatic(t){const{key:n,prefix:e}=t;this.init(n,e,this.defaultValue(),!0)}clearAll(t=!1,n=!1){this.data.forEach((e,i)=>{const[r,s]=h.extractPrefix(i);this.clear(s,r,t,n)})}clear(t,n,e=!1,i=!1){const r=h.prefix(t,n);e||this.callListeners(t,n);const s=this.data.get(r);if(s&&(this.data.delete(r),s.isStatic&&!i)){const a=B.find(u=>u.key===t&&u.prefix===n);a&&this.initStatic(a)}}get(t,n){let e=this.has(t,n);if(e)return this.data.get(e)}setValue(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&(r.value=e,this.data.set(i,r))}has(t,n){return this.data.has(h.prefix(t,n))?h.prefix(t,n):this.data.has(h.prefix(t,"_global"))?h.prefix(t,"_global"):void 0}static prefix(t,n){if(t.includes("//"))throw new Error("key cannot contain '//'");return`${n}//${t}`}static extractPrefix(t){const n=t.split("//");return[n[0],n.slice(1).join("//")]}useEffect(t,n,e=null){d.useEffect(()=>()=>{e?.(),P(`[${h.prefix(t,n)}]`,"unmount effect");const i=this.get(t,n);i&&i.listeners?.length===0&&this.clear(t,n)},[t,n])}}class _{constructor(t){this.sharedData=t}_get(t,n){let e,i=n;if(typeof t!="string"){const{key:a,prefix:u}=t;e=a,i=u}else e=D(t);const r=i||"_global",s=this.sharedData.get(e,r);return s?{value:s.value,key:e,prefix:r}:{key:e,prefix:r,value:void 0}}get(t,n){return this._get(t,n).value}set(t,n,e){let i,r=e;if(typeof t!="string"){const{key:a,prefix:u}=t;i=a,r=u}else i=D(t);const s=r||"_global";this.sharedData.init(i,s,n),this.sharedData.setValue(i,s,n),this.sharedData.callListeners(i,s)}update(t,n,e){const i=this._get(t,e);if(i){const r=n(i.value);this.set(i.key,r,i.prefix)}}clearAll(){this.sharedData.clearAll()}clearScope(t){const n=t||"_global";this.sharedData.data.forEach((e,i)=>{const[r,s]=h.extractPrefix(i);r===n&&(this.sharedData.clear(s,r),this.sharedData.callListeners(s,r))})}resolve(t){const{key:n,prefix:e}=t;return this.get(n,e)}clear(t,n){let e,i;typeof t=="string"?(e=t,i=n||"_global"):(e=t.key,i=t.prefix),this.sharedData.clear(e,i)}has(t,n="_global"){const e=n||"_global";return!!this.sharedData.has(t,e)}getAll(){const t={};return this.sharedData.data.forEach((n,e)=>{const[i,r]=h.extractPrefix(e);t[i]=t[i]||{},t[i][r]=n.value}),t}subscribe(t,n,e){let i,r;return typeof t=="string"?(i=t,r=e||"_global"):(i=t.key,r=t.prefix),this.sharedData.addListener(i,r,n),()=>{this.sharedData.removeListener(i,r,n)}}}const j=o=>{const t=V();return{prefix:o??t?.scopeName??"_global"}};function W(o){return o&&o.__esModule&&Object.prototype.hasOwnProperty.call(o,"default")?o.default:o}var C,T;function k(){if(T)return C;T=1;var o=typeof Element<"u",t=typeof Map=="function",n=typeof Set=="function",e=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function i(r,s){if(r===s)return!0;if(r&&s&&typeof r=="object"&&typeof s=="object"){if(r.constructor!==s.constructor)return!1;var a,u,p;if(Array.isArray(r)){if(a=r.length,a!=s.length)return!1;for(u=a;u--!==0;)if(!i(r[u],s[u]))return!1;return!0}var l;if(t&&r instanceof Map&&s instanceof Map){if(r.size!==s.size)return!1;for(l=r.entries();!(u=l.next()).done;)if(!s.has(u.value[0]))return!1;for(l=r.entries();!(u=l.next()).done;)if(!i(u.value[1],s.get(u.value[0])))return!1;return!0}if(n&&r instanceof Set&&s instanceof Set){if(r.size!==s.size)return!1;for(l=r.entries();!(u=l.next()).done;)if(!s.has(u.value[0]))return!1;return!0}if(e&&ArrayBuffer.isView(r)&&ArrayBuffer.isView(s)){if(a=r.length,a!=s.length)return!1;for(u=a;u--!==0;)if(r[u]!==s[u])return!1;return!0}if(r.constructor===RegExp)return r.source===s.source&&r.flags===s.flags;if(r.valueOf!==Object.prototype.valueOf&&typeof r.valueOf=="function"&&typeof s.valueOf=="function")return r.valueOf()===s.valueOf();if(r.toString!==Object.prototype.toString&&typeof r.toString=="function"&&typeof s.toString=="function")return r.toString()===s.toString();if(p=Object.keys(r),a=p.length,a!==Object.keys(s).length)return!1;for(u=a;u--!==0;)if(!Object.prototype.hasOwnProperty.call(s,p[u]))return!1;if(o&&r instanceof Element)return!1;for(u=a;u--!==0;)if(!((p[u]==="_owner"||p[u]==="__v"||p[u]==="__o")&&r.$$typeof)&&!i(r[p[u]],s[p[u]]))return!1;return!0}return r!==r&&s!==s}return C=function(s,a){try{return i(s,a)}catch(u){if((u.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw u}},C}var H=k();const z=W(H),
|
|
8
|
+
(function(c,A){typeof exports=="object"&&typeof module<"u"?A(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],A):(c=typeof globalThis<"u"?globalThis:c||self,A(c.ReactSharedStates={},c.jsxRuntime,c.React))})(this,(function(c,A,d){"use strict";c.isDevMode=!1;const G=o=>{c.isDevMode=o},P=(...o)=>{c.isDevMode&&console.log("%c[react-shared-states]","color: #007acc; font-weight: bold",...o)},D=o=>{if(!o)throw new Error("Value is empty");return o},K=()=>Math.random().toString(36).substring(2,15),$=d.createContext(void 0),I=({children:o,scopeName:t})=>{if(t&&t.includes("//"))throw new Error("scopeName cannot contain '//'");return t||(t=d.useMemo(()=>K(),[])),A.jsx($.Provider,{value:{scopeName:t},children:o})},V=()=>d.useContext($),B=[];class h{constructor(t=()=>null){this.defaultValue=t}data=new Map;addListener(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&r.listeners.push(e)}removeListener(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&(r.listeners=r.listeners.filter(s=>s!==e))}callListeners(t,n){const e=h.prefix(t,n),i=this.data.get(e);i&&i.listeners.forEach(r=>r())}init(t,n,e,i=!1){const r=h.prefix(t,n);this.data.has(r)||this.data.set(r,{value:e,isStatic:i||void 0,listeners:[]})}createStatic(t,n,e){const i=e??"_global",r={key:K(),prefix:i,...t};return B.push(r),this.init(r.key,r.prefix,n,!0),this.defaultValue=()=>n,r}initStatic(t){const{key:n,prefix:e}=t;this.init(n,e,this.defaultValue(),!0)}clearAll(t=!1,n=!1){this.data.forEach((e,i)=>{const[r,s]=h.extractPrefix(i);this.clear(s,r,t,n)})}clear(t,n,e=!1,i=!1){const r=h.prefix(t,n);e||this.callListeners(t,n);const s=this.data.get(r);if(s&&(this.data.delete(r),s.isStatic&&!i)){const a=B.find(u=>u.key===t&&u.prefix===n);a&&this.initStatic(a)}}get(t,n){let e=this.has(t,n);if(e)return this.data.get(e)}setValue(t,n,e){const i=h.prefix(t,n),r=this.data.get(i);r&&(r.value=e,this.data.set(i,r))}has(t,n){return this.data.has(h.prefix(t,n))?h.prefix(t,n):this.data.has(h.prefix(t,"_global"))?h.prefix(t,"_global"):void 0}static prefix(t,n){if(t.includes("//"))throw new Error("key cannot contain '//'");return`${n}//${t}`}static extractPrefix(t){const n=t.split("//");return[n[0],n.slice(1).join("//")]}useEffect(t,n,e=null){d.useEffect(()=>()=>{e?.(),P(`[${h.prefix(t,n)}]`,"unmount effect");const i=this.get(t,n);i&&i.listeners?.length===0&&this.clear(t,n)},[t,n])}}class _{constructor(t){this.sharedData=t}_get(t,n){let e,i=n;if(typeof t!="string"){const{key:a,prefix:u}=t;e=a,i=u}else e=D(t);const r=i||"_global",s=this.sharedData.get(e,r);return s?{value:s.value,key:e,prefix:r}:{key:e,prefix:r,value:void 0}}get(t,n){return this._get(t,n).value}set(t,n,e){let i,r=e;if(typeof t!="string"){const{key:a,prefix:u}=t;i=a,r=u}else i=D(t);const s=r||"_global";this.sharedData.init(i,s,n),this.sharedData.setValue(i,s,n),this.sharedData.callListeners(i,s)}update(t,n,e){const i=this._get(t,e);if(i){const r=n(i.value);this.set(i.key,r,i.prefix)}}clearAll(){this.sharedData.clearAll()}clearScope(t){const n=t||"_global";this.sharedData.data.forEach((e,i)=>{const[r,s]=h.extractPrefix(i);r===n&&(this.sharedData.clear(s,r),this.sharedData.callListeners(s,r))})}resolve(t){const{key:n,prefix:e}=t;return this.get(n,e)}clear(t,n){let e,i;typeof t=="string"?(e=t,i=n||"_global"):(e=t.key,i=t.prefix),this.sharedData.clear(e,i)}has(t,n="_global"){const e=n||"_global";return!!this.sharedData.has(t,e)}getAll(){const t={};return this.sharedData.data.forEach((n,e)=>{const[i,r]=h.extractPrefix(e);t[i]=t[i]||{},t[i][r]=n.value}),t}subscribe(t,n,e){let i,r;return typeof t=="string"?(i=t,r=e||"_global"):(i=t.key,r=t.prefix),this.sharedData.addListener(i,r,n),()=>{this.sharedData.removeListener(i,r,n)}}}const j=o=>{const t=V();return{prefix:o??t?.scopeName??"_global"}};function W(o){return o&&o.__esModule&&Object.prototype.hasOwnProperty.call(o,"default")?o.default:o}var C,T;function k(){if(T)return C;T=1;var o=typeof Element<"u",t=typeof Map=="function",n=typeof Set=="function",e=typeof ArrayBuffer=="function"&&!!ArrayBuffer.isView;function i(r,s){if(r===s)return!0;if(r&&s&&typeof r=="object"&&typeof s=="object"){if(r.constructor!==s.constructor)return!1;var a,u,p;if(Array.isArray(r)){if(a=r.length,a!=s.length)return!1;for(u=a;u--!==0;)if(!i(r[u],s[u]))return!1;return!0}var l;if(t&&r instanceof Map&&s instanceof Map){if(r.size!==s.size)return!1;for(l=r.entries();!(u=l.next()).done;)if(!s.has(u.value[0]))return!1;for(l=r.entries();!(u=l.next()).done;)if(!i(u.value[1],s.get(u.value[0])))return!1;return!0}if(n&&r instanceof Set&&s instanceof Set){if(r.size!==s.size)return!1;for(l=r.entries();!(u=l.next()).done;)if(!s.has(u.value[0]))return!1;return!0}if(e&&ArrayBuffer.isView(r)&&ArrayBuffer.isView(s)){if(a=r.length,a!=s.length)return!1;for(u=a;u--!==0;)if(r[u]!==s[u])return!1;return!0}if(r.constructor===RegExp)return r.source===s.source&&r.flags===s.flags;if(r.valueOf!==Object.prototype.valueOf&&typeof r.valueOf=="function"&&typeof s.valueOf=="function")return r.valueOf()===s.valueOf();if(r.toString!==Object.prototype.toString&&typeof r.toString=="function"&&typeof s.toString=="function")return r.toString()===s.toString();if(p=Object.keys(r),a=p.length,a!==Object.keys(s).length)return!1;for(u=a;u--!==0;)if(!Object.prototype.hasOwnProperty.call(s,p[u]))return!1;if(o&&r instanceof Element)return!1;for(u=a;u--!==0;)if(!((p[u]==="_owner"||p[u]==="__v"||p[u]==="__o")&&r.$$typeof)&&!i(r[p[u]],s[p[u]]))return!1;return!0}return r!==r&&s!==s}return C=function(s,a){try{return i(s,a)}catch(u){if((u.message||"").match(/stack|recursion/i))return console.warn("react-fast-compare cannot handle circular refs"),!1;throw u}},C}var H=k();const z=W(H),v=new h,J=new _(v),Q=(o,t)=>v.createStatic({initialValue:o},o,t);function U(o,t,n){let e,i,r=n;if(typeof o!="string"){const{key:f,initialValue:y,prefix:m}=o;e=f,i=y,r=m}else e=D(o),i=t;const{prefix:s}=j(r);v.init(e,s,i);const a=d.useMemo(()=>f=>(v.init(e,s,t),v.addListener(e,s,f),()=>{v.removeListener(e,s,f)}),[e,s,t]),u=d.useMemo(()=>()=>v.get(e,s)?.value,[e,s]),p=d.useSyncExternalStore(a,u),l=f=>{const y=typeof f=="function"?f(v.get(e,s)?.value):f;z(y,p)||(v.setValue(e,s,y),v.callListeners(e,s))};return v.useEffect(e,s),[p,l]}function X(o,t,n){let e,i=n;if(typeof o!="string"){const{key:l,prefix:f}=o;e=l,i=f}else e=D(o);const{prefix:r}=j(i),s=d.useRef(void 0),a=d.useMemo(()=>l=>(v.init(e,r,void 0),v.addListener(e,r,l),()=>{v.removeListener(e,r,l)}),[e,r]),u=d.useMemo(()=>()=>{const l=v.get(e,r)?.value,f=t(l);return z(s.current,f)?s.current:(s.current=f,f)},[e,r,t]),p=d.useSyncExternalStore(a,u);return v.useEffect(e,r),p}const x=new h,M=new _(x),O={results:void 0,isLoading:!1,error:void 0},Y=(o,t)=>x.createStatic({fn:o},O,t);function Z(o,t,n){let e,i,r=n;if(typeof o!="string"){const{key:f,fn:y,prefix:m}=o;e=f,i=y,r=m}else e=D(o),i=t;const{prefix:s}=j(r);x.init(e,s,O);const a=d.useMemo(()=>f=>(x.init(e,s,O),x.addListener(e,s,f),()=>{x.removeListener(e,s,f)}),[e,s]),u=d.useMemo(()=>()=>x.get(e,s).value,[e,s]),p=d.useSyncExternalStore(a,u),l=async(f,...y)=>{const m=x.get(e,s);if(!f&&(m.value.isLoading||m.value.results!==void 0))return m.value;M.update(e,b=>({...b,isLoading:!0,error:void 0}),s);try{const b=await i(...y);M.set(e,{results:b,isLoading:!1,error:void 0},s)}catch(b){M.update(e,S=>({...S,isLoading:!1,error:b}),s)}};return x.useEffect(e,s),{state:p,trigger:(...f)=>{l(!1,...f)},forceTrigger:(...f)=>{l(!0,...f)},clear:()=>{M.set(e,O,s)}}}const g=new h,E=new _(g),R={data:void 0,isLoading:!1,error:void 0,subscribed:!1,unsubscribe:void 0},N=(o,t,n)=>g.createStatic({subscriber:o},{...R,data:t?.initialValue},n);async function q(o,t){const n=g.get(o,t);n?.value.unsubscribe&&(n.value.unsubscribe(),E.update(o,e=>({...e,unsubscribe:void 0,subscribed:!1}),t))}function ee(o,t,n){let e,i,r=n,s=!1;if(typeof o!="string"){const{key:S,subscriber:L,prefix:w,triggerImmediately:F}=o;e=S,i=L,r=w,s=F??!1}else e=D(o),i=t;const{prefix:a}=j(r);g.init(e,a,R);const u=d.useMemo(()=>S=>(g.init(e,a,R),g.addListener(e,a,S),()=>{g.removeListener(e,a,S)}),[e,a]),p=d.useMemo(()=>()=>g.get(e,a).value,[e,a]),l=d.useSyncExternalStore(u,p),f=S=>{E.update(e,L=>({...L,data:S}),a)},y=S=>{E.update(e,L=>({...L,isLoading:!1,data:void 0,error:S}),a)},m=()=>{E.update(e,S=>({...S,isLoading:!1}),a)},b=async S=>{const L=g.get(e,a);if(S&&await q(e,a),L.value.subscribed&&!S)return L.value;P("triggered !!"),E.update(e,w=>({...w,isLoading:!0,error:void 0}),a);try{const w=await i(f,y,m);E.update(e,F=>({...F,unsubscribe:w,subscribed:!0,isLoading:!1}),a)}catch(w){E.update(e,F=>({...F,isLoading:!1,error:w}),a)}};return d.useEffect(()=>()=>{P(`[${h.prefix(e,a)}]`,"unmount effect2"),g.get(e,a)?.listeners.length===0&&q(e,a)},[e,a]),g.useEffect(e,a),d.useEffect(()=>{s&&b(!1)},[]),{state:l,trigger:()=>{b(!1)},forceTrigger:()=>{b(!0)},unsubscribe:()=>{q(e,a)}}}c.SharedStatesProvider=I,c.createSharedFunction=Y,c.createSharedState=Q,c.createSharedSubscription=N,c.setDevMode=G,c.sharedFunctionsApi=M,c.sharedStatesApi=J,c.sharedSubscriptionsApi=E,c.useSharedContext=j,c.useSharedFunction=Z,c.useSharedState=U,c.useSharedStateSelector=X,c.useSharedSubscription=ee,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED