chizu 0.2.26 → 0.2.28

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