chizu 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/chizu.js CHANGED
@@ -3,56 +3,50 @@ var D = (t) => {
3
3
  throw TypeError(t);
4
4
  };
5
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 b = (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)), y = (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 h, Fragment as B } from "react/jsx-runtime";
9
- import x from "eventemitter3";
10
- import * as c from "react";
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";
10
+ import * as i from "react";
11
11
  import { Immer as q } from "immer";
12
12
  import p from "lodash/get";
13
13
  import M from "traverse";
14
- import * as f from "react-router-dom";
15
- const O = c.createContext({
16
- appEmitter: new x()
14
+ import * as d from "react-router-dom";
15
+ const O = i.createContext({
16
+ appEmitter: new k()
17
17
  });
18
- function _() {
19
- return c.useContext(O);
18
+ function A() {
19
+ return i.useContext(O);
20
20
  }
21
21
  function z(t) {
22
22
  return () => {
23
- const e = c.useMemo(
23
+ const e = i.useMemo(
24
24
  () => ({
25
- appEmitter: new x()
25
+ appEmitter: new k()
26
26
  }),
27
27
  []
28
28
  );
29
- return /* @__PURE__ */ h(O.Provider, { value: e, children: /* @__PURE__ */ h(t, {}) });
29
+ return /* @__PURE__ */ m(O.Provider, { value: e, children: /* @__PURE__ */ m(t, {}) });
30
30
  };
31
31
  }
32
- function F(t) {
33
- return t;
34
- }
35
- function J(t) {
36
- return t;
37
- }
38
- class A {
32
+ class U {
39
33
  constructor(e) {
40
34
  this.value = e;
41
35
  }
42
36
  }
43
- class T {
37
+ class F {
44
38
  static Draft(e) {
45
- return new A(e);
39
+ return new U(e);
46
40
  }
47
41
  }
48
- b(T, "Op", {
42
+ y(F, "Op", {
49
43
  Add: 1,
50
44
  Remove: 2,
51
45
  Update: 4,
52
46
  Move: 8,
53
47
  Replace: 16
54
48
  });
55
- 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 || {});
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 || {});
56
50
  const l = {
57
51
  immer: new q(),
58
52
  annotations: Symbol("annotations")
@@ -60,25 +54,25 @@ const l = {
60
54
  l.immer.setAutoFreeze(!1);
61
55
  class S {
62
56
  constructor(e, n, r = null) {
63
- b(this, "process");
57
+ y(this, "process");
64
58
  this.value = e, this.operations = n, this.field = r, this.process = null;
65
59
  }
66
60
  attach(e) {
67
61
  return this.process = e, this;
68
62
  }
69
63
  }
70
- function $(t, e = []) {
64
+ function J(t, e = []) {
71
65
  return new S(t, e);
72
66
  }
73
- class k {
67
+ class x {
74
68
  constructor(e, n = e) {
75
69
  this.stateless = e, this.stateful = n;
76
70
  }
77
71
  get validatable() {
78
- return C(this.stateful);
72
+ return _(this.stateful);
79
73
  }
80
74
  }
81
- function C(t, e = []) {
75
+ function _(t, e = []) {
82
76
  return new Proxy(t, {
83
77
  get(n, r) {
84
78
  switch (r) {
@@ -87,10 +81,10 @@ function C(t, e = []) {
87
81
  const o = R(t, e);
88
82
  if (!o) return !1;
89
83
  const u = new Set(
90
- o.flatMap((i) => i.operations)
84
+ o.flatMap((c) => c.operations)
91
85
  );
92
86
  return !!(Array.from(u).reduce(
93
- (i, d) => i | (d ?? 0),
87
+ (c, f) => c | (f ?? 0),
94
88
  0
95
89
  ) & s);
96
90
  };
@@ -100,11 +94,11 @@ function C(t, e = []) {
100
94
  return () => {
101
95
  const s = R(t, e);
102
96
  if (!s) return p(t, e);
103
- const o = s.flatMap((u) => u.operations).find((u) => u instanceof A);
97
+ const o = s.flatMap((u) => u.operations).find((u) => u instanceof U);
104
98
  return o ? o.value : p(t, e);
105
99
  };
106
100
  }
107
- return C(t, [...e, String(r)]);
101
+ return _(t, [...e, String(r)]);
108
102
  }
109
103
  });
110
104
  }
@@ -112,7 +106,7 @@ function R(t, e) {
112
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]) ?? [];
113
107
  return o.length > 0 ? o : null;
114
108
  }
115
- function L(t, e, n) {
109
+ function T(t, e, n) {
116
110
  function r(o) {
117
111
  return M(o).forEach(function() {
118
112
  if (this.key === l.annotations) {
@@ -132,23 +126,23 @@ function L(t, e, n) {
132
126
  const u = typeof this.node.value == "object", a = [
133
127
  ...u ? this.path : this.path.slice(0, -1),
134
128
  l.annotations
135
- ], i = p(t.stateful, a) ?? [], d = this.node.attach(e);
129
+ ], c = p(t.stateful, a) ?? [], f = this.node.attach(e);
136
130
  u ? this.update(
137
131
  {
138
132
  ...this.node.value,
139
- [l.annotations]: [d, ...i]
133
+ [l.annotations]: [f, ...c]
140
134
  },
141
135
  !0
142
- ) : (this.parent && (this.parent.node[l.annotations] = [d, ...i]), this.update(this.node.value, !0));
136
+ ) : (this.parent && (this.parent.node[l.annotations] = [f, ...c]), this.update(this.node.value, !0));
143
137
  }
144
138
  });
145
139
  }
146
- return new k(
140
+ return new x(
147
141
  r(l.immer.produce(t.stateless, n)),
148
142
  s(l.immer.produce(t.stateful, n))
149
143
  );
150
144
  }
151
- function Q(t, e) {
145
+ function $(t, e) {
152
146
  const n = M(t.stateful).forEach(function() {
153
147
  if (this.key === l.annotations) {
154
148
  this.block();
@@ -167,10 +161,10 @@ function Q(t, e) {
167
161
  );
168
162
  }
169
163
  });
170
- return new k(t.stateless, n);
164
+ return new x(t.stateless, n);
171
165
  }
172
- function W(t) {
173
- return c.useMemo(
166
+ function Q(t) {
167
+ return i.useMemo(
174
168
  () => ({
175
169
  controller: {
176
170
  get model() {
@@ -184,14 +178,14 @@ function W(t) {
184
178
  attributes: t.options.props,
185
179
  actions: {
186
180
  annotate(e, n) {
187
- return $(e, n);
181
+ return J(e, n);
188
182
  },
189
183
  produce(e) {
190
- return (n, r) => L(n, r, e);
184
+ return (n, r) => T(n, r, e);
191
185
  },
192
186
  dispatch([e, ...n]) {
193
187
  const r = Promise.withResolvers();
194
- return t.dispatchers.dispatch(e, n, r), r.promise;
188
+ return e && t.dispatchers.dispatch(e, n, r), r.promise;
195
189
  }
196
190
  }
197
191
  },
@@ -207,7 +201,7 @@ function W(t) {
207
201
  actions: {
208
202
  dispatch([e, ...n]) {
209
203
  const r = Promise.withResolvers();
210
- return t.dispatchers.dispatch(e, n, r), r.promise;
204
+ return e && t.dispatchers.dispatch(e, n, r), r.promise;
211
205
  }
212
206
  }
213
207
  }
@@ -215,26 +209,26 @@ function W(t) {
215
209
  []
216
210
  );
217
211
  }
218
- function G(t) {
219
- return c.useMemo(() => {
212
+ function W(t) {
213
+ return i.useMemo(() => {
220
214
  var r, s;
221
215
  const e = (s = (r = t.options).controller) == null ? void 0 : s.call(r, t.actions.controller);
222
216
  return e ? (Object.entries(e).forEach(([o, u]) => t.dispatchers.attach(o, u)), e) : void 0;
223
217
  }, []);
224
218
  }
225
- function H(t) {
219
+ function G(t) {
226
220
  return (e, n) => async (r = Promise.withResolvers(), s) => {
227
221
  if (typeof n != "function") return;
228
222
  const o = Symbol("process"), u = n(...s);
229
223
  for (; ; ) {
230
- const { value: a, done: i } = await u.next();
231
- if (i) {
232
- const U = a(t.model.current, o);
233
- t.model.current = Q(U, o), t.update.rerender();
224
+ const { value: a, done: c } = await u.next();
225
+ if (c) {
226
+ const C = a(t.model.current, o);
227
+ t.model.current = $(C, o), t.update.rerender();
234
228
  break;
235
229
  }
236
- const d = a;
237
- t.model.current = d(t.model.current, o), t.update.rerender();
230
+ const f = a;
231
+ t.model.current = f(t.model.current, o), t.update.rerender();
238
232
  }
239
233
  r.resolve();
240
234
  };
@@ -243,11 +237,11 @@ function j(t) {
243
237
  return t.startsWith("distributed");
244
238
  }
245
239
  var v, g;
246
- class K extends Error {
240
+ class H extends Error {
247
241
  constructor(n, r = null) {
248
242
  super(String(r));
249
- y(this, v);
250
- y(this, g);
243
+ b(this, v);
244
+ b(this, g);
251
245
  E(this, v, n), E(this, g, r);
252
246
  }
253
247
  get type() {
@@ -258,31 +252,31 @@ class K extends Error {
258
252
  }
259
253
  }
260
254
  v = new WeakMap(), g = new WeakMap();
261
- function V(t) {
262
- const e = _(), n = H(t);
263
- return c.useMemo(() => {
264
- const r = new x(), s = e.appEmitter;
255
+ function K(t) {
256
+ const e = A(), n = G(t);
257
+ return i.useMemo(() => {
258
+ const r = new k(), s = e.appEmitter;
265
259
  return {
266
260
  attach(o, u) {
267
261
  const a = String(o);
268
262
  j(a) ? s.on(a, n(o, u)) : r.on(a, n(o, u));
269
263
  },
270
264
  dispatch(o, u, a) {
271
- const i = String(o);
272
- j(i) ? s.emit(i, a, u) : r.emit(i, a, u);
265
+ const c = String(o);
266
+ j(c) ? s.emit(c, a, u) : r.emit(c, a, u);
273
267
  }
274
268
  };
275
269
  }, []);
276
270
  }
277
- function X() {
278
- const t = c.useRef(null);
279
- return c.useMemo(() => ({ customElement: t }), []);
271
+ function L() {
272
+ const t = i.useRef(null);
273
+ return i.useMemo(() => ({ customElement: t }), []);
280
274
  }
281
- function Y(t) {
275
+ function V(t) {
282
276
  var n, r, s, o;
283
- const e = c.useRef(!1);
284
- c.useLayoutEffect(() => {
285
- t.dispatchers.dispatch(m.Derive, [
277
+ const e = i.useRef(!1);
278
+ i.useLayoutEffect(() => {
279
+ t.dispatchers.dispatch(h.Derive, [
286
280
  t.options.props,
287
281
  t.router.current
288
282
  ]);
@@ -291,72 +285,50 @@ function Y(t) {
291
285
  (n = t.router.current) == null ? void 0 : n.location,
292
286
  (r = t.router.current) == null ? void 0 : r.params,
293
287
  (o = (s = t.router.current) == null ? void 0 : s.search) == null ? void 0 : o[0]
294
- ]), c.useLayoutEffect(() => {
288
+ ]), i.useLayoutEffect(() => {
295
289
  if (!e.current)
296
- return e.current = !0, t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
290
+ return e.current = !0, t.dispatchers.dispatch(h.Mount, []), t.dispatchers.dispatch(h.Node, [
297
291
  t.elements.customElement.current
298
- ]), () => t.dispatchers.dispatch(m.Unmount, []);
292
+ ]), () => t.dispatchers.dispatch(h.Unmount, []);
299
293
  }, []);
300
294
  }
301
- function Z(t) {
302
- return c.useMemo(
303
- () => ({
304
- output(e) {
305
- const n = t.elements.customElement.current;
306
- console.groupCollapsed(
307
- "%cRendered",
308
- "background: rgb(217, 235, 240); color: rgb(0, 51, 102); border-radius: 2px; padding: 0 5px",
309
- n
310
- ), console.groupEnd();
311
- }
312
- }),
313
- []
314
- );
295
+ function X(t) {
296
+ const e = i.useMemo(() => t.options.model ?? {}, []);
297
+ return i.useRef(new x(e, e));
315
298
  }
316
- function tt(t) {
317
- const e = c.useMemo(() => t.options.model ?? {}, []);
318
- return c.useRef(new k(e, e));
299
+ function Y() {
300
+ return i.useRef(/* @__PURE__ */ new Set());
319
301
  }
320
- function et() {
321
- return c.useRef(/* @__PURE__ */ new Set());
322
- }
323
- function nt() {
324
- return c.useRef(null);
302
+ function Z() {
303
+ return i.useRef(null);
325
304
  }
326
- function rt({ using: t, children: e }) {
327
- return f.useInRouterContext() ? /* @__PURE__ */ h(ot, { using: t, children: e }) : e();
305
+ function tt({ using: t, children: e }) {
306
+ return d.useInRouterContext() ? /* @__PURE__ */ m(et, { using: t, children: e }) : e();
328
307
  }
329
- function ot({ using: t, children: e }) {
308
+ function et({ using: t, children: e }) {
330
309
  return t.current = {
331
- navigate: f.useNavigate(),
332
- location: f.useLocation(),
333
- params: f.useParams(),
334
- search: f.useSearchParams()
335
- }, /* @__PURE__ */ h(B, { children: e() });
310
+ navigate: d.useNavigate(),
311
+ location: d.useLocation(),
312
+ params: d.useParams(),
313
+ search: d.useSearchParams()
314
+ }, /* @__PURE__ */ m(B, { children: e() });
336
315
  }
337
- function st() {
338
- const [t, e] = c.useReducer((n) => n + 1, 0);
339
- return c.useMemo(() => ({ hash: t, rerender: e }), [t]);
316
+ function nt() {
317
+ const [t, e] = i.useReducer((n) => n + 1, 0);
318
+ return i.useMemo(() => ({ hash: t, rerender: e }), [t]);
340
319
  }
341
- function ut({
320
+ function rt({
342
321
  options: t
343
322
  }) {
344
- const e = _(), n = st(), r = et(), s = X(), o = nt(), u = tt({ options: t }), a = Z({ options: t, elements: s }), i = V({
345
- app: e,
346
- options: t,
347
- update: n,
348
- model: u,
349
- logger: a,
350
- queue: r
351
- }), d = W({ app: e, options: t, model: u, dispatchers: i, router: o });
352
- return G({ options: t, dispatchers: i, actions: d }), Y({ options: t, dispatchers: i, elements: s, router: o }), c.useMemo(() => (a.output({}), c.createElement(t.name, {
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 });
324
+ return W({ options: t, dispatchers: a, actions: c }), V({ options: t, dispatchers: a, elements: s, router: o }), i.useMemo(() => i.createElement(t.name, {
353
325
  ref: s.customElement,
354
- children: /* @__PURE__ */ h(rt, { using: o, children: () => t.view(d.view) })
355
- })), [n.hash]);
326
+ children: /* @__PURE__ */ m(tt, { using: o, children: () => t.view(c.view) })
327
+ }), [n.hash]);
356
328
  }
357
- function ct(t) {
358
- return (e) => c.memo(
359
- (n) => ut({
329
+ function ot(t) {
330
+ return (e) => i.memo(
331
+ (n) => rt({
360
332
  options: {
361
333
  ...e,
362
334
  name: t.join(""),
@@ -366,28 +338,25 @@ function ct(t) {
366
338
  (n, r) => JSON.stringify(n) === JSON.stringify(r)
367
339
  );
368
340
  }
369
- function it(t) {
370
- return t;
371
- }
372
- function at(t) {
341
+ function st(t) {
373
342
  return new Promise((e) => setTimeout(e, t));
374
343
  }
375
- function lt(t) {
344
+ function ut(t) {
376
345
  return t ? !!(t && typeof t != "symbol") : Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`);
377
346
  }
378
- function dt(t) {
379
- return t instanceof K;
347
+ function it(t) {
348
+ return t instanceof H;
380
349
  }
381
- const bt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
350
+ const mt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
382
351
  __proto__: null,
383
- isEventError: dt,
384
- pk: lt,
385
- sleep: at
386
- }, Symbol.toStringTag, { value: "Module" })), wt = { app: z, module: ct, model: J, view: it, controller: F };
352
+ isEventError: it,
353
+ pk: ut,
354
+ sleep: st
355
+ }, Symbol.toStringTag, { value: "Module" })), pt = { app: z, module: ot };
387
356
  export {
388
- K as EventError,
389
- m as Lifecycle,
390
- T as State,
391
- wt as create,
392
- bt as utils
357
+ H as EventError,
358
+ h as Lifecycle,
359
+ F as State,
360
+ pt as create,
361
+ mt as utils
393
362
  };
@@ -1 +1 @@
1
- (function(o,s){typeof exports=="object"&&typeof module<"u"?s(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"],s):(o=typeof globalThis<"u"?globalThis:o||self,s(o.Chizu={},o.jsxRuntime,o.EventEmitter3,o.React,o.Immer,o.get,o.Traverse,o.ReactRouterDOM))})(this,function(o,s,l,w,z,v,E,B){"use strict";var pe=Object.defineProperty;var N=o=>{throw TypeError(o)};var ve=(o,s,l)=>s in o?pe(o,s,{enumerable:!0,configurable:!0,writable:!0,value:l}):o[s]=l;var j=(o,s,l)=>ve(o,typeof s!="symbol"?s+"":s,l),U=(o,s,l)=>s.has(o)||N("Cannot "+l);var O=(o,s,l)=>(U(o,s,"read from private field"),l?l.call(o):s.get(o)),k=(o,s,l)=>s.has(o)?N("Cannot add the same private member more than once"):s instanceof WeakSet?s.add(o):s.set(o,l),x=(o,s,l,w)=>(U(o,s,"write to private field"),w?w.call(o,l):s.set(o,l),l);var y,b;function D(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 i=D(w),g=D(B),P=i.createContext({appEmitter:new l});function _(){return i.useContext(P)}function F(e){return()=>{const t=i.useMemo(()=>({appEmitter:new l}),[]);return s.jsx(P.Provider,{value:t,children:s.jsx(e,{})})}}function J(e){return e}function L(e){return e}class q{constructor(t){this.value=t}}class C{static Draft(t){return new q(t)}}j(C,"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="distributed/lifecycle/error",e.Unmount="lifecycle/unmount",e))(p||{});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 $(e,t=[]){return new M(e,t)}class S{constructor(t,n=t){this.stateless=t,this.stateful=n}get validatable(){return T(this.stateful)}}function T(e,t=[]){return new Proxy(e,{get(n,r){switch(r){case"is":return c=>{const u=R(e,t);if(!u)return!1;const a=new Set(u.flatMap(d=>d.operations));return!!(Array.from(a).reduce((d,m)=>d|(m??0),0)&c)};case"pending":return()=>!!R(e,t);case"draft":return()=>{const c=R(e,t);if(!c)return v(e,t);const u=c.flatMap(a=>a.operations).find(a=>a instanceof q);return u?u.value:v(e,t)}}return T(e,[...t,String(r)])}})}function R(e,t){const r=typeof v(e,t)=="object"?t:t.slice(0,-1),c=r.length===0?e:v(e,r),u=(c==null?void 0:c[h.annotations])??[];return u.length>0?u:null}function Q(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 c(u){return E(u).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node instanceof M){const a=typeof this.node.value=="object",f=[...a?this.path:this.path.slice(0,-1),h.annotations],d=v(e.stateful,f)??[],m=this.node.attach(t);a?this.update({...this.node.value,[h.annotations]:[m,...d]},!0):(this.parent&&(this.parent.node[h.annotations]=[m,...d]),this.update(this.node.value,!0))}})}return new S(r(h.immer.produce(e.stateless,n)),c(h.immer.produce(e.stateful,n)))}function W(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(c=>c.process!==t)},!0)}});return new S(e.stateless,n)}function G(e){return i.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)=>Q(n,r,t)},dispatch([t,...n]){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]){const r=Promise.withResolvers();return e.dispatchers.dispatch(t,n,r),r.promise}}}}),[])}function H(e){return i.useMemo(()=>{var r,c;const t=(c=(r=e.options).controller)==null?void 0:c.call(r,e.actions.controller);return t?(Object.entries(t).forEach(([u,a])=>e.dispatchers.attach(u,a)),t):void 0},[])}function K(e){return(t,n)=>async(r=Promise.withResolvers(),c)=>{if(typeof n!="function")return;const u=Symbol("process"),a=n(...c);for(;;){const{value:f,done:d}=await a.next();if(d){const me=f(e.model.current,u);e.model.current=W(me,u),e.update.rerender();break}const m=f;e.model.current=m(e.model.current,u),e.update.rerender()}r.resolve()}}function A(e){return e.startsWith("distributed")}class I extends Error{constructor(n,r=null){super(String(r));k(this,y);k(this,b);x(this,y,n),x(this,b,r)}get type(){return O(this,y)}get message(){return O(this,b)||""}}y=new WeakMap,b=new WeakMap;function V(e){const t=_(),n=K(e);return i.useMemo(()=>{const r=new l,c=t.appEmitter;return{attach(u,a){const f=String(u);A(f)?c.on(f,n(u,a)):r.on(f,n(u,a))},dispatch(u,a,f){const d=String(u);A(d)?c.emit(d,f,a):r.emit(d,f,a)}}},[])}function X(){const e=i.useRef(null);return i.useMemo(()=>({customElement:e}),[])}function Y(e){var n,r,c,u;const t=i.useRef(!1);i.useLayoutEffect(()=>{e.dispatchers.dispatch(p.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=(c=e.router.current)==null?void 0:c.search)==null?void 0:u[0]]),i.useLayoutEffect(()=>{if(!t.current)return t.current=!0,e.dispatchers.dispatch(p.Mount,[]),e.dispatchers.dispatch(p.Node,[e.elements.customElement.current]),()=>e.dispatchers.dispatch(p.Unmount,[])},[])}function Z(e){return i.useMemo(()=>({output(t){const n=e.elements.customElement.current;console.groupCollapsed("%cRendered","background: rgb(217, 235, 240); color: rgb(0, 51, 102); border-radius: 2px; padding: 0 5px",n),console.groupEnd()}}),[])}function ee(e){const t=i.useMemo(()=>e.options.model??{},[]);return i.useRef(new S(t,t))}function te(){return i.useRef(new Set)}function ne(){return i.useRef(null)}function re({using:e,children:t}){return g.useInRouterContext()?s.jsx(oe,{using:e,children:t}):t()}function oe({using:e,children:t}){return e.current={navigate:g.useNavigate(),location:g.useLocation(),params:g.useParams(),search:g.useSearchParams()},s.jsx(s.Fragment,{children:t()})}function se(){const[e,t]=i.useReducer(n=>n+1,0);return i.useMemo(()=>({hash:e,rerender:t}),[e])}function ue({options:e}){const t=_(),n=se(),r=te(),c=X(),u=ne(),a=ee({options:e}),f=Z({options:e,elements:c}),d=V({app:t,options:e,update:n,model:a,logger:f,queue:r}),m=G({app:t,options:e,model:a,dispatchers:d,router:u});return H({options:e,dispatchers:d,actions:m}),Y({options:e,dispatchers:d,elements:c,router:u}),i.useMemo(()=>(f.output({}),i.createElement(e.name,{ref:c.customElement,children:s.jsx(re,{using:u,children:()=>e.view(m.view)})})),[n.hash])}function ce(e){return t=>i.memo(n=>ue({options:{...t,name:e.join(""),props:n}}),(n,r)=>JSON.stringify(n)===JSON.stringify(r))}function ie(e){return e}function ae(e){return new Promise(t=>setTimeout(t,e))}function le(e){return e?!!(e&&typeof e!="symbol"):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}function de(e){return e instanceof I}const fe=Object.freeze(Object.defineProperty({__proto__:null,isEventError:de,pk:le,sleep:ae},Symbol.toStringTag,{value:"Module"})),he={app:F,module:ce,model:L,view:ie,controller:J};o.EventError=I,o.Lifecycle=p,o.State=C,o.create=he,o.utils=fe,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
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"})});
package/dist/index.d.ts CHANGED
@@ -1,18 +1,16 @@
1
1
  import { default as app } from './app/index.tsx';
2
- import { default as controller } from './controller/index.ts';
3
- import { default as model } from './model/index.ts';
2
+ import { ControllerDefinition } from './controller/types.ts';
4
3
  import { default as module } from './module/index.tsx';
5
- import { default as view } from './view/index.ts';
4
+ import { ViewArgs, ViewDefinition } from './view/types.ts';
6
5
  export { Lifecycle, State } from './types/index.ts';
7
6
  export * as utils from './utils/index.ts';
8
7
  export declare const create: {
9
8
  app: typeof app;
10
9
  module: typeof module;
11
- model: typeof model;
12
- view: typeof view;
13
- controller: typeof controller;
14
10
  };
15
11
  export { EventError } from './module/renderer/dispatchers/utils.ts';
16
12
  export type * as Create from './types/index.ts';
17
- export type { ViewArgs as Args } from './view/types.ts';
18
13
  export type { Pk } from './types/index.ts';
14
+ export type { ViewDefinition as View };
15
+ export type { ViewArgs as Within };
16
+ export type { ControllerDefinition as Controller };
@@ -1,6 +1,5 @@
1
1
  import { UseApp } from '../../../app/types.ts';
2
2
  import { ModuleDefinition } from '../../../types/index.ts';
3
- import { UseLogger } from '../logger/types.ts';
4
3
  import { UseModel } from '../model/types.ts';
5
4
  import { UseQueue } from '../queue/types.ts';
6
5
  import { UseOptions } from '../types.ts';
@@ -11,7 +10,6 @@ export type Props<M extends ModuleDefinition> = {
11
10
  options: UseOptions<M>;
12
11
  update: UseUpdate;
13
12
  model: UseModel;
14
- logger: UseLogger;
15
13
  queue: UseQueue;
16
14
  };
17
15
  export type UseDispatchers = ReturnType<typeof useDispatchers>;
@@ -28,7 +28,7 @@ export declare enum Lifecycle {
28
28
  Unmount = "lifecycle/unmount"
29
29
  }
30
30
  export type Model = Record<string, any>;
31
- export type Actions = [ActionName] | [ActionName, ...ActionPayload];
31
+ export type Actions = [] | [ActionName] | [ActionName, ...ActionPayload];
32
32
  export type Props = Record<string, unknown>;
33
33
  export type Query = null | string;
34
34
  export type Module<T extends {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chizu",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "main": "./dist/chizu.js",
6
6
  "types": "./dist/index.d.ts",
@@ -43,13 +43,13 @@
43
43
  "jest": "^29.7.0",
44
44
  "jest-environment-jsdom": "^29.7.0",
45
45
  "lucide-react": "^0.503.0",
46
- "prettier": "^3.4.2",
46
+ "prettier": "^3.5.3",
47
47
  "react-dom": "^19.0.0",
48
48
  "react-test-renderer": "^19.0.0",
49
49
  "rollup-plugin-visualizer": "^5.14.0",
50
50
  "ts-jest": "^29.2.6",
51
51
  "ts-node": "^10.9.2",
52
- "typescript": "~5.7.3",
52
+ "typescript": "^5.8.3",
53
53
  "vite": "^6.0.5",
54
54
  "vite-plugin-dts": "^4.5.4",
55
55
  "wait-on": "^8.0.3"
@@ -1,5 +0,0 @@
1
- import { ModuleDefinition } from '../../../types/index.ts';
2
- import { Metrics, Props } from './types.ts';
3
- export default function useLogger<M extends ModuleDefinition>(props: Props<M>): {
4
- output(_metrics: Metrics): void;
5
- };
@@ -1,10 +0,0 @@
1
- import { ModuleDefinition } from '../../../types/index.ts';
2
- import { UseElements } from '../elements/types.ts';
3
- import { UseOptions } from '../types.ts';
4
- import { default as useLogger } from './index.ts';
5
- export type Props<M extends ModuleDefinition> = {
6
- options: UseOptions<M>;
7
- elements: UseElements;
8
- };
9
- export type UseLogger = ReturnType<typeof useLogger>;
10
- export type Metrics = {};