chizu 0.2.4 → 0.2.6

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 CHANGED
@@ -30,7 +30,7 @@ Controllers are responsible for mutating the state of the view. In the below exa
30
30
  <kbd>Controller</kbd>
31
31
 
32
32
  ```tsx
33
- export default create.controller<Module>((module) => {
33
+ export default (function ProfileController(module) {
34
34
  return {
35
35
  async *[Events.Name](name) {
36
36
  return module.actions.produce((draft) => {
@@ -38,13 +38,13 @@ export default create.controller<Module>((module) => {
38
38
  });
39
39
  },
40
40
  };
41
- });
41
+ } as Controller<Module>);
42
42
  ```
43
43
 
44
44
  <kbd>View</kbd>
45
45
 
46
46
  ```tsx
47
- export default create.view<Module>((module) => {
47
+ export default (function ProfileView(module) {
48
48
  return (
49
49
  <>
50
50
  <p>Hey {module.model.name}</p>
@@ -56,7 +56,7 @@ export default create.view<Module>((module) => {
56
56
  </button>
57
57
  </>
58
58
  );
59
- });
59
+ } as View<Module>);
60
60
  ```
61
61
 
62
62
  Fetching the name from an external source using an `actions.io` causes the controller event (`Events.Name`) and associated view to be invoked twice &ndash; once with a record of mutations to display a pending state, and then again with the model once it's been mutated.
@@ -64,7 +64,7 @@ Fetching the name from an external source using an `actions.io` causes the contr
64
64
  <kbd>Controller</kbd>
65
65
 
66
66
  ```tsx
67
- export default create.controller<Module>((module) => {
67
+ export default (function ProfileController(module) {
68
68
  return {
69
69
  async *[Events.Name]() {
70
70
  yield module.actions.produce((draft) => {
@@ -78,13 +78,13 @@ export default create.controller<Module>((module) => {
78
78
  });
79
79
  },
80
80
  };
81
- });
81
+ } as Controller<Module>);
82
82
  ```
83
83
 
84
84
  <kbd>View</kbd>
85
85
 
86
86
  ```tsx
87
- export default create.view<Module>((module) => {
87
+ export default (function ProfileView(module) {
88
88
  return (
89
89
  <>
90
90
  <p>Hey {module.model.name}</p>
@@ -94,7 +94,7 @@ export default create.view<Module>((module) => {
94
94
  </button>
95
95
  </>
96
96
  );
97
- });
97
+ } as View<Module>);
98
98
  ```
99
99
 
100
100
  <!-- In the above example the name is fetched asynchronously &ndash; however there is no feedback to the user, we can improve that by using the `module.actions.state` and `module.validate` helpers: -->
@@ -102,7 +102,7 @@ export default create.view<Module>((module) => {
102
102
  <kbd>Controller</kbd>
103
103
 
104
104
  ```tsx
105
- export default create.controller<Module>((module) => {
105
+ export default (function ProfileController(module) {
106
106
  return {
107
107
  async *[Events.Name]() {
108
108
  yield module.actions.produce((draft) => {
@@ -115,13 +115,13 @@ export default create.controller<Module>((module) => {
115
115
  });
116
116
  },
117
117
  };
118
- });
118
+ } as Controller<Module>);
119
119
  ```
120
120
 
121
121
  <kbd>View</kbd>
122
122
 
123
123
  ```tsx
124
- export default create.view<Module>((module) => {
124
+ export default (function ProfileView(module) {
125
125
  return (
126
126
  <>
127
127
  <p>Hey {module.model.name}</p>
@@ -136,7 +136,7 @@ export default create.view<Module>((module) => {
136
136
  </button>
137
137
  </>
138
138
  );
139
- });
139
+ } as View<Module>);
140
140
  ```
141
141
 
142
142
  ## Handling Errors
@@ -157,7 +157,7 @@ export const enum Errors {
157
157
  <kbd>Controller</kbd>
158
158
 
159
159
  ```tsx
160
- export default create.controller<Module>((module) => {
160
+ export default (function ProfileController(module) {
161
161
  return {
162
162
  *[Events.Name]() {
163
163
  yield module.actions.produce((draft) => {
@@ -173,7 +173,7 @@ export default create.controller<Module>((module) => {
173
173
  });
174
174
  },
175
175
  };
176
- });
176
+ }) as Controller<Module>;
177
177
  ```
178
178
 
179
179
  However showing a toast message is not always relevant, you may want a more detailed error message such as a user not found message &ndash; although you could introduce another property for such errors in your model, you could mark the property as fallible by giving it a `Maybe` type because it then keeps everything nicely associated with the `name` property rather than creating another property:
@@ -181,7 +181,7 @@ However showing a toast message is not always relevant, you may want a more deta
181
181
  <kbd>Controller</kbd>
182
182
 
183
183
  ```tsx
184
- export default create.controller<Module>((module) => {
184
+ export default (function ProfileView(module) {
185
185
  return {
186
186
  async *[Events.Name]() {
187
187
  yield module.actions.produce((draft) => {
@@ -201,5 +201,5 @@ export default create.controller<Module>((module) => {
201
201
  });
202
202
  },
203
203
  };
204
- });
204
+ } as View<Module>);
205
205
  ```
package/dist/chizu.js CHANGED
@@ -1,54 +1,54 @@
1
- var I = Object.defineProperty;
2
- var D = (t) => {
1
+ var N = Object.defineProperty;
2
+ var k = (t) => {
3
3
  throw TypeError(t);
4
4
  };
5
- var N = (t, e, n) => e in t ? I(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
6
- var y = (t, e, n) => N(t, typeof e != "symbol" ? e + "" : e, n), P = (t, e, n) => e.has(t) || D("Cannot " + n);
7
- var w = (t, e, n) => (P(t, e, "read from private field"), n ? n.call(t) : e.get(t)), b = (t, e, n) => e.has(t) ? D("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, n), E = (t, e, n, r) => (P(t, e, "write to private field"), r ? r.call(t, n) : e.set(t, n), n);
8
- import { jsx as m, Fragment as B } from "react/jsx-runtime";
9
- import k from "eventemitter3";
5
+ var B = (t, e, n) => e in t ? N(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
6
+ var y = (t, e, n) => B(t, typeof e != "symbol" ? e + "" : e, n), j = (t, e, n) => e.has(t) || k("Cannot " + n);
7
+ var w = (t, e, n) => (j(t, e, "read from private field"), n ? n.call(t) : e.get(t)), b = (t, e, n) => e.has(t) ? k("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, n), E = (t, e, n, r) => (j(t, e, "write to private field"), r ? r.call(t, n) : e.set(t, n), n);
8
+ import { jsx as h, Fragment as q } from "react/jsx-runtime";
9
+ import x from "eventemitter3";
10
10
  import * as i from "react";
11
- import { Immer as q } from "immer";
11
+ import { Immer as z } from "immer";
12
12
  import p from "lodash/get";
13
13
  import M from "traverse";
14
- import * as d from "react-router-dom";
15
- const O = i.createContext({
16
- appEmitter: new k()
14
+ import * as f from "react-router-dom";
15
+ const A = i.createContext({
16
+ appEmitter: new x()
17
17
  });
18
- function A() {
19
- return i.useContext(O);
18
+ function U() {
19
+ return i.useContext(A);
20
20
  }
21
- function z(t) {
21
+ function F(t) {
22
22
  return () => {
23
23
  const e = i.useMemo(
24
24
  () => ({
25
- appEmitter: new k()
25
+ appEmitter: new x()
26
26
  }),
27
27
  []
28
28
  );
29
- return /* @__PURE__ */ m(O.Provider, { value: e, children: /* @__PURE__ */ m(t, {}) });
29
+ return /* @__PURE__ */ h(A.Provider, { value: e, children: /* @__PURE__ */ h(t, {}) });
30
30
  };
31
31
  }
32
- class U {
32
+ class _ {
33
33
  constructor(e) {
34
34
  this.value = e;
35
35
  }
36
36
  }
37
- class F {
37
+ class J {
38
38
  static Draft(e) {
39
- return new U(e);
39
+ return new _(e);
40
40
  }
41
41
  }
42
- y(F, "Op", {
42
+ y(J, "Op", {
43
43
  Add: 1,
44
44
  Remove: 2,
45
45
  Update: 4,
46
46
  Move: 8,
47
47
  Replace: 16
48
48
  });
49
- var h = /* @__PURE__ */ ((t) => (t.Mount = "lifecycle/mount", t.Node = "lifecycle/node", t.Derive = "lifecycle/derive", t.Error = "distributed/lifecycle/error", t.Unmount = "lifecycle/unmount", t))(h || {});
49
+ var m = /* @__PURE__ */ ((t) => (t.Mount = "lifecycle/mount", t.Node = "lifecycle/node", t.Derive = "lifecycle/derive", t.Error = "distributed/lifecycle/error", t.Unmount = "lifecycle/unmount", t))(m || {});
50
50
  const l = {
51
- immer: new q(),
51
+ immer: new z(),
52
52
  annotations: Symbol("annotations")
53
53
  };
54
54
  l.immer.setAutoFreeze(!1);
@@ -61,18 +61,18 @@ class S {
61
61
  return this.process = e, this;
62
62
  }
63
63
  }
64
- function J(t, e = []) {
64
+ function T(t, e = []) {
65
65
  return new S(t, e);
66
66
  }
67
- class x {
67
+ class P {
68
68
  constructor(e, n = e) {
69
69
  this.stateless = e, this.stateful = n;
70
70
  }
71
71
  get validatable() {
72
- return _(this.stateful);
72
+ return C(this.stateful);
73
73
  }
74
74
  }
75
- function _(t, e = []) {
75
+ function C(t, e = []) {
76
76
  return new Proxy(t, {
77
77
  get(n, r) {
78
78
  switch (r) {
@@ -84,7 +84,7 @@ function _(t, e = []) {
84
84
  o.flatMap((c) => c.operations)
85
85
  );
86
86
  return !!(Array.from(u).reduce(
87
- (c, f) => c | (f ?? 0),
87
+ (c, d) => c | (d ?? 0),
88
88
  0
89
89
  ) & s);
90
90
  };
@@ -94,11 +94,11 @@ function _(t, e = []) {
94
94
  return () => {
95
95
  const s = R(t, e);
96
96
  if (!s) return p(t, e);
97
- const o = s.flatMap((u) => u.operations).find((u) => u instanceof U);
97
+ const o = s.flatMap((u) => u.operations).find((u) => u instanceof _);
98
98
  return o ? o.value : p(t, e);
99
99
  };
100
100
  }
101
- return _(t, [...e, String(r)]);
101
+ return C(t, [...e, String(r)]);
102
102
  }
103
103
  });
104
104
  }
@@ -106,7 +106,7 @@ function R(t, e) {
106
106
  const r = typeof p(t, e) == "object" ? e : e.slice(0, -1), s = r.length === 0 ? t : p(t, r), o = (s == null ? void 0 : s[l.annotations]) ?? [];
107
107
  return o.length > 0 ? o : null;
108
108
  }
109
- function T(t, e, n) {
109
+ function $(t, e, n) {
110
110
  function r(o) {
111
111
  return M(o).forEach(function() {
112
112
  if (this.key === l.annotations) {
@@ -126,23 +126,23 @@ function T(t, e, n) {
126
126
  const u = typeof this.node.value == "object", a = [
127
127
  ...u ? this.path : this.path.slice(0, -1),
128
128
  l.annotations
129
- ], c = p(t.stateful, a) ?? [], f = this.node.attach(e);
129
+ ], c = p(t.stateful, a) ?? [], d = this.node.attach(e);
130
130
  u ? this.update(
131
131
  {
132
132
  ...this.node.value,
133
- [l.annotations]: [f, ...c]
133
+ [l.annotations]: [d, ...c]
134
134
  },
135
135
  !0
136
- ) : (this.parent && (this.parent.node[l.annotations] = [f, ...c]), this.update(this.node.value, !0));
136
+ ) : (this.parent && (this.parent.node[l.annotations] = [d, ...c]), this.update(this.node.value, !0));
137
137
  }
138
138
  });
139
139
  }
140
- return new x(
140
+ return new P(
141
141
  r(l.immer.produce(t.stateless, n)),
142
142
  s(l.immer.produce(t.stateful, n))
143
143
  );
144
144
  }
145
- function $(t, e) {
145
+ function D(t, e) {
146
146
  const n = M(t.stateful).forEach(function() {
147
147
  if (this.key === l.annotations) {
148
148
  this.block();
@@ -161,7 +161,7 @@ function $(t, e) {
161
161
  );
162
162
  }
163
163
  });
164
- return new x(t.stateless, n);
164
+ return new P(t.stateless, n);
165
165
  }
166
166
  function Q(t) {
167
167
  return i.useMemo(
@@ -178,14 +178,15 @@ function Q(t) {
178
178
  attributes: t.options.props,
179
179
  actions: {
180
180
  annotate(e, n) {
181
- return J(e, n);
181
+ return T(e, n);
182
182
  },
183
183
  produce(e) {
184
- return (n, r) => T(n, r, e);
184
+ return (n, r) => $(n, r, e);
185
185
  },
186
186
  dispatch([e, ...n]) {
187
+ if (e == null) return Promise.reject();
187
188
  const r = Promise.withResolvers();
188
- return e && t.dispatchers.dispatch(e, n, r), r.promise;
189
+ return t.dispatchers.dispatch(e, n, r), r.promise;
189
190
  }
190
191
  }
191
192
  },
@@ -200,8 +201,9 @@ function Q(t) {
200
201
  attributes: t.options.props,
201
202
  actions: {
202
203
  dispatch([e, ...n]) {
204
+ if (e == null) return Promise.reject();
203
205
  const r = Promise.withResolvers();
204
- return e && t.dispatchers.dispatch(e, n, r), r.promise;
206
+ return t.dispatchers.dispatch(e, n, r), r.promise;
205
207
  }
206
208
  }
207
209
  }
@@ -220,20 +222,24 @@ function G(t) {
220
222
  return (e, n) => async (r = Promise.withResolvers(), s) => {
221
223
  if (typeof n != "function") return;
222
224
  const o = Symbol("process"), u = n(...s);
225
+ if (typeof u == "function") {
226
+ const a = u(t.model.current, o);
227
+ return t.model.current = D(a, o), t.update.rerender(), void r.resolve();
228
+ }
223
229
  for (; ; ) {
224
230
  const { value: a, done: c } = await u.next();
225
231
  if (c) {
226
- const C = a(t.model.current, o);
227
- t.model.current = $(C, o), t.update.rerender();
232
+ const I = a(t.model.current, o);
233
+ t.model.current = D(I, o), t.update.rerender();
228
234
  break;
229
235
  }
230
- const f = a;
231
- t.model.current = f(t.model.current, o), t.update.rerender();
236
+ const d = a;
237
+ t.model.current = d(t.model.current, o), t.update.rerender();
232
238
  }
233
239
  r.resolve();
234
240
  };
235
241
  }
236
- function j(t) {
242
+ function O(t) {
237
243
  return t.startsWith("distributed");
238
244
  }
239
245
  var v, g;
@@ -253,17 +259,17 @@ class H extends Error {
253
259
  }
254
260
  v = new WeakMap(), g = new WeakMap();
255
261
  function K(t) {
256
- const e = A(), n = G(t);
262
+ const e = U(), n = G(t);
257
263
  return i.useMemo(() => {
258
- const r = new k(), s = e.appEmitter;
264
+ const r = new x(), s = e.appEmitter;
259
265
  return {
260
266
  attach(o, u) {
261
267
  const a = String(o);
262
- j(a) ? s.on(a, n(o, u)) : r.on(a, n(o, u));
268
+ O(a) ? s.on(a, n(o, u)) : r.on(a, n(o, u));
263
269
  },
264
270
  dispatch(o, u, a) {
265
271
  const c = String(o);
266
- j(c) ? s.emit(c, a, u) : r.emit(c, a, u);
272
+ O(c) ? s.emit(c, a, u) : r.emit(c, a, u);
267
273
  }
268
274
  };
269
275
  }, []);
@@ -276,7 +282,7 @@ function V(t) {
276
282
  var n, r, s, o;
277
283
  const e = i.useRef(!1);
278
284
  i.useLayoutEffect(() => {
279
- t.dispatchers.dispatch(h.Derive, [
285
+ t.dispatchers.dispatch(m.Derive, [
280
286
  t.options.props,
281
287
  t.router.current
282
288
  ]);
@@ -287,14 +293,14 @@ function V(t) {
287
293
  (o = (s = t.router.current) == null ? void 0 : s.search) == null ? void 0 : o[0]
288
294
  ]), i.useLayoutEffect(() => {
289
295
  if (!e.current)
290
- return e.current = !0, t.dispatchers.dispatch(h.Mount, []), t.dispatchers.dispatch(h.Node, [
296
+ return e.current = !0, t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
291
297
  t.elements.customElement.current
292
- ]), () => t.dispatchers.dispatch(h.Unmount, []);
298
+ ]), () => t.dispatchers.dispatch(m.Unmount, []);
293
299
  }, []);
294
300
  }
295
301
  function X(t) {
296
302
  const e = i.useMemo(() => t.options.model ?? {}, []);
297
- return i.useRef(new x(e, e));
303
+ return i.useRef(new P(e, e));
298
304
  }
299
305
  function Y() {
300
306
  return i.useRef(/* @__PURE__ */ new Set());
@@ -303,15 +309,15 @@ function Z() {
303
309
  return i.useRef(null);
304
310
  }
305
311
  function tt({ using: t, children: e }) {
306
- return d.useInRouterContext() ? /* @__PURE__ */ m(et, { using: t, children: e }) : e();
312
+ return f.useInRouterContext() ? /* @__PURE__ */ h(et, { using: t, children: e }) : e();
307
313
  }
308
314
  function et({ using: t, children: e }) {
309
315
  return t.current = {
310
- navigate: d.useNavigate(),
311
- location: d.useLocation(),
312
- params: d.useParams(),
313
- search: d.useSearchParams()
314
- }, /* @__PURE__ */ m(B, { children: e() });
316
+ navigate: f.useNavigate(),
317
+ location: f.useLocation(),
318
+ params: f.useParams(),
319
+ search: f.useSearchParams()
320
+ }, /* @__PURE__ */ h(q, { children: e() });
315
321
  }
316
322
  function nt() {
317
323
  const [t, e] = i.useReducer((n) => n + 1, 0);
@@ -320,10 +326,11 @@ function nt() {
320
326
  function rt({
321
327
  options: t
322
328
  }) {
323
- const e = A(), n = nt(), r = Y(), s = L(), o = Z(), u = X({ options: t }), a = K({ app: e, options: t, update: n, model: u, queue: r }), c = Q({ app: e, options: t, model: u, dispatchers: a, router: o });
329
+ const e = U(), n = nt(), r = Y(), s = L(), o = Z(), u = X({ options: t }), a = K({ app: e, options: t, update: n, model: u, queue: r }), c = Q({ app: e, options: t, model: u, dispatchers: a, router: o });
324
330
  return W({ options: t, dispatchers: a, actions: c }), V({ options: t, dispatchers: a, elements: s, router: o }), i.useMemo(() => i.createElement(t.name, {
325
331
  ref: s.customElement,
326
- children: /* @__PURE__ */ m(tt, { using: o, children: () => t.view(c.view) })
332
+ style: { display: "contents" },
333
+ children: /* @__PURE__ */ h(tt, { using: o, children: () => t.view(c.view) })
327
334
  }), [n.hash]);
328
335
  }
329
336
  function ot(t) {
@@ -347,16 +354,16 @@ function ut(t) {
347
354
  function it(t) {
348
355
  return t instanceof H;
349
356
  }
350
- const mt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
357
+ const ht = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
351
358
  __proto__: null,
352
359
  isEventError: it,
353
360
  pk: ut,
354
361
  sleep: st
355
- }, Symbol.toStringTag, { value: "Module" })), pt = { app: z, module: ot };
362
+ }, Symbol.toStringTag, { value: "Module" })), pt = { app: F, module: ot };
356
363
  export {
357
364
  H as EventError,
358
- h as Lifecycle,
359
- F as State,
365
+ m as Lifecycle,
366
+ J as State,
360
367
  pt as create,
361
- mt as utils
368
+ ht as utils
362
369
  };
@@ -1 +1 @@
1
- (function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("react/jsx-runtime"),require("eventemitter3"),require("react"),require("immer"),require("lodash/get"),require("traverse"),require("react-router-dom")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","eventemitter3","react","immer","lodash/get","traverse","react-router-dom"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s.Chizu={},s.jsxRuntime,s.EventEmitter3,s.React,s.Immer,s.get,s.Traverse,s.ReactRouterDOM))})(this,function(s,o,l,w,z,v,E,B){"use strict";var de=Object.defineProperty;var N=s=>{throw TypeError(s)};var fe=(s,o,l)=>o in s?de(s,o,{enumerable:!0,configurable:!0,writable:!0,value:l}):s[o]=l;var j=(s,o,l)=>fe(s,typeof o!="symbol"?o+"":o,l),U=(s,o,l)=>o.has(s)||N("Cannot "+l);var O=(s,o,l)=>(U(s,o,"read from private field"),l?l.call(s):o.get(s)),k=(s,o,l)=>o.has(s)?N("Cannot add the same private member more than once"):o instanceof WeakSet?o.add(s):o.set(s,l),D=(s,o,l,w)=>(U(s,o,"write to private field"),w?w.call(s,l):o.set(s,l),l);var g,b;function P(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const a=P(w),y=P(B),_=a.createContext({appEmitter:new l});function x(){return a.useContext(_)}function F(e){return()=>{const t=a.useMemo(()=>({appEmitter:new l}),[]);return o.jsx(_.Provider,{value:t,children:o.jsx(e,{})})}}class q{constructor(t){this.value=t}}class T{static Draft(t){return new q(t)}}j(T,"Op",{Add:1,Remove:2,Update:4,Move:8,Replace:16});var m=(e=>(e.Mount="lifecycle/mount",e.Node="lifecycle/node",e.Derive="lifecycle/derive",e.Error="distributed/lifecycle/error",e.Unmount="lifecycle/unmount",e))(m||{});const h={immer:new z.Immer,annotations:Symbol("annotations")};h.immer.setAutoFreeze(!1);class M{constructor(t,n,r=null){j(this,"process");this.value=t,this.operations=n,this.field=r,this.process=null}attach(t){return this.process=t,this}}function J(e,t=[]){return new M(e,t)}class S{constructor(t,n=t){this.stateless=t,this.stateful=n}get validatable(){return A(this.stateful)}}function A(e,t=[]){return new Proxy(e,{get(n,r){switch(r){case"is":return i=>{const u=R(e,t);if(!u)return!1;const c=new Set(u.flatMap(f=>f.operations));return!!(Array.from(c).reduce((f,p)=>f|(p??0),0)&i)};case"pending":return()=>!!R(e,t);case"draft":return()=>{const i=R(e,t);if(!i)return v(e,t);const u=i.flatMap(c=>c.operations).find(c=>c instanceof q);return u?u.value:v(e,t)}}return A(e,[...t,String(r)])}})}function R(e,t){const r=typeof v(e,t)=="object"?t:t.slice(0,-1),i=r.length===0?e:v(e,r),u=(i==null?void 0:i[h.annotations])??[];return u.length>0?u:null}function $(e,t,n){function r(u){return E(u).forEach(function(){if(this.key===h.annotations){this.block();return}this.node instanceof M&&this.update(this.node.value)})}function i(u){return E(u).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node instanceof M){const c=typeof this.node.value=="object",d=[...c?this.path:this.path.slice(0,-1),h.annotations],f=v(e.stateful,d)??[],p=this.node.attach(t);c?this.update({...this.node.value,[h.annotations]:[p,...f]},!0):(this.parent&&(this.parent.node[h.annotations]=[p,...f]),this.update(this.node.value,!0))}})}return new S(r(h.immer.produce(e.stateless,n)),i(h.immer.produce(e.stateful,n)))}function L(e,t){const n=E(e.stateful).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node&&this.node[h.annotations]){const r=this.node[h.annotations];this.update({...this.node,[h.annotations]:r.filter(i=>i.process!==t)},!0)}});return new S(e.stateless,n)}function Q(e){return a.useMemo(()=>({controller:{get model(){return e.model.current.stateful},get router(){return e.router.current},queue:[],handlers:e.options.props,attributes:e.options.props,actions:{annotate(t,n){return J(t,n)},produce(t){return(n,r)=>$(n,r,t)},dispatch([t,...n]){const r=Promise.withResolvers();return t&&e.dispatchers.dispatch(t,n,r),r.promise}}},view:{get model(){return e.model.current.stateless},get validate(){return e.model.current.validatable},handlers:e.options.props,attributes:e.options.props,actions:{dispatch([t,...n]){const r=Promise.withResolvers();return t&&e.dispatchers.dispatch(t,n,r),r.promise}}}}),[])}function W(e){return a.useMemo(()=>{var r,i;const t=(i=(r=e.options).controller)==null?void 0:i.call(r,e.actions.controller);return t?(Object.entries(t).forEach(([u,c])=>e.dispatchers.attach(u,c)),t):void 0},[])}function G(e){return(t,n)=>async(r=Promise.withResolvers(),i)=>{if(typeof n!="function")return;const u=Symbol("process"),c=n(...i);for(;;){const{value:d,done:f}=await c.next();if(f){const le=d(e.model.current,u);e.model.current=L(le,u),e.update.rerender();break}const p=d;e.model.current=p(e.model.current,u),e.update.rerender()}r.resolve()}}function C(e){return e.startsWith("distributed")}class I extends Error{constructor(n,r=null){super(String(r));k(this,g);k(this,b);D(this,g,n),D(this,b,r)}get type(){return O(this,g)}get message(){return O(this,b)||""}}g=new WeakMap,b=new WeakMap;function H(e){const t=x(),n=G(e);return a.useMemo(()=>{const r=new l,i=t.appEmitter;return{attach(u,c){const d=String(u);C(d)?i.on(d,n(u,c)):r.on(d,n(u,c))},dispatch(u,c,d){const f=String(u);C(f)?i.emit(f,d,c):r.emit(f,d,c)}}},[])}function K(){const e=a.useRef(null);return a.useMemo(()=>({customElement:e}),[])}function V(e){var n,r,i,u;const t=a.useRef(!1);a.useLayoutEffect(()=>{e.dispatchers.dispatch(m.Derive,[e.options.props,e.router.current])},[e.options.props,(n=e.router.current)==null?void 0:n.location,(r=e.router.current)==null?void 0:r.params,(u=(i=e.router.current)==null?void 0:i.search)==null?void 0:u[0]]),a.useLayoutEffect(()=>{if(!t.current)return t.current=!0,e.dispatchers.dispatch(m.Mount,[]),e.dispatchers.dispatch(m.Node,[e.elements.customElement.current]),()=>e.dispatchers.dispatch(m.Unmount,[])},[])}function X(e){const t=a.useMemo(()=>e.options.model??{},[]);return a.useRef(new S(t,t))}function Y(){return a.useRef(new Set)}function Z(){return a.useRef(null)}function ee({using:e,children:t}){return y.useInRouterContext()?o.jsx(te,{using:e,children:t}):t()}function te({using:e,children:t}){return e.current={navigate:y.useNavigate(),location:y.useLocation(),params:y.useParams(),search:y.useSearchParams()},o.jsx(o.Fragment,{children:t()})}function ne(){const[e,t]=a.useReducer(n=>n+1,0);return a.useMemo(()=>({hash:e,rerender:t}),[e])}function re({options:e}){const t=x(),n=ne(),r=Y(),i=K(),u=Z(),c=X({options:e}),d=H({app:t,options:e,update:n,model:c,queue:r}),f=Q({app:t,options:e,model:c,dispatchers:d,router:u});return W({options:e,dispatchers:d,actions:f}),V({options:e,dispatchers:d,elements:i,router:u}),a.useMemo(()=>a.createElement(e.name,{ref:i.customElement,children:o.jsx(ee,{using:u,children:()=>e.view(f.view)})}),[n.hash])}function se(e){return t=>a.memo(n=>re({options:{...t,name:e.join(""),props:n}}),(n,r)=>JSON.stringify(n)===JSON.stringify(r))}function oe(e){return new Promise(t=>setTimeout(t,e))}function ue(e){return e?!!(e&&typeof e!="symbol"):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}function ie(e){return e instanceof I}const ce=Object.freeze(Object.defineProperty({__proto__:null,isEventError:ie,pk:ue,sleep:oe},Symbol.toStringTag,{value:"Module"})),ae={app:F,module:se};s.EventError=I,s.Lifecycle=m,s.State=T,s.create=ae,s.utils=ce,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
1
+ (function(o,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("react/jsx-runtime"),require("eventemitter3"),require("react"),require("immer"),require("lodash/get"),require("traverse"),require("react-router-dom")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","eventemitter3","react","immer","lodash/get","traverse","react-router-dom"],u):(o=typeof globalThis<"u"?globalThis:o||self,u(o.Chizu={},o.jsxRuntime,o.EventEmitter3,o.React,o.Immer,o.get,o.Traverse,o.ReactRouterDOM))})(this,function(o,u,l,w,B,v,E,F){"use strict";var de=Object.defineProperty;var U=o=>{throw TypeError(o)};var fe=(o,u,l)=>u in o?de(o,u,{enumerable:!0,configurable:!0,writable:!0,value:l}):o[u]=l;var j=(o,u,l)=>fe(o,typeof u!="symbol"?u+"":u,l),z=(o,u,l)=>u.has(o)||U("Cannot "+l);var O=(o,u,l)=>(z(o,u,"read from private field"),l?l.call(o):u.get(o)),P=(o,u,l)=>u.has(o)?U("Cannot add the same private member more than once"):u instanceof WeakSet?u.add(o):u.set(o,l),D=(o,u,l,w)=>(z(o,u,"write to private field"),w?w.call(o,l):u.set(o,l),l);var g,b;function k(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const a=k(w),y=k(F),_=a.createContext({appEmitter:new l});function x(){return a.useContext(_)}function J(e){return()=>{const t=a.useMemo(()=>({appEmitter:new l}),[]);return u.jsx(_.Provider,{value:t,children:u.jsx(e,{})})}}class q{constructor(t){this.value=t}}class T{static Draft(t){return new q(t)}}j(T,"Op",{Add:1,Remove:2,Update:4,Move:8,Replace:16});var m=(e=>(e.Mount="lifecycle/mount",e.Node="lifecycle/node",e.Derive="lifecycle/derive",e.Error="distributed/lifecycle/error",e.Unmount="lifecycle/unmount",e))(m||{});const h={immer:new B.Immer,annotations:Symbol("annotations")};h.immer.setAutoFreeze(!1);class M{constructor(t,n,r=null){j(this,"process");this.value=t,this.operations=n,this.field=r,this.process=null}attach(t){return this.process=t,this}}function $(e,t=[]){return new M(e,t)}class S{constructor(t,n=t){this.stateless=t,this.stateful=n}get validatable(){return A(this.stateful)}}function A(e,t=[]){return new Proxy(e,{get(n,r){switch(r){case"is":return i=>{const s=R(e,t);if(!s)return!1;const c=new Set(s.flatMap(f=>f.operations));return!!(Array.from(c).reduce((f,p)=>f|(p??0),0)&i)};case"pending":return()=>!!R(e,t);case"draft":return()=>{const i=R(e,t);if(!i)return v(e,t);const s=i.flatMap(c=>c.operations).find(c=>c instanceof q);return s?s.value:v(e,t)}}return A(e,[...t,String(r)])}})}function R(e,t){const r=typeof v(e,t)=="object"?t:t.slice(0,-1),i=r.length===0?e:v(e,r),s=(i==null?void 0:i[h.annotations])??[];return s.length>0?s:null}function L(e,t,n){function r(s){return E(s).forEach(function(){if(this.key===h.annotations){this.block();return}this.node instanceof M&&this.update(this.node.value)})}function i(s){return E(s).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node instanceof M){const c=typeof this.node.value=="object",d=[...c?this.path:this.path.slice(0,-1),h.annotations],f=v(e.stateful,d)??[],p=this.node.attach(t);c?this.update({...this.node.value,[h.annotations]:[p,...f]},!0):(this.parent&&(this.parent.node[h.annotations]=[p,...f]),this.update(this.node.value,!0))}})}return new S(r(h.immer.produce(e.stateless,n)),i(h.immer.produce(e.stateful,n)))}function C(e,t){const n=E(e.stateful).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node&&this.node[h.annotations]){const r=this.node[h.annotations];this.update({...this.node,[h.annotations]:r.filter(i=>i.process!==t)},!0)}});return new S(e.stateless,n)}function Q(e){return a.useMemo(()=>({controller:{get model(){return e.model.current.stateful},get router(){return e.router.current},queue:[],handlers:e.options.props,attributes:e.options.props,actions:{annotate(t,n){return $(t,n)},produce(t){return(n,r)=>L(n,r,t)},dispatch([t,...n]){if(t==null)return Promise.reject();const r=Promise.withResolvers();return e.dispatchers.dispatch(t,n,r),r.promise}}},view:{get model(){return e.model.current.stateless},get validate(){return e.model.current.validatable},handlers:e.options.props,attributes:e.options.props,actions:{dispatch([t,...n]){if(t==null)return Promise.reject();const r=Promise.withResolvers();return e.dispatchers.dispatch(t,n,r),r.promise}}}}),[])}function W(e){return a.useMemo(()=>{var r,i;const t=(i=(r=e.options).controller)==null?void 0:i.call(r,e.actions.controller);return t?(Object.entries(t).forEach(([s,c])=>e.dispatchers.attach(s,c)),t):void 0},[])}function G(e){return(t,n)=>async(r=Promise.withResolvers(),i)=>{if(typeof n!="function")return;const s=Symbol("process"),c=n(...i);if(typeof c=="function"){const d=c(e.model.current,s);return e.model.current=C(d,s),e.update.rerender(),void r.resolve()}for(;;){const{value:d,done:f}=await c.next();if(f){const le=d(e.model.current,s);e.model.current=C(le,s),e.update.rerender();break}const p=d;e.model.current=p(e.model.current,s),e.update.rerender()}r.resolve()}}function I(e){return e.startsWith("distributed")}class N extends Error{constructor(n,r=null){super(String(r));P(this,g);P(this,b);D(this,g,n),D(this,b,r)}get type(){return O(this,g)}get message(){return O(this,b)||""}}g=new WeakMap,b=new WeakMap;function H(e){const t=x(),n=G(e);return a.useMemo(()=>{const r=new l,i=t.appEmitter;return{attach(s,c){const d=String(s);I(d)?i.on(d,n(s,c)):r.on(d,n(s,c))},dispatch(s,c,d){const f=String(s);I(f)?i.emit(f,d,c):r.emit(f,d,c)}}},[])}function K(){const e=a.useRef(null);return a.useMemo(()=>({customElement:e}),[])}function V(e){var n,r,i,s;const t=a.useRef(!1);a.useLayoutEffect(()=>{e.dispatchers.dispatch(m.Derive,[e.options.props,e.router.current])},[e.options.props,(n=e.router.current)==null?void 0:n.location,(r=e.router.current)==null?void 0:r.params,(s=(i=e.router.current)==null?void 0:i.search)==null?void 0:s[0]]),a.useLayoutEffect(()=>{if(!t.current)return t.current=!0,e.dispatchers.dispatch(m.Mount,[]),e.dispatchers.dispatch(m.Node,[e.elements.customElement.current]),()=>e.dispatchers.dispatch(m.Unmount,[])},[])}function X(e){const t=a.useMemo(()=>e.options.model??{},[]);return a.useRef(new S(t,t))}function Y(){return a.useRef(new Set)}function Z(){return a.useRef(null)}function ee({using:e,children:t}){return y.useInRouterContext()?u.jsx(te,{using:e,children:t}):t()}function te({using:e,children:t}){return e.current={navigate:y.useNavigate(),location:y.useLocation(),params:y.useParams(),search:y.useSearchParams()},u.jsx(u.Fragment,{children:t()})}function ne(){const[e,t]=a.useReducer(n=>n+1,0);return a.useMemo(()=>({hash:e,rerender:t}),[e])}function re({options:e}){const t=x(),n=ne(),r=Y(),i=K(),s=Z(),c=X({options:e}),d=H({app:t,options:e,update:n,model:c,queue:r}),f=Q({app:t,options:e,model:c,dispatchers:d,router:s});return W({options:e,dispatchers:d,actions:f}),V({options:e,dispatchers:d,elements:i,router:s}),a.useMemo(()=>a.createElement(e.name,{ref:i.customElement,style:{display:"contents"},children:u.jsx(ee,{using:s,children:()=>e.view(f.view)})}),[n.hash])}function oe(e){return t=>a.memo(n=>re({options:{...t,name:e.join(""),props:n}}),(n,r)=>JSON.stringify(n)===JSON.stringify(r))}function se(e){return new Promise(t=>setTimeout(t,e))}function ue(e){return e?!!(e&&typeof e!="symbol"):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}function ie(e){return e instanceof N}const ce=Object.freeze(Object.defineProperty({__proto__:null,isEventError:ie,pk:ue,sleep:se},Symbol.toStringTag,{value:"Module"})),ae={app:J,module:oe};o.EventError=N,o.Lifecycle=m,o.State=T,o.create=ae,o.utils=ce,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
@@ -20,7 +20,7 @@ export type ActionEvent<M extends ModuleDefinition> = (...args: M["Actions"][num
20
20
  type ActionEvents<M extends ModuleDefinition> = {
21
21
  [K in Head<M["Actions"]>]: (payload: Payload<M["Actions"], K>) => ActionGenerator<M>;
22
22
  };
23
- export type ActionGenerator<M extends ModuleDefinition> = AsyncGenerator<(models: Models<M["Model"]>, process: Symbol) => Models<M["Model"]>, (models: Models<M["Model"]>, process: Symbol) => Models<M["Model"]>, unknown>;
23
+ export type ActionGenerator<M extends ModuleDefinition> = ((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>;
24
24
  export type ControllerDefinition<M extends ModuleDefinition> = (controller: ControllerArgs<M>) => ControllerInstance<M>;
25
25
  export type ControllerInstance<M extends ModuleDefinition> = {
26
26
  [Lifecycle.Mount]?(): ActionGenerator<M>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chizu",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "type": "module",
5
5
  "main": "./dist/chizu.js",
6
6
  "types": "./dist/index.d.ts",