react-state-custom 1.0.15 → 1.0.18
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 +33 -88
- package/dist/index.es.js +140 -133
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/state-utils/createAutoCtx.d.ts +1 -1
- package/package.json +10 -3
- package/src/state-utils/createAutoCtx.tsx +10 -2
package/README.md
CHANGED
|
@@ -53,41 +53,27 @@ The [API Documentation](./API_DOCUMENTATION.md) is organized into the following
|
|
|
53
53
|
|
|
54
54
|
## 🔧 Quick Example
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
import { useDataContext, useDataSource, useDataSubscribe } from 'react-state-custom';
|
|
56
|
+
### Using createRootCtx for Advanced State Management
|
|
58
57
|
|
|
59
|
-
interface AppState {
|
|
60
|
-
user: User | null;
|
|
61
|
-
theme: 'light' | 'dark';
|
|
62
|
-
}
|
|
63
58
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const user = useCurrentUser();
|
|
68
|
-
const theme = useTheme();
|
|
69
|
-
|
|
70
|
-
useDataSource(ctx, 'user', user);
|
|
71
|
-
useDataSource(ctx, 'theme', theme);
|
|
72
|
-
|
|
73
|
-
return <>{children}</>;
|
|
74
|
-
}
|
|
59
|
+
file main.tsx
|
|
60
|
+
```typescript
|
|
61
|
+
import { AutoRootCtx } from 'react-state-custom';
|
|
75
62
|
|
|
76
|
-
//
|
|
77
|
-
function
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
63
|
+
// Root Component
|
|
64
|
+
function App({children}) {
|
|
65
|
+
return (
|
|
66
|
+
<>
|
|
67
|
+
<AutoRootCtx />
|
|
68
|
+
{children}
|
|
69
|
+
</>
|
|
70
|
+
);
|
|
82
71
|
}
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
## 🔧 Additional Examples
|
|
86
|
-
|
|
87
|
-
### Using createRootCtx for Advanced State Management
|
|
88
72
|
|
|
73
|
+
```
|
|
74
|
+
file userState.ts
|
|
89
75
|
```typescript
|
|
90
|
-
import { createRootCtx,
|
|
76
|
+
import { createRootCtx, createAutoCtx, } from 'react-state-custom';
|
|
91
77
|
|
|
92
78
|
interface UserState {
|
|
93
79
|
user: User | null;
|
|
@@ -108,21 +94,20 @@ function useUserState(props: { userId: string }) {
|
|
|
108
94
|
return { user, loading, error };
|
|
109
95
|
}
|
|
110
96
|
|
|
111
|
-
//
|
|
112
|
-
const {
|
|
97
|
+
// Register State hook
|
|
98
|
+
const { useCtxState: useUserCtxState } = createAutoCtx(createRootCtx(
|
|
113
99
|
'user-state',
|
|
114
100
|
useUserState
|
|
115
|
-
);
|
|
101
|
+
));
|
|
116
102
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
103
|
+
export { useUserCtxState }
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
file UserProfile.tsx
|
|
108
|
+
```typescript
|
|
109
|
+
import { useDataSubscribeMultiple, useQuickSubscribe } from 'react-state-custom';
|
|
110
|
+
import { useUserCtxState } from "./userState.ts"
|
|
126
111
|
|
|
127
112
|
// Consumer component using useDataSubscribeMultiple
|
|
128
113
|
function UserProfile({ userId }: { userId: string }) {
|
|
@@ -134,57 +119,17 @@ function UserProfile({ userId }: { userId: string }) {
|
|
|
134
119
|
|
|
135
120
|
return <div>Welcome, {user?.name}!</div>;
|
|
136
121
|
}
|
|
137
|
-
```
|
|
138
122
|
|
|
139
|
-
|
|
123
|
+
// Or using useQuickSubscribe for Simplified Access
|
|
140
124
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
interface SettingsState {
|
|
145
|
-
theme: 'light' | 'dark';
|
|
146
|
-
language: string;
|
|
147
|
-
notifications: boolean;
|
|
148
|
-
updateSetting: (key: string, value: any) => void;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Component using useQuickSubscribe for easy property access
|
|
152
|
-
function SettingsPanel() {
|
|
153
|
-
const ctx = useDataContext<SettingsState>('settings');
|
|
125
|
+
function UserProfileV2({ userId }: { userId: string }) {
|
|
126
|
+
const ctx = useUserCtxState({ userId });
|
|
127
|
+
const { user, loading, error } = useQuickSubscribe(ctx);
|
|
154
128
|
|
|
155
|
-
|
|
156
|
-
|
|
129
|
+
if (loading) return <div>Loading user...</div>;
|
|
130
|
+
if (error) return <div>Error: {error}</div>;
|
|
157
131
|
|
|
158
|
-
return
|
|
159
|
-
<div className={`settings-panel ${theme}`}>
|
|
160
|
-
<h2>Settings</h2>
|
|
161
|
-
|
|
162
|
-
<label>
|
|
163
|
-
Theme:
|
|
164
|
-
<select value={theme} onChange={(e) => updateSetting('theme', e.target.value)}>
|
|
165
|
-
<option value="light">Light</option>
|
|
166
|
-
<option value="dark">Dark</option>
|
|
167
|
-
</select>
|
|
168
|
-
</label>
|
|
169
|
-
|
|
170
|
-
<label>
|
|
171
|
-
Language:
|
|
172
|
-
<input
|
|
173
|
-
value={language}
|
|
174
|
-
onChange={(e) => updateSetting('language', e.target.value)}
|
|
175
|
-
/>
|
|
176
|
-
</label>
|
|
177
|
-
|
|
178
|
-
<label>
|
|
179
|
-
<input
|
|
180
|
-
type="checkbox"
|
|
181
|
-
checked={notifications}
|
|
182
|
-
onChange={(e) => updateSetting('notifications', e.target.checked)}
|
|
183
|
-
/>
|
|
184
|
-
Enable notifications
|
|
185
|
-
</label>
|
|
186
|
-
</div>
|
|
187
|
-
);
|
|
132
|
+
return <div>Welcome, {user?.name}!</div>;
|
|
188
133
|
}
|
|
189
134
|
```
|
|
190
135
|
|
package/dist/index.es.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { useRef as
|
|
1
|
+
import { useRef as D, useMemo as d, useEffect as b, useState as S, useCallback as O, Fragment as R } from "react";
|
|
2
2
|
import { jsx as g, Fragment as T } from "react/jsx-runtime";
|
|
3
|
-
function w(
|
|
4
|
-
let r = null, s = function(...
|
|
3
|
+
function w(t, e) {
|
|
4
|
+
let r = null, s = function(...u) {
|
|
5
5
|
r && clearTimeout(r), r = setTimeout(() => {
|
|
6
|
-
|
|
7
|
-
},
|
|
6
|
+
t(...u);
|
|
7
|
+
}, e);
|
|
8
8
|
};
|
|
9
9
|
return s.cancel = () => clearTimeout(r), s;
|
|
10
10
|
}
|
|
11
|
-
function V(
|
|
12
|
-
const
|
|
11
|
+
function V(t) {
|
|
12
|
+
const e = /* @__PURE__ */ new Map();
|
|
13
13
|
return function(...r) {
|
|
14
14
|
const s = JSON.stringify(r);
|
|
15
|
-
if (
|
|
16
|
-
return
|
|
17
|
-
const
|
|
18
|
-
return
|
|
15
|
+
if (e.has(s))
|
|
16
|
+
return e.get(s);
|
|
17
|
+
const u = t(...r);
|
|
18
|
+
return e.set(s, u), u;
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
-
const v = () => Math.random().toString().slice(2), j = (
|
|
22
|
-
const { current: { computedHash:
|
|
21
|
+
const v = () => Math.random().toString().slice(2), j = (t) => {
|
|
22
|
+
const { current: { computedHash: e } } = D({
|
|
23
23
|
/**
|
|
24
24
|
* Getter for the computed hash function.
|
|
25
25
|
*
|
|
@@ -29,17 +29,17 @@ const v = () => Math.random().toString().slice(2), j = (e) => {
|
|
|
29
29
|
*/
|
|
30
30
|
get computedHash() {
|
|
31
31
|
let r = [], s = v();
|
|
32
|
-
return (
|
|
32
|
+
return (u) => {
|
|
33
33
|
let n = !1;
|
|
34
|
-
return n = n || !
|
|
34
|
+
return n = n || !u != !r, n = n || u?.length != r?.length, n = n || u.some((a, c) => a != r[c]), r = u, n && (s = v()), s;
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
});
|
|
38
|
-
return t
|
|
38
|
+
return e(t);
|
|
39
39
|
};
|
|
40
40
|
class G extends Event {
|
|
41
|
-
constructor(
|
|
42
|
-
super(
|
|
41
|
+
constructor(e, r) {
|
|
42
|
+
super(e), this.event = e, this.value = r;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
class H extends EventTarget {
|
|
@@ -47,8 +47,8 @@ class H extends EventTarget {
|
|
|
47
47
|
* Create a new Context instance.
|
|
48
48
|
* @param name - The name of the context (for debugging).
|
|
49
49
|
*/
|
|
50
|
-
constructor(
|
|
51
|
-
console.log("[CONTEXT] %s",
|
|
50
|
+
constructor(e) {
|
|
51
|
+
console.log("[CONTEXT] %s", e), super(), this.name = e;
|
|
52
52
|
}
|
|
53
53
|
// private event = new EventEmitter()
|
|
54
54
|
/**
|
|
@@ -64,8 +64,8 @@ class H extends EventTarget {
|
|
|
64
64
|
* @param key - The key to update.
|
|
65
65
|
* @param value - The new value.
|
|
66
66
|
*/
|
|
67
|
-
publish(
|
|
68
|
-
r != this.data[
|
|
67
|
+
publish(e, r) {
|
|
68
|
+
r != this.data[e] && (this.data[e] = r, this.dispatchEvent(new G(String(e), r)));
|
|
69
69
|
}
|
|
70
70
|
/**
|
|
71
71
|
* Subscribe to changes for a specific key in the context.
|
|
@@ -73,100 +73,100 @@ class H extends EventTarget {
|
|
|
73
73
|
* @param _listener - Callback invoked with the new value.
|
|
74
74
|
* @returns Unsubscribe function.
|
|
75
75
|
*/
|
|
76
|
-
subscribe(
|
|
77
|
-
const s = ({ event:
|
|
76
|
+
subscribe(e, r) {
|
|
77
|
+
const s = ({ event: u, value: n }) => {
|
|
78
78
|
r(n);
|
|
79
79
|
};
|
|
80
|
-
return this.addEventListener(String(
|
|
80
|
+
return this.addEventListener(String(e), s), e in this.data && r(this.data[e]), () => (this.removeEventListener(String(e), s), void 0);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
const A = V((
|
|
84
|
-
const r = new Error("[ctx] useRegistryChecker failed " + JSON.stringify({ names:
|
|
85
|
-
|
|
83
|
+
const A = V((t) => new H(t)), p = (t = "noname") => d(() => A(t), [t]), N = (t, ...e) => {
|
|
84
|
+
const r = new Error("[ctx] useRegistryChecker failed " + JSON.stringify({ names: e, ctx: t?.name ?? "undefined" }));
|
|
85
|
+
b(
|
|
86
86
|
() => {
|
|
87
|
-
if (
|
|
88
|
-
return
|
|
89
|
-
|
|
87
|
+
if (t)
|
|
88
|
+
return e.some((s) => t.registry.has(s)) && console.error(r), e.forEach((s) => t.registry.add(s)), () => {
|
|
89
|
+
e.forEach((s) => t.registry.delete(s));
|
|
90
90
|
};
|
|
91
91
|
},
|
|
92
|
-
[
|
|
92
|
+
[t, e.length]
|
|
93
93
|
);
|
|
94
|
-
}, x = (
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}, [
|
|
98
|
-
}, W = (
|
|
99
|
-
const [{ value: s },
|
|
100
|
-
return
|
|
101
|
-
if (
|
|
102
|
-
let n = r == 0 ? (c) =>
|
|
103
|
-
return s !=
|
|
94
|
+
}, x = (t, e, r) => {
|
|
95
|
+
b(() => {
|
|
96
|
+
t && t.data[e] != r && t.publish(e, r);
|
|
97
|
+
}, [e, r, t]), N(t, e);
|
|
98
|
+
}, W = (t, e, r = 0) => {
|
|
99
|
+
const [{ value: s }, u] = S(() => ({ value: t?.data?.[e] }));
|
|
100
|
+
return b(() => {
|
|
101
|
+
if (t) {
|
|
102
|
+
let n = r == 0 ? (c) => u({ value: c }) : w((c) => u({ value: c }), r), a = t.subscribe(e, n);
|
|
103
|
+
return s != t.data[e] && u({ value: t.data[e] }), () => {
|
|
104
104
|
a();
|
|
105
105
|
};
|
|
106
106
|
}
|
|
107
|
-
}, [
|
|
108
|
-
}, z = (
|
|
109
|
-
const [, s] = S(0),
|
|
110
|
-
() => r(
|
|
111
|
-
[r,
|
|
107
|
+
}, [e, t]), t?.data[e];
|
|
108
|
+
}, z = (t, e, r) => {
|
|
109
|
+
const [, s] = S(0), u = d(
|
|
110
|
+
() => r(t?.data[e]),
|
|
111
|
+
[r, t?.data[e]]
|
|
112
112
|
);
|
|
113
|
-
return
|
|
114
|
-
if (
|
|
115
|
-
let n =
|
|
116
|
-
let
|
|
117
|
-
|
|
118
|
-
}, c =
|
|
113
|
+
return b(() => {
|
|
114
|
+
if (t) {
|
|
115
|
+
let n = u, a = () => {
|
|
116
|
+
let o = r(t.data[e]);
|
|
117
|
+
o != n && (n = o, s((l) => l + 1));
|
|
118
|
+
}, c = t.subscribe(e, a);
|
|
119
119
|
return a(), () => c();
|
|
120
120
|
}
|
|
121
|
-
}, [
|
|
122
|
-
}, M = (
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
for (let [r, s] of
|
|
126
|
-
|
|
127
|
-
}, [
|
|
128
|
-
}, K = (
|
|
129
|
-
const [, r] = S(0), s =
|
|
130
|
-
return
|
|
131
|
-
if (
|
|
132
|
-
let
|
|
121
|
+
}, [e, t]), u;
|
|
122
|
+
}, M = (t, ...e) => {
|
|
123
|
+
b(() => {
|
|
124
|
+
if (t)
|
|
125
|
+
for (let [r, s] of e)
|
|
126
|
+
t.data[r] != s && t.publish(r, s);
|
|
127
|
+
}, [t, j(e.flat())]), N(t, ...e.map((r) => r[0]));
|
|
128
|
+
}, K = (t, ...e) => {
|
|
129
|
+
const [, r] = S(0), s = e.map((u) => t?.data?.[u]);
|
|
130
|
+
return b(() => {
|
|
131
|
+
if (t) {
|
|
132
|
+
let u = s;
|
|
133
133
|
const n = w(() => {
|
|
134
|
-
let
|
|
135
|
-
|
|
134
|
+
let o = e.map((l) => t?.data?.[l]);
|
|
135
|
+
e.some((l, i) => u[i] != o[i]) && (u = o, r((l) => l + 1));
|
|
136
136
|
}, 1);
|
|
137
|
-
let a =
|
|
137
|
+
let a = e.map((o) => t.subscribe(o, n)), c = setTimeout(n, 1);
|
|
138
138
|
return () => {
|
|
139
|
-
clearTimeout(c), n.cancel(), a.forEach((
|
|
139
|
+
clearTimeout(c), n.cancel(), a.forEach((o) => o());
|
|
140
140
|
};
|
|
141
141
|
}
|
|
142
|
-
}, [
|
|
143
|
-
}, P = (
|
|
144
|
-
const [, s] = S(0),
|
|
145
|
-
return
|
|
146
|
-
if (
|
|
147
|
-
let n =
|
|
142
|
+
}, [t, ...e]), Object.fromEntries(e.map((u, n) => [u, s[n]]));
|
|
143
|
+
}, P = (t, e = 50, ...r) => {
|
|
144
|
+
const [, s] = S(0), u = r.map((n) => t?.data?.[n]);
|
|
145
|
+
return b(() => {
|
|
146
|
+
if (t) {
|
|
147
|
+
let n = u;
|
|
148
148
|
const a = w(() => {
|
|
149
|
-
let l = r.map((i) =>
|
|
150
|
-
r.some((i,
|
|
151
|
-
},
|
|
152
|
-
let c = r.map((l) =>
|
|
149
|
+
let l = r.map((i) => t?.data?.[i]);
|
|
150
|
+
r.some((i, m) => n[m] != l[m]) && (n = l, s((i) => i + 1));
|
|
151
|
+
}, e);
|
|
152
|
+
let c = r.map((l) => t.subscribe(l, a)), o = setTimeout(a, 1);
|
|
153
153
|
return () => {
|
|
154
|
-
clearTimeout(
|
|
154
|
+
clearTimeout(o), a.cancel(), c.forEach((l) => l());
|
|
155
155
|
};
|
|
156
156
|
}
|
|
157
|
-
}, [
|
|
158
|
-
}, Q = (
|
|
157
|
+
}, [t, ...r]), u;
|
|
158
|
+
}, Q = (t, e) => {
|
|
159
159
|
const r = (n) => [
|
|
160
|
-
|
|
160
|
+
t,
|
|
161
161
|
...Object.entries(n ?? {}).sort((a, c) => a[0].localeCompare(c[0])).flat()
|
|
162
162
|
].join("-");
|
|
163
163
|
let s = /* @__PURE__ */ new Set();
|
|
164
|
-
const
|
|
165
|
-
const a =
|
|
164
|
+
const u = (n) => {
|
|
165
|
+
const a = e(n), c = r(n), o = p(c), l = d(() => new Error().stack, []);
|
|
166
166
|
return M(
|
|
167
|
-
|
|
167
|
+
o,
|
|
168
168
|
...Object.entries(a)
|
|
169
|
-
),
|
|
169
|
+
), b(() => {
|
|
170
170
|
if (s.has(c)) {
|
|
171
171
|
const i = new Error("RootContext " + c + " are mounted more than once");
|
|
172
172
|
throw i.stack = l, i;
|
|
@@ -176,19 +176,19 @@ const A = V((e) => new H(e)), p = (e = "noname") => d(() => A(e), [e]), N = (e,
|
|
|
176
176
|
};
|
|
177
177
|
}), /* @__PURE__ */ g(T, {});
|
|
178
178
|
};
|
|
179
|
-
return
|
|
179
|
+
return u.displayName = `State[${e?.name ?? "??"}]`, {
|
|
180
180
|
resolveCtxName: r,
|
|
181
|
-
Root:
|
|
181
|
+
Root: u,
|
|
182
182
|
/**
|
|
183
183
|
* Strict consumer: throws if the corresponding Root for these props isn't mounted.
|
|
184
184
|
* Use in development/tests to fail fast when wiring is incorrect.
|
|
185
185
|
*/
|
|
186
186
|
useCtxStateStrict: (n) => {
|
|
187
187
|
const a = r(n), c = d(() => new Error().stack, []);
|
|
188
|
-
return
|
|
188
|
+
return b(() => {
|
|
189
189
|
if (!s.has(a)) {
|
|
190
|
-
const
|
|
191
|
-
throw
|
|
190
|
+
const o = new Error("RootContext [" + a + "] is not mounted");
|
|
191
|
+
throw o.stack = c, o;
|
|
192
192
|
}
|
|
193
193
|
}, [a]), p(a);
|
|
194
194
|
},
|
|
@@ -198,34 +198,34 @@ const A = V((e) => new H(e)), p = (e = "noname") => d(() => A(e), [e]), N = (e,
|
|
|
198
198
|
*/
|
|
199
199
|
useCtxState: (n) => {
|
|
200
200
|
const a = r(n), c = d(() => new Error().stack, []);
|
|
201
|
-
return
|
|
201
|
+
return b(() => {
|
|
202
202
|
if (!s.has(a)) {
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
let l = setTimeout(() => console.error(
|
|
203
|
+
const o = new Error("RootContext [" + a + "] is not mounted");
|
|
204
|
+
o.stack = c;
|
|
205
|
+
let l = setTimeout(() => console.error(o), 1e3);
|
|
206
206
|
return () => clearTimeout(l);
|
|
207
207
|
}
|
|
208
208
|
}, [s.has(a)]), p(a);
|
|
209
209
|
}
|
|
210
210
|
};
|
|
211
211
|
}, F = /* @__PURE__ */ function() {
|
|
212
|
-
const
|
|
213
|
-
return (
|
|
214
|
-
let r =
|
|
215
|
-
return r ||
|
|
212
|
+
const t = /* @__PURE__ */ new WeakMap();
|
|
213
|
+
return (e) => {
|
|
214
|
+
let r = t.get(e);
|
|
215
|
+
return r || t.set(e, r = (e?.name ?? "") + Math.random().toString()), r;
|
|
216
216
|
};
|
|
217
|
-
}(), J = (
|
|
218
|
-
...Object.entries(
|
|
219
|
-
].join("-"), U = ({ Wrapper:
|
|
220
|
-
const
|
|
217
|
+
}(), J = (t) => [
|
|
218
|
+
...Object.entries(t ?? {}).sort((e, r) => e[0].localeCompare(r[0])).flat()
|
|
219
|
+
].join("-"), U = ({ Wrapper: t = R }) => {
|
|
220
|
+
const e = p("auto-ctx"), [r, s] = S({}), u = O(
|
|
221
221
|
(n, a) => {
|
|
222
|
-
const c = F(n),
|
|
222
|
+
const c = F(n), o = J(a);
|
|
223
223
|
return s(({
|
|
224
224
|
[c]: {
|
|
225
225
|
Component: l = n,
|
|
226
226
|
subState: {
|
|
227
|
-
[
|
|
228
|
-
...
|
|
227
|
+
[o]: i = { params: a, counter: 0 },
|
|
228
|
+
...m
|
|
229
229
|
} = {}
|
|
230
230
|
} = {},
|
|
231
231
|
...h
|
|
@@ -234,8 +234,8 @@ const A = V((e) => new H(e)), p = (e = "noname") => d(() => A(e), [e]), N = (e,
|
|
|
234
234
|
[c]: {
|
|
235
235
|
Component: l,
|
|
236
236
|
subState: {
|
|
237
|
-
...
|
|
238
|
-
[
|
|
237
|
+
...m,
|
|
238
|
+
[o]: {
|
|
239
239
|
...i,
|
|
240
240
|
counter: i.counter + 1
|
|
241
241
|
}
|
|
@@ -245,8 +245,8 @@ const A = V((e) => new H(e)), p = (e = "noname") => d(() => A(e), [e]), N = (e,
|
|
|
245
245
|
[c]: {
|
|
246
246
|
Component: l = n,
|
|
247
247
|
subState: {
|
|
248
|
-
[
|
|
249
|
-
...
|
|
248
|
+
[o]: i = { params: a, counter: 0 },
|
|
249
|
+
...m
|
|
250
250
|
} = {}
|
|
251
251
|
} = {},
|
|
252
252
|
...h
|
|
@@ -255,9 +255,9 @@ const A = V((e) => new H(e)), p = (e = "noname") => d(() => A(e), [e]), N = (e,
|
|
|
255
255
|
[c]: {
|
|
256
256
|
Component: l,
|
|
257
257
|
subState: {
|
|
258
|
-
...
|
|
258
|
+
...m,
|
|
259
259
|
...i.counter > 1 ? {
|
|
260
|
-
[
|
|
260
|
+
[o]: {
|
|
261
261
|
...i,
|
|
262
262
|
counter: i.counter - 1
|
|
263
263
|
}
|
|
@@ -269,22 +269,29 @@ const A = V((e) => new H(e)), p = (e = "noname") => d(() => A(e), [e]), N = (e,
|
|
|
269
269
|
[]
|
|
270
270
|
);
|
|
271
271
|
return M(
|
|
272
|
-
|
|
273
|
-
["subscribe",
|
|
272
|
+
e,
|
|
273
|
+
["subscribe", u],
|
|
274
274
|
["state", r]
|
|
275
275
|
), /* @__PURE__ */ g(T, { children: Object.entries(r).flatMap(
|
|
276
|
-
([n, { Component: a, subState: c }]) => Object.entries(c).map(([
|
|
276
|
+
([n, { Component: a, subState: c }]) => Object.entries(c).map(([o, { counter: l, params: i }]) => ({ key: n + o, Component: a, params: i, counter: l })).filter((o) => o.counter > 0).map(({ key: o, params: l, Component: i }) => /* @__PURE__ */ g(t, { children: /* @__PURE__ */ g(i, { ...l }) }, o))
|
|
277
277
|
) });
|
|
278
|
-
}, X = ({ Root:
|
|
279
|
-
useCtxState: (
|
|
280
|
-
const
|
|
281
|
-
return
|
|
278
|
+
}, X = ({ Root: t, useCtxState: e, useCtxStateStrict: r, resolveCtxName: s }, u = 0) => ({
|
|
279
|
+
useCtxState: (n) => {
|
|
280
|
+
const a = s(n), c = W(p("auto-ctx"), "subscribe");
|
|
281
|
+
return b(() => {
|
|
282
|
+
if (u == 0)
|
|
283
|
+
return c?.(t, n);
|
|
284
|
+
{
|
|
285
|
+
let o = c?.(t, n);
|
|
286
|
+
return () => setTimeout(o, u);
|
|
287
|
+
}
|
|
288
|
+
}, [c, a]), p(a);
|
|
282
289
|
}
|
|
283
|
-
}), q = (
|
|
284
|
-
const [,
|
|
290
|
+
}), q = (t) => {
|
|
291
|
+
const [, e] = S(0), { proxy: r, finalGetter: s, openGetter: u, clean: n } = d(
|
|
285
292
|
() => {
|
|
286
|
-
const a = /* @__PURE__ */ new Set(), c = {},
|
|
287
|
-
|
|
293
|
+
const a = /* @__PURE__ */ new Set(), c = {}, o = /* @__PURE__ */ new Map(), l = new Proxy(
|
|
294
|
+
t?.data,
|
|
288
295
|
{
|
|
289
296
|
get(f, C) {
|
|
290
297
|
if (i)
|
|
@@ -293,24 +300,24 @@ const A = V((e) => new H(e)), p = (e = "noname") => d(() => A(e), [e]), N = (e,
|
|
|
293
300
|
}
|
|
294
301
|
}
|
|
295
302
|
);
|
|
296
|
-
let i = !0,
|
|
297
|
-
[...a.values()].some((f) => c[f] !=
|
|
303
|
+
let i = !0, m = w(() => {
|
|
304
|
+
[...a.values()].some((f) => c[f] != t?.data?.[f]) && e((f) => f + 1);
|
|
298
305
|
}, 0), h = () => {
|
|
299
306
|
i = !0, a.clear();
|
|
300
307
|
}, E = () => {
|
|
301
|
-
i = !1, [...a.values()].filter((f) => !
|
|
302
|
-
|
|
303
|
-
}), [...
|
|
304
|
-
|
|
308
|
+
i = !1, [...a.values()].filter((f) => !o.has(f)).forEach((f) => {
|
|
309
|
+
o.set(f, t?.subscribe(f, m));
|
|
310
|
+
}), [...o.keys()].filter((f) => !a.has(f)).forEach((f) => {
|
|
311
|
+
o.get(f)?.(), o.delete(f);
|
|
305
312
|
});
|
|
306
313
|
};
|
|
307
314
|
return { proxy: l, finalGetter: E, openGetter: h, clean: () => {
|
|
308
|
-
h(), E(),
|
|
315
|
+
h(), E(), e((f) => f + 1);
|
|
309
316
|
} };
|
|
310
317
|
},
|
|
311
|
-
[
|
|
318
|
+
[t]
|
|
312
319
|
);
|
|
313
|
-
return
|
|
320
|
+
return u(), setTimeout(s, 0), b(
|
|
314
321
|
() => () => n(),
|
|
315
322
|
[n]
|
|
316
323
|
), r;
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/state-utils/utils.ts","../src/state-utils/useArrayHash.ts","../src/state-utils/ctx.ts","../src/state-utils/createRootCtx.tsx","../src/state-utils/createAutoCtx.tsx","../src/state-utils/useQuickSubscribe.ts"],"sourcesContent":["// Debounce function\nexport function debounce<T extends (...args: any[]) => any>(\n func: T,\n wait: number\n): ((...args: Parameters<T>) => void) & { cancel: any } {\n let timeout: ReturnType<typeof setTimeout> | null = null;\n\n let fn: Function & { cancel: any } = function (...args: Parameters<T>): void {\n if (timeout) {\n clearTimeout(timeout);\n }\n timeout = setTimeout(() => {\n func(...args);\n }, wait);\n } as any; \n\n fn.cancel = () => clearTimeout(timeout!);\n\n return fn as any;\n}\n\n// Memoize function\nexport function memoize<T extends (...args: any[]) => any>(\n func: T\n): (...args: Parameters<T>) => ReturnType<T> {\n const cache = new Map<string, ReturnType<T>>();\n\n return function (...args: Parameters<T>): ReturnType<T> {\n const key = JSON.stringify(args);\n if (cache.has(key)) {\n return cache.get(key) as ReturnType<T>;\n }\n const result = func(...args);\n cache.set(key, result);\n return result;\n };\n}\n\n","import { useRef } from \"react\"\n\n\nconst randomHash = () => Math.random().toString().slice(2)\n\n/**\n * useArrayHash\n *\n * A custom hook that computes a stable hash for an array of values.\n * The hash changes only when the array's contents differ from the previous call.\n *\n * @param e - The input array to hash.\n * @returns A string hash that updates when the array changes.\n *\n * How it works:\n * - Tracks the previous array and its hash using a `useRef`.\n * - Compares the new array to the previous one by length and element equality.\n * - If any difference is detected, generates a new random hash.\n */\nexport const useArrayHash = (e: any[]): string => {\n\n const { current: { computedHash } } = useRef({\n /**\n * Getter for the computed hash function.\n *\n * - Initializes with an empty array and a random hash.\n * - Returns a function that compares the current array to the previous one.\n * - Updates the hash if any difference is detected.\n */\n get computedHash() {\n let currentValues: any[] = []\n let currentHash = randomHash()\n return (e: any[]) => {\n let isDiff = false\n\n // Check for differences in array existence, length, or elements.\n isDiff = isDiff || ((!e) != (!currentValues))\n isDiff = isDiff || (e?.length != currentValues?.length);\n isDiff = isDiff || (e.some((f, i) => f != currentValues[i]));\n\n // Update the hash if differences are found.\n currentValues = e;\n if (isDiff) {\n currentHash = randomHash()\n }\n\n return currentHash\n }\n }\n })\n\n return computedHash(e)\n}","import { debounce, memoize } from \"./utils\";\nimport { useEffect, useMemo, useState } from \"react\"\nimport { useArrayHash } from \"./useArrayHash\"\n\n\n\nclass DataEvent extends Event {\n constructor(\n public event: string,\n public value: any\n ) {\n super(event);\n }\n}\n\n/**\n * Generic context for managing shared state and event subscriptions.\n * @template D - The shape of the data managed by the context.\n */\nexport class Context<D> extends EventTarget {\n /**\n * Create a new Context instance.\n * @param name - The name of the context (for debugging).\n */\n constructor(public name: string) {\n console.log(\"[CONTEXT] %s\", name)\n // this.event.setMaxListeners(100)\n super();\n }\n\n // private event = new EventEmitter()\n\n /**\n * The current data held by the context.\n */\n public data: Partial<D> = {}\n /**\n * Registry for tracking active keys (for duplicate detection).\n */\n public registry = new Set<string>()\n\n /**\n * Publish a value to the context and notify subscribers if it changed.\n * @param key - The key to update.\n * @param value - The new value.\n */\n public publish(key: keyof D, value: D[typeof key] | undefined) {\n\n if (value != this.data[key]) {\n this.data[key] = value\n // console.count(\"[COUNT] \" + String(key))\n // this.event.emit(String(key), { value })\n this.dispatchEvent(new DataEvent(String(key), value))\n }\n }\n\n /**\n * Subscribe to changes for a specific key in the context.\n * @param key - The key to subscribe to.\n * @param _listener - Callback invoked with the new value.\n * @returns Unsubscribe function.\n */\n public subscribe(key: keyof D, _listener: (e: D[typeof key] | undefined) => void) {\n\n const listener = ({ event, value }: any) => {\n _listener(value)\n }\n\n this.addEventListener(String(key), listener)\n // console.log(\"listenerCount:\", String(key), this.event.listenerCount(String(key)))\n\n if (key in this.data) _listener(this.data[key])\n\n return () => (this.removeEventListener(String(key), listener), undefined)\n }\n\n}\n\n/**\n * Get or create a memoized Context instance by name.\n * @param name - The context name.\n * @returns The Context instance.\n */\nexport const getContext = memoize((name: string) => new Context<any>(name))\n\n/**\n * Type alias for a function that returns a Context instance.\n */\nexport type getContext<D> = (e: string) => Context<D>\n\n/**\n * React hook to get a typed Context instance by name.\n * @param name - The context name.\n * @returns The Context instance.\n */\nexport const useDataContext = <D>(name: string = \"noname\") => {\n\n const ctx = useMemo(() => getContext(name), [name])\n\n return ctx as any as Context<D>\n}\n\n/**\n * Internal hook to check for duplicate registry entries in a context.\n * Warns if any of the provided names are already registered.\n * @param ctx - The context instance.\n * @param names - Names to check and register.\n */\nconst useRegistryChecker = (ctx: Context<any> | undefined, ...names: string[]) => {\n // return;\n const stack = new Error(\"[ctx] useRegistryChecker failed \" + JSON.stringify({ names, ctx: ctx?.name ?? 'undefined' }))\n\n useEffect(\n () => {\n if (ctx) {\n if (names.some(name => ctx.registry.has(name))) {\n console.error(stack)\n }\n names.forEach(e => ctx.registry.add(e))\n\n // console.debug(\"[ctx] %s%s add datasource\", componentId, ctx.name, names)\n return () => {\n // console.debug(\"[ctx] %s %s remove datasource\", componentId, ctx.name, names)\n\n names.forEach(e => ctx.registry.delete(e))\n }\n }\n },\n [ctx, names.length]\n )\n\n}\n\n/**\n * React hook to publish a value to the context when it changes.\n * @param ctx - The context instance.\n * @param key - The key to update.\n * @param value - The new value.\n */\nexport const useDataSource = <D, K extends keyof D>(ctx: Context<D> | undefined, key: K, value: D[K] | undefined) => {\n //@ts-check\n useEffect(() => {\n if (ctx && ctx.data[key] != value) {\n\n ctx.publish(key, value)\n }\n }, [key, value, ctx])\n\n useRegistryChecker(ctx, key as any)\n}\n\n/**\n * React hook to subscribe to a context value, with optional debounce.\n * @param ctx - The context instance.\n * @param key - The key to subscribe to.\n * @param debounceTime - Debounce time in ms (default 0).\n * @returns The current value for the key.\n */\nexport const useDataSubscribe = <D, K extends keyof D>(ctx: Context<D> | undefined, key: K, debounceTime = 0): D[K] | undefined => {\n //@ts-check\n const [{ value }, setState] = useState(() => ({ value: ctx?.data?.[key] }))\n\n useEffect(() => {\n if (ctx) {\n let callback = debounceTime == 0\n ? (value: any) => setState({ value } as any)\n : debounce((value: any) => setState({ value } as any), debounceTime)\n let unsub = ctx.subscribe(key, callback)\n value != ctx.data[key] && setState({ value: ctx.data[key] })\n return () => {\n unsub()\n }\n }\n }, [key, ctx])\n\n return ctx?.data[key]\n}\n\n/**\n * React hook to subscribe to a context value and transform it before returning.\n * @param ctx - The context instance.\n * @param key - The key to subscribe to.\n * @param transform - Function to transform the value.\n * @returns The transformed value.\n */\nexport const useDataSubscribeWithTransform = <D, K extends keyof D, E>(ctx: Context<D> | undefined, key: K, transform: (e: D[K] | undefined) => E): E => {\n const [, setState] = useState(0)\n const result = useMemo(\n () => transform(ctx?.data[key]),\n [transform, ctx?.data[key]]\n )\n\n useEffect(() => {\n if (ctx) {\n let preValue = result\n let callback = () => {\n let newValue = transform(ctx.data[key])\n if (newValue != preValue) {\n preValue = newValue;\n setState(e => e + 1)\n };\n }\n let unsub = ctx.subscribe(key, callback)\n callback();\n return () => unsub()\n }\n }, [key, ctx])\n\n return result\n}\n\n/**\n * React hook to publish multiple values to the context.\n * @param ctx - The context instance.\n * @param entries - Array of [key, value] pairs to update.\n */\nexport const useDataSourceMultiple = <D, T extends readonly (keyof D)[]>(\n ctx: Context<D> | undefined,\n ...entries: { -readonly [P in keyof T]: [T[P], D[T[P]]] }\n) => {\n //@ts-check\n useEffect(() => {\n if (ctx) {\n for (let [key, value] of entries) {\n ctx.data[key] != value && ctx.publish(key, value)\n }\n }\n }, [ctx, useArrayHash(entries.flat())])\n\n useRegistryChecker(ctx, ...entries.map(e => e[0]) as any)\n\n}\n\n/**\n * React hook to subscribe to multiple context values.\n * @param ctx - The context instance.\n * @param keys - Keys to subscribe to.\n * @returns An object with the current values for the keys.\n */\nexport const useDataSubscribeMultiple = <D, K extends keyof D>(\n ctx: Context<D> | undefined,\n ...keys: K[]\n): Pick<D, K> => {\n const [, setCounter] = useState(0)\n\n const returnValues = keys.map(key => ctx?.data?.[key])\n\n useEffect(() => {\n if (ctx) {\n let prevValues = returnValues\n const callback = debounce(() => {\n let currentValues = keys.map(key => ctx?.data?.[key])\n if (keys.some((key, i) => prevValues[i] != currentValues[i])) {\n // console.log(\"DIFF\", keys.filter((e, i) => prevValues[i] != currentValues[i]))\n prevValues = currentValues\n setCounter(c => c + 1)\n }\n }, 1)\n\n let handles = keys.map(key => ctx.subscribe(key, callback))\n\n let firstCall = setTimeout(callback, 1);\n\n return () => {\n clearTimeout(firstCall)\n callback.cancel();\n handles.forEach(unsub => unsub())\n }\n\n }\n }, [ctx, ...keys])\n\n\n return Object\n .fromEntries(keys.map((key, index) => [key, returnValues[index]])) as any\n}\n\n/**\n * React hook to subscribe to multiple context values with throttling.\n * @param ctx - The context instance.\n * @param debounceTime - Debounce time in ms (default 50).\n * @param keys - Keys to subscribe to.\n * @returns Array of current values for the keys.\n */\nexport const useDataSubscribeMultipleWithDebounce = <D, K extends (keyof D)[]>(\n ctx: Context<D> | undefined,\n debounceTime = 50,\n ...keys: K\n): { [i in keyof K]: D[K[i]] | undefined } => {\n //@ts-check\n const [, setCounter] = useState(0)\n\n const returnValues = keys.map(key => ctx?.data?.[key])\n\n useEffect(() => {\n if (ctx) {\n let prevValues = returnValues\n const callback = debounce(() => {\n let currentValues = keys.map(key => ctx?.data?.[key])\n if (keys.some((key, i) => prevValues[i] != currentValues[i])) {\n prevValues = currentValues\n setCounter(c => c + 1)\n }\n }, debounceTime)\n\n let handles = keys.map(key => ctx.subscribe(key, callback))\n\n let firstCall = setTimeout(callback, 1);\n\n return () => {\n clearTimeout(firstCall)\n callback.cancel();\n handles.forEach(unsub => unsub())\n }\n\n }\n }, [ctx, ...keys])\n\n return returnValues as any\n}\n\n\n\n","import { useEffect, useMemo } from \"react\"\nimport { useDataContext, useDataSourceMultiple, type Context } from \"./ctx\"\n\n\n\n/**\n * createRootCtx\n *\n * Factory that creates a headless \"Root\" component and companion hooks for a context namespace.\n * It derives a unique context name from a base `name` and a props object `U`, then publishes\n * a computed state `V` (from `useFn`) to that context.\n *\n * Usage (manual mounting):\n * ```\n * const { Root, useCtxState } = createRootCtx('user-state', useUserState)\n * ...\n * // Mount exactly one Root per unique props combination\n * <Root userId={id} />\n * ...\n * // Read anywhere ,using the same props shape\n * const user = useCtxState({ userId: id })\n *```\n * Strict vs lenient consumers:\n * - useCtxStateStrict(props) throws if a matching Root is not mounted.\n * - useCtxState(props) logs an error (after 1s) instead of throwing.\n *\n * Multiple instances safety:\n * - Mounting more than one Root with the same resolved context name throws (guards accidental duplicates).\n *\n * Name resolution notes:\n * - The context name is built from `name` + sorted key/value pairs of `props` (U), joined by \"-\".\n * - Prefer stable, primitive props to avoid collisions; if you need automation, pair with `createAutoCtx` and\n * mount a single <AutoRootCtx Wrapper={ErrorBoundary} /> at the app root so you don't manually mount `Root`.\n */\nexport const createRootCtx = <U extends object, V extends object>(name: string, useFn: (e: U) => V) => {\n\n const resolveCtxName = (e: U) => [\n name,\n ...Object\n .entries(e ?? {})\n .sort((e, f) => e[0].localeCompare(f[0]))\n .flat()\n ].join(\"-\")\n\n let ctxMountedCheck = new Set<string>()\n\n\n const RootState: React.FC<U> = (e: U) => {\n const state = useFn(e)\n const ctxName = resolveCtxName(e)\n const ctx = useDataContext<V>(ctxName)\n const stack = useMemo(() => new Error().stack, [])\n\n useDataSourceMultiple(\n ctx,\n ...Object.entries(state) as any\n )\n\n useEffect(() => {\n if (ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext \" + ctxName + \" are mounted more than once\")\n err.stack = stack;\n throw err\n }\n ctxMountedCheck.add(ctxName)\n return () => { ctxMountedCheck.delete(ctxName) };\n })\n\n return <></>\n }\n\n RootState.displayName = `State[${useFn?.name??'??'}]`\n\n return {\n resolveCtxName,\n Root: RootState,\n /**\n * Strict consumer: throws if the corresponding Root for these props isn't mounted.\n * Use in development/tests to fail fast when wiring is incorrect.\n */\n useCtxStateStrict: (e: U): Context<V> => {\n const ctxName = resolveCtxName(e)\n\n const stack = useMemo(() => new Error().stack, [])\n\n useEffect(() => {\n if (!ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext [\" + ctxName + \"] is not mounted\")\n err.stack = stack;\n throw err\n }\n }, [ctxName])\n\n return useDataContext<V>(ctxName)\n },\n /**\n * Lenient consumer: schedules a console.error if the Root isn't mounted instead of throwing.\n * Useful in production to avoid hard crashes while still surfacing misconfiguration.\n */\n useCtxState: (e: U): Context<V> => {\n const ctxName = resolveCtxName(e)\n\n const stack = useMemo(() => new Error().stack, [])\n\n useEffect(() => {\n if (!ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext [\" + ctxName + \"] is not mounted\")\n err.stack = stack;\n let timeout = setTimeout(() => console.error(err), 1000)\n return () => clearTimeout(timeout)\n }\n }, [ctxMountedCheck.has(ctxName)])\n\n return useDataContext<V>(ctxName)\n }\n }\n}","import { useEffect, useState, Fragment, useCallback } from \"react\"\nimport { useDataContext, useDataSourceMultiple, useDataSubscribe, type Context } from \"./ctx\"\nimport { createRootCtx } from \"./createRootCtx\"\n\n\n\n\n\n\nconst weakmapName = (function () {\n const weakmap = new WeakMap()\n\n return (e: any): string => {\n let result = weakmap.get(e);\n if (!result) {\n weakmap.set(e, result = (e?.name ?? \"\") + Math.random().toString())\n }\n return result\n }\n})()\n\n\nconst resolveName = (e: any) => [\n ...Object\n .entries(e ?? {})\n .sort((e, f) => e[0].localeCompare(f[0]))\n .flat()\n].join(\"-\")\n\n/**\n * Inline docs: createAutoCtx + AutoRootCtx\n *\n * Quick start\n * 1) Mount <AutoRootCtx /> ONCE near your app root. Provide a Wrapper that acts like an ErrorBoundary to isolate and log errors.\n * Example: <AutoRootCtx Wrapper={MyErrorBoundary} />\n *\n * 2) Create auto contexts from your root context factories:\n * ```\n * const { useCtxState: useTestCtxState } = createAutoCtx(createRootCtx('test-state', stateFn))\n * const { useCtxState: useOtherCtxState } = createAutoCtx(createRootCtx('other-state', otherFn))\n * ```\n * 3) Use them in components:\n * ```\n * const ctx = useTestCtxState({ userId })\n * const { property1, property2 } = useDataSubscribeMultiple(ctx,'property1','property2')\n * // No need to mount the Root returned by createRootCtx directly — AutoRootCtx manages it for you.\n * ```\n * Notes\n * - AutoRootCtx must be mounted before any useCtxState hooks created by createAutoCtx run.\n * - Wrapper should be an ErrorBoundary-like component that simply renders {children}; no extra providers or layout required.\n * - For each unique params object (by stable stringified key), AutoRootCtx ensures a corresponding Root instance is rendered.\n */\n\nexport const AutoRootCtx = ({ Wrapper = Fragment }) => {\n\n const ctx = useDataContext<any>(\"auto-ctx\")\n\n\n const [state, setState] = useState<Record<string, { Component: React.FC, subState: Record<string, { params: any, counter: number }> }>>({})\n\n\n const subscribeRoot = useCallback(\n (Comp: any, params: any) => {\n const weakName = weakmapName(Comp);\n const key = resolveName(params);\n\n setState(({\n [weakName]: {\n Component = Comp,\n subState: {\n [key]: preState = { params, counter: 0 },\n ...subState\n } = {}\n } = {},\n ...state\n }) => ({\n ...state,\n [weakName]: {\n Component,\n subState: {\n ...subState,\n [key]: {\n ...preState,\n counter: preState.counter + 1,\n },\n },\n }\n }));\n\n return () => setState(({\n [weakName]: {\n Component = Comp,\n subState: {\n [key]: preState = { params, counter: 0 },\n ...subState\n } = {}\n } = {},\n ...state\n }) => ({\n ...state,\n [weakName]: {\n Component,\n subState: {\n ...subState,\n ...preState.counter > 1 ? {\n [key]: {\n ...preState,\n counter: preState.counter - 1,\n },\n } : {},\n },\n }\n }))\n\n },\n []\n )\n\n useDataSourceMultiple(ctx,\n [\"subscribe\", subscribeRoot],\n [\"state\", state],\n )\n\n\n return <>\n {Object.entries(state)\n .flatMap(([k1, { Component, subState }]) => Object\n .entries(subState)\n .map(([k2, { counter, params }]) => ({ key: k1 + k2, Component, params, counter }))\n .filter(e => e.counter > 0)\n .map(({ key, params, Component }) => <Wrapper key={key} >\n <Component {...params} />\n </Wrapper>)\n )\n }\n </>\n\n}\n\n/**\n * createAutoCtx\n *\n * Bridges a Root context (from createRootCtx) to the global AutoRootCtx renderer.\n * You do NOT mount the Root component yourself — just mount <AutoRootCtx /> once at the app root.\n *\n * Usage: \n * ```\n * const { useCtxState: useTestCtxState } = createAutoCtx(createRootCtx(\n * 'test-state', \n * stateFn\n * ))\n * const { useCtxState: useOtherCtxState } = createAutoCtx(createRootCtx(\n * 'other-state', \n * otherFn\n * ))\n * ```\n * \n * Then inside components:\n * ```\n * const ctxState = useTestCtxState({ any: 'params' })\n * ```\n * AutoRootCtx will subscribe/unsubscribe instances per unique params and render the appropriate Root under the hood.\n */\nexport const createAutoCtx = <U extends object, V extends object,>({ Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V>>) => {\n\n return {\n\n useCtxState: (e: U): Context<V> => {\n\n const ctxName = resolveCtxName(e)\n\n const subscribe = useDataSubscribe(useDataContext<any>(\"auto-ctx\"), \"subscribe\")\n\n useEffect(() => {\n // Subscribe this component to an AutoRootCtx-managed Root instance keyed by e.\n // AutoRootCtx handles instance ref-counting and cleanup on unmount.\n return subscribe?.(Root, e)\n }, [subscribe, ctxName])\n\n return useDataContext<V>(ctxName)\n }\n }\n}","\nimport { debounce } from \"./utils\";\nimport { useState, useMemo, useEffect } from \"react\";\nimport type { Context } from \"./ctx\";\n\n/**\n * useQuickSubscribe is a custom React hook for efficiently subscribing to specific properties of a context's data object.\n * \n * @template D - The shape of the context data.\n * @param {Context<D> | undefined} ctx - The context object containing data and a subscribe method.\n * @returns {Partial<D>} A proxy object that mirrors the context data, automatically subscribing to properties as they are accessed.\n *\n * This hook tracks which properties of the context data are accessed by the component and subscribes to updates for only those properties.\n * When any of the subscribed properties change, the hook triggers a re-render. Subscriptions are managed and cleaned up automatically\n * when the component unmounts or the context changes. This approach minimizes unnecessary re-renders and resource usage by only\n * subscribing to the data that the component actually uses.\n *\n * Example usage:\n * const {name} = useQuickSubscribe(userContext);\n * // Accessing name will subscribe to changes in 'name' only\n * return <div>{name}</div>;\n */\n\nexport const useQuickSubscribe = <D>(\n ctx: Context<D> | undefined\n): {\n [P in keyof D]?: D[P] | undefined;\n } => {\n\n const [, setCounter] = useState(0);\n\n const { proxy, finalGetter, openGetter, clean } = useMemo(\n () => {\n\n const allKeys = new Set<keyof D>()\n const allCompareValue: { [P in keyof D]?: D[P] | undefined; } = {}\n const allUnsub = new Map()\n\n const proxy = new Proxy(\n ctx?.data as any,\n {\n get(target, p) {\n if (isOpenGetter) {\n allKeys.add(p as keyof D)\n return allCompareValue[p as keyof D] = target[p];\n } else {\n throw new Error(\"now allow here\")\n }\n }\n }\n ) as any\n\n let isOpenGetter = true;\n\n\n let onChange = debounce(() => {\n if ([...allKeys.values()]\n .some(k => allCompareValue[k] != ctx?.data?.[k])) {\n setCounter(c => c + 1)\n }\n }, 0)\n\n let openGetter = () => {\n isOpenGetter = true\n allKeys.clear()\n }\n\n let finalGetter = () => {\n isOpenGetter = false;\n\n [...allKeys.values()]\n .filter(k => !allUnsub.has(k))\n .forEach(k => {\n allUnsub.set(k, ctx?.subscribe(k, onChange))\n });\n\n [...allUnsub.keys()]\n .filter(k => !allKeys.has(k))\n .forEach(k => {\n let unsub = allUnsub.get(k)\n unsub?.();\n allUnsub.delete(k);\n });\n\n }\n\n let clean = () => {\n openGetter();\n finalGetter();\n setCounter(c => c + 1)\n }\n\n return { proxy, finalGetter, openGetter, clean }\n },\n [ctx]\n )\n\n openGetter();\n\n setTimeout(finalGetter, 0)\n\n useEffect(\n () => () => clean(),\n [clean]\n )\n\n return proxy;\n\n\n};\n"],"names":["debounce","func","wait","timeout","fn","args","memoize","cache","key","result","randomHash","useArrayHash","computedHash","useRef","currentValues","currentHash","e","isDiff","f","i","DataEvent","event","value","Context","name","_listener","listener","getContext","useDataContext","useMemo","useRegistryChecker","ctx","names","stack","useEffect","useDataSource","useDataSubscribe","debounceTime","setState","useState","callback","unsub","useDataSubscribeWithTransform","transform","preValue","newValue","useDataSourceMultiple","entries","useDataSubscribeMultiple","keys","setCounter","returnValues","prevValues","c","handles","firstCall","index","useDataSubscribeMultipleWithDebounce","createRootCtx","useFn","resolveCtxName","ctxMountedCheck","RootState","state","ctxName","err","jsx","Fragment","weakmapName","weakmap","resolveName","AutoRootCtx","Wrapper","subscribeRoot","useCallback","Comp","params","weakName","Component","preState","subState","k1","k2","counter","createAutoCtx","Root","useCtxState","useCtxStateStrict","subscribe","useQuickSubscribe","proxy","finalGetter","openGetter","clean","allKeys","allCompareValue","allUnsub","target","p","isOpenGetter","onChange","k"],"mappings":";;AACO,SAASA,EACdC,GACAC,GACsD;AACtD,MAAIC,IAAgD,MAEhDC,IAAiC,YAAaC,GAA2B;AAC3E,IAAIF,KACF,aAAaA,CAAO,GAEtBA,IAAU,WAAW,MAAM;AACzB,MAAAF,EAAK,GAAGI,CAAI;AAAA,IACd,GAAGH,CAAI;AAAA,EACT;AAEA,SAAAE,EAAG,SAAS,MAAM,aAAaD,CAAQ,GAEhCC;AACT;AAGO,SAASE,EACdL,GAC2C;AAC3C,QAAMM,wBAAY,IAAA;AAElB,SAAO,YAAaF,GAAoC;AACtD,UAAMG,IAAM,KAAK,UAAUH,CAAI;AAC/B,QAAIE,EAAM,IAAIC,CAAG;AACf,aAAOD,EAAM,IAAIC,CAAG;AAEtB,UAAMC,IAASR,EAAK,GAAGI,CAAI;AAC3B,WAAAE,EAAM,IAAIC,GAAKC,CAAM,GACdA;AAAA,EACT;AACF;ACjCA,MAAMC,IAAa,MAAM,KAAK,OAAA,EAAS,SAAA,EAAW,MAAM,CAAC,GAgB5CC,IAAe,CAAC,MAAqB;AAEhD,QAAM,EAAE,SAAS,EAAE,cAAAC,EAAA,EAAa,IAAMC,EAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQ3C,IAAI,eAAe;AACjB,UAAIC,IAAuB,CAAA,GACvBC,IAAcL,EAAA;AAClB,aAAO,CAACM,MAAa;AACnB,YAAIC,IAAS;AAGb,eAAAA,IAASA,KAAY,CAACD,KAAO,CAACF,GAC9BG,IAASA,KAAWD,GAAG,UAAUF,GAAe,QAChDG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C,CAAC,GAEH,CAAC,GAAGJ,EAAS,KAAA,CAAM,EAChB,OAAO,CAAAK,MAAK,CAACP,EAAQ,IAAIO,CAAC,CAAC,EAC3B,QAAQ,CAAAA,MAAK;AAEZ,UADYL,EAAS,IAAIK,CAAC,IAC1B,GACAL,EAAS,OAAOK,CAAC;AAAA,QACnB,CAAC;AAAA,MAEL;AAQA,aAAO,EAAE,OAAAX,GAAO,aAAAC,GAAa,YAAAC,GAAY,OAN7B,MAAM;AAChBA,QAAAA,EAAAA,GACAD,EAAAA,GACAzC,EAAW,CAAAG,MAAKA,IAAI,CAAC;AAAA,MACvB,EAEyCwC;AAAAA,IAC3C;AAAA,IACA,CAAC9D,CAAG;AAAA,EAAA;AAGN,SAAA6D,EAAA,GAEA,WAAWD,GAAa,CAAC,GAEzBzD;AAAA,IACE,MAAM,MAAM2D,EAAA;AAAA,IACZ,CAACA,CAAK;AAAA,EAAA,GAGDH;AAGT;"}
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/state-utils/utils.ts","../src/state-utils/useArrayHash.ts","../src/state-utils/ctx.ts","../src/state-utils/createRootCtx.tsx","../src/state-utils/createAutoCtx.tsx","../src/state-utils/useQuickSubscribe.ts"],"sourcesContent":["// Debounce function\nexport function debounce<T extends (...args: any[]) => any>(\n func: T,\n wait: number\n): ((...args: Parameters<T>) => void) & { cancel: any } {\n let timeout: ReturnType<typeof setTimeout> | null = null;\n\n let fn: Function & { cancel: any } = function (...args: Parameters<T>): void {\n if (timeout) {\n clearTimeout(timeout);\n }\n timeout = setTimeout(() => {\n func(...args);\n }, wait);\n } as any; \n\n fn.cancel = () => clearTimeout(timeout!);\n\n return fn as any;\n}\n\n// Memoize function\nexport function memoize<T extends (...args: any[]) => any>(\n func: T\n): (...args: Parameters<T>) => ReturnType<T> {\n const cache = new Map<string, ReturnType<T>>();\n\n return function (...args: Parameters<T>): ReturnType<T> {\n const key = JSON.stringify(args);\n if (cache.has(key)) {\n return cache.get(key) as ReturnType<T>;\n }\n const result = func(...args);\n cache.set(key, result);\n return result;\n };\n}\n\n","import { useRef } from \"react\"\n\n\nconst randomHash = () => Math.random().toString().slice(2)\n\n/**\n * useArrayHash\n *\n * A custom hook that computes a stable hash for an array of values.\n * The hash changes only when the array's contents differ from the previous call.\n *\n * @param e - The input array to hash.\n * @returns A string hash that updates when the array changes.\n *\n * How it works:\n * - Tracks the previous array and its hash using a `useRef`.\n * - Compares the new array to the previous one by length and element equality.\n * - If any difference is detected, generates a new random hash.\n */\nexport const useArrayHash = (e: any[]): string => {\n\n const { current: { computedHash } } = useRef({\n /**\n * Getter for the computed hash function.\n *\n * - Initializes with an empty array and a random hash.\n * - Returns a function that compares the current array to the previous one.\n * - Updates the hash if any difference is detected.\n */\n get computedHash() {\n let currentValues: any[] = []\n let currentHash = randomHash()\n return (e: any[]) => {\n let isDiff = false\n\n // Check for differences in array existence, length, or elements.\n isDiff = isDiff || ((!e) != (!currentValues))\n isDiff = isDiff || (e?.length != currentValues?.length);\n isDiff = isDiff || (e.some((f, i) => f != currentValues[i]));\n\n // Update the hash if differences are found.\n currentValues = e;\n if (isDiff) {\n currentHash = randomHash()\n }\n\n return currentHash\n }\n }\n })\n\n return computedHash(e)\n}","import { debounce, memoize } from \"./utils\";\nimport { useEffect, useMemo, useState } from \"react\"\nimport { useArrayHash } from \"./useArrayHash\"\n\n\n\nclass DataEvent extends Event {\n constructor(\n public event: string,\n public value: any\n ) {\n super(event);\n }\n}\n\n/**\n * Generic context for managing shared state and event subscriptions.\n * @template D - The shape of the data managed by the context.\n */\nexport class Context<D> extends EventTarget {\n /**\n * Create a new Context instance.\n * @param name - The name of the context (for debugging).\n */\n constructor(public name: string) {\n console.log(\"[CONTEXT] %s\", name)\n // this.event.setMaxListeners(100)\n super();\n }\n\n // private event = new EventEmitter()\n\n /**\n * The current data held by the context.\n */\n public data: Partial<D> = {}\n /**\n * Registry for tracking active keys (for duplicate detection).\n */\n public registry = new Set<string>()\n\n /**\n * Publish a value to the context and notify subscribers if it changed.\n * @param key - The key to update.\n * @param value - The new value.\n */\n public publish(key: keyof D, value: D[typeof key] | undefined) {\n\n if (value != this.data[key]) {\n this.data[key] = value\n // console.count(\"[COUNT] \" + String(key))\n // this.event.emit(String(key), { value })\n this.dispatchEvent(new DataEvent(String(key), value))\n }\n }\n\n /**\n * Subscribe to changes for a specific key in the context.\n * @param key - The key to subscribe to.\n * @param _listener - Callback invoked with the new value.\n * @returns Unsubscribe function.\n */\n public subscribe(key: keyof D, _listener: (e: D[typeof key] | undefined) => void) {\n\n const listener = ({ event, value }: any) => {\n _listener(value)\n }\n\n this.addEventListener(String(key), listener)\n // console.log(\"listenerCount:\", String(key), this.event.listenerCount(String(key)))\n\n if (key in this.data) _listener(this.data[key])\n\n return () => (this.removeEventListener(String(key), listener), undefined)\n }\n\n}\n\n/**\n * Get or create a memoized Context instance by name.\n * @param name - The context name.\n * @returns The Context instance.\n */\nexport const getContext = memoize((name: string) => new Context<any>(name))\n\n/**\n * Type alias for a function that returns a Context instance.\n */\nexport type getContext<D> = (e: string) => Context<D>\n\n/**\n * React hook to get a typed Context instance by name.\n * @param name - The context name.\n * @returns The Context instance.\n */\nexport const useDataContext = <D>(name: string = \"noname\") => {\n\n const ctx = useMemo(() => getContext(name), [name])\n\n return ctx as any as Context<D>\n}\n\n/**\n * Internal hook to check for duplicate registry entries in a context.\n * Warns if any of the provided names are already registered.\n * @param ctx - The context instance.\n * @param names - Names to check and register.\n */\nconst useRegistryChecker = (ctx: Context<any> | undefined, ...names: string[]) => {\n // return;\n const stack = new Error(\"[ctx] useRegistryChecker failed \" + JSON.stringify({ names, ctx: ctx?.name ?? 'undefined' }))\n\n useEffect(\n () => {\n if (ctx) {\n if (names.some(name => ctx.registry.has(name))) {\n console.error(stack)\n }\n names.forEach(e => ctx.registry.add(e))\n\n // console.debug(\"[ctx] %s%s add datasource\", componentId, ctx.name, names)\n return () => {\n // console.debug(\"[ctx] %s %s remove datasource\", componentId, ctx.name, names)\n\n names.forEach(e => ctx.registry.delete(e))\n }\n }\n },\n [ctx, names.length]\n )\n\n}\n\n/**\n * React hook to publish a value to the context when it changes.\n * @param ctx - The context instance.\n * @param key - The key to update.\n * @param value - The new value.\n */\nexport const useDataSource = <D, K extends keyof D>(ctx: Context<D> | undefined, key: K, value: D[K] | undefined) => {\n //@ts-check\n useEffect(() => {\n if (ctx && ctx.data[key] != value) {\n\n ctx.publish(key, value)\n }\n }, [key, value, ctx])\n\n useRegistryChecker(ctx, key as any)\n}\n\n/**\n * React hook to subscribe to a context value, with optional debounce.\n * @param ctx - The context instance.\n * @param key - The key to subscribe to.\n * @param debounceTime - Debounce time in ms (default 0).\n * @returns The current value for the key.\n */\nexport const useDataSubscribe = <D, K extends keyof D>(ctx: Context<D> | undefined, key: K, debounceTime = 0): D[K] | undefined => {\n //@ts-check\n const [{ value }, setState] = useState(() => ({ value: ctx?.data?.[key] }))\n\n useEffect(() => {\n if (ctx) {\n let callback = debounceTime == 0\n ? (value: any) => setState({ value } as any)\n : debounce((value: any) => setState({ value } as any), debounceTime)\n let unsub = ctx.subscribe(key, callback)\n value != ctx.data[key] && setState({ value: ctx.data[key] })\n return () => {\n unsub()\n }\n }\n }, [key, ctx])\n\n return ctx?.data[key]\n}\n\n/**\n * React hook to subscribe to a context value and transform it before returning.\n * @param ctx - The context instance.\n * @param key - The key to subscribe to.\n * @param transform - Function to transform the value.\n * @returns The transformed value.\n */\nexport const useDataSubscribeWithTransform = <D, K extends keyof D, E>(ctx: Context<D> | undefined, key: K, transform: (e: D[K] | undefined) => E): E => {\n const [, setState] = useState(0)\n const result = useMemo(\n () => transform(ctx?.data[key]),\n [transform, ctx?.data[key]]\n )\n\n useEffect(() => {\n if (ctx) {\n let preValue = result\n let callback = () => {\n let newValue = transform(ctx.data[key])\n if (newValue != preValue) {\n preValue = newValue;\n setState(e => e + 1)\n };\n }\n let unsub = ctx.subscribe(key, callback)\n callback();\n return () => unsub()\n }\n }, [key, ctx])\n\n return result\n}\n\n/**\n * React hook to publish multiple values to the context.\n * @param ctx - The context instance.\n * @param entries - Array of [key, value] pairs to update.\n */\nexport const useDataSourceMultiple = <D, T extends readonly (keyof D)[]>(\n ctx: Context<D> | undefined,\n ...entries: { -readonly [P in keyof T]: [T[P], D[T[P]]] }\n) => {\n //@ts-check\n useEffect(() => {\n if (ctx) {\n for (let [key, value] of entries) {\n ctx.data[key] != value && ctx.publish(key, value)\n }\n }\n }, [ctx, useArrayHash(entries.flat())])\n\n useRegistryChecker(ctx, ...entries.map(e => e[0]) as any)\n\n}\n\n/**\n * React hook to subscribe to multiple context values.\n * @param ctx - The context instance.\n * @param keys - Keys to subscribe to.\n * @returns An object with the current values for the keys.\n */\nexport const useDataSubscribeMultiple = <D, K extends keyof D>(\n ctx: Context<D> | undefined,\n ...keys: K[]\n): Pick<D, K> => {\n const [, setCounter] = useState(0)\n\n const returnValues = keys.map(key => ctx?.data?.[key])\n\n useEffect(() => {\n if (ctx) {\n let prevValues = returnValues\n const callback = debounce(() => {\n let currentValues = keys.map(key => ctx?.data?.[key])\n if (keys.some((key, i) => prevValues[i] != currentValues[i])) {\n // console.log(\"DIFF\", keys.filter((e, i) => prevValues[i] != currentValues[i]))\n prevValues = currentValues\n setCounter(c => c + 1)\n }\n }, 1)\n\n let handles = keys.map(key => ctx.subscribe(key, callback))\n\n let firstCall = setTimeout(callback, 1);\n\n return () => {\n clearTimeout(firstCall)\n callback.cancel();\n handles.forEach(unsub => unsub())\n }\n\n }\n }, [ctx, ...keys])\n\n\n return Object\n .fromEntries(keys.map((key, index) => [key, returnValues[index]])) as any\n}\n\n/**\n * React hook to subscribe to multiple context values with throttling.\n * @param ctx - The context instance.\n * @param debounceTime - Debounce time in ms (default 50).\n * @param keys - Keys to subscribe to.\n * @returns Array of current values for the keys.\n */\nexport const useDataSubscribeMultipleWithDebounce = <D, K extends (keyof D)[]>(\n ctx: Context<D> | undefined,\n debounceTime = 50,\n ...keys: K\n): { [i in keyof K]: D[K[i]] | undefined } => {\n //@ts-check\n const [, setCounter] = useState(0)\n\n const returnValues = keys.map(key => ctx?.data?.[key])\n\n useEffect(() => {\n if (ctx) {\n let prevValues = returnValues\n const callback = debounce(() => {\n let currentValues = keys.map(key => ctx?.data?.[key])\n if (keys.some((key, i) => prevValues[i] != currentValues[i])) {\n prevValues = currentValues\n setCounter(c => c + 1)\n }\n }, debounceTime)\n\n let handles = keys.map(key => ctx.subscribe(key, callback))\n\n let firstCall = setTimeout(callback, 1);\n\n return () => {\n clearTimeout(firstCall)\n callback.cancel();\n handles.forEach(unsub => unsub())\n }\n\n }\n }, [ctx, ...keys])\n\n return returnValues as any\n}\n\n\n\n","import { useEffect, useMemo } from \"react\"\nimport { useDataContext, useDataSourceMultiple, type Context } from \"./ctx\"\n\n\n\n/**\n * createRootCtx\n *\n * Factory that creates a headless \"Root\" component and companion hooks for a context namespace.\n * It derives a unique context name from a base `name` and a props object `U`, then publishes\n * a computed state `V` (from `useFn`) to that context.\n *\n * Usage (manual mounting):\n * ```\n * const { Root, useCtxState } = createRootCtx('user-state', useUserState)\n * ...\n * // Mount exactly one Root per unique props combination\n * <Root userId={id} />\n * ...\n * // Read anywhere ,using the same props shape\n * const user = useCtxState({ userId: id })\n *```\n * Strict vs lenient consumers:\n * - useCtxStateStrict(props) throws if a matching Root is not mounted.\n * - useCtxState(props) logs an error (after 1s) instead of throwing.\n *\n * Multiple instances safety:\n * - Mounting more than one Root with the same resolved context name throws (guards accidental duplicates).\n *\n * Name resolution notes:\n * - The context name is built from `name` + sorted key/value pairs of `props` (U), joined by \"-\".\n * - Prefer stable, primitive props to avoid collisions; if you need automation, pair with `createAutoCtx` and\n * mount a single <AutoRootCtx Wrapper={ErrorBoundary} /> at the app root so you don't manually mount `Root`.\n */\nexport const createRootCtx = <U extends object, V extends object>(name: string, useFn: (e: U) => V) => {\n\n const resolveCtxName = (e: U) => [\n name,\n ...Object\n .entries(e ?? {})\n .sort((e, f) => e[0].localeCompare(f[0]))\n .flat()\n ].join(\"-\")\n\n let ctxMountedCheck = new Set<string>()\n\n\n const RootState: React.FC<U> = (e: U) => {\n const state = useFn(e)\n const ctxName = resolveCtxName(e)\n const ctx = useDataContext<V>(ctxName)\n const stack = useMemo(() => new Error().stack, [])\n\n useDataSourceMultiple(\n ctx,\n ...Object.entries(state) as any\n )\n\n useEffect(() => {\n if (ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext \" + ctxName + \" are mounted more than once\")\n err.stack = stack;\n throw err\n }\n ctxMountedCheck.add(ctxName)\n return () => { ctxMountedCheck.delete(ctxName) };\n })\n\n return <></>\n }\n\n RootState.displayName = `State[${useFn?.name??'??'}]`\n\n return {\n resolveCtxName,\n Root: RootState,\n /**\n * Strict consumer: throws if the corresponding Root for these props isn't mounted.\n * Use in development/tests to fail fast when wiring is incorrect.\n */\n useCtxStateStrict: (e: U): Context<V> => {\n const ctxName = resolveCtxName(e)\n\n const stack = useMemo(() => new Error().stack, [])\n\n useEffect(() => {\n if (!ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext [\" + ctxName + \"] is not mounted\")\n err.stack = stack;\n throw err\n }\n }, [ctxName])\n\n return useDataContext<V>(ctxName)\n },\n /**\n * Lenient consumer: schedules a console.error if the Root isn't mounted instead of throwing.\n * Useful in production to avoid hard crashes while still surfacing misconfiguration.\n */\n useCtxState: (e: U): Context<V> => {\n const ctxName = resolveCtxName(e)\n\n const stack = useMemo(() => new Error().stack, [])\n\n useEffect(() => {\n if (!ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext [\" + ctxName + \"] is not mounted\")\n err.stack = stack;\n let timeout = setTimeout(() => console.error(err), 1000)\n return () => clearTimeout(timeout)\n }\n }, [ctxMountedCheck.has(ctxName)])\n\n return useDataContext<V>(ctxName)\n }\n }\n}","import { useEffect, useState, Fragment, useCallback } from \"react\"\nimport { useDataContext, useDataSourceMultiple, useDataSubscribe, type Context } from \"./ctx\"\nimport { createRootCtx } from \"./createRootCtx\"\n\n\n\n\n\n\nconst weakmapName = (function () {\n const weakmap = new WeakMap()\n\n return (e: any): string => {\n let result = weakmap.get(e);\n if (!result) {\n weakmap.set(e, result = (e?.name ?? \"\") + Math.random().toString())\n }\n return result\n }\n})()\n\n\nconst resolveName = (e: any) => [\n ...Object\n .entries(e ?? {})\n .sort((e, f) => e[0].localeCompare(f[0]))\n .flat()\n].join(\"-\")\n\n/**\n * Inline docs: createAutoCtx + AutoRootCtx\n *\n * Quick start\n * 1) Mount <AutoRootCtx /> ONCE near your app root. Provide a Wrapper that acts like an ErrorBoundary to isolate and log errors.\n * Example: <AutoRootCtx Wrapper={MyErrorBoundary} />\n *\n * 2) Create auto contexts from your root context factories:\n * ```\n * const { useCtxState: useTestCtxState } = createAutoCtx(createRootCtx('test-state', stateFn))\n * const { useCtxState: useOtherCtxState } = createAutoCtx(createRootCtx('other-state', otherFn))\n * ```\n * 3) Use them in components:\n * ```\n * const ctx = useTestCtxState({ userId })\n * const { property1, property2 } = useDataSubscribeMultiple(ctx,'property1','property2')\n * // No need to mount the Root returned by createRootCtx directly — AutoRootCtx manages it for you.\n * ```\n * Notes\n * - AutoRootCtx must be mounted before any useCtxState hooks created by createAutoCtx run.\n * - Wrapper should be an ErrorBoundary-like component that simply renders {children}; no extra providers or layout required.\n * - For each unique params object (by stable stringified key), AutoRootCtx ensures a corresponding Root instance is rendered.\n */\n\nexport const AutoRootCtx = ({ Wrapper = Fragment }) => {\n\n const ctx = useDataContext<any>(\"auto-ctx\")\n\n\n const [state, setState] = useState<Record<string, { Component: React.FC, subState: Record<string, { params: any, counter: number }> }>>({})\n\n\n const subscribeRoot = useCallback(\n (Comp: any, params: any) => {\n const weakName = weakmapName(Comp);\n const key = resolveName(params);\n\n setState(({\n [weakName]: {\n Component = Comp,\n subState: {\n [key]: preState = { params, counter: 0 },\n ...subState\n } = {}\n } = {},\n ...state\n }) => ({\n ...state,\n [weakName]: {\n Component,\n subState: {\n ...subState,\n [key]: {\n ...preState,\n counter: preState.counter + 1,\n },\n },\n }\n }));\n\n return () => setState(({\n [weakName]: {\n Component = Comp,\n subState: {\n [key]: preState = { params, counter: 0 },\n ...subState\n } = {}\n } = {},\n ...state\n }) => ({\n ...state,\n [weakName]: {\n Component,\n subState: {\n ...subState,\n ...preState.counter > 1 ? {\n [key]: {\n ...preState,\n counter: preState.counter - 1,\n },\n } : {},\n },\n }\n }))\n\n },\n []\n )\n\n useDataSourceMultiple(ctx,\n [\"subscribe\", subscribeRoot],\n [\"state\", state],\n )\n\n\n return <>\n {Object.entries(state)\n .flatMap(([k1, { Component, subState }]) => Object\n .entries(subState)\n .map(([k2, { counter, params }]) => ({ key: k1 + k2, Component, params, counter }))\n .filter(e => e.counter > 0)\n .map(({ key, params, Component }) => <Wrapper key={key} >\n <Component {...params} />\n </Wrapper>)\n )\n }\n </>\n\n}\n\n/**\n * createAutoCtx\n *\n * Bridges a Root context (from createRootCtx) to the global AutoRootCtx renderer.\n * You do NOT mount the Root component yourself — just mount <AutoRootCtx /> once at the app root.\n *\n * Usage: \n * ```\n * const { useCtxState: useTestCtxState } = createAutoCtx(createRootCtx(\n * 'test-state', \n * stateFn\n * ))\n * const { useCtxState: useOtherCtxState } = createAutoCtx(createRootCtx(\n * 'other-state', \n * otherFn\n * ))\n * ```\n * \n * Then inside components:\n * ```\n * const ctxState = useTestCtxState({ any: 'params' })\n * ```\n * AutoRootCtx will subscribe/unsubscribe instances per unique params and render the appropriate Root under the hood.\n */\nexport const createAutoCtx = <U extends object, V extends object,>(\n { Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V>>,\n unmountTime = 0\n) => {\n\n return {\n\n useCtxState: (e: U): Context<V> => {\n\n const ctxName = resolveCtxName(e)\n\n const subscribe = useDataSubscribe(useDataContext<any>(\"auto-ctx\"), \"subscribe\")\n\n useEffect(() => {\n // Subscribe this component to an AutoRootCtx-managed Root instance keyed by e.\n // AutoRootCtx handles instance ref-counting and cleanup on unmount.\n if (unmountTime == 0) {\n return subscribe?.(Root, e)\n } else {\n let unsub = subscribe?.(Root, e)\n return () => setTimeout(unsub, unmountTime)\n }\n }, [subscribe, ctxName])\n\n return useDataContext<V>(ctxName)\n }\n }\n}","\nimport { debounce } from \"./utils\";\nimport { useState, useMemo, useEffect } from \"react\";\nimport type { Context } from \"./ctx\";\n\n/**\n * useQuickSubscribe is a custom React hook for efficiently subscribing to specific properties of a context's data object.\n * \n * @template D - The shape of the context data.\n * @param {Context<D> | undefined} ctx - The context object containing data and a subscribe method.\n * @returns {Partial<D>} A proxy object that mirrors the context data, automatically subscribing to properties as they are accessed.\n *\n * This hook tracks which properties of the context data are accessed by the component and subscribes to updates for only those properties.\n * When any of the subscribed properties change, the hook triggers a re-render. Subscriptions are managed and cleaned up automatically\n * when the component unmounts or the context changes. This approach minimizes unnecessary re-renders and resource usage by only\n * subscribing to the data that the component actually uses.\n *\n * Example usage:\n * const {name} = useQuickSubscribe(userContext);\n * // Accessing name will subscribe to changes in 'name' only\n * return <div>{name}</div>;\n */\n\nexport const useQuickSubscribe = <D>(\n ctx: Context<D> | undefined\n): {\n [P in keyof D]?: D[P] | undefined;\n } => {\n\n const [, setCounter] = useState(0);\n\n const { proxy, finalGetter, openGetter, clean } = useMemo(\n () => {\n\n const allKeys = new Set<keyof D>()\n const allCompareValue: { [P in keyof D]?: D[P] | undefined; } = {}\n const allUnsub = new Map()\n\n const proxy = new Proxy(\n ctx?.data as any,\n {\n get(target, p) {\n if (isOpenGetter) {\n allKeys.add(p as keyof D)\n return allCompareValue[p as keyof D] = target[p];\n } else {\n throw new Error(\"now allow here\")\n }\n }\n }\n ) as any\n\n let isOpenGetter = true;\n\n\n let onChange = debounce(() => {\n if ([...allKeys.values()]\n .some(k => allCompareValue[k] != ctx?.data?.[k])) {\n setCounter(c => c + 1)\n }\n }, 0)\n\n let openGetter = () => {\n isOpenGetter = true\n allKeys.clear()\n }\n\n let finalGetter = () => {\n isOpenGetter = false;\n\n [...allKeys.values()]\n .filter(k => !allUnsub.has(k))\n .forEach(k => {\n allUnsub.set(k, ctx?.subscribe(k, onChange))\n });\n\n [...allUnsub.keys()]\n .filter(k => !allKeys.has(k))\n .forEach(k => {\n let unsub = allUnsub.get(k)\n unsub?.();\n allUnsub.delete(k);\n });\n\n }\n\n let clean = () => {\n openGetter();\n finalGetter();\n setCounter(c => c + 1)\n }\n\n return { proxy, finalGetter, openGetter, clean }\n },\n [ctx]\n )\n\n openGetter();\n\n setTimeout(finalGetter, 0)\n\n useEffect(\n () => () => clean(),\n [clean]\n )\n\n return proxy;\n\n\n};\n"],"names":["debounce","func","wait","timeout","fn","args","memoize","cache","key","result","randomHash","useArrayHash","e","computedHash","useRef","currentValues","currentHash","isDiff","f","i","DataEvent","event","value","Context","name","_listener","listener","getContext","useDataContext","useMemo","useRegistryChecker","ctx","names","stack","useEffect","useDataSource","useDataSubscribe","debounceTime","setState","useState","callback","unsub","useDataSubscribeWithTransform","transform","preValue","newValue","useDataSourceMultiple","entries","useDataSubscribeMultiple","keys","setCounter","returnValues","prevValues","c","handles","firstCall","index","useDataSubscribeMultipleWithDebounce","createRootCtx","useFn","resolveCtxName","ctxMountedCheck","RootState","state","ctxName","err","jsx","Fragment","weakmapName","weakmap","resolveName","AutoRootCtx","Wrapper","subscribeRoot","useCallback","Comp","params","weakName","Component","preState","subState","k1","k2","counter","createAutoCtx","Root","useCtxState","useCtxStateStrict","unmountTime","subscribe","useQuickSubscribe","proxy","finalGetter","openGetter","clean","allKeys","allCompareValue","allUnsub","target","p","isOpenGetter","onChange","k"],"mappings":";;AACO,SAASA,EACdC,GACAC,GACsD;AACtD,MAAIC,IAAgD,MAEhDC,IAAiC,YAAaC,GAA2B;AAC3E,IAAIF,KACF,aAAaA,CAAO,GAEtBA,IAAU,WAAW,MAAM;AACzB,MAAAF,EAAK,GAAGI,CAAI;AAAA,IACd,GAAGH,CAAI;AAAA,EACT;AAEA,SAAAE,EAAG,SAAS,MAAM,aAAaD,CAAQ,GAEhCC;AACT;AAGO,SAASE,EACdL,GAC2C;AAC3C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,IAC3C;AAAA,IACA,CAAC/D,CAAG;AAAA,EAAA;AAGN,SAAA8D,EAAA,GAEA,WAAWD,GAAa,CAAC,GAEzB1D;AAAA,IACE,MAAM,MAAM4D,EAAA;AAAA,IACZ,CAACA,CAAK;AAAA,EAAA,GAGDH;AAGT;"}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(b,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("react"),require("react/jsx-runtime")):typeof define=="function"&&define.amd?define(["exports","react","react/jsx-runtime"],i):(b=typeof globalThis<"u"?globalThis:b||self,i(b.RState={},b.React,b.jsxRuntime))})(this,function(b,i,S){"use strict";function E(t
|
|
1
|
+
(function(b,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("react"),require("react/jsx-runtime")):typeof define=="function"&&define.amd?define(["exports","react","react/jsx-runtime"],i):(b=typeof globalThis<"u"?globalThis:b||self,i(b.RState={},b.React,b.jsxRuntime))})(this,function(b,i,S){"use strict";function E(e,t){let s=null,r=function(...o){s&&clearTimeout(s),s=setTimeout(()=>{e(...o)},t)};return r.cancel=()=>clearTimeout(s),r}function O(e){const t=new Map;return function(...s){const r=JSON.stringify(s);if(t.has(r))return t.get(r);const o=e(...s);return t.set(r,o),o}}const w=()=>Math.random().toString().slice(2),M=e=>{const{current:{computedHash:t}}=i.useRef({get computedHash(){let s=[],r=w();return o=>{let n=!1;return n=n||!o!=!s,n=n||o?.length!=s?.length,n=n||o.some((u,c)=>u!=s[c]),s=o,n&&(r=w()),r}}});return t(e)};class R extends Event{constructor(t,s){super(t),this.event=t,this.value=s}}class v extends EventTarget{constructor(t){console.log("[CONTEXT] %s",t),super(),this.name=t}data={};registry=new Set;publish(t,s){s!=this.data[t]&&(this.data[t]=s,this.dispatchEvent(new R(String(t),s)))}subscribe(t,s){const r=({event:o,value:n})=>{s(n)};return this.addEventListener(String(t),r),t in this.data&&s(this.data[t]),()=>(this.removeEventListener(String(t),r),void 0)}}const D=O(e=>new v(e)),m=(e="noname")=>i.useMemo(()=>D(e),[e]),T=(e,...t)=>{const s=new Error("[ctx] useRegistryChecker failed "+JSON.stringify({names:t,ctx:e?.name??"undefined"}));i.useEffect(()=>{if(e)return t.some(r=>e.registry.has(r))&&console.error(s),t.forEach(r=>e.registry.add(r)),()=>{t.forEach(r=>e.registry.delete(r))}},[e,t.length])},V=(e,t,s)=>{i.useEffect(()=>{e&&e.data[t]!=s&&e.publish(t,s)},[t,s,e]),T(e,t)},j=(e,t,s=0)=>{const[{value:r},o]=i.useState(()=>({value:e?.data?.[t]}));return i.useEffect(()=>{if(e){let n=s==0?c=>o({value:c}):E(c=>o({value:c}),s),u=e.subscribe(t,n);return r!=e.data[t]&&o({value:e.data[t]}),()=>{u()}}},[t,e]),e?.data[t]},G=(e,t,s)=>{const[,r]=i.useState(0),o=i.useMemo(()=>s(e?.data[t]),[s,e?.data[t]]);return i.useEffect(()=>{if(e){let n=o,u=()=>{let a=s(e.data[t]);a!=n&&(n=a,r(l=>l+1))},c=e.subscribe(t,u);return u(),()=>c()}},[t,e]),o},g=(e,...t)=>{i.useEffect(()=>{if(e)for(let[s,r]of t)e.data[s]!=r&&e.publish(s,r)},[e,M(t.flat())]),T(e,...t.map(s=>s[0]))},A=(e,...t)=>{const[,s]=i.useState(0),r=t.map(o=>e?.data?.[o]);return i.useEffect(()=>{if(e){let o=r;const n=E(()=>{let a=t.map(l=>e?.data?.[l]);t.some((l,f)=>o[f]!=a[f])&&(o=a,s(l=>l+1))},1);let u=t.map(a=>e.subscribe(a,n)),c=setTimeout(n,1);return()=>{clearTimeout(c),n.cancel(),u.forEach(a=>a())}}},[e,...t]),Object.fromEntries(t.map((o,n)=>[o,r[n]]))},H=(e,t=50,...s)=>{const[,r]=i.useState(0),o=s.map(n=>e?.data?.[n]);return i.useEffect(()=>{if(e){let n=o;const u=E(()=>{let l=s.map(f=>e?.data?.[f]);s.some((f,h)=>n[h]!=l[h])&&(n=l,r(f=>f+1))},t);let c=s.map(l=>e.subscribe(l,u)),a=setTimeout(u,1);return()=>{clearTimeout(a),u.cancel(),c.forEach(l=>l())}}},[e,...s]),o},W=(e,t)=>{const s=n=>[e,...Object.entries(n??{}).sort((u,c)=>u[0].localeCompare(c[0])).flat()].join("-");let r=new Set;const o=n=>{const u=t(n),c=s(n),a=m(c),l=i.useMemo(()=>new Error().stack,[]);return g(a,...Object.entries(u)),i.useEffect(()=>{if(r.has(c)){const f=new Error("RootContext "+c+" are mounted more than once");throw f.stack=l,f}return r.add(c),()=>{r.delete(c)}}),S.jsx(S.Fragment,{})};return o.displayName=`State[${t?.name??"??"}]`,{resolveCtxName:s,Root:o,useCtxStateStrict:n=>{const u=s(n),c=i.useMemo(()=>new Error().stack,[]);return i.useEffect(()=>{if(!r.has(u)){const a=new Error("RootContext ["+u+"] is not mounted");throw a.stack=c,a}},[u]),m(u)},useCtxState:n=>{const u=s(n),c=i.useMemo(()=>new Error().stack,[]);return i.useEffect(()=>{if(!r.has(u)){const a=new Error("RootContext ["+u+"] is not mounted");a.stack=c;let l=setTimeout(()=>console.error(a),1e3);return()=>clearTimeout(l)}},[r.has(u)]),m(u)}}},k=function(){const e=new WeakMap;return t=>{let s=e.get(t);return s||e.set(t,s=(t?.name??"")+Math.random().toString()),s}}(),q=e=>[...Object.entries(e??{}).sort((t,s)=>t[0].localeCompare(s[0])).flat()].join("-"),F=({Wrapper:e=i.Fragment})=>{const t=m("auto-ctx"),[s,r]=i.useState({}),o=i.useCallback((n,u)=>{const c=k(n),a=q(u);return r(({[c]:{Component:l=n,subState:{[a]:f={params:u,counter:0},...h}={}}={},...C})=>({...C,[c]:{Component:l,subState:{...h,[a]:{...f,counter:f.counter+1}}}})),()=>r(({[c]:{Component:l=n,subState:{[a]:f={params:u,counter:0},...h}={}}={},...C})=>({...C,[c]:{Component:l,subState:{...h,...f.counter>1?{[a]:{...f,counter:f.counter-1}}:{}}}}))},[]);return g(t,["subscribe",o],["state",s]),S.jsx(S.Fragment,{children:Object.entries(s).flatMap(([n,{Component:u,subState:c}])=>Object.entries(c).map(([a,{counter:l,params:f}])=>({key:n+a,Component:u,params:f,counter:l})).filter(a=>a.counter>0).map(({key:a,params:l,Component:f})=>S.jsx(e,{children:S.jsx(f,{...l})},a)))})},J=({Root:e,useCtxState:t,useCtxStateStrict:s,resolveCtxName:r},o=0)=>({useCtxState:n=>{const u=r(n),c=j(m("auto-ctx"),"subscribe");return i.useEffect(()=>{if(o==0)return c?.(e,n);{let a=c?.(e,n);return()=>setTimeout(a,o)}},[c,u]),m(u)}}),L=e=>{const[,t]=i.useState(0),{proxy:s,finalGetter:r,openGetter:o,clean:n}=i.useMemo(()=>{const u=new Set,c={},a=new Map,l=new Proxy(e?.data,{get(d,p){if(f)return u.add(p),c[p]=d[p];throw new Error("now allow here")}});let f=!0,h=E(()=>{[...u.values()].some(d=>c[d]!=e?.data?.[d])&&t(d=>d+1)},0),C=()=>{f=!0,u.clear()},N=()=>{f=!1,[...u.values()].filter(d=>!a.has(d)).forEach(d=>{a.set(d,e?.subscribe(d,h))}),[...a.keys()].filter(d=>!u.has(d)).forEach(d=>{a.get(d)?.(),a.delete(d)})};return{proxy:l,finalGetter:N,openGetter:C,clean:()=>{C(),N(),t(d=>d+1)}}},[e]);return o(),setTimeout(r,0),i.useEffect(()=>()=>n(),[n]),s};b.AutoRootCtx=F,b.Context=v,b.createAutoCtx=J,b.createRootCtx=W,b.getContext=D,b.useArrayHash=M,b.useDataContext=m,b.useDataSource=V,b.useDataSourceMultiple=g,b.useDataSubscribe=j,b.useDataSubscribeMultiple=A,b.useDataSubscribeMultipleWithDebounce=H,b.useDataSubscribeWithTransform=G,b.useQuickSubscribe=L,Object.defineProperty(b,Symbol.toStringTag,{value:"Module"})});
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/state-utils/utils.ts","../src/state-utils/useArrayHash.ts","../src/state-utils/ctx.ts","../src/state-utils/createRootCtx.tsx","../src/state-utils/createAutoCtx.tsx","../src/state-utils/useQuickSubscribe.ts"],"sourcesContent":["// Debounce function\nexport function debounce<T extends (...args: any[]) => any>(\n func: T,\n wait: number\n): ((...args: Parameters<T>) => void) & { cancel: any } {\n let timeout: ReturnType<typeof setTimeout> | null = null;\n\n let fn: Function & { cancel: any } = function (...args: Parameters<T>): void {\n if (timeout) {\n clearTimeout(timeout);\n }\n timeout = setTimeout(() => {\n func(...args);\n }, wait);\n } as any; \n\n fn.cancel = () => clearTimeout(timeout!);\n\n return fn as any;\n}\n\n// Memoize function\nexport function memoize<T extends (...args: any[]) => any>(\n func: T\n): (...args: Parameters<T>) => ReturnType<T> {\n const cache = new Map<string, ReturnType<T>>();\n\n return function (...args: Parameters<T>): ReturnType<T> {\n const key = JSON.stringify(args);\n if (cache.has(key)) {\n return cache.get(key) as ReturnType<T>;\n }\n const result = func(...args);\n cache.set(key, result);\n return result;\n };\n}\n\n","import { useRef } from \"react\"\n\n\nconst randomHash = () => Math.random().toString().slice(2)\n\n/**\n * useArrayHash\n *\n * A custom hook that computes a stable hash for an array of values.\n * The hash changes only when the array's contents differ from the previous call.\n *\n * @param e - The input array to hash.\n * @returns A string hash that updates when the array changes.\n *\n * How it works:\n * - Tracks the previous array and its hash using a `useRef`.\n * - Compares the new array to the previous one by length and element equality.\n * - If any difference is detected, generates a new random hash.\n */\nexport const useArrayHash = (e: any[]): string => {\n\n const { current: { computedHash } } = useRef({\n /**\n * Getter for the computed hash function.\n *\n * - Initializes with an empty array and a random hash.\n * - Returns a function that compares the current array to the previous one.\n * - Updates the hash if any difference is detected.\n */\n get computedHash() {\n let currentValues: any[] = []\n let currentHash = randomHash()\n return (e: any[]) => {\n let isDiff = false\n\n // Check for differences in array existence, length, or elements.\n isDiff = isDiff || ((!e) != (!currentValues))\n isDiff = isDiff || (e?.length != currentValues?.length);\n isDiff = isDiff || (e.some((f, i) => f != currentValues[i]));\n\n // Update the hash if differences are found.\n currentValues = e;\n if (isDiff) {\n currentHash = randomHash()\n }\n\n return currentHash\n }\n }\n })\n\n return computedHash(e)\n}","import { debounce, memoize } from \"./utils\";\nimport { useEffect, useMemo, useState } from \"react\"\nimport { useArrayHash } from \"./useArrayHash\"\n\n\n\nclass DataEvent extends Event {\n constructor(\n public event: string,\n public value: any\n ) {\n super(event);\n }\n}\n\n/**\n * Generic context for managing shared state and event subscriptions.\n * @template D - The shape of the data managed by the context.\n */\nexport class Context<D> extends EventTarget {\n /**\n * Create a new Context instance.\n * @param name - The name of the context (for debugging).\n */\n constructor(public name: string) {\n console.log(\"[CONTEXT] %s\", name)\n // this.event.setMaxListeners(100)\n super();\n }\n\n // private event = new EventEmitter()\n\n /**\n * The current data held by the context.\n */\n public data: Partial<D> = {}\n /**\n * Registry for tracking active keys (for duplicate detection).\n */\n public registry = new Set<string>()\n\n /**\n * Publish a value to the context and notify subscribers if it changed.\n * @param key - The key to update.\n * @param value - The new value.\n */\n public publish(key: keyof D, value: D[typeof key] | undefined) {\n\n if (value != this.data[key]) {\n this.data[key] = value\n // console.count(\"[COUNT] \" + String(key))\n // this.event.emit(String(key), { value })\n this.dispatchEvent(new DataEvent(String(key), value))\n }\n }\n\n /**\n * Subscribe to changes for a specific key in the context.\n * @param key - The key to subscribe to.\n * @param _listener - Callback invoked with the new value.\n * @returns Unsubscribe function.\n */\n public subscribe(key: keyof D, _listener: (e: D[typeof key] | undefined) => void) {\n\n const listener = ({ event, value }: any) => {\n _listener(value)\n }\n\n this.addEventListener(String(key), listener)\n // console.log(\"listenerCount:\", String(key), this.event.listenerCount(String(key)))\n\n if (key in this.data) _listener(this.data[key])\n\n return () => (this.removeEventListener(String(key), listener), undefined)\n }\n\n}\n\n/**\n * Get or create a memoized Context instance by name.\n * @param name - The context name.\n * @returns The Context instance.\n */\nexport const getContext = memoize((name: string) => new Context<any>(name))\n\n/**\n * Type alias for a function that returns a Context instance.\n */\nexport type getContext<D> = (e: string) => Context<D>\n\n/**\n * React hook to get a typed Context instance by name.\n * @param name - The context name.\n * @returns The Context instance.\n */\nexport const useDataContext = <D>(name: string = \"noname\") => {\n\n const ctx = useMemo(() => getContext(name), [name])\n\n return ctx as any as Context<D>\n}\n\n/**\n * Internal hook to check for duplicate registry entries in a context.\n * Warns if any of the provided names are already registered.\n * @param ctx - The context instance.\n * @param names - Names to check and register.\n */\nconst useRegistryChecker = (ctx: Context<any> | undefined, ...names: string[]) => {\n // return;\n const stack = new Error(\"[ctx] useRegistryChecker failed \" + JSON.stringify({ names, ctx: ctx?.name ?? 'undefined' }))\n\n useEffect(\n () => {\n if (ctx) {\n if (names.some(name => ctx.registry.has(name))) {\n console.error(stack)\n }\n names.forEach(e => ctx.registry.add(e))\n\n // console.debug(\"[ctx] %s%s add datasource\", componentId, ctx.name, names)\n return () => {\n // console.debug(\"[ctx] %s %s remove datasource\", componentId, ctx.name, names)\n\n names.forEach(e => ctx.registry.delete(e))\n }\n }\n },\n [ctx, names.length]\n )\n\n}\n\n/**\n * React hook to publish a value to the context when it changes.\n * @param ctx - The context instance.\n * @param key - The key to update.\n * @param value - The new value.\n */\nexport const useDataSource = <D, K extends keyof D>(ctx: Context<D> | undefined, key: K, value: D[K] | undefined) => {\n //@ts-check\n useEffect(() => {\n if (ctx && ctx.data[key] != value) {\n\n ctx.publish(key, value)\n }\n }, [key, value, ctx])\n\n useRegistryChecker(ctx, key as any)\n}\n\n/**\n * React hook to subscribe to a context value, with optional debounce.\n * @param ctx - The context instance.\n * @param key - The key to subscribe to.\n * @param debounceTime - Debounce time in ms (default 0).\n * @returns The current value for the key.\n */\nexport const useDataSubscribe = <D, K extends keyof D>(ctx: Context<D> | undefined, key: K, debounceTime = 0): D[K] | undefined => {\n //@ts-check\n const [{ value }, setState] = useState(() => ({ value: ctx?.data?.[key] }))\n\n useEffect(() => {\n if (ctx) {\n let callback = debounceTime == 0\n ? (value: any) => setState({ value } as any)\n : debounce((value: any) => setState({ value } as any), debounceTime)\n let unsub = ctx.subscribe(key, callback)\n value != ctx.data[key] && setState({ value: ctx.data[key] })\n return () => {\n unsub()\n }\n }\n }, [key, ctx])\n\n return ctx?.data[key]\n}\n\n/**\n * React hook to subscribe to a context value and transform it before returning.\n * @param ctx - The context instance.\n * @param key - The key to subscribe to.\n * @param transform - Function to transform the value.\n * @returns The transformed value.\n */\nexport const useDataSubscribeWithTransform = <D, K extends keyof D, E>(ctx: Context<D> | undefined, key: K, transform: (e: D[K] | undefined) => E): E => {\n const [, setState] = useState(0)\n const result = useMemo(\n () => transform(ctx?.data[key]),\n [transform, ctx?.data[key]]\n )\n\n useEffect(() => {\n if (ctx) {\n let preValue = result\n let callback = () => {\n let newValue = transform(ctx.data[key])\n if (newValue != preValue) {\n preValue = newValue;\n setState(e => e + 1)\n };\n }\n let unsub = ctx.subscribe(key, callback)\n callback();\n return () => unsub()\n }\n }, [key, ctx])\n\n return result\n}\n\n/**\n * React hook to publish multiple values to the context.\n * @param ctx - The context instance.\n * @param entries - Array of [key, value] pairs to update.\n */\nexport const useDataSourceMultiple = <D, T extends readonly (keyof D)[]>(\n ctx: Context<D> | undefined,\n ...entries: { -readonly [P in keyof T]: [T[P], D[T[P]]] }\n) => {\n //@ts-check\n useEffect(() => {\n if (ctx) {\n for (let [key, value] of entries) {\n ctx.data[key] != value && ctx.publish(key, value)\n }\n }\n }, [ctx, useArrayHash(entries.flat())])\n\n useRegistryChecker(ctx, ...entries.map(e => e[0]) as any)\n\n}\n\n/**\n * React hook to subscribe to multiple context values.\n * @param ctx - The context instance.\n * @param keys - Keys to subscribe to.\n * @returns An object with the current values for the keys.\n */\nexport const useDataSubscribeMultiple = <D, K extends keyof D>(\n ctx: Context<D> | undefined,\n ...keys: K[]\n): Pick<D, K> => {\n const [, setCounter] = useState(0)\n\n const returnValues = keys.map(key => ctx?.data?.[key])\n\n useEffect(() => {\n if (ctx) {\n let prevValues = returnValues\n const callback = debounce(() => {\n let currentValues = keys.map(key => ctx?.data?.[key])\n if (keys.some((key, i) => prevValues[i] != currentValues[i])) {\n // console.log(\"DIFF\", keys.filter((e, i) => prevValues[i] != currentValues[i]))\n prevValues = currentValues\n setCounter(c => c + 1)\n }\n }, 1)\n\n let handles = keys.map(key => ctx.subscribe(key, callback))\n\n let firstCall = setTimeout(callback, 1);\n\n return () => {\n clearTimeout(firstCall)\n callback.cancel();\n handles.forEach(unsub => unsub())\n }\n\n }\n }, [ctx, ...keys])\n\n\n return Object\n .fromEntries(keys.map((key, index) => [key, returnValues[index]])) as any\n}\n\n/**\n * React hook to subscribe to multiple context values with throttling.\n * @param ctx - The context instance.\n * @param debounceTime - Debounce time in ms (default 50).\n * @param keys - Keys to subscribe to.\n * @returns Array of current values for the keys.\n */\nexport const useDataSubscribeMultipleWithDebounce = <D, K extends (keyof D)[]>(\n ctx: Context<D> | undefined,\n debounceTime = 50,\n ...keys: K\n): { [i in keyof K]: D[K[i]] | undefined } => {\n //@ts-check\n const [, setCounter] = useState(0)\n\n const returnValues = keys.map(key => ctx?.data?.[key])\n\n useEffect(() => {\n if (ctx) {\n let prevValues = returnValues\n const callback = debounce(() => {\n let currentValues = keys.map(key => ctx?.data?.[key])\n if (keys.some((key, i) => prevValues[i] != currentValues[i])) {\n prevValues = currentValues\n setCounter(c => c + 1)\n }\n }, debounceTime)\n\n let handles = keys.map(key => ctx.subscribe(key, callback))\n\n let firstCall = setTimeout(callback, 1);\n\n return () => {\n clearTimeout(firstCall)\n callback.cancel();\n handles.forEach(unsub => unsub())\n }\n\n }\n }, [ctx, ...keys])\n\n return returnValues as any\n}\n\n\n\n","import { useEffect, useMemo } from \"react\"\nimport { useDataContext, useDataSourceMultiple, type Context } from \"./ctx\"\n\n\n\n/**\n * createRootCtx\n *\n * Factory that creates a headless \"Root\" component and companion hooks for a context namespace.\n * It derives a unique context name from a base `name` and a props object `U`, then publishes\n * a computed state `V` (from `useFn`) to that context.\n *\n * Usage (manual mounting):\n * ```\n * const { Root, useCtxState } = createRootCtx('user-state', useUserState)\n * ...\n * // Mount exactly one Root per unique props combination\n * <Root userId={id} />\n * ...\n * // Read anywhere ,using the same props shape\n * const user = useCtxState({ userId: id })\n *```\n * Strict vs lenient consumers:\n * - useCtxStateStrict(props) throws if a matching Root is not mounted.\n * - useCtxState(props) logs an error (after 1s) instead of throwing.\n *\n * Multiple instances safety:\n * - Mounting more than one Root with the same resolved context name throws (guards accidental duplicates).\n *\n * Name resolution notes:\n * - The context name is built from `name` + sorted key/value pairs of `props` (U), joined by \"-\".\n * - Prefer stable, primitive props to avoid collisions; if you need automation, pair with `createAutoCtx` and\n * mount a single <AutoRootCtx Wrapper={ErrorBoundary} /> at the app root so you don't manually mount `Root`.\n */\nexport const createRootCtx = <U extends object, V extends object>(name: string, useFn: (e: U) => V) => {\n\n const resolveCtxName = (e: U) => [\n name,\n ...Object\n .entries(e ?? {})\n .sort((e, f) => e[0].localeCompare(f[0]))\n .flat()\n ].join(\"-\")\n\n let ctxMountedCheck = new Set<string>()\n\n\n const RootState: React.FC<U> = (e: U) => {\n const state = useFn(e)\n const ctxName = resolveCtxName(e)\n const ctx = useDataContext<V>(ctxName)\n const stack = useMemo(() => new Error().stack, [])\n\n useDataSourceMultiple(\n ctx,\n ...Object.entries(state) as any\n )\n\n useEffect(() => {\n if (ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext \" + ctxName + \" are mounted more than once\")\n err.stack = stack;\n throw err\n }\n ctxMountedCheck.add(ctxName)\n return () => { ctxMountedCheck.delete(ctxName) };\n })\n\n return <></>\n }\n\n RootState.displayName = `State[${useFn?.name??'??'}]`\n\n return {\n resolveCtxName,\n Root: RootState,\n /**\n * Strict consumer: throws if the corresponding Root for these props isn't mounted.\n * Use in development/tests to fail fast when wiring is incorrect.\n */\n useCtxStateStrict: (e: U): Context<V> => {\n const ctxName = resolveCtxName(e)\n\n const stack = useMemo(() => new Error().stack, [])\n\n useEffect(() => {\n if (!ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext [\" + ctxName + \"] is not mounted\")\n err.stack = stack;\n throw err\n }\n }, [ctxName])\n\n return useDataContext<V>(ctxName)\n },\n /**\n * Lenient consumer: schedules a console.error if the Root isn't mounted instead of throwing.\n * Useful in production to avoid hard crashes while still surfacing misconfiguration.\n */\n useCtxState: (e: U): Context<V> => {\n const ctxName = resolveCtxName(e)\n\n const stack = useMemo(() => new Error().stack, [])\n\n useEffect(() => {\n if (!ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext [\" + ctxName + \"] is not mounted\")\n err.stack = stack;\n let timeout = setTimeout(() => console.error(err), 1000)\n return () => clearTimeout(timeout)\n }\n }, [ctxMountedCheck.has(ctxName)])\n\n return useDataContext<V>(ctxName)\n }\n }\n}","import { useEffect, useState, Fragment, useCallback } from \"react\"\nimport { useDataContext, useDataSourceMultiple, useDataSubscribe, type Context } from \"./ctx\"\nimport { createRootCtx } from \"./createRootCtx\"\n\n\n\n\n\n\nconst weakmapName = (function () {\n const weakmap = new WeakMap()\n\n return (e: any): string => {\n let result = weakmap.get(e);\n if (!result) {\n weakmap.set(e, result = (e?.name ?? \"\") + Math.random().toString())\n }\n return result\n }\n})()\n\n\nconst resolveName = (e: any) => [\n ...Object\n .entries(e ?? {})\n .sort((e, f) => e[0].localeCompare(f[0]))\n .flat()\n].join(\"-\")\n\n/**\n * Inline docs: createAutoCtx + AutoRootCtx\n *\n * Quick start\n * 1) Mount <AutoRootCtx /> ONCE near your app root. Provide a Wrapper that acts like an ErrorBoundary to isolate and log errors.\n * Example: <AutoRootCtx Wrapper={MyErrorBoundary} />\n *\n * 2) Create auto contexts from your root context factories:\n * ```\n * const { useCtxState: useTestCtxState } = createAutoCtx(createRootCtx('test-state', stateFn))\n * const { useCtxState: useOtherCtxState } = createAutoCtx(createRootCtx('other-state', otherFn))\n * ```\n * 3) Use them in components:\n * ```\n * const ctx = useTestCtxState({ userId })\n * const { property1, property2 } = useDataSubscribeMultiple(ctx,'property1','property2')\n * // No need to mount the Root returned by createRootCtx directly — AutoRootCtx manages it for you.\n * ```\n * Notes\n * - AutoRootCtx must be mounted before any useCtxState hooks created by createAutoCtx run.\n * - Wrapper should be an ErrorBoundary-like component that simply renders {children}; no extra providers or layout required.\n * - For each unique params object (by stable stringified key), AutoRootCtx ensures a corresponding Root instance is rendered.\n */\n\nexport const AutoRootCtx = ({ Wrapper = Fragment }) => {\n\n const ctx = useDataContext<any>(\"auto-ctx\")\n\n\n const [state, setState] = useState<Record<string, { Component: React.FC, subState: Record<string, { params: any, counter: number }> }>>({})\n\n\n const subscribeRoot = useCallback(\n (Comp: any, params: any) => {\n const weakName = weakmapName(Comp);\n const key = resolveName(params);\n\n setState(({\n [weakName]: {\n Component = Comp,\n subState: {\n [key]: preState = { params, counter: 0 },\n ...subState\n } = {}\n } = {},\n ...state\n }) => ({\n ...state,\n [weakName]: {\n Component,\n subState: {\n ...subState,\n [key]: {\n ...preState,\n counter: preState.counter + 1,\n },\n },\n }\n }));\n\n return () => setState(({\n [weakName]: {\n Component = Comp,\n subState: {\n [key]: preState = { params, counter: 0 },\n ...subState\n } = {}\n } = {},\n ...state\n }) => ({\n ...state,\n [weakName]: {\n Component,\n subState: {\n ...subState,\n ...preState.counter > 1 ? {\n [key]: {\n ...preState,\n counter: preState.counter - 1,\n },\n } : {},\n },\n }\n }))\n\n },\n []\n )\n\n useDataSourceMultiple(ctx,\n [\"subscribe\", subscribeRoot],\n [\"state\", state],\n )\n\n\n return <>\n {Object.entries(state)\n .flatMap(([k1, { Component, subState }]) => Object\n .entries(subState)\n .map(([k2, { counter, params }]) => ({ key: k1 + k2, Component, params, counter }))\n .filter(e => e.counter > 0)\n .map(({ key, params, Component }) => <Wrapper key={key} >\n <Component {...params} />\n </Wrapper>)\n )\n }\n </>\n\n}\n\n/**\n * createAutoCtx\n *\n * Bridges a Root context (from createRootCtx) to the global AutoRootCtx renderer.\n * You do NOT mount the Root component yourself — just mount <AutoRootCtx /> once at the app root.\n *\n * Usage: \n * ```\n * const { useCtxState: useTestCtxState } = createAutoCtx(createRootCtx(\n * 'test-state', \n * stateFn\n * ))\n * const { useCtxState: useOtherCtxState } = createAutoCtx(createRootCtx(\n * 'other-state', \n * otherFn\n * ))\n * ```\n * \n * Then inside components:\n * ```\n * const ctxState = useTestCtxState({ any: 'params' })\n * ```\n * AutoRootCtx will subscribe/unsubscribe instances per unique params and render the appropriate Root under the hood.\n */\nexport const createAutoCtx = <U extends object, V extends object,>({ Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V>>) => {\n\n return {\n\n useCtxState: (e: U): Context<V> => {\n\n const ctxName = resolveCtxName(e)\n\n const subscribe = useDataSubscribe(useDataContext<any>(\"auto-ctx\"), \"subscribe\")\n\n useEffect(() => {\n // Subscribe this component to an AutoRootCtx-managed Root instance keyed by e.\n // AutoRootCtx handles instance ref-counting and cleanup on unmount.\n return subscribe?.(Root, e)\n }, [subscribe, ctxName])\n\n return useDataContext<V>(ctxName)\n }\n }\n}","\nimport { debounce } from \"./utils\";\nimport { useState, useMemo, useEffect } from \"react\";\nimport type { Context } from \"./ctx\";\n\n/**\n * useQuickSubscribe is a custom React hook for efficiently subscribing to specific properties of a context's data object.\n * \n * @template D - The shape of the context data.\n * @param {Context<D> | undefined} ctx - The context object containing data and a subscribe method.\n * @returns {Partial<D>} A proxy object that mirrors the context data, automatically subscribing to properties as they are accessed.\n *\n * This hook tracks which properties of the context data are accessed by the component and subscribes to updates for only those properties.\n * When any of the subscribed properties change, the hook triggers a re-render. Subscriptions are managed and cleaned up automatically\n * when the component unmounts or the context changes. This approach minimizes unnecessary re-renders and resource usage by only\n * subscribing to the data that the component actually uses.\n *\n * Example usage:\n * const {name} = useQuickSubscribe(userContext);\n * // Accessing name will subscribe to changes in 'name' only\n * return <div>{name}</div>;\n */\n\nexport const useQuickSubscribe = <D>(\n ctx: Context<D> | undefined\n): {\n [P in keyof D]?: D[P] | undefined;\n } => {\n\n const [, setCounter] = useState(0);\n\n const { proxy, finalGetter, openGetter, clean } = useMemo(\n () => {\n\n const allKeys = new Set<keyof D>()\n const allCompareValue: { [P in keyof D]?: D[P] | undefined; } = {}\n const allUnsub = new Map()\n\n const proxy = new Proxy(\n ctx?.data as any,\n {\n get(target, p) {\n if (isOpenGetter) {\n allKeys.add(p as keyof D)\n return allCompareValue[p as keyof D] = target[p];\n } else {\n throw new Error(\"now allow here\")\n }\n }\n }\n ) as any\n\n let isOpenGetter = true;\n\n\n let onChange = debounce(() => {\n if ([...allKeys.values()]\n .some(k => allCompareValue[k] != ctx?.data?.[k])) {\n setCounter(c => c + 1)\n }\n }, 0)\n\n let openGetter = () => {\n isOpenGetter = true\n allKeys.clear()\n }\n\n let finalGetter = () => {\n isOpenGetter = false;\n\n [...allKeys.values()]\n .filter(k => !allUnsub.has(k))\n .forEach(k => {\n allUnsub.set(k, ctx?.subscribe(k, onChange))\n });\n\n [...allUnsub.keys()]\n .filter(k => !allKeys.has(k))\n .forEach(k => {\n let unsub = allUnsub.get(k)\n unsub?.();\n allUnsub.delete(k);\n });\n\n }\n\n let clean = () => {\n openGetter();\n finalGetter();\n setCounter(c => c + 1)\n }\n\n return { proxy, finalGetter, openGetter, clean }\n },\n [ctx]\n )\n\n openGetter();\n\n setTimeout(finalGetter, 0)\n\n useEffect(\n () => () => clean(),\n [clean]\n )\n\n return proxy;\n\n\n};\n"],"names":["debounce","func","wait","timeout","fn","args","memoize","cache","key","result","randomHash","useArrayHash","e","computedHash","useRef","currentValues","currentHash","isDiff","f","i","DataEvent","event","value","Context","name","_listener","listener","getContext","useDataContext","useMemo","useRegistryChecker","ctx","names","stack","useEffect","useDataSource","useDataSubscribe","debounceTime","setState","useState","callback","unsub","useDataSubscribeWithTransform","transform","preValue","newValue","useDataSourceMultiple","entries","useDataSubscribeMultiple","keys","setCounter","returnValues","prevValues","c","handles","firstCall","index","useDataSubscribeMultipleWithDebounce","createRootCtx","useFn","resolveCtxName","ctxMountedCheck","RootState","state","ctxName","err","jsx","Fragment","weakmapName","weakmap","resolveName","AutoRootCtx","Wrapper","subscribeRoot","useCallback","Comp","params","weakName","Component","preState","subState","k1","k2","counter","createAutoCtx","Root","useCtxState","useCtxStateStrict","subscribe","useQuickSubscribe","proxy","finalGetter","openGetter","clean","allKeys","allCompareValue","allUnsub","target","isOpenGetter","onChange","k"],"mappings":"iUACO,SAASA,EACdC,EACAC,EACsD,CACtD,IAAIC,EAAgD,KAEhDC,EAAiC,YAAaC,EAA2B,CACvEF,GACF,aAAaA,CAAO,EAEtBA,EAAU,WAAW,IAAM,CACzBF,EAAK,GAAGI,CAAI,CACd,EAAGH,CAAI,CACT,EAEA,OAAAE,EAAG,OAAS,IAAM,aAAaD,CAAQ,EAEhCC,CACT,CAGO,SAASE,EACdL,EAC2C,CAC3C,MAAMM,MAAY,IAElB,OAAO,YAAaF,EAAoC,CACtD,MAAMG,EAAM,KAAK,UAAUH,CAAI,EAC/B,GAAIE,EAAM,IAAIC,CAAG,EACf,OAAOD,EAAM,IAAIC,CAAG,EAEtB,MAAMC,EAASR,EAAK,GAAGI,CAAI,EAC3B,OAAAE,EAAM,IAAIC,EAAKC,CAAM,EACdA,CACT,CACF,CCjCA,MAAMC,EAAa,IAAM,KAAK,OAAA,EAAS,SAAA,EAAW,MAAM,CAAC,EAgB5CC,EAAgBC,GAAqB,CAEhD,KAAM,CAAE,QAAS,CAAE,aAAAC,CAAA,CAAa,EAAMC,EAAAA,OAAO,CAQ3C,IAAI,cAAe,CACjB,IAAIC,EAAuB,CAAA,EACvBC,EAAcN,EAAA,EAClB,OAAQE,GAAa,CACnB,IAAIK,EAAS,GAGb,OAAAA,EAASA,GAAY,CAACL,GAAO,CAACG,EAC9BE,EAASA,GAAWL,GAAG,QAAUG,GAAe,OAChDE,EAASA,GAAWL,EAAE,KAAK,CAACM,EAAGC,IAAMD,GAAKH,EAAcI,CAAC,CAAC,EAG1DJ,EAAgBH,EACZK,IACFD,EAAcN,EAAA,GAGTM,CACT,CACF,CAAA,CACD,EAED,OAAOH,EAAaD,CAAC,CACvB,EC9CA,MAAMQ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GAAK,CAACN,EAAQ,IAAIM,CAAC,CAAC,EAC3B,QAAQA,GAAK,CACAJ,EAAS,IAAII,CAAC,IAC1B,EACAJ,EAAS,OAAOI,CAAC,CACnB,CAAC,CAEL,EAQA,MAAO,CAAE,MAAAV,EAAO,YAAAC,EAAa,WAAAC,EAAY,MAN7B,IAAM,CAChBA,EAAAA,EACAD,EAAAA,EACAzC,EAAWG,GAAKA,EAAI,CAAC,CACvB,CAEyCwC,CAC3C,EACA,CAAC9D,CAAG,CAAA,EAGN,OAAA6D,EAAA,EAEA,WAAWD,EAAa,CAAC,EAEzBzD,EAAAA,UACE,IAAM,IAAM2D,EAAA,EACZ,CAACA,CAAK,CAAA,EAGDH,CAGT"}
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/state-utils/utils.ts","../src/state-utils/useArrayHash.ts","../src/state-utils/ctx.ts","../src/state-utils/createRootCtx.tsx","../src/state-utils/createAutoCtx.tsx","../src/state-utils/useQuickSubscribe.ts"],"sourcesContent":["// Debounce function\nexport function debounce<T extends (...args: any[]) => any>(\n func: T,\n wait: number\n): ((...args: Parameters<T>) => void) & { cancel: any } {\n let timeout: ReturnType<typeof setTimeout> | null = null;\n\n let fn: Function & { cancel: any } = function (...args: Parameters<T>): void {\n if (timeout) {\n clearTimeout(timeout);\n }\n timeout = setTimeout(() => {\n func(...args);\n }, wait);\n } as any; \n\n fn.cancel = () => clearTimeout(timeout!);\n\n return fn as any;\n}\n\n// Memoize function\nexport function memoize<T extends (...args: any[]) => any>(\n func: T\n): (...args: Parameters<T>) => ReturnType<T> {\n const cache = new Map<string, ReturnType<T>>();\n\n return function (...args: Parameters<T>): ReturnType<T> {\n const key = JSON.stringify(args);\n if (cache.has(key)) {\n return cache.get(key) as ReturnType<T>;\n }\n const result = func(...args);\n cache.set(key, result);\n return result;\n };\n}\n\n","import { useRef } from \"react\"\n\n\nconst randomHash = () => Math.random().toString().slice(2)\n\n/**\n * useArrayHash\n *\n * A custom hook that computes a stable hash for an array of values.\n * The hash changes only when the array's contents differ from the previous call.\n *\n * @param e - The input array to hash.\n * @returns A string hash that updates when the array changes.\n *\n * How it works:\n * - Tracks the previous array and its hash using a `useRef`.\n * - Compares the new array to the previous one by length and element equality.\n * - If any difference is detected, generates a new random hash.\n */\nexport const useArrayHash = (e: any[]): string => {\n\n const { current: { computedHash } } = useRef({\n /**\n * Getter for the computed hash function.\n *\n * - Initializes with an empty array and a random hash.\n * - Returns a function that compares the current array to the previous one.\n * - Updates the hash if any difference is detected.\n */\n get computedHash() {\n let currentValues: any[] = []\n let currentHash = randomHash()\n return (e: any[]) => {\n let isDiff = false\n\n // Check for differences in array existence, length, or elements.\n isDiff = isDiff || ((!e) != (!currentValues))\n isDiff = isDiff || (e?.length != currentValues?.length);\n isDiff = isDiff || (e.some((f, i) => f != currentValues[i]));\n\n // Update the hash if differences are found.\n currentValues = e;\n if (isDiff) {\n currentHash = randomHash()\n }\n\n return currentHash\n }\n }\n })\n\n return computedHash(e)\n}","import { debounce, memoize } from \"./utils\";\nimport { useEffect, useMemo, useState } from \"react\"\nimport { useArrayHash } from \"./useArrayHash\"\n\n\n\nclass DataEvent extends Event {\n constructor(\n public event: string,\n public value: any\n ) {\n super(event);\n }\n}\n\n/**\n * Generic context for managing shared state and event subscriptions.\n * @template D - The shape of the data managed by the context.\n */\nexport class Context<D> extends EventTarget {\n /**\n * Create a new Context instance.\n * @param name - The name of the context (for debugging).\n */\n constructor(public name: string) {\n console.log(\"[CONTEXT] %s\", name)\n // this.event.setMaxListeners(100)\n super();\n }\n\n // private event = new EventEmitter()\n\n /**\n * The current data held by the context.\n */\n public data: Partial<D> = {}\n /**\n * Registry for tracking active keys (for duplicate detection).\n */\n public registry = new Set<string>()\n\n /**\n * Publish a value to the context and notify subscribers if it changed.\n * @param key - The key to update.\n * @param value - The new value.\n */\n public publish(key: keyof D, value: D[typeof key] | undefined) {\n\n if (value != this.data[key]) {\n this.data[key] = value\n // console.count(\"[COUNT] \" + String(key))\n // this.event.emit(String(key), { value })\n this.dispatchEvent(new DataEvent(String(key), value))\n }\n }\n\n /**\n * Subscribe to changes for a specific key in the context.\n * @param key - The key to subscribe to.\n * @param _listener - Callback invoked with the new value.\n * @returns Unsubscribe function.\n */\n public subscribe(key: keyof D, _listener: (e: D[typeof key] | undefined) => void) {\n\n const listener = ({ event, value }: any) => {\n _listener(value)\n }\n\n this.addEventListener(String(key), listener)\n // console.log(\"listenerCount:\", String(key), this.event.listenerCount(String(key)))\n\n if (key in this.data) _listener(this.data[key])\n\n return () => (this.removeEventListener(String(key), listener), undefined)\n }\n\n}\n\n/**\n * Get or create a memoized Context instance by name.\n * @param name - The context name.\n * @returns The Context instance.\n */\nexport const getContext = memoize((name: string) => new Context<any>(name))\n\n/**\n * Type alias for a function that returns a Context instance.\n */\nexport type getContext<D> = (e: string) => Context<D>\n\n/**\n * React hook to get a typed Context instance by name.\n * @param name - The context name.\n * @returns The Context instance.\n */\nexport const useDataContext = <D>(name: string = \"noname\") => {\n\n const ctx = useMemo(() => getContext(name), [name])\n\n return ctx as any as Context<D>\n}\n\n/**\n * Internal hook to check for duplicate registry entries in a context.\n * Warns if any of the provided names are already registered.\n * @param ctx - The context instance.\n * @param names - Names to check and register.\n */\nconst useRegistryChecker = (ctx: Context<any> | undefined, ...names: string[]) => {\n // return;\n const stack = new Error(\"[ctx] useRegistryChecker failed \" + JSON.stringify({ names, ctx: ctx?.name ?? 'undefined' }))\n\n useEffect(\n () => {\n if (ctx) {\n if (names.some(name => ctx.registry.has(name))) {\n console.error(stack)\n }\n names.forEach(e => ctx.registry.add(e))\n\n // console.debug(\"[ctx] %s%s add datasource\", componentId, ctx.name, names)\n return () => {\n // console.debug(\"[ctx] %s %s remove datasource\", componentId, ctx.name, names)\n\n names.forEach(e => ctx.registry.delete(e))\n }\n }\n },\n [ctx, names.length]\n )\n\n}\n\n/**\n * React hook to publish a value to the context when it changes.\n * @param ctx - The context instance.\n * @param key - The key to update.\n * @param value - The new value.\n */\nexport const useDataSource = <D, K extends keyof D>(ctx: Context<D> | undefined, key: K, value: D[K] | undefined) => {\n //@ts-check\n useEffect(() => {\n if (ctx && ctx.data[key] != value) {\n\n ctx.publish(key, value)\n }\n }, [key, value, ctx])\n\n useRegistryChecker(ctx, key as any)\n}\n\n/**\n * React hook to subscribe to a context value, with optional debounce.\n * @param ctx - The context instance.\n * @param key - The key to subscribe to.\n * @param debounceTime - Debounce time in ms (default 0).\n * @returns The current value for the key.\n */\nexport const useDataSubscribe = <D, K extends keyof D>(ctx: Context<D> | undefined, key: K, debounceTime = 0): D[K] | undefined => {\n //@ts-check\n const [{ value }, setState] = useState(() => ({ value: ctx?.data?.[key] }))\n\n useEffect(() => {\n if (ctx) {\n let callback = debounceTime == 0\n ? (value: any) => setState({ value } as any)\n : debounce((value: any) => setState({ value } as any), debounceTime)\n let unsub = ctx.subscribe(key, callback)\n value != ctx.data[key] && setState({ value: ctx.data[key] })\n return () => {\n unsub()\n }\n }\n }, [key, ctx])\n\n return ctx?.data[key]\n}\n\n/**\n * React hook to subscribe to a context value and transform it before returning.\n * @param ctx - The context instance.\n * @param key - The key to subscribe to.\n * @param transform - Function to transform the value.\n * @returns The transformed value.\n */\nexport const useDataSubscribeWithTransform = <D, K extends keyof D, E>(ctx: Context<D> | undefined, key: K, transform: (e: D[K] | undefined) => E): E => {\n const [, setState] = useState(0)\n const result = useMemo(\n () => transform(ctx?.data[key]),\n [transform, ctx?.data[key]]\n )\n\n useEffect(() => {\n if (ctx) {\n let preValue = result\n let callback = () => {\n let newValue = transform(ctx.data[key])\n if (newValue != preValue) {\n preValue = newValue;\n setState(e => e + 1)\n };\n }\n let unsub = ctx.subscribe(key, callback)\n callback();\n return () => unsub()\n }\n }, [key, ctx])\n\n return result\n}\n\n/**\n * React hook to publish multiple values to the context.\n * @param ctx - The context instance.\n * @param entries - Array of [key, value] pairs to update.\n */\nexport const useDataSourceMultiple = <D, T extends readonly (keyof D)[]>(\n ctx: Context<D> | undefined,\n ...entries: { -readonly [P in keyof T]: [T[P], D[T[P]]] }\n) => {\n //@ts-check\n useEffect(() => {\n if (ctx) {\n for (let [key, value] of entries) {\n ctx.data[key] != value && ctx.publish(key, value)\n }\n }\n }, [ctx, useArrayHash(entries.flat())])\n\n useRegistryChecker(ctx, ...entries.map(e => e[0]) as any)\n\n}\n\n/**\n * React hook to subscribe to multiple context values.\n * @param ctx - The context instance.\n * @param keys - Keys to subscribe to.\n * @returns An object with the current values for the keys.\n */\nexport const useDataSubscribeMultiple = <D, K extends keyof D>(\n ctx: Context<D> | undefined,\n ...keys: K[]\n): Pick<D, K> => {\n const [, setCounter] = useState(0)\n\n const returnValues = keys.map(key => ctx?.data?.[key])\n\n useEffect(() => {\n if (ctx) {\n let prevValues = returnValues\n const callback = debounce(() => {\n let currentValues = keys.map(key => ctx?.data?.[key])\n if (keys.some((key, i) => prevValues[i] != currentValues[i])) {\n // console.log(\"DIFF\", keys.filter((e, i) => prevValues[i] != currentValues[i]))\n prevValues = currentValues\n setCounter(c => c + 1)\n }\n }, 1)\n\n let handles = keys.map(key => ctx.subscribe(key, callback))\n\n let firstCall = setTimeout(callback, 1);\n\n return () => {\n clearTimeout(firstCall)\n callback.cancel();\n handles.forEach(unsub => unsub())\n }\n\n }\n }, [ctx, ...keys])\n\n\n return Object\n .fromEntries(keys.map((key, index) => [key, returnValues[index]])) as any\n}\n\n/**\n * React hook to subscribe to multiple context values with throttling.\n * @param ctx - The context instance.\n * @param debounceTime - Debounce time in ms (default 50).\n * @param keys - Keys to subscribe to.\n * @returns Array of current values for the keys.\n */\nexport const useDataSubscribeMultipleWithDebounce = <D, K extends (keyof D)[]>(\n ctx: Context<D> | undefined,\n debounceTime = 50,\n ...keys: K\n): { [i in keyof K]: D[K[i]] | undefined } => {\n //@ts-check\n const [, setCounter] = useState(0)\n\n const returnValues = keys.map(key => ctx?.data?.[key])\n\n useEffect(() => {\n if (ctx) {\n let prevValues = returnValues\n const callback = debounce(() => {\n let currentValues = keys.map(key => ctx?.data?.[key])\n if (keys.some((key, i) => prevValues[i] != currentValues[i])) {\n prevValues = currentValues\n setCounter(c => c + 1)\n }\n }, debounceTime)\n\n let handles = keys.map(key => ctx.subscribe(key, callback))\n\n let firstCall = setTimeout(callback, 1);\n\n return () => {\n clearTimeout(firstCall)\n callback.cancel();\n handles.forEach(unsub => unsub())\n }\n\n }\n }, [ctx, ...keys])\n\n return returnValues as any\n}\n\n\n\n","import { useEffect, useMemo } from \"react\"\nimport { useDataContext, useDataSourceMultiple, type Context } from \"./ctx\"\n\n\n\n/**\n * createRootCtx\n *\n * Factory that creates a headless \"Root\" component and companion hooks for a context namespace.\n * It derives a unique context name from a base `name` and a props object `U`, then publishes\n * a computed state `V` (from `useFn`) to that context.\n *\n * Usage (manual mounting):\n * ```\n * const { Root, useCtxState } = createRootCtx('user-state', useUserState)\n * ...\n * // Mount exactly one Root per unique props combination\n * <Root userId={id} />\n * ...\n * // Read anywhere ,using the same props shape\n * const user = useCtxState({ userId: id })\n *```\n * Strict vs lenient consumers:\n * - useCtxStateStrict(props) throws if a matching Root is not mounted.\n * - useCtxState(props) logs an error (after 1s) instead of throwing.\n *\n * Multiple instances safety:\n * - Mounting more than one Root with the same resolved context name throws (guards accidental duplicates).\n *\n * Name resolution notes:\n * - The context name is built from `name` + sorted key/value pairs of `props` (U), joined by \"-\".\n * - Prefer stable, primitive props to avoid collisions; if you need automation, pair with `createAutoCtx` and\n * mount a single <AutoRootCtx Wrapper={ErrorBoundary} /> at the app root so you don't manually mount `Root`.\n */\nexport const createRootCtx = <U extends object, V extends object>(name: string, useFn: (e: U) => V) => {\n\n const resolveCtxName = (e: U) => [\n name,\n ...Object\n .entries(e ?? {})\n .sort((e, f) => e[0].localeCompare(f[0]))\n .flat()\n ].join(\"-\")\n\n let ctxMountedCheck = new Set<string>()\n\n\n const RootState: React.FC<U> = (e: U) => {\n const state = useFn(e)\n const ctxName = resolveCtxName(e)\n const ctx = useDataContext<V>(ctxName)\n const stack = useMemo(() => new Error().stack, [])\n\n useDataSourceMultiple(\n ctx,\n ...Object.entries(state) as any\n )\n\n useEffect(() => {\n if (ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext \" + ctxName + \" are mounted more than once\")\n err.stack = stack;\n throw err\n }\n ctxMountedCheck.add(ctxName)\n return () => { ctxMountedCheck.delete(ctxName) };\n })\n\n return <></>\n }\n\n RootState.displayName = `State[${useFn?.name??'??'}]`\n\n return {\n resolveCtxName,\n Root: RootState,\n /**\n * Strict consumer: throws if the corresponding Root for these props isn't mounted.\n * Use in development/tests to fail fast when wiring is incorrect.\n */\n useCtxStateStrict: (e: U): Context<V> => {\n const ctxName = resolveCtxName(e)\n\n const stack = useMemo(() => new Error().stack, [])\n\n useEffect(() => {\n if (!ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext [\" + ctxName + \"] is not mounted\")\n err.stack = stack;\n throw err\n }\n }, [ctxName])\n\n return useDataContext<V>(ctxName)\n },\n /**\n * Lenient consumer: schedules a console.error if the Root isn't mounted instead of throwing.\n * Useful in production to avoid hard crashes while still surfacing misconfiguration.\n */\n useCtxState: (e: U): Context<V> => {\n const ctxName = resolveCtxName(e)\n\n const stack = useMemo(() => new Error().stack, [])\n\n useEffect(() => {\n if (!ctxMountedCheck.has(ctxName)) {\n const err = new Error(\"RootContext [\" + ctxName + \"] is not mounted\")\n err.stack = stack;\n let timeout = setTimeout(() => console.error(err), 1000)\n return () => clearTimeout(timeout)\n }\n }, [ctxMountedCheck.has(ctxName)])\n\n return useDataContext<V>(ctxName)\n }\n }\n}","import { useEffect, useState, Fragment, useCallback } from \"react\"\nimport { useDataContext, useDataSourceMultiple, useDataSubscribe, type Context } from \"./ctx\"\nimport { createRootCtx } from \"./createRootCtx\"\n\n\n\n\n\n\nconst weakmapName = (function () {\n const weakmap = new WeakMap()\n\n return (e: any): string => {\n let result = weakmap.get(e);\n if (!result) {\n weakmap.set(e, result = (e?.name ?? \"\") + Math.random().toString())\n }\n return result\n }\n})()\n\n\nconst resolveName = (e: any) => [\n ...Object\n .entries(e ?? {})\n .sort((e, f) => e[0].localeCompare(f[0]))\n .flat()\n].join(\"-\")\n\n/**\n * Inline docs: createAutoCtx + AutoRootCtx\n *\n * Quick start\n * 1) Mount <AutoRootCtx /> ONCE near your app root. Provide a Wrapper that acts like an ErrorBoundary to isolate and log errors.\n * Example: <AutoRootCtx Wrapper={MyErrorBoundary} />\n *\n * 2) Create auto contexts from your root context factories:\n * ```\n * const { useCtxState: useTestCtxState } = createAutoCtx(createRootCtx('test-state', stateFn))\n * const { useCtxState: useOtherCtxState } = createAutoCtx(createRootCtx('other-state', otherFn))\n * ```\n * 3) Use them in components:\n * ```\n * const ctx = useTestCtxState({ userId })\n * const { property1, property2 } = useDataSubscribeMultiple(ctx,'property1','property2')\n * // No need to mount the Root returned by createRootCtx directly — AutoRootCtx manages it for you.\n * ```\n * Notes\n * - AutoRootCtx must be mounted before any useCtxState hooks created by createAutoCtx run.\n * - Wrapper should be an ErrorBoundary-like component that simply renders {children}; no extra providers or layout required.\n * - For each unique params object (by stable stringified key), AutoRootCtx ensures a corresponding Root instance is rendered.\n */\n\nexport const AutoRootCtx = ({ Wrapper = Fragment }) => {\n\n const ctx = useDataContext<any>(\"auto-ctx\")\n\n\n const [state, setState] = useState<Record<string, { Component: React.FC, subState: Record<string, { params: any, counter: number }> }>>({})\n\n\n const subscribeRoot = useCallback(\n (Comp: any, params: any) => {\n const weakName = weakmapName(Comp);\n const key = resolveName(params);\n\n setState(({\n [weakName]: {\n Component = Comp,\n subState: {\n [key]: preState = { params, counter: 0 },\n ...subState\n } = {}\n } = {},\n ...state\n }) => ({\n ...state,\n [weakName]: {\n Component,\n subState: {\n ...subState,\n [key]: {\n ...preState,\n counter: preState.counter + 1,\n },\n },\n }\n }));\n\n return () => setState(({\n [weakName]: {\n Component = Comp,\n subState: {\n [key]: preState = { params, counter: 0 },\n ...subState\n } = {}\n } = {},\n ...state\n }) => ({\n ...state,\n [weakName]: {\n Component,\n subState: {\n ...subState,\n ...preState.counter > 1 ? {\n [key]: {\n ...preState,\n counter: preState.counter - 1,\n },\n } : {},\n },\n }\n }))\n\n },\n []\n )\n\n useDataSourceMultiple(ctx,\n [\"subscribe\", subscribeRoot],\n [\"state\", state],\n )\n\n\n return <>\n {Object.entries(state)\n .flatMap(([k1, { Component, subState }]) => Object\n .entries(subState)\n .map(([k2, { counter, params }]) => ({ key: k1 + k2, Component, params, counter }))\n .filter(e => e.counter > 0)\n .map(({ key, params, Component }) => <Wrapper key={key} >\n <Component {...params} />\n </Wrapper>)\n )\n }\n </>\n\n}\n\n/**\n * createAutoCtx\n *\n * Bridges a Root context (from createRootCtx) to the global AutoRootCtx renderer.\n * You do NOT mount the Root component yourself — just mount <AutoRootCtx /> once at the app root.\n *\n * Usage: \n * ```\n * const { useCtxState: useTestCtxState } = createAutoCtx(createRootCtx(\n * 'test-state', \n * stateFn\n * ))\n * const { useCtxState: useOtherCtxState } = createAutoCtx(createRootCtx(\n * 'other-state', \n * otherFn\n * ))\n * ```\n * \n * Then inside components:\n * ```\n * const ctxState = useTestCtxState({ any: 'params' })\n * ```\n * AutoRootCtx will subscribe/unsubscribe instances per unique params and render the appropriate Root under the hood.\n */\nexport const createAutoCtx = <U extends object, V extends object,>(\n { Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V>>,\n unmountTime = 0\n) => {\n\n return {\n\n useCtxState: (e: U): Context<V> => {\n\n const ctxName = resolveCtxName(e)\n\n const subscribe = useDataSubscribe(useDataContext<any>(\"auto-ctx\"), \"subscribe\")\n\n useEffect(() => {\n // Subscribe this component to an AutoRootCtx-managed Root instance keyed by e.\n // AutoRootCtx handles instance ref-counting and cleanup on unmount.\n if (unmountTime == 0) {\n return subscribe?.(Root, e)\n } else {\n let unsub = subscribe?.(Root, e)\n return () => setTimeout(unsub, unmountTime)\n }\n }, [subscribe, ctxName])\n\n return useDataContext<V>(ctxName)\n }\n }\n}","\nimport { debounce } from \"./utils\";\nimport { useState, useMemo, useEffect } from \"react\";\nimport type { Context } from \"./ctx\";\n\n/**\n * useQuickSubscribe is a custom React hook for efficiently subscribing to specific properties of a context's data object.\n * \n * @template D - The shape of the context data.\n * @param {Context<D> | undefined} ctx - The context object containing data and a subscribe method.\n * @returns {Partial<D>} A proxy object that mirrors the context data, automatically subscribing to properties as they are accessed.\n *\n * This hook tracks which properties of the context data are accessed by the component and subscribes to updates for only those properties.\n * When any of the subscribed properties change, the hook triggers a re-render. Subscriptions are managed and cleaned up automatically\n * when the component unmounts or the context changes. This approach minimizes unnecessary re-renders and resource usage by only\n * subscribing to the data that the component actually uses.\n *\n * Example usage:\n * const {name} = useQuickSubscribe(userContext);\n * // Accessing name will subscribe to changes in 'name' only\n * return <div>{name}</div>;\n */\n\nexport const useQuickSubscribe = <D>(\n ctx: Context<D> | undefined\n): {\n [P in keyof D]?: D[P] | undefined;\n } => {\n\n const [, setCounter] = useState(0);\n\n const { proxy, finalGetter, openGetter, clean } = useMemo(\n () => {\n\n const allKeys = new Set<keyof D>()\n const allCompareValue: { [P in keyof D]?: D[P] | undefined; } = {}\n const allUnsub = new Map()\n\n const proxy = new Proxy(\n ctx?.data as any,\n {\n get(target, p) {\n if (isOpenGetter) {\n allKeys.add(p as keyof D)\n return allCompareValue[p as keyof D] = target[p];\n } else {\n throw new Error(\"now allow here\")\n }\n }\n }\n ) as any\n\n let isOpenGetter = true;\n\n\n let onChange = debounce(() => {\n if ([...allKeys.values()]\n .some(k => allCompareValue[k] != ctx?.data?.[k])) {\n setCounter(c => c + 1)\n }\n }, 0)\n\n let openGetter = () => {\n isOpenGetter = true\n allKeys.clear()\n }\n\n let finalGetter = () => {\n isOpenGetter = false;\n\n [...allKeys.values()]\n .filter(k => !allUnsub.has(k))\n .forEach(k => {\n allUnsub.set(k, ctx?.subscribe(k, onChange))\n });\n\n [...allUnsub.keys()]\n .filter(k => !allKeys.has(k))\n .forEach(k => {\n let unsub = allUnsub.get(k)\n unsub?.();\n allUnsub.delete(k);\n });\n\n }\n\n let clean = () => {\n openGetter();\n finalGetter();\n setCounter(c => c + 1)\n }\n\n return { proxy, finalGetter, openGetter, clean }\n },\n [ctx]\n )\n\n openGetter();\n\n setTimeout(finalGetter, 0)\n\n useEffect(\n () => () => clean(),\n [clean]\n )\n\n return proxy;\n\n\n};\n"],"names":["debounce","func","wait","timeout","fn","args","memoize","cache","key","result","randomHash","useArrayHash","computedHash","useRef","currentValues","currentHash","e","isDiff","f","i","DataEvent","event","value","Context","name","_listener","listener","getContext","useDataContext","useMemo","useRegistryChecker","ctx","names","stack","useEffect","useDataSource","useDataSubscribe","debounceTime","setState","useState","callback","unsub","useDataSubscribeWithTransform","transform","preValue","newValue","useDataSourceMultiple","entries","useDataSubscribeMultiple","keys","setCounter","returnValues","prevValues","c","handles","firstCall","index","useDataSubscribeMultipleWithDebounce","createRootCtx","useFn","resolveCtxName","ctxMountedCheck","RootState","state","ctxName","err","jsx","Fragment","weakmapName","weakmap","resolveName","AutoRootCtx","Wrapper","subscribeRoot","useCallback","Comp","params","weakName","Component","preState","subState","k1","k2","counter","createAutoCtx","Root","useCtxState","useCtxStateStrict","unmountTime","subscribe","useQuickSubscribe","proxy","finalGetter","openGetter","clean","allKeys","allCompareValue","allUnsub","target","isOpenGetter","onChange","k"],"mappings":"iUACO,SAASA,EACdC,EACAC,EACsD,CACtD,IAAIC,EAAgD,KAEhDC,EAAiC,YAAaC,EAA2B,CACvEF,GACF,aAAaA,CAAO,EAEtBA,EAAU,WAAW,IAAM,CACzBF,EAAK,GAAGI,CAAI,CACd,EAAGH,CAAI,CACT,EAEA,OAAAE,EAAG,OAAS,IAAM,aAAaD,CAAQ,EAEhCC,CACT,CAGO,SAASE,EACdL,EAC2C,CAC3C,MAAMM,MAAY,IAElB,OAAO,YAAaF,EAAoC,CACtD,MAAMG,EAAM,KAAK,UAAUH,CAAI,EAC/B,GAAIE,EAAM,IAAIC,CAAG,EACf,OAAOD,EAAM,IAAIC,CAAG,EAEtB,MAAMC,EAASR,EAA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@@ export declare const AutoRootCtx: ({ Wrapper }: {
|
|
|
50
50
|
* ```
|
|
51
51
|
* AutoRootCtx will subscribe/unsubscribe instances per unique params and render the appropriate Root under the hood.
|
|
52
52
|
*/
|
|
53
|
-
export declare const createAutoCtx: <U extends object, V extends object>({ Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V
|
|
53
|
+
export declare const createAutoCtx: <U extends object, V extends object>({ Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V>>, unmountTime?: number) => {
|
|
54
54
|
useCtxState: (e: U) => Context<V>;
|
|
55
55
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-state-custom",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.18",
|
|
4
|
+
"description": "The `react-state-custom` library provides a powerful set of tools for managing shared state in React applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.umd.js",
|
|
7
7
|
"module": "dist/index.es.js",
|
|
@@ -18,7 +18,14 @@
|
|
|
18
18
|
"dev": "vite",
|
|
19
19
|
"test": "echo \"No tests specified\" && exit 0"
|
|
20
20
|
},
|
|
21
|
-
"keywords": [
|
|
21
|
+
"keywords": [
|
|
22
|
+
"frontend",
|
|
23
|
+
"hooks",
|
|
24
|
+
"library",
|
|
25
|
+
"state",
|
|
26
|
+
"react",
|
|
27
|
+
"react-hook"
|
|
28
|
+
],
|
|
22
29
|
"author": "Your Name",
|
|
23
30
|
"license": "MIT",
|
|
24
31
|
"peerDependencies": {
|
|
@@ -161,7 +161,10 @@ export const AutoRootCtx = ({ Wrapper = Fragment }) => {
|
|
|
161
161
|
* ```
|
|
162
162
|
* AutoRootCtx will subscribe/unsubscribe instances per unique params and render the appropriate Root under the hood.
|
|
163
163
|
*/
|
|
164
|
-
export const createAutoCtx = <U extends object, V extends object,>(
|
|
164
|
+
export const createAutoCtx = <U extends object, V extends object,>(
|
|
165
|
+
{ Root, useCtxState, useCtxStateStrict, resolveCtxName }: ReturnType<typeof createRootCtx<U, V>>,
|
|
166
|
+
unmountTime = 0
|
|
167
|
+
) => {
|
|
165
168
|
|
|
166
169
|
return {
|
|
167
170
|
|
|
@@ -174,7 +177,12 @@ export const createAutoCtx = <U extends object, V extends object,>({ Root, useCt
|
|
|
174
177
|
useEffect(() => {
|
|
175
178
|
// Subscribe this component to an AutoRootCtx-managed Root instance keyed by e.
|
|
176
179
|
// AutoRootCtx handles instance ref-counting and cleanup on unmount.
|
|
177
|
-
|
|
180
|
+
if (unmountTime == 0) {
|
|
181
|
+
return subscribe?.(Root, e)
|
|
182
|
+
} else {
|
|
183
|
+
let unsub = subscribe?.(Root, e)
|
|
184
|
+
return () => setTimeout(unsub, unmountTime)
|
|
185
|
+
}
|
|
178
186
|
}, [subscribe, ctxName])
|
|
179
187
|
|
|
180
188
|
return useDataContext<V>(ctxName)
|