react-state-custom 1.0.3 → 1.0.4
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.es.js
CHANGED
package/dist/index.umd.js
CHANGED
|
@@ -19,4 +19,4 @@
|
|
|
19
19
|
<%s {...props} />
|
|
20
20
|
React keys must be passed directly to JSX without using spread:
|
|
21
21
|
let props = %s;
|
|
22
|
-
<%s key={someKey} {...props} />`,R,C,U,C),Ne[C+R]=!0)}if(C=null,T!==void 0&&(r(T),C=""+T),d(p)&&(r(p.key),C=""+p.key),"key"in p){T={};for(var ce in p)ce!=="key"&&(T[ce]=p[ce])}else T=p;return C&&h(T,typeof i=="function"?i.displayName||i.name||"Unknown":i),_(i,C,N,k,c(),T,ue,fe)}function O(i){typeof i=="object"&&i!==null&&i.$$typeof===P&&i._store&&(i._store.validated=1)}var L=m,P=Symbol.for("react.transitional.element"),F=Symbol.for("react.portal"),E=Symbol.for("react.fragment"),S=Symbol.for("react.strict_mode"),A=Symbol.for("react.profiler"),Y=Symbol.for("react.consumer"),G=Symbol.for("react.context"),s=Symbol.for("react.forward_ref"),a=Symbol.for("react.suspense"),o=Symbol.for("react.suspense_list"),f=Symbol.for("react.memo"),l=Symbol.for("react.lazy"),g=Symbol.for("react.activity"),v=Symbol.for("react.client.reference"),j=L.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,M=Object.prototype.hasOwnProperty,ne=Array.isArray,K=console.createTask?console.createTask:function(){return null};L={"react-stack-bottom-frame":function(i){return i()}};var Ce,je={},Le=L["react-stack-bottom-frame"].bind(L,n)(),Pe=K(u(n)),Ne={};X.Fragment=E,X.jsx=function(i,p,T,R,k){var N=1e4>j.recentlyCreatedOwnerStacks++;return w(i,p,T,!1,R,k,N?Error("react-stack-top-frame"):Le,N?K(u(i)):Pe)},X.jsxs=function(i,p,T,R,k){var N=1e4>j.recentlyCreatedOwnerStacks++;return w(i,p,T,!0,R,k,N?Error("react-stack-top-frame"):Le,N?K(u(i)):Pe)}}()),X}var Se;function ar(){return Se||(Se=1,process.env.NODE_ENV==="production"?re.exports=nr():re.exports=or()),re.exports}var Re=ar();const ir=(e,t)=>{const r=c=>[e,...Object.entries(c??{}).sort((n,d)=>n[0].localeCompare(d[0])).flat()].join("-");let u=new Set;return{Root:c=>{const n=r(c),d=te(n),h=t(c),b=m.useMemo(()=>new Error().stack,[]);return Te(d,...Object.entries(h)),m.useEffect(()=>{if(u.has(n)){const _=new Error("RootContext "+n+" are mounted more than once");throw _.stack=b,_}return u.add(n),()=>{u.delete(n)}}),Re.jsx(Re.Fragment,{})},useCtxStateStrict:c=>{const n=r(c),d=m.useMemo(()=>new Error().stack,[]);return m.useEffect(()=>{if(!u.has(n)){const h=new Error("RootContext ["+n+"] is not mounted");throw h.stack=d,h}},[n]),te(n)},useCtxState:c=>{const n=r(c),d=m.useMemo(()=>new Error().stack,[]);return m.useEffect(()=>{if(!u.has(n)){const h=new Error("RootContext ["+n+"] is not mounted");h.stack=d;let b=setTimeout(()=>console.error(h),1e3);return()=>clearTimeout(b)}},[u.has(n)]),te(n)}}},sr=e=>{const[,t]=m.useState(0),{proxy:r,finalGetter:u,openGetter:c,clean:n}=m.useMemo(()=>{const d=new Set,h={},b=new Map,_=new Proxy(e==null?void 0:e.data,{get(E,S){if(w)return d.add(S),h[S]=E[S];throw new Error("now allow here")}});let w=!0,O=ee(()=>{[...d.values()].some(E=>{var S;return h[E]!=((S=e==null?void 0:e.data)==null?void 0:S[E])})&&t(E=>E+1)},0),L=()=>{w=!0,d.clear()},P=()=>{w=!1,[...d.values()].filter(E=>!b.has(E)).forEach(E=>{b.set(E,e==null?void 0:e.subscribe(E,O))}),[...b.keys()].filter(E=>!d.has(E)).forEach(E=>{let S=b.get(E);S==null||S(),b.delete(E)})};return{proxy:_,finalGetter:P,openGetter:L,clean:()=>{L(),P(),t(E=>E+1)}}},[e]);return c(),setTimeout(u,0),m.useEffect(()=>()=>n(),[n]),r};y.Context=_e,y.createRootCtx=ir,y.getContext=Ee,y.useDataContext=te,y.useDataSource=$t,y.useDataSourceMultiple=Te,y.useDataSubscribe=qt,y.useDataSubscribeMultiple=tr,y.useDataSubscribeMultipleWithDebounce=rr,y.useDataSubscribeWithTransform=er,y.useQuickSubscribe=sr,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"})});
|
|
22
|
+
<%s key={someKey} {...props} />`,R,C,U,C),Ne[C+R]=!0)}if(C=null,T!==void 0&&(r(T),C=""+T),d(p)&&(r(p.key),C=""+p.key),"key"in p){T={};for(var ce in p)ce!=="key"&&(T[ce]=p[ce])}else T=p;return C&&h(T,typeof i=="function"?i.displayName||i.name||"Unknown":i),_(i,C,N,k,c(),T,ue,fe)}function O(i){typeof i=="object"&&i!==null&&i.$$typeof===P&&i._store&&(i._store.validated=1)}var L=m,P=Symbol.for("react.transitional.element"),F=Symbol.for("react.portal"),E=Symbol.for("react.fragment"),S=Symbol.for("react.strict_mode"),A=Symbol.for("react.profiler"),Y=Symbol.for("react.consumer"),G=Symbol.for("react.context"),s=Symbol.for("react.forward_ref"),a=Symbol.for("react.suspense"),o=Symbol.for("react.suspense_list"),f=Symbol.for("react.memo"),l=Symbol.for("react.lazy"),g=Symbol.for("react.activity"),v=Symbol.for("react.client.reference"),j=L.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,M=Object.prototype.hasOwnProperty,ne=Array.isArray,K=console.createTask?console.createTask:function(){return null};L={"react-stack-bottom-frame":function(i){return i()}};var Ce,je={},Le=L["react-stack-bottom-frame"].bind(L,n)(),Pe=K(u(n)),Ne={};X.Fragment=E,X.jsx=function(i,p,T,R,k){var N=1e4>j.recentlyCreatedOwnerStacks++;return w(i,p,T,!1,R,k,N?Error("react-stack-top-frame"):Le,N?K(u(i)):Pe)},X.jsxs=function(i,p,T,R,k){var N=1e4>j.recentlyCreatedOwnerStacks++;return w(i,p,T,!0,R,k,N?Error("react-stack-top-frame"):Le,N?K(u(i)):Pe)}}()),X}var Se;function ar(){return Se||(Se=1,process.env.NODE_ENV==="production"?re.exports=nr():re.exports=or()),re.exports}var Re=ar();const ir=(e,t)=>{const r=c=>[e,...Object.entries(c??{}).sort((n,d)=>n[0].localeCompare(d[0])).flat()].join("-");let u=new Set;return{resolveCtxName:r,Root:c=>{const n=r(c),d=te(n),h=t(c),b=m.useMemo(()=>new Error().stack,[]);return Te(d,...Object.entries(h)),m.useEffect(()=>{if(u.has(n)){const _=new Error("RootContext "+n+" are mounted more than once");throw _.stack=b,_}return u.add(n),()=>{u.delete(n)}}),Re.jsx(Re.Fragment,{})},useCtxStateStrict:c=>{const n=r(c),d=m.useMemo(()=>new Error().stack,[]);return m.useEffect(()=>{if(!u.has(n)){const h=new Error("RootContext ["+n+"] is not mounted");throw h.stack=d,h}},[n]),te(n)},useCtxState:c=>{const n=r(c),d=m.useMemo(()=>new Error().stack,[]);return m.useEffect(()=>{if(!u.has(n)){const h=new Error("RootContext ["+n+"] is not mounted");h.stack=d;let b=setTimeout(()=>console.error(h),1e3);return()=>clearTimeout(b)}},[u.has(n)]),te(n)}}},sr=e=>{const[,t]=m.useState(0),{proxy:r,finalGetter:u,openGetter:c,clean:n}=m.useMemo(()=>{const d=new Set,h={},b=new Map,_=new Proxy(e==null?void 0:e.data,{get(E,S){if(w)return d.add(S),h[S]=E[S];throw new Error("now allow here")}});let w=!0,O=ee(()=>{[...d.values()].some(E=>{var S;return h[E]!=((S=e==null?void 0:e.data)==null?void 0:S[E])})&&t(E=>E+1)},0),L=()=>{w=!0,d.clear()},P=()=>{w=!1,[...d.values()].filter(E=>!b.has(E)).forEach(E=>{b.set(E,e==null?void 0:e.subscribe(E,O))}),[...b.keys()].filter(E=>!d.has(E)).forEach(E=>{let S=b.get(E);S==null||S(),b.delete(E)})};return{proxy:_,finalGetter:P,openGetter:L,clean:()=>{L(),P(),t(E=>E+1)}}},[e]);return c(),setTimeout(u,0),m.useEffect(()=>()=>n(),[n]),r};y.Context=_e,y.createRootCtx=ir,y.getContext=Ee,y.useDataContext=te,y.useDataSource=$t,y.useDataSourceMultiple=Te,y.useDataSubscribe=qt,y.useDataSubscribeMultiple=tr,y.useDataSubscribeMultipleWithDebounce=rr,y.useDataSubscribeWithTransform=er,y.useQuickSubscribe=sr,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Context } from './ctx';
|
|
2
|
+
import { createRootCtx } from './createRootCtx';
|
|
3
|
+
export declare const AutoRootCtx: ({ Wrapper }: {
|
|
4
|
+
Wrapper?: import('react').ExoticComponent<import('react').FragmentProps> | undefined;
|
|
5
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
export declare const createAutoCtx: <U extends object, V extends object>({ Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V>>) => {
|
|
7
|
+
useCtxState: (e: U) => Context<V>;
|
|
8
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Context } from './ctx';
|
|
2
2
|
export declare const createRootCtx: <U extends object, V extends object>(name: string, useFn: (e: U) => V) => {
|
|
3
|
+
resolveCtxName: (e: U) => string;
|
|
3
4
|
Root: (e: U) => import("react/jsx-runtime").JSX.Element;
|
|
4
5
|
useCtxStateStrict: (e: U) => Context<V>;
|
|
5
6
|
useCtxState: (e: U) => Context<V>;
|
package/package.json
CHANGED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { createContext, useEffect, useMemo, useState, Fragment, useCallback } from "react"
|
|
2
|
+
import { getContext, useDataContext, useDataSource, useDataSourceMultiple, useDataSubscribe, type Context } from "./ctx"
|
|
3
|
+
import { createRootCtx } from "./createRootCtx"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const weakmapName = (function () {
|
|
11
|
+
const weakmap = new WeakMap()
|
|
12
|
+
|
|
13
|
+
return (e: any): string => {
|
|
14
|
+
let result = weakmap.get(e);
|
|
15
|
+
if (!result) {
|
|
16
|
+
weakmap.set(e, result = (e?.name ?? "") + Math.random().toString())
|
|
17
|
+
}
|
|
18
|
+
return result
|
|
19
|
+
}
|
|
20
|
+
})()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
const resolveName = (e: any) => [
|
|
24
|
+
...Object
|
|
25
|
+
.entries(e ?? {})
|
|
26
|
+
.sort((e, f) => e[0].localeCompare(f[0]))
|
|
27
|
+
.flat()
|
|
28
|
+
].join("-")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
export const AutoRootCtx = ({ Wrapper = Fragment }) => {
|
|
32
|
+
|
|
33
|
+
const ctx = useDataContext<any>("auto-ctx")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const [state, setState] = useState<Record<string, { Component: React.FC, subState: Record<string, { params: any, counter: number }> }>>({})
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
const subscribeRoot = useCallback(
|
|
40
|
+
(Comp: any, params: any) => {
|
|
41
|
+
const weakName = weakmapName(Comp);
|
|
42
|
+
const key = resolveName(params);
|
|
43
|
+
|
|
44
|
+
setState(({
|
|
45
|
+
[weakName]: {
|
|
46
|
+
Component = Comp,
|
|
47
|
+
subState: {
|
|
48
|
+
[key]: preState = { params, counter: 0 },
|
|
49
|
+
...subState
|
|
50
|
+
} = {}
|
|
51
|
+
} = {},
|
|
52
|
+
...state
|
|
53
|
+
}) => ({
|
|
54
|
+
...state,
|
|
55
|
+
[weakName]: {
|
|
56
|
+
Component,
|
|
57
|
+
subState: {
|
|
58
|
+
...subState,
|
|
59
|
+
[key]: {
|
|
60
|
+
...preState,
|
|
61
|
+
counter: preState.counter + 1,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
return () => setState(({
|
|
68
|
+
[weakName]: {
|
|
69
|
+
Component = Comp,
|
|
70
|
+
subState: {
|
|
71
|
+
[key]: preState = { params, counter: 0 },
|
|
72
|
+
...subState
|
|
73
|
+
} = {}
|
|
74
|
+
} = {},
|
|
75
|
+
...state
|
|
76
|
+
}) => ({
|
|
77
|
+
...state,
|
|
78
|
+
[weakName]: {
|
|
79
|
+
Component,
|
|
80
|
+
subState: {
|
|
81
|
+
...subState,
|
|
82
|
+
...preState.counter > 1 ? {
|
|
83
|
+
[key]: {
|
|
84
|
+
...preState,
|
|
85
|
+
counter: preState.counter - 1,
|
|
86
|
+
},
|
|
87
|
+
} : {},
|
|
88
|
+
},
|
|
89
|
+
}
|
|
90
|
+
}))
|
|
91
|
+
|
|
92
|
+
},
|
|
93
|
+
[]
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
useDataSourceMultiple(ctx,
|
|
97
|
+
["subscribe", subscribeRoot],
|
|
98
|
+
["state", state],
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
return <>
|
|
103
|
+
{Object.entries(state)
|
|
104
|
+
.flatMap(([k1, { Component, subState }]) => Object
|
|
105
|
+
.entries(subState)
|
|
106
|
+
.map(([k2, { counter, params }]) => ({ key: k1 + k2, Component, params, counter }))
|
|
107
|
+
.filter(e => e.counter > 0)
|
|
108
|
+
.map(({ key, params, Component }) => <Wrapper key={key} >
|
|
109
|
+
<Component {...params} />
|
|
110
|
+
</Wrapper>)
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
</>
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const createAutoCtx = <U extends object, V extends object,>({ Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V>>) => {
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
|
|
121
|
+
useCtxState: (e: U): Context<V> => {
|
|
122
|
+
|
|
123
|
+
const ctxName = resolveCtxName(e)
|
|
124
|
+
|
|
125
|
+
const subscribe = useDataSubscribe(useDataContext<any>("auto-ctx"), "subscribe")
|
|
126
|
+
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
return subscribe(Root, e)
|
|
129
|
+
}, [subscribe, ctxName])
|
|
130
|
+
|
|
131
|
+
return useDataContext<V>(ctxName)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|