chizu 0.2.24 → 0.2.25
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 +24 -0
- package/dist/chizu.js +222 -219
- package/dist/chizu.umd.cjs +1 -1
- package/dist/controller/types.d.ts +1 -1
- package/dist/module/renderer/dispatchers/utils.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -246,3 +246,27 @@ export default <Actions<Module>>function Actions(module) {
|
|
|
246
246
|
};
|
|
247
247
|
};
|
|
248
248
|
```
|
|
249
|
+
|
|
250
|
+
If you need the context values to be reactive and fire the `Lifecycle.Derive` method then simply add it to your `props` definition when you initialise your scoped component:
|
|
251
|
+
|
|
252
|
+
```tsx
|
|
253
|
+
export default function Profile(props: Props): React.ReactElement {
|
|
254
|
+
const name = React.useContext(NameContext);
|
|
255
|
+
|
|
256
|
+
return (
|
|
257
|
+
<Scope<Module> using={{ model, actions, props: { ...props, name } }}>
|
|
258
|
+
{(module) => (
|
|
259
|
+
<>
|
|
260
|
+
<p>Hey {module.model.name}</p>
|
|
261
|
+
|
|
262
|
+
<button
|
|
263
|
+
onClick={() => module.actions.dispatch([Action.Name, randomName()])}
|
|
264
|
+
>
|
|
265
|
+
Switch profile
|
|
266
|
+
</button>
|
|
267
|
+
</>
|
|
268
|
+
)}
|
|
269
|
+
</Scope>
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
```
|
package/dist/chizu.js
CHANGED
|
@@ -1,231 +1,231 @@
|
|
|
1
1
|
var Q = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
throw TypeError(
|
|
2
|
+
var _ = (e) => {
|
|
3
|
+
throw TypeError(e);
|
|
4
4
|
};
|
|
5
|
-
var W = (
|
|
6
|
-
var
|
|
7
|
-
var
|
|
5
|
+
var W = (e, t, n) => t in e ? Q(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
6
|
+
var g = (e, t, n) => W(e, typeof t != "symbol" ? t + "" : t, n), A = (e, t, n) => t.has(e) || _("Cannot " + n);
|
|
7
|
+
var x = (e, t, n) => (A(e, t, "read from private field"), n ? n.call(e) : t.get(e)), R = (e, t, n) => t.has(e) ? _("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(e) : t.set(e, n), S = (e, t, n, r) => (A(e, t, "write to private field"), r ? r.call(e, n) : t.set(e, n), n);
|
|
8
8
|
import { jsx as C } from "react/jsx-runtime";
|
|
9
|
-
import * as
|
|
9
|
+
import * as c from "react";
|
|
10
10
|
import { useRef as B } from "react";
|
|
11
|
-
import
|
|
11
|
+
import M from "eventemitter3";
|
|
12
12
|
import { Immer as G, enablePatches as K } from "immer";
|
|
13
13
|
import m from "lodash/get";
|
|
14
|
-
import
|
|
14
|
+
import D from "traverse";
|
|
15
15
|
class H {
|
|
16
|
-
constructor(
|
|
17
|
-
this.value =
|
|
16
|
+
constructor(t) {
|
|
17
|
+
this.value = t;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
class V {
|
|
21
|
-
static Draft(
|
|
22
|
-
return new H(
|
|
21
|
+
static Draft(t) {
|
|
22
|
+
return new H(t);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
g(V, "Op", {
|
|
26
26
|
Add: 1,
|
|
27
27
|
Remove: 2,
|
|
28
28
|
Update: 4,
|
|
29
29
|
Move: 8,
|
|
30
30
|
Replace: 16
|
|
31
31
|
});
|
|
32
|
-
var h = /* @__PURE__ */ ((
|
|
33
|
-
function X(
|
|
34
|
-
return new Promise((
|
|
32
|
+
var h = /* @__PURE__ */ ((e) => (e.Mount = "lifecycle/mount", e.Node = "lifecycle/node", e.Derive = "lifecycle/derive", e.Error = "lifecycle/error", e.Unmount = "lifecycle/unmount", e))(h || {}), O = /* @__PURE__ */ ((e) => (e[e.Default = 0] = "Default", e[e.Error = 1] = "Error", e))(O || {});
|
|
33
|
+
function X(e) {
|
|
34
|
+
return new Promise((t) => setTimeout(t, e));
|
|
35
35
|
}
|
|
36
|
-
function Y(
|
|
37
|
-
return
|
|
36
|
+
function Y(e) {
|
|
37
|
+
return e ? !!(e && typeof e != "symbol") : Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`);
|
|
38
38
|
}
|
|
39
|
-
function
|
|
40
|
-
return JSON.stringify(
|
|
39
|
+
function w(e) {
|
|
40
|
+
return JSON.stringify(e);
|
|
41
41
|
}
|
|
42
|
-
const E = Symbol("meta"),
|
|
42
|
+
const E = Symbol("meta"), ge = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
43
43
|
__proto__: null,
|
|
44
|
-
hash:
|
|
44
|
+
hash: w,
|
|
45
45
|
meta: E,
|
|
46
46
|
pk: Y,
|
|
47
47
|
sleep: X
|
|
48
48
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
49
|
-
function f(
|
|
50
|
-
const n =
|
|
51
|
-
return
|
|
52
|
-
const s =
|
|
49
|
+
function f(e, t) {
|
|
50
|
+
const n = c.useRef(null), r = c.useRef(null);
|
|
51
|
+
return c.useMemo(() => {
|
|
52
|
+
const s = w(t);
|
|
53
53
|
if (r.current !== s) {
|
|
54
54
|
r.current = s;
|
|
55
|
-
const o =
|
|
55
|
+
const o = e();
|
|
56
56
|
return n.current = o, o;
|
|
57
57
|
}
|
|
58
58
|
return n.current;
|
|
59
|
-
},
|
|
59
|
+
}, t);
|
|
60
60
|
}
|
|
61
|
-
function
|
|
62
|
-
const n =
|
|
63
|
-
|
|
64
|
-
const r =
|
|
61
|
+
function k(e, t) {
|
|
62
|
+
const n = c.useRef(null);
|
|
63
|
+
c.useEffect(() => {
|
|
64
|
+
const r = w(t);
|
|
65
65
|
if (n.current !== r)
|
|
66
|
-
return n.current = r,
|
|
67
|
-
},
|
|
66
|
+
return n.current = r, e();
|
|
67
|
+
}, t);
|
|
68
68
|
}
|
|
69
|
-
const T =
|
|
70
|
-
instance: new
|
|
69
|
+
const T = c.createContext({
|
|
70
|
+
instance: new M()
|
|
71
71
|
});
|
|
72
72
|
function $() {
|
|
73
|
-
return
|
|
73
|
+
return c.useContext(T);
|
|
74
74
|
}
|
|
75
|
-
function
|
|
76
|
-
const
|
|
75
|
+
function xe({ children: e }) {
|
|
76
|
+
const t = f(
|
|
77
77
|
() => ({
|
|
78
|
-
instance: new
|
|
78
|
+
instance: new M()
|
|
79
79
|
}),
|
|
80
80
|
[]
|
|
81
81
|
);
|
|
82
|
-
return /* @__PURE__ */ C(T.Provider, { value:
|
|
82
|
+
return /* @__PURE__ */ C(T.Provider, { value: t, children: e });
|
|
83
83
|
}
|
|
84
|
-
const
|
|
84
|
+
const l = {
|
|
85
85
|
immer: new G(),
|
|
86
86
|
annotations: Symbol("annotations")
|
|
87
87
|
};
|
|
88
88
|
K();
|
|
89
|
-
|
|
89
|
+
l.immer.setAutoFreeze(!1);
|
|
90
90
|
class j {
|
|
91
|
-
constructor(
|
|
92
|
-
|
|
93
|
-
this.value =
|
|
91
|
+
constructor(t, n, r = null) {
|
|
92
|
+
g(this, "process");
|
|
93
|
+
this.value = t, this.operations = n, this.field = r, this.process = null;
|
|
94
94
|
}
|
|
95
|
-
attach(
|
|
96
|
-
return this.process =
|
|
95
|
+
attach(t) {
|
|
96
|
+
return this.process = t, this;
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
function Z(
|
|
100
|
-
return new j(
|
|
99
|
+
function Z(e, t = []) {
|
|
100
|
+
return new j(e, t);
|
|
101
101
|
}
|
|
102
|
-
class
|
|
103
|
-
constructor(
|
|
104
|
-
this.stateless =
|
|
102
|
+
class U {
|
|
103
|
+
constructor(t, n = t) {
|
|
104
|
+
this.stateless = t, this.stateful = n;
|
|
105
105
|
}
|
|
106
106
|
get validatable() {
|
|
107
107
|
return z(this.stateful);
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
function z(
|
|
111
|
-
return new Proxy(
|
|
110
|
+
function z(e, t = []) {
|
|
111
|
+
return new Proxy(e, {
|
|
112
112
|
get(n, r) {
|
|
113
113
|
switch (r) {
|
|
114
114
|
case "is":
|
|
115
115
|
return (s) => {
|
|
116
|
-
const o =
|
|
116
|
+
const o = P(e, t);
|
|
117
117
|
if (!o) return !1;
|
|
118
|
-
const
|
|
119
|
-
o.flatMap((
|
|
118
|
+
const u = new Set(
|
|
119
|
+
o.flatMap((d) => d.operations)
|
|
120
120
|
);
|
|
121
|
-
return !!(Array.from(
|
|
122
|
-
(
|
|
121
|
+
return !!(Array.from(u).reduce(
|
|
122
|
+
(d, a) => d | (a ?? 0),
|
|
123
123
|
0
|
|
124
124
|
) & s);
|
|
125
125
|
};
|
|
126
126
|
case "pending":
|
|
127
|
-
return () => !!
|
|
127
|
+
return () => !!P(e, t);
|
|
128
128
|
case "draft":
|
|
129
129
|
return () => {
|
|
130
|
-
const s =
|
|
131
|
-
if (!s) return m(
|
|
132
|
-
const o = s.flatMap((
|
|
133
|
-
return o ? o.value : m(
|
|
130
|
+
const s = P(e, t);
|
|
131
|
+
if (!s) return m(e, t);
|
|
132
|
+
const o = s.flatMap((u) => u.operations).find((u) => u instanceof H);
|
|
133
|
+
return o ? o.value : m(e, t);
|
|
134
134
|
};
|
|
135
135
|
}
|
|
136
|
-
return z(
|
|
136
|
+
return z(e, [...t, String(r)]);
|
|
137
137
|
}
|
|
138
138
|
});
|
|
139
139
|
}
|
|
140
|
-
function
|
|
141
|
-
const r = typeof m(
|
|
140
|
+
function P(e, t) {
|
|
141
|
+
const r = typeof m(e, t) == "object" ? t : t.slice(0, -1), s = r.length === 0 ? e : m(e, r), o = (s == null ? void 0 : s[l.annotations]) ?? [];
|
|
142
142
|
return o.length > 0 ? o : null;
|
|
143
143
|
}
|
|
144
|
-
function q(
|
|
145
|
-
function r(
|
|
146
|
-
return
|
|
147
|
-
if (this.key ===
|
|
144
|
+
function q(e, t, n) {
|
|
145
|
+
function r(u) {
|
|
146
|
+
return D(u).forEach(function() {
|
|
147
|
+
if (this.key === l.annotations) {
|
|
148
148
|
this.block();
|
|
149
149
|
return;
|
|
150
150
|
}
|
|
151
151
|
this.node instanceof j && this.update(this.node.value);
|
|
152
152
|
});
|
|
153
153
|
}
|
|
154
|
-
function s(
|
|
155
|
-
return
|
|
156
|
-
if (this.key ===
|
|
154
|
+
function s(u) {
|
|
155
|
+
return D(u).forEach(function() {
|
|
156
|
+
if (this.key === l.annotations) {
|
|
157
157
|
this.block();
|
|
158
158
|
return;
|
|
159
159
|
}
|
|
160
160
|
if (this.node instanceof j) {
|
|
161
|
-
const i = typeof this.node.value == "object",
|
|
161
|
+
const i = typeof this.node.value == "object", d = [
|
|
162
162
|
...i ? this.path : this.path.slice(0, -1),
|
|
163
|
-
|
|
164
|
-
],
|
|
163
|
+
l.annotations
|
|
164
|
+
], a = m(e.stateful, d) ?? [], y = this.node.attach(t);
|
|
165
165
|
i ? this.update(
|
|
166
166
|
{
|
|
167
167
|
...this.node.value,
|
|
168
|
-
[
|
|
168
|
+
[l.annotations]: [y, ...a]
|
|
169
169
|
},
|
|
170
170
|
!0
|
|
171
|
-
) : (this.parent && (this.parent.node[
|
|
171
|
+
) : (this.parent && (this.parent.node[l.annotations] = [y, ...a]), this.update(this.node.value, !0));
|
|
172
172
|
}
|
|
173
173
|
});
|
|
174
174
|
}
|
|
175
|
-
const o = (
|
|
176
|
-
return new
|
|
177
|
-
r(
|
|
178
|
-
s(
|
|
175
|
+
const o = (u) => n(u, u[E]);
|
|
176
|
+
return new U(
|
|
177
|
+
r(l.immer.produce(e.stateless, o)),
|
|
178
|
+
s(l.immer.produce(e.stateful, o))
|
|
179
179
|
);
|
|
180
180
|
}
|
|
181
|
-
function
|
|
182
|
-
const n =
|
|
183
|
-
if (this.key ===
|
|
181
|
+
function b(e, t) {
|
|
182
|
+
const n = D(e.stateful).forEach(function() {
|
|
183
|
+
if (this.key === l.annotations) {
|
|
184
184
|
this.block();
|
|
185
185
|
return;
|
|
186
186
|
}
|
|
187
|
-
if (this.node && this.node[
|
|
188
|
-
const r = this.node[
|
|
187
|
+
if (this.node && this.node[l.annotations]) {
|
|
188
|
+
const r = this.node[l.annotations];
|
|
189
189
|
this.update(
|
|
190
190
|
{
|
|
191
191
|
...this.node,
|
|
192
|
-
[
|
|
193
|
-
(s) => s.process !==
|
|
192
|
+
[l.annotations]: r.filter(
|
|
193
|
+
(s) => s.process !== t
|
|
194
194
|
)
|
|
195
195
|
},
|
|
196
196
|
!0
|
|
197
197
|
);
|
|
198
198
|
}
|
|
199
199
|
});
|
|
200
|
-
return new
|
|
200
|
+
return new U(e.stateless, n);
|
|
201
201
|
}
|
|
202
|
-
function L(
|
|
202
|
+
function L(e) {
|
|
203
203
|
return f(
|
|
204
204
|
() => ({
|
|
205
205
|
controller: {
|
|
206
206
|
get model() {
|
|
207
|
-
return
|
|
207
|
+
return e.model.current.stateful;
|
|
208
208
|
},
|
|
209
209
|
queue: [],
|
|
210
210
|
actions: {
|
|
211
|
-
annotate(
|
|
212
|
-
return Z(
|
|
211
|
+
annotate(t, n) {
|
|
212
|
+
return Z(t, n);
|
|
213
213
|
},
|
|
214
|
-
produce(
|
|
215
|
-
return (n, r) => q(n, r,
|
|
214
|
+
produce(t) {
|
|
215
|
+
return (n, r) => q(n, r, t);
|
|
216
216
|
},
|
|
217
|
-
dispatch([
|
|
218
|
-
if (
|
|
217
|
+
dispatch([t, ...n]) {
|
|
218
|
+
if (t == null) return Promise.reject();
|
|
219
219
|
const r = Promise.withResolvers();
|
|
220
|
-
return
|
|
220
|
+
return e.dispatchers.dispatch(t, n, r), r.promise;
|
|
221
221
|
},
|
|
222
|
-
context(
|
|
223
|
-
const n = Object.keys(
|
|
224
|
-
return
|
|
222
|
+
context(t) {
|
|
223
|
+
const n = Object.keys(t);
|
|
224
|
+
return e.context.use.current = t, e.context.update(), n.reduce(
|
|
225
225
|
(r, s) => ({
|
|
226
226
|
...r,
|
|
227
227
|
get [s]() {
|
|
228
|
-
return
|
|
228
|
+
return e.context.values.current[s];
|
|
229
229
|
}
|
|
230
230
|
}),
|
|
231
231
|
{}
|
|
@@ -235,21 +235,21 @@ function L(t) {
|
|
|
235
235
|
},
|
|
236
236
|
view: {
|
|
237
237
|
get model() {
|
|
238
|
-
return
|
|
238
|
+
return e.model.current.stateless;
|
|
239
239
|
},
|
|
240
240
|
get validate() {
|
|
241
|
-
return
|
|
241
|
+
return e.model.current.validatable;
|
|
242
242
|
},
|
|
243
243
|
boundary: {
|
|
244
|
-
is(
|
|
245
|
-
return
|
|
244
|
+
is(t) {
|
|
245
|
+
return e.model.current.stateless[E].boundary === t;
|
|
246
246
|
}
|
|
247
247
|
},
|
|
248
248
|
actions: {
|
|
249
|
-
dispatch([
|
|
250
|
-
if (
|
|
249
|
+
dispatch([t, ...n]) {
|
|
250
|
+
if (t == null) return Promise.reject();
|
|
251
251
|
const r = Promise.withResolvers();
|
|
252
|
-
return
|
|
252
|
+
return e.dispatchers.dispatch(t, n, r), r.promise;
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
}
|
|
@@ -257,188 +257,191 @@ function L(t) {
|
|
|
257
257
|
[]
|
|
258
258
|
);
|
|
259
259
|
}
|
|
260
|
-
function
|
|
261
|
-
const
|
|
262
|
-
return
|
|
263
|
-
if (
|
|
264
|
-
return Object.entries(
|
|
260
|
+
function ee(e) {
|
|
261
|
+
const t = B(null);
|
|
262
|
+
return t.current == null && (t.current = e.options.using.actions(e.actions.controller)), f(() => {
|
|
263
|
+
if (t.current)
|
|
264
|
+
return Object.entries(t.current).forEach(([r, s]) => e.dispatchers.attach(r, s)), t.current;
|
|
265
265
|
}, []);
|
|
266
266
|
}
|
|
267
|
-
function N(
|
|
268
|
-
return
|
|
267
|
+
function N(e) {
|
|
268
|
+
return e.startsWith("distributed");
|
|
269
269
|
}
|
|
270
|
-
function
|
|
271
|
-
return
|
|
270
|
+
function Re(e) {
|
|
271
|
+
return e instanceof F;
|
|
272
272
|
}
|
|
273
|
-
function I(
|
|
274
|
-
return
|
|
273
|
+
function I(e) {
|
|
274
|
+
return e instanceof F || e instanceof Error ? e : new Error("Unknown error", { cause: e });
|
|
275
275
|
}
|
|
276
|
-
var
|
|
276
|
+
var v, p;
|
|
277
277
|
class F extends Error {
|
|
278
278
|
constructor(n, r = null) {
|
|
279
279
|
super(String(r));
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
280
|
+
R(this, v);
|
|
281
|
+
R(this, p);
|
|
282
|
+
S(this, v, n), S(this, p, r);
|
|
283
283
|
}
|
|
284
284
|
get type() {
|
|
285
|
-
return
|
|
285
|
+
return x(this, v);
|
|
286
286
|
}
|
|
287
287
|
get message() {
|
|
288
|
-
return
|
|
288
|
+
return x(this, p) || "";
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
|
-
|
|
292
|
-
function
|
|
293
|
-
children:
|
|
291
|
+
v = new WeakMap(), p = new WeakMap();
|
|
292
|
+
function te({
|
|
293
|
+
children: e
|
|
294
294
|
}) {
|
|
295
|
-
return
|
|
295
|
+
return e();
|
|
296
296
|
}
|
|
297
|
-
function
|
|
298
|
-
return (
|
|
299
|
-
if (typeof n != "function") return;
|
|
300
|
-
const o = Symbol("process"),
|
|
297
|
+
function ne(e) {
|
|
298
|
+
return (t, n) => async (r = Promise.withResolvers(), s) => {
|
|
299
|
+
if (typeof n != "function") return void r.resolve();
|
|
300
|
+
const o = Symbol("process"), u = n(...s);
|
|
301
301
|
try {
|
|
302
|
-
if (
|
|
303
|
-
if (typeof
|
|
304
|
-
const i =
|
|
305
|
-
|
|
306
|
-
return;
|
|
302
|
+
if (u == null) return void r.resolve();
|
|
303
|
+
if (typeof u == "function") {
|
|
304
|
+
const i = u(e.model.current, o);
|
|
305
|
+
return e.model.current = b(i, o), e.update.rerender(), void r.resolve();
|
|
307
306
|
}
|
|
308
307
|
for (; ; ) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
308
|
+
if (u instanceof Promise) {
|
|
309
|
+
const a = await u;
|
|
310
|
+
if (a == null) return void r.resolve();
|
|
311
|
+
const y = a(e.model.current, o);
|
|
312
|
+
return e.model.current = b(y, o), e.update.rerender(), void r.resolve();
|
|
313
|
+
}
|
|
314
|
+
const { value: i, done: d } = await u.next();
|
|
315
|
+
if (d) {
|
|
316
|
+
const a = i(e.model.current, o);
|
|
317
|
+
return e.model.current = b(a, o), e.update.rerender(), void r.resolve();
|
|
314
318
|
}
|
|
315
|
-
|
|
316
|
-
t.model.current = d(t.model.current, o), t.update.rerender(), r.resolve();
|
|
319
|
+
e.model.current = i(e.model.current, o), e.update.rerender();
|
|
317
320
|
}
|
|
318
321
|
} catch (i) {
|
|
319
|
-
|
|
322
|
+
b(e.model.current, o), e.update.rerender(), e.unicast.emit(h.Error, r, [I(i)]);
|
|
320
323
|
}
|
|
321
324
|
};
|
|
322
325
|
}
|
|
323
|
-
function
|
|
324
|
-
const
|
|
326
|
+
function re(e) {
|
|
327
|
+
const t = $(), n = f(() => new M(), []), r = ne({ ...e, unicast: n });
|
|
325
328
|
return f(() => ({
|
|
326
329
|
attach(s, o) {
|
|
327
|
-
const
|
|
328
|
-
N(
|
|
330
|
+
const u = String(s);
|
|
331
|
+
N(u) ? t.instance.on(u, r(s, o)) : n.on(u, r(s, o));
|
|
329
332
|
},
|
|
330
|
-
dispatch(s, o,
|
|
333
|
+
dispatch(s, o, u) {
|
|
331
334
|
const i = String(s);
|
|
332
|
-
N(i) ?
|
|
335
|
+
N(i) ? t.instance.emit(i, u, o) : n.emit(i, u, o);
|
|
333
336
|
}
|
|
334
337
|
}), []);
|
|
335
338
|
}
|
|
336
|
-
function
|
|
337
|
-
const
|
|
338
|
-
return f(() => ({ customElement:
|
|
339
|
+
function se() {
|
|
340
|
+
const e = c.useRef(null);
|
|
341
|
+
return f(() => ({ customElement: e }), []);
|
|
339
342
|
}
|
|
340
|
-
function
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}, [
|
|
344
|
-
|
|
345
|
-
]), () =>
|
|
343
|
+
function oe(e) {
|
|
344
|
+
k(() => {
|
|
345
|
+
e.dispatchers.dispatch(h.Derive, []);
|
|
346
|
+
}, [e.options.using.props]), k(() => (e.dispatchers.dispatch(h.Mount, []), e.dispatchers.dispatch(h.Node, [
|
|
347
|
+
e.elements.customElement.current
|
|
348
|
+
]), () => e.dispatchers.dispatch(h.Unmount, [])), []);
|
|
346
349
|
}
|
|
347
|
-
function
|
|
348
|
-
const
|
|
349
|
-
return
|
|
350
|
+
function ue(e) {
|
|
351
|
+
const t = f(() => ({ ...e.options.using.model ?? {}, [E]: { boundary: O.Default } }), []);
|
|
352
|
+
return c.useRef(new U(t, t));
|
|
350
353
|
}
|
|
351
|
-
function
|
|
352
|
-
return
|
|
354
|
+
function ce() {
|
|
355
|
+
return c.useRef(/* @__PURE__ */ new Set());
|
|
353
356
|
}
|
|
354
|
-
function
|
|
355
|
-
const [
|
|
356
|
-
return f(() => ({ hash:
|
|
357
|
+
function ie() {
|
|
358
|
+
const [e, t] = c.useReducer((n) => n + 1, 0);
|
|
359
|
+
return f(() => ({ hash: e, rerender: t }), [e]);
|
|
357
360
|
}
|
|
358
|
-
const
|
|
361
|
+
const ae = {
|
|
359
362
|
elementName: "x-chizu"
|
|
360
|
-
}, J =
|
|
363
|
+
}, J = c.createContext(
|
|
361
364
|
null
|
|
362
365
|
);
|
|
363
|
-
function
|
|
364
|
-
const
|
|
365
|
-
if (!
|
|
366
|
+
function Se() {
|
|
367
|
+
const e = c.useContext(J);
|
|
368
|
+
if (!e)
|
|
366
369
|
throw new Error("useScoped is not being used within a scoped module.");
|
|
367
|
-
return
|
|
370
|
+
return e;
|
|
368
371
|
}
|
|
369
|
-
class
|
|
370
|
-
constructor(
|
|
371
|
-
super(
|
|
372
|
+
class le extends c.Component {
|
|
373
|
+
constructor(t) {
|
|
374
|
+
super(t), this.state = { error: null };
|
|
372
375
|
}
|
|
373
|
-
componentDidCatch(
|
|
376
|
+
componentDidCatch(t) {
|
|
374
377
|
const n = Symbol("process"), r = q(this.props.model.current, n, (s, o) => {
|
|
375
|
-
o.boundary =
|
|
378
|
+
o.boundary = O.Error;
|
|
376
379
|
});
|
|
377
|
-
this.props.model.current = r, this.props.update.rerender(), this.props.dispatchers.dispatch(h.Error, [I(
|
|
380
|
+
this.props.model.current = r, this.props.update.rerender(), this.props.dispatchers.dispatch(h.Error, [I(t)]);
|
|
378
381
|
}
|
|
379
382
|
render() {
|
|
380
|
-
return /* @__PURE__ */ C(
|
|
383
|
+
return /* @__PURE__ */ C(te, { children: this.props.children });
|
|
381
384
|
}
|
|
382
385
|
}
|
|
383
|
-
function
|
|
384
|
-
const
|
|
386
|
+
function de() {
|
|
387
|
+
const e = c.useRef({}), t = c.useRef({});
|
|
385
388
|
function n() {
|
|
386
|
-
|
|
387
|
-
Object.entries(
|
|
389
|
+
t.current = Object.fromEntries(
|
|
390
|
+
Object.entries(e.current).map(([r, s]) => [r, c.use(s)])
|
|
388
391
|
);
|
|
389
392
|
}
|
|
390
|
-
return n(), f(() => ({ use:
|
|
393
|
+
return n(), f(() => ({ use: e, values: t, update: n }), []);
|
|
391
394
|
}
|
|
392
|
-
function
|
|
395
|
+
function fe() {
|
|
393
396
|
return B(0);
|
|
394
397
|
}
|
|
395
|
-
function
|
|
396
|
-
options:
|
|
398
|
+
function he({
|
|
399
|
+
options: e
|
|
397
400
|
}) {
|
|
398
|
-
const
|
|
401
|
+
const t = ie(), n = ce(), r = fe(), s = se(), o = $(), u = de(), i = ue({ options: e }), d = re({
|
|
399
402
|
broadcast: o,
|
|
400
|
-
options:
|
|
401
|
-
update:
|
|
403
|
+
options: e,
|
|
404
|
+
update: t,
|
|
402
405
|
model: i,
|
|
403
406
|
queue: n
|
|
404
|
-
}),
|
|
407
|
+
}), a = L({
|
|
405
408
|
model: i,
|
|
406
|
-
dispatchers:
|
|
407
|
-
context:
|
|
409
|
+
dispatchers: d,
|
|
410
|
+
context: u
|
|
408
411
|
});
|
|
409
|
-
return
|
|
412
|
+
return ee({ options: e, dispatchers: d, actions: a }), oe({ options: e, dispatchers: d, elements: s }), f(() => c.createElement(le, {
|
|
410
413
|
model: i,
|
|
411
|
-
dispatchers:
|
|
412
|
-
update:
|
|
413
|
-
module:
|
|
414
|
+
dispatchers: d,
|
|
415
|
+
update: t,
|
|
416
|
+
module: a.view,
|
|
414
417
|
children() {
|
|
415
|
-
return
|
|
416
|
-
value:
|
|
418
|
+
return c.createElement(J.Provider, {
|
|
419
|
+
value: a.view,
|
|
417
420
|
// eslint-disable-next-line react/no-children-prop
|
|
418
|
-
children:
|
|
421
|
+
children: c.createElement(ae.elementName, {
|
|
419
422
|
ref: s.customElement,
|
|
420
423
|
style: { display: "contents" },
|
|
421
|
-
children:
|
|
424
|
+
children: e.children(a.view)
|
|
422
425
|
})
|
|
423
426
|
});
|
|
424
427
|
}
|
|
425
428
|
}), [
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
+
t.hash,
|
|
430
|
+
w(e.using.props),
|
|
431
|
+
e.passive ? r.current++ : 0
|
|
429
432
|
]);
|
|
430
433
|
}
|
|
431
|
-
function
|
|
432
|
-
return /* @__PURE__ */ C(
|
|
434
|
+
function Pe(e) {
|
|
435
|
+
return /* @__PURE__ */ C(he, { options: e });
|
|
433
436
|
}
|
|
434
437
|
export {
|
|
435
|
-
|
|
436
|
-
|
|
438
|
+
O as Boundary,
|
|
439
|
+
xe as Broadcaster,
|
|
437
440
|
h as Lifecycle,
|
|
438
|
-
|
|
441
|
+
Pe as Scope,
|
|
439
442
|
V as State,
|
|
440
443
|
F as TypedError,
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
+
Re as isTypedError,
|
|
445
|
+
Se as useScoped,
|
|
446
|
+
ge as utils
|
|
444
447
|
};
|
package/dist/chizu.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("react/jsx-runtime"),require("react"),require("eventemitter3"),require("immer"),require("lodash/get"),require("traverse")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react","eventemitter3","immer","lodash/get","traverse"],c):(s=typeof globalThis<"u"?globalThis:s||self,c(s.Chizu={},s.jsxRuntime,s.React,s.EventEmitter3,s.Immer,s.get,s.Traverse))})(this,function(s,c,l,y,
|
|
1
|
+
(function(s,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("react/jsx-runtime"),require("react"),require("eventemitter3"),require("immer"),require("lodash/get"),require("traverse")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react","eventemitter3","immer","lodash/get","traverse"],c):(s=typeof globalThis<"u"?globalThis:s||self,c(s.Chizu={},s.jsxRuntime,s.React,s.EventEmitter3,s.Immer,s.get,s.Traverse))})(this,function(s,c,l,y,k,b,P){"use strict";var be=Object.defineProperty;var W=s=>{throw TypeError(s)};var we=(s,c,l)=>c in s?be(s,c,{enumerable:!0,configurable:!0,writable:!0,value:l}):s[c]=l;var T=(s,c,l)=>we(s,typeof c!="symbol"?c+"":c,l),G=(s,c,l)=>c.has(s)||W("Cannot "+l);var _=(s,c,l)=>(G(s,c,"read from private field"),l?l.call(s):c.get(s)),q=(s,c,l)=>c.has(s)?W("Cannot add the same private member more than once"):c instanceof WeakSet?c.add(s):c.set(s,l),U=(s,c,l,y)=>(G(s,c,"write to private field"),y?y.call(s,l):c.set(s,l),l);var w,E;function K(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:()=>e[r]})}}return t.default=e,Object.freeze(t)}const a=K(l);class A{constructor(t){this.value=t}}class B{static Draft(t){return new A(t)}}T(B,"Op",{Add:1,Remove:2,Update:4,Move:8,Replace:16});var p=(e=>(e.Mount="lifecycle/mount",e.Node="lifecycle/node",e.Derive="lifecycle/derive",e.Error="lifecycle/error",e.Unmount="lifecycle/unmount",e))(p||{}),g=(e=>(e[e.Default=0]="Default",e[e.Error=1]="Error",e))(g||{});function V(e){return new Promise(t=>setTimeout(t,e))}function X(e){return e?!!(e&&typeof e!="symbol"):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}function S(e){return JSON.stringify(e)}const j=Symbol("meta"),Y=Object.freeze(Object.defineProperty({__proto__:null,hash:S,meta:j,pk:X,sleep:V},Symbol.toStringTag,{value:"Module"}));function v(e,t){const r=a.useRef(null),n=a.useRef(null);return a.useMemo(()=>{const o=S(t);if(n.current!==o){n.current=o;const u=e();return r.current=u,u}return r.current},t)}function N(e,t){const r=a.useRef(null);a.useEffect(()=>{const n=S(t);if(r.current!==n)return r.current=n,e()},t)}const z=a.createContext({instance:new y});function H(){return a.useContext(z)}function Z({children:e}){const t=v(()=>({instance:new y}),[]);return c.jsx(z.Provider,{value:t,children:e})}const h={immer:new k.Immer,annotations:Symbol("annotations")};k.enablePatches(),h.immer.setAutoFreeze(!1);class R{constructor(t,r,n=null){T(this,"process");this.value=t,this.operations=r,this.field=n,this.process=null}attach(t){return this.process=t,this}}function L(e,t=[]){return new R(e,t)}class D{constructor(t,r=t){this.stateless=t,this.stateful=r}get validatable(){return $(this.stateful)}}function $(e,t=[]){return new Proxy(e,{get(r,n){switch(n){case"is":return o=>{const u=C(e,t);if(!u)return!1;const i=new Set(u.flatMap(m=>m.operations));return!!(Array.from(i).reduce((m,f)=>m|(f??0),0)&o)};case"pending":return()=>!!C(e,t);case"draft":return()=>{const o=C(e,t);if(!o)return b(e,t);const u=o.flatMap(i=>i.operations).find(i=>i instanceof A);return u?u.value:b(e,t)}}return $(e,[...t,String(n)])}})}function C(e,t){const n=typeof b(e,t)=="object"?t:t.slice(0,-1),o=n.length===0?e:b(e,n),u=(o==null?void 0:o[h.annotations])??[];return u.length>0?u:null}function I(e,t,r){function n(i){return P(i).forEach(function(){if(this.key===h.annotations){this.block();return}this.node instanceof R&&this.update(this.node.value)})}function o(i){return P(i).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node instanceof R){const d=typeof this.node.value=="object",m=[...d?this.path:this.path.slice(0,-1),h.annotations],f=b(e.stateful,m)??[],O=this.node.attach(t);d?this.update({...this.node.value,[h.annotations]:[O,...f]},!0):(this.parent&&(this.parent.node[h.annotations]=[O,...f]),this.update(this.node.value,!0))}})}const u=i=>r(i,i[j]);return new D(n(h.immer.produce(e.stateless,u)),o(h.immer.produce(e.stateful,u)))}function x(e,t){const r=P(e.stateful).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node&&this.node[h.annotations]){const n=this.node[h.annotations];this.update({...this.node,[h.annotations]:n.filter(o=>o.process!==t)},!0)}});return new D(e.stateless,r)}function ee(e){return v(()=>({controller:{get model(){return e.model.current.stateful},queue:[],actions:{annotate(t,r){return L(t,r)},produce(t){return(r,n)=>I(r,n,t)},dispatch([t,...r]){if(t==null)return Promise.reject();const n=Promise.withResolvers();return e.dispatchers.dispatch(t,r,n),n.promise},context(t){const r=Object.keys(t);return e.context.use.current=t,e.context.update(),r.reduce((n,o)=>({...n,get[o](){return e.context.values.current[o]}}),{})}}},view:{get model(){return e.model.current.stateless},get validate(){return e.model.current.validatable},boundary:{is(t){return e.model.current.stateless[j].boundary===t}},actions:{dispatch([t,...r]){if(t==null)return Promise.reject();const n=Promise.withResolvers();return e.dispatchers.dispatch(t,r,n),n.promise}}}}),[])}function te(e){const t=l.useRef(null);return t.current==null&&(t.current=e.options.using.actions(e.actions.controller)),v(()=>{if(t.current)return Object.entries(t.current).forEach(([n,o])=>e.dispatchers.attach(n,o)),t.current},[])}function F(e){return e.startsWith("distributed")}function ne(e){return e instanceof M}function J(e){return e instanceof M||e instanceof Error?e:new Error("Unknown error",{cause:e})}class M extends Error{constructor(r,n=null){super(String(n));q(this,w);q(this,E);U(this,w,r),U(this,E,n)}get type(){return _(this,w)}get message(){return _(this,E)||""}}w=new WeakMap,E=new WeakMap;function re({children:e}){return e()}function se(e){return(t,r)=>async(n=Promise.withResolvers(),o)=>{if(typeof r!="function")return void n.resolve();const u=Symbol("process"),i=r(...o);try{if(i==null)return void n.resolve();if(typeof i=="function"){const d=i(e.model.current,u);return e.model.current=x(d,u),e.update.rerender(),void n.resolve()}for(;;){if(i instanceof Promise){const f=await i;if(f==null)return void n.resolve();const O=f(e.model.current,u);return e.model.current=x(O,u),e.update.rerender(),void n.resolve()}const{value:d,done:m}=await i.next();if(m){const f=d(e.model.current,u);return e.model.current=x(f,u),e.update.rerender(),void n.resolve()}e.model.current=d(e.model.current,u),e.update.rerender()}}catch(d){x(e.model.current,u),e.update.rerender(),e.unicast.emit(p.Error,n,[J(d)])}}}function oe(e){const t=H(),r=v(()=>new y,[]),n=se({...e,unicast:r});return v(()=>({attach(o,u){const i=String(o);F(i)?t.instance.on(i,n(o,u)):r.on(i,n(o,u))},dispatch(o,u,i){const d=String(o);F(d)?t.instance.emit(d,i,u):r.emit(d,i,u)}}),[])}function ue(){const e=a.useRef(null);return v(()=>({customElement:e}),[])}function ce(e){N(()=>{e.dispatchers.dispatch(p.Derive,[])},[e.options.using.props]),N(()=>(e.dispatchers.dispatch(p.Mount,[]),e.dispatchers.dispatch(p.Node,[e.elements.customElement.current]),()=>e.dispatchers.dispatch(p.Unmount,[])),[])}function ie(e){const t=v(()=>({...e.options.using.model??{},[j]:{boundary:g.Default}}),[]);return a.useRef(new D(t,t))}function ae(){return a.useRef(new Set)}function le(){const[e,t]=a.useReducer(r=>r+1,0);return v(()=>({hash:e,rerender:t}),[e])}const de={elementName:"x-chizu"},Q=a.createContext(null);function fe(){const e=a.useContext(Q);if(!e)throw new Error("useScoped is not being used within a scoped module.");return e}class he extends a.Component{constructor(t){super(t),this.state={error:null}}componentDidCatch(t){const r=Symbol("process"),n=I(this.props.model.current,r,(o,u)=>{u.boundary=g.Error});this.props.model.current=n,this.props.update.rerender(),this.props.dispatchers.dispatch(p.Error,[J(t)])}render(){return c.jsx(re,{children:this.props.children})}}function me(){const e=a.useRef({}),t=a.useRef({});function r(){t.current=Object.fromEntries(Object.entries(e.current).map(([n,o])=>[n,a.use(o)]))}return r(),v(()=>({use:e,values:t,update:r}),[])}function ve(){return l.useRef(0)}function pe({options:e}){const t=le(),r=ae(),n=ve(),o=ue(),u=H(),i=me(),d=ie({options:e}),m=oe({broadcast:u,options:e,update:t,model:d,queue:r}),f=ee({model:d,dispatchers:m,context:i});return te({options:e,dispatchers:m,actions:f}),ce({options:e,dispatchers:m,elements:o}),v(()=>a.createElement(he,{model:d,dispatchers:m,update:t,module:f.view,children(){return a.createElement(Q.Provider,{value:f.view,children:a.createElement(de.elementName,{ref:o.customElement,style:{display:"contents"},children:e.children(f.view)})})}}),[t.hash,S(e.using.props),e.passive?n.current++:0])}function ye(e){return c.jsx(pe,{options:e})}s.Boundary=g,s.Broadcaster=Z,s.Lifecycle=p,s.Scope=ye,s.State=B,s.TypedError=M,s.isTypedError=ne,s.useScoped=fe,s.utils=Y,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -17,7 +17,7 @@ export type ActionEvent<M extends ModuleDefinition> = (...args: M["Actions"][num
|
|
|
17
17
|
type ActionEvents<M extends ModuleDefinition> = {
|
|
18
18
|
[K in Head<M["Actions"]>]: (payload: Payload<M["Actions"], K>) => ActionGenerator<M>;
|
|
19
19
|
};
|
|
20
|
-
export type ActionGenerator<M extends ModuleDefinition> = void | ((models: Models<M["Model"]>, process: symbol) => Models<M["Model"]>) | AsyncGenerator<(models: Models<M["Model"]>, process: symbol) => Models<M["Model"]>, (models: Models<M["Model"]>, process: symbol) => Models<M["Model"]>, unknown>;
|
|
20
|
+
export type ActionGenerator<M extends ModuleDefinition> = void | Promise<void> | ((models: Models<M["Model"]>, process: symbol) => Models<M["Model"]>) | Promise<(models: Models<M["Model"]>, process: symbol) => Models<M["Model"]>> | AsyncGenerator<(models: Models<M["Model"]>, process: symbol) => Models<M["Model"]>, (models: Models<M["Model"]>, process: symbol) => Models<M["Model"]>, unknown>;
|
|
21
21
|
export type ControllerDefinition<M extends ModuleDefinition> = (controller: ControllerArgs<M>) => ControllerInstance<M>;
|
|
22
22
|
export type ControllerInstance<M extends ModuleDefinition> = {
|
|
23
23
|
[Lifecycle.Mount]?(): ActionGenerator<M>;
|
|
@@ -3,6 +3,7 @@ import { ModuleDefinition, Task } from '../../../types/index.ts';
|
|
|
3
3
|
import { Head, Tail } from '../types.ts';
|
|
4
4
|
import { UseDispatchHandlerProps } from './types.ts';
|
|
5
5
|
/**
|
|
6
|
+
* @function useDispatcher
|
|
6
7
|
* @param props {UseDispatchHandlerProps<M>}
|
|
7
8
|
* @returns {(name: Head<M["Actions"]>, ƒ: GeneratorFn<M>) => (payload: Tail<M["Actions"]>) => Promise<void>}
|
|
8
9
|
*/
|