chizu 0.2.9 → 0.2.10

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
@@ -1,28 +1,28 @@
1
- var J = Object.defineProperty;
2
- var D = (t) => {
1
+ var q = Object.defineProperty;
2
+ var k = (t) => {
3
3
  throw TypeError(t);
4
4
  };
5
- var q = (t, e, n) => e in t ? J(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
6
- var w = (t, e, n) => q(t, typeof e != "symbol" ? e + "" : e, n), k = (t, e, n) => e.has(t) || D("Cannot " + n);
7
- var E = (t, e, n) => (k(t, e, "read from private field"), n ? n.call(t) : e.get(t)), R = (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), b = (t, e, n, r) => (k(t, e, "write to private field"), r ? r.call(t, n) : e.set(t, n), n);
8
- import { jsx as p, Fragment as z } from "react/jsx-runtime";
9
- import { Immer as F } from "immer";
5
+ var z = (t, e, n) => e in t ? q(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
6
+ var E = (t, e, n) => z(t, typeof e != "symbol" ? e + "" : e, n), O = (t, e, n) => e.has(t) || k("Cannot " + n);
7
+ var R = (t, e, n) => (O(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), S = (t, e, n, r) => (O(t, e, "write to private field"), r ? r.call(t, n) : e.set(t, n), n);
8
+ import { jsx as p, Fragment as F } from "react/jsx-runtime";
9
+ import { Immer as T } from "immer";
10
10
  import v from "lodash/get";
11
- import x from "traverse";
11
+ import M from "traverse";
12
12
  import * as a from "react";
13
- import P from "eventemitter3";
13
+ import j from "eventemitter3";
14
14
  import * as h from "react-router-dom";
15
- class _ {
15
+ class C {
16
16
  constructor(e) {
17
17
  this.value = e;
18
18
  }
19
19
  }
20
- class T {
20
+ class $ {
21
21
  static Draft(e) {
22
- return new _(e);
22
+ return new C(e);
23
23
  }
24
24
  }
25
- w(T, "Op", {
25
+ E($, "Op", {
26
26
  Add: 1,
27
27
  Remove: 2,
28
28
  Update: 4,
@@ -31,102 +31,102 @@ w(T, "Op", {
31
31
  });
32
32
  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 || {});
33
33
  const l = {
34
- immer: new F(),
34
+ immer: new T(),
35
35
  annotations: Symbol("annotations")
36
36
  };
37
37
  l.immer.setAutoFreeze(!1);
38
- class M {
38
+ class P {
39
39
  constructor(e, n, r = null) {
40
- w(this, "process");
40
+ E(this, "process");
41
41
  this.value = e, this.operations = n, this.field = r, this.process = null;
42
42
  }
43
43
  attach(e) {
44
44
  return this.process = e, this;
45
45
  }
46
46
  }
47
- function $(t, e = []) {
48
- return new M(t, e);
47
+ function J(t, e = []) {
48
+ return new P(t, e);
49
49
  }
50
- class O {
50
+ class D {
51
51
  constructor(e, n = e) {
52
52
  this.stateless = e, this.stateful = n;
53
53
  }
54
54
  get validatable() {
55
- return C(this.stateful);
55
+ return I(this.stateful);
56
56
  }
57
57
  }
58
- function C(t, e = []) {
58
+ function I(t, e = []) {
59
59
  return new Proxy(t, {
60
60
  get(n, r) {
61
61
  switch (r) {
62
62
  case "is":
63
63
  return (s) => {
64
- const o = S(t, e);
64
+ const o = x(t, e);
65
65
  if (!o) return !1;
66
66
  const u = new Set(
67
- o.flatMap((c) => c.operations)
67
+ o.flatMap((i) => i.operations)
68
68
  );
69
69
  return !!(Array.from(u).reduce(
70
- (c, d) => c | (d ?? 0),
70
+ (i, d) => i | (d ?? 0),
71
71
  0
72
72
  ) & s);
73
73
  };
74
74
  case "pending":
75
- return () => !!S(t, e);
75
+ return () => !!x(t, e);
76
76
  case "draft":
77
77
  return () => {
78
- const s = S(t, e);
78
+ const s = x(t, e);
79
79
  if (!s) return v(t, e);
80
- const o = s.flatMap((u) => u.operations).find((u) => u instanceof _);
80
+ const o = s.flatMap((u) => u.operations).find((u) => u instanceof C);
81
81
  return o ? o.value : v(t, e);
82
82
  };
83
83
  }
84
- return C(t, [...e, String(r)]);
84
+ return I(t, [...e, String(r)]);
85
85
  }
86
86
  });
87
87
  }
88
- function S(t, e) {
88
+ function x(t, e) {
89
89
  const r = typeof v(t, e) == "object" ? e : e.slice(0, -1), s = r.length === 0 ? t : v(t, r), o = (s == null ? void 0 : s[l.annotations]) ?? [];
90
90
  return o.length > 0 ? o : null;
91
91
  }
92
92
  function Q(t, e, n) {
93
93
  function r(o) {
94
- return x(o).forEach(function() {
94
+ return M(o).forEach(function() {
95
95
  if (this.key === l.annotations) {
96
96
  this.block();
97
97
  return;
98
98
  }
99
- this.node instanceof M && this.update(this.node.value);
99
+ this.node instanceof P && this.update(this.node.value);
100
100
  });
101
101
  }
102
102
  function s(o) {
103
- return x(o).forEach(function() {
103
+ return M(o).forEach(function() {
104
104
  if (this.key === l.annotations) {
105
105
  this.block();
106
106
  return;
107
107
  }
108
- if (this.node instanceof M) {
109
- const u = typeof this.node.value == "object", i = [
108
+ if (this.node instanceof P) {
109
+ const u = typeof this.node.value == "object", c = [
110
110
  ...u ? this.path : this.path.slice(0, -1),
111
111
  l.annotations
112
- ], c = v(t.stateful, i) ?? [], d = this.node.attach(e);
112
+ ], i = v(t.stateful, c) ?? [], d = this.node.attach(e);
113
113
  u ? this.update(
114
114
  {
115
115
  ...this.node.value,
116
- [l.annotations]: [d, ...c]
116
+ [l.annotations]: [d, ...i]
117
117
  },
118
118
  !0
119
- ) : (this.parent && (this.parent.node[l.annotations] = [d, ...c]), this.update(this.node.value, !0));
119
+ ) : (this.parent && (this.parent.node[l.annotations] = [d, ...i]), this.update(this.node.value, !0));
120
120
  }
121
121
  });
122
122
  }
123
- return new O(
123
+ return new D(
124
124
  r(l.immer.produce(t.stateless, n)),
125
125
  s(l.immer.produce(t.stateful, n))
126
126
  );
127
127
  }
128
128
  function A(t, e) {
129
- const n = x(t.stateful).forEach(function() {
129
+ const n = M(t.stateful).forEach(function() {
130
130
  if (this.key === l.annotations) {
131
131
  this.block();
132
132
  return;
@@ -144,7 +144,7 @@ function A(t, e) {
144
144
  );
145
145
  }
146
146
  });
147
- return new O(t.stateless, n);
147
+ return new D(t.stateless, n);
148
148
  }
149
149
  function W(t) {
150
150
  return (e, n) => async (r = Promise.withResolvers(), s) => {
@@ -153,41 +153,41 @@ function W(t) {
153
153
  if (u == null)
154
154
  return void r.resolve();
155
155
  if (typeof u == "function") {
156
- const i = u(t.model.current, o);
157
- return t.model.current = A(i, o), t.update.rerender(), void r.resolve();
156
+ const c = u(t.model.current, o);
157
+ return t.model.current = A(c, o), t.update.rerender(), void r.resolve();
158
158
  }
159
159
  for (; ; ) {
160
- const { value: i, done: c } = await u.next();
161
- if (c) {
162
- const H = i(t.model.current, o);
163
- t.model.current = A(H, o), t.update.rerender();
160
+ const { value: c, done: i } = await u.next();
161
+ if (i) {
162
+ const N = c(t.model.current, o);
163
+ t.model.current = A(N, o), t.update.rerender();
164
164
  break;
165
165
  }
166
- const d = i;
166
+ const d = c;
167
167
  t.model.current = d(t.model.current, o), t.update.rerender();
168
168
  }
169
169
  r.resolve();
170
170
  };
171
171
  }
172
- function N(t) {
172
+ function U(t) {
173
173
  return t.startsWith("distributed");
174
174
  }
175
- var g, y;
175
+ var y, w;
176
176
  class G extends Error {
177
177
  constructor(n, r = null) {
178
178
  super(String(r));
179
- R(this, g);
180
- R(this, y);
181
- b(this, g, n), b(this, y, r);
179
+ b(this, y);
180
+ b(this, w);
181
+ S(this, y, n), S(this, w, r);
182
182
  }
183
183
  get type() {
184
- return E(this, g);
184
+ return R(this, y);
185
185
  }
186
186
  get message() {
187
- return E(this, y) || "";
187
+ return R(this, w) || "";
188
188
  }
189
189
  }
190
- g = new WeakMap(), y = new WeakMap();
190
+ y = new WeakMap(), w = new WeakMap();
191
191
  function K(t) {
192
192
  return new Promise((e) => setTimeout(e, t));
193
193
  }
@@ -197,12 +197,12 @@ function V(t) {
197
197
  function X(t) {
198
198
  return t instanceof G;
199
199
  }
200
- function j(t) {
200
+ function g(t) {
201
201
  return JSON.stringify(t);
202
202
  }
203
203
  const gt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
204
204
  __proto__: null,
205
- hash: j,
205
+ hash: g,
206
206
  isEventError: X,
207
207
  pk: V,
208
208
  sleep: K
@@ -210,7 +210,7 @@ const gt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
210
210
  function f(t, e) {
211
211
  const n = a.useRef(null), r = a.useRef(null);
212
212
  return a.useMemo(() => {
213
- const s = j(e);
213
+ const s = g(e);
214
214
  if (r.current !== s) {
215
215
  r.current = s;
216
216
  const o = t();
@@ -219,29 +219,29 @@ function f(t, e) {
219
219
  return n.current;
220
220
  }, e);
221
221
  }
222
- function U(t, e) {
222
+ function _(t, e) {
223
223
  const n = a.useRef(null);
224
224
  a.useEffect(() => {
225
- const r = j(e);
225
+ const r = g(e);
226
226
  if (n.current !== r)
227
227
  return n.current = r, t();
228
228
  }, e);
229
229
  }
230
- const I = a.createContext({
231
- appEmitter: new P()
230
+ const B = a.createContext({
231
+ appEmitter: new j()
232
232
  });
233
- function B() {
234
- return a.useContext(I);
233
+ function H() {
234
+ return a.useContext(B);
235
235
  }
236
236
  function Y(t) {
237
237
  return () => {
238
238
  const e = f(
239
239
  () => ({
240
- appEmitter: new P()
240
+ appEmitter: new j()
241
241
  }),
242
242
  []
243
243
  );
244
- return /* @__PURE__ */ p(I.Provider, { value: e, children: /* @__PURE__ */ p(t, {}) });
244
+ return /* @__PURE__ */ p(B.Provider, { value: e, children: /* @__PURE__ */ p(t, {}) });
245
245
  };
246
246
  }
247
247
  function Z(t) {
@@ -258,7 +258,7 @@ function Z(t) {
258
258
  props: t.options.props,
259
259
  actions: {
260
260
  annotate(e, n) {
261
- return $(e, n);
261
+ return J(e, n);
262
262
  },
263
263
  produce(e) {
264
264
  return (n, r) => Q(n, r, e);
@@ -298,17 +298,17 @@ function L(t) {
298
298
  }, []);
299
299
  }
300
300
  function tt(t) {
301
- const e = B(), n = W(t);
301
+ const e = H(), n = W(t);
302
302
  return f(() => {
303
- const r = new P(), s = e.appEmitter;
303
+ const r = new j(), s = e.appEmitter;
304
304
  return {
305
305
  attach(o, u) {
306
- const i = String(o);
307
- N(i) ? s.on(i, n(o, u)) : r.on(i, n(o, u));
308
- },
309
- dispatch(o, u, i) {
310
306
  const c = String(o);
311
- N(c) ? s.emit(c, i, u) : r.emit(c, i, u);
307
+ U(c) ? s.on(c, n(o, u)) : r.on(c, n(o, u));
308
+ },
309
+ dispatch(o, u, c) {
310
+ const i = String(o);
311
+ U(i) ? s.emit(i, c, u) : r.emit(i, c, u);
312
312
  }
313
313
  };
314
314
  }, []);
@@ -319,20 +319,20 @@ function et() {
319
319
  }
320
320
  function nt(t) {
321
321
  var e, n, r, s;
322
- U(() => {
323
- t.dispatchers.dispatch(m.Derive, []);
322
+ _(() => {
323
+ t.dispatchers.dispatch(m.Derive, []), t.update.rerender();
324
324
  }, [
325
325
  t.options.props,
326
326
  (e = t.router.current) == null ? void 0 : e.location,
327
327
  (n = t.router.current) == null ? void 0 : n.params,
328
328
  (s = (r = t.router.current) == null ? void 0 : r.search) == null ? void 0 : s[0]
329
- ]), U(() => (t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
329
+ ]), _(() => (t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
330
330
  t.elements.customElement.current
331
331
  ]), () => t.dispatchers.dispatch(m.Unmount, [])), []);
332
332
  }
333
333
  function rt(t) {
334
334
  const e = f(() => t.options.model ?? {}, []);
335
- return a.useRef(new O(e, e));
335
+ return a.useRef(new D(e, e));
336
336
  }
337
337
  function ot() {
338
338
  return a.useRef(/* @__PURE__ */ new Set());
@@ -341,28 +341,28 @@ function st() {
341
341
  return a.useRef(null);
342
342
  }
343
343
  function ut({ using: t, children: e }) {
344
- return h.useInRouterContext() ? /* @__PURE__ */ p(it, { using: t, children: e }) : e();
344
+ return h.useInRouterContext() ? /* @__PURE__ */ p(ct, { using: t, children: e }) : e();
345
345
  }
346
- function it({ using: t, children: e }) {
346
+ function ct({ using: t, children: e }) {
347
347
  return t.current = {
348
348
  navigate: h.useNavigate(),
349
349
  location: h.useLocation(),
350
350
  params: h.useParams(),
351
351
  search: h.useSearchParams()
352
- }, /* @__PURE__ */ p(z, { children: e() });
352
+ }, /* @__PURE__ */ p(F, { children: e() });
353
353
  }
354
- function ct() {
354
+ function it() {
355
355
  const [t, e] = a.useReducer((n) => n + 1, 0);
356
356
  return f(() => ({ hash: t, rerender: e }), [t]);
357
357
  }
358
358
  function at({
359
359
  options: t
360
360
  }) {
361
- const e = B(), n = ct(), r = ot(), s = et(), o = st(), u = rt({ options: t }), i = tt({ app: e, options: t, update: n, model: u, queue: r }), c = Z({ app: e, options: t, model: u, dispatchers: i, router: o });
362
- return L({ options: t, dispatchers: i, actions: c }), nt({ options: t, dispatchers: i, elements: s, router: o }), f(() => a.createElement(t.name, {
361
+ const e = H(), n = it(), r = ot(), s = et(), o = st(), u = rt({ options: t }), c = tt({ app: e, options: t, update: n, model: u, queue: r }), i = Z({ app: e, options: t, model: u, dispatchers: c, router: o });
362
+ return L({ options: t, dispatchers: c, actions: i }), nt({ options: t, dispatchers: c, elements: s, router: o, update: n }), f(() => a.createElement(t.name, {
363
363
  ref: s.customElement,
364
364
  style: { display: "contents" },
365
- children: /* @__PURE__ */ p(ut, { using: o, children: () => t.view(c.view) })
365
+ children: /* @__PURE__ */ p(ut, { using: o, children: () => t.view(i.view) })
366
366
  }), [n.hash]);
367
367
  }
368
368
  function lt(t) {
@@ -374,14 +374,14 @@ function lt(t) {
374
374
  props: n
375
375
  }
376
376
  }),
377
- (n, r) => JSON.stringify(n) === JSON.stringify(r)
377
+ (n, r) => g(n) === g(r)
378
378
  );
379
379
  }
380
380
  const yt = { app: Y, module: lt };
381
381
  export {
382
382
  G as EventError,
383
383
  m as Lifecycle,
384
- T as State,
384
+ $ as State,
385
385
  yt as create,
386
386
  gt as utils
387
387
  };
@@ -1 +1 @@
1
- (function(s,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("react/jsx-runtime"),require("immer"),require("lodash/get"),require("traverse"),require("react"),require("eventemitter3"),require("react-router-dom")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","immer","lodash/get","traverse","react","eventemitter3","react-router-dom"],u):(s=typeof globalThis<"u"?globalThis:s||self,u(s.Chizu={},s.jsxRuntime,s.Immer,s.get,s.Traverse,s.React,s.EventEmitter3,s.ReactRouterDOM))})(this,function(s,u,l,m,S,F,E,$){"use strict";var me=Object.defineProperty;var H=s=>{throw TypeError(s)};var pe=(s,u,l)=>u in s?me(s,u,{enumerable:!0,configurable:!0,writable:!0,value:l}):s[u]=l;var P=(s,u,l)=>pe(s,typeof u!="symbol"?u+"":u,l),J=(s,u,l)=>u.has(s)||H("Cannot "+l);var D=(s,u,l)=>(J(s,u,"read from private field"),l?l.call(s):u.get(s)),_=(s,u,l)=>u.has(s)?H("Cannot add the same private member more than once"):u instanceof WeakSet?u.add(s):u.set(s,l),x=(s,u,l,m)=>(J(s,u,"write to private field"),m?m.call(s,l):u.set(s,l),l);var w,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 d=k(F),g=k($);class q{constructor(t){this.value=t}}class N{static Draft(t){return new q(t)}}P(N,"Op",{Add:1,Remove:2,Update:4,Move:8,Replace:16});var v=(e=>(e.Mount="lifecycle/mount",e.Node="lifecycle/node",e.Derive="lifecycle/derive",e.Error="distributed/lifecycle/error",e.Unmount="lifecycle/unmount",e))(v||{});const h={immer:new l.Immer,annotations:Symbol("annotations")};h.immer.setAutoFreeze(!1);class R{constructor(t,n,r=null){P(this,"process");this.value=t,this.operations=n,this.field=r,this.process=null}attach(t){return this.process=t,this}}function Q(e,t=[]){return new R(e,t)}class j{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 o=O(e,t);if(!o)return!1;const i=new Set(o.flatMap(f=>f.operations));return!!(Array.from(i).reduce((f,y)=>f|(y??0),0)&c)};case"pending":return()=>!!O(e,t);case"draft":return()=>{const c=O(e,t);if(!c)return m(e,t);const o=c.flatMap(i=>i.operations).find(i=>i instanceof q);return o?o.value:m(e,t)}}return T(e,[...t,String(r)])}})}function O(e,t){const r=typeof m(e,t)=="object"?t:t.slice(0,-1),c=r.length===0?e:m(e,r),o=(c==null?void 0:c[h.annotations])??[];return o.length>0?o:null}function W(e,t,n){function r(o){return S(o).forEach(function(){if(this.key===h.annotations){this.block();return}this.node instanceof R&&this.update(this.node.value)})}function c(o){return S(o).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node instanceof R){const i=typeof this.node.value=="object",a=[...i?this.path:this.path.slice(0,-1),h.annotations],f=m(e.stateful,a)??[],y=this.node.attach(t);i?this.update({...this.node.value,[h.annotations]:[y,...f]},!0):(this.parent&&(this.parent.node[h.annotations]=[y,...f]),this.update(this.node.value,!0))}})}return new j(r(h.immer.produce(e.stateless,n)),c(h.immer.produce(e.stateful,n)))}function A(e,t){const n=S(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 j(e.stateless,n)}function G(e){return(t,n)=>async(r=Promise.withResolvers(),c)=>{if(typeof n!="function")return;const o=Symbol("process"),i=n(...c);if(i==null)return void r.resolve();if(typeof i=="function"){const a=i(e.model.current,o);return e.model.current=A(a,o),e.update.rerender(),void r.resolve()}for(;;){const{value:a,done:f}=await i.next();if(f){const he=a(e.model.current,o);e.model.current=A(he,o),e.update.rerender();break}const y=a;e.model.current=y(e.model.current,o),e.update.rerender()}r.resolve()}}function C(e){return e.startsWith("distributed")}class I extends Error{constructor(n,r=null){super(String(r));_(this,w);_(this,b);x(this,w,n),x(this,b,r)}get type(){return D(this,w)}get message(){return D(this,b)||""}}w=new WeakMap,b=new WeakMap;function K(e){return new Promise(t=>setTimeout(t,e))}function V(e){return e?!!(e&&typeof e!="symbol"):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}function X(e){return e instanceof I}function M(e){return JSON.stringify(e)}const Y=Object.freeze(Object.defineProperty({__proto__:null,hash:M,isEventError:X,pk:V,sleep:K},Symbol.toStringTag,{value:"Module"}));function p(e,t){const n=d.useRef(null),r=d.useRef(null);return d.useMemo(()=>{const c=M(t);if(r.current!==c){r.current=c;const o=e();return n.current=o,o}return n.current},t)}function U(e,t){const n=d.useRef(null);d.useEffect(()=>{const r=M(t);if(n.current!==r)return n.current=r,e()},t)}const z=d.createContext({appEmitter:new E});function B(){return d.useContext(z)}function Z(e){return()=>{const t=p(()=>({appEmitter:new E}),[]);return u.jsx(z.Provider,{value:t,children:u.jsx(e,{})})}}function L(e){return p(()=>({controller:{get model(){return e.model.current.stateful},get router(){return e.router.current},queue:[],props:e.options.props,actions:{annotate(t,n){return Q(t,n)},produce(t){return(n,r)=>W(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},props: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 ee(e){return p(()=>{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(([o,i])=>e.dispatchers.attach(o,i)),t):void 0},[])}function te(e){const t=B(),n=G(e);return p(()=>{const r=new E,c=t.appEmitter;return{attach(o,i){const a=String(o);C(a)?c.on(a,n(o,i)):r.on(a,n(o,i))},dispatch(o,i,a){const f=String(o);C(f)?c.emit(f,a,i):r.emit(f,a,i)}}},[])}function ne(){const e=d.useRef(null);return p(()=>({customElement:e}),[])}function re(e){var t,n,r,c;U(()=>{e.dispatchers.dispatch(v.Derive,[])},[e.options.props,(t=e.router.current)==null?void 0:t.location,(n=e.router.current)==null?void 0:n.params,(c=(r=e.router.current)==null?void 0:r.search)==null?void 0:c[0]]),U(()=>(e.dispatchers.dispatch(v.Mount,[]),e.dispatchers.dispatch(v.Node,[e.elements.customElement.current]),()=>e.dispatchers.dispatch(v.Unmount,[])),[])}function se(e){const t=p(()=>e.options.model??{},[]);return d.useRef(new j(t,t))}function oe(){return d.useRef(new Set)}function ue(){return d.useRef(null)}function ce({using:e,children:t}){return g.useInRouterContext()?u.jsx(ie,{using:e,children:t}):t()}function ie({using:e,children:t}){return e.current={navigate:g.useNavigate(),location:g.useLocation(),params:g.useParams(),search:g.useSearchParams()},u.jsx(u.Fragment,{children:t()})}function ae(){const[e,t]=d.useReducer(n=>n+1,0);return p(()=>({hash:e,rerender:t}),[e])}function le({options:e}){const t=B(),n=ae(),r=oe(),c=ne(),o=ue(),i=se({options:e}),a=te({app:t,options:e,update:n,model:i,queue:r}),f=L({app:t,options:e,model:i,dispatchers:a,router:o});return ee({options:e,dispatchers:a,actions:f}),re({options:e,dispatchers:a,elements:c,router:o}),p(()=>d.createElement(e.name,{ref:c.customElement,style:{display:"contents"},children:u.jsx(ce,{using:o,children:()=>e.view(f.view)})}),[n.hash])}function fe(e){return t=>d.memo(n=>le({options:{...t,name:e.join(""),props:n}}),(n,r)=>JSON.stringify(n)===JSON.stringify(r))}const de={app:Z,module:fe};s.EventError=I,s.Lifecycle=v,s.State=N,s.create=de,s.utils=Y,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
1
+ (function(s,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("react/jsx-runtime"),require("immer"),require("lodash/get"),require("traverse"),require("react"),require("eventemitter3"),require("react-router-dom")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","immer","lodash/get","traverse","react","eventemitter3","react-router-dom"],u):(s=typeof globalThis<"u"?globalThis:s||self,u(s.Chizu={},s.jsxRuntime,s.Immer,s.get,s.Traverse,s.React,s.EventEmitter3,s.ReactRouterDOM))})(this,function(s,u,l,m,R,$,S,J){"use strict";var me=Object.defineProperty;var H=s=>{throw TypeError(s)};var pe=(s,u,l)=>u in s?me(s,u,{enumerable:!0,configurable:!0,writable:!0,value:l}):s[u]=l;var P=(s,u,l)=>pe(s,typeof u!="symbol"?u+"":u,l),F=(s,u,l)=>u.has(s)||H("Cannot "+l);var D=(s,u,l)=>(F(s,u,"read from private field"),l?l.call(s):u.get(s)),_=(s,u,l)=>u.has(s)?H("Cannot add the same private member more than once"):u instanceof WeakSet?u.add(s):u.set(s,l),x=(s,u,l,m)=>(F(s,u,"write to private field"),m?m.call(s,l):u.set(s,l),l);var b,E;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 d=k($),w=k(J);class q{constructor(t){this.value=t}}class T{static Draft(t){return new q(t)}}P(T,"Op",{Add:1,Remove:2,Update:4,Move:8,Replace:16});var v=(e=>(e.Mount="lifecycle/mount",e.Node="lifecycle/node",e.Derive="lifecycle/derive",e.Error="distributed/lifecycle/error",e.Unmount="lifecycle/unmount",e))(v||{});const h={immer:new l.Immer,annotations:Symbol("annotations")};h.immer.setAutoFreeze(!1);class j{constructor(t,n,r=null){P(this,"process");this.value=t,this.operations=n,this.field=r,this.process=null}attach(t){return this.process=t,this}}function Q(e,t=[]){return new j(e,t)}class O{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 c=>{const o=M(e,t);if(!o)return!1;const i=new Set(o.flatMap(f=>f.operations));return!!(Array.from(i).reduce((f,y)=>f|(y??0),0)&c)};case"pending":return()=>!!M(e,t);case"draft":return()=>{const c=M(e,t);if(!c)return m(e,t);const o=c.flatMap(i=>i.operations).find(i=>i instanceof q);return o?o.value:m(e,t)}}return A(e,[...t,String(r)])}})}function M(e,t){const r=typeof m(e,t)=="object"?t:t.slice(0,-1),c=r.length===0?e:m(e,r),o=(c==null?void 0:c[h.annotations])??[];return o.length>0?o:null}function W(e,t,n){function r(o){return R(o).forEach(function(){if(this.key===h.annotations){this.block();return}this.node instanceof j&&this.update(this.node.value)})}function c(o){return R(o).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node instanceof j){const i=typeof this.node.value=="object",a=[...i?this.path:this.path.slice(0,-1),h.annotations],f=m(e.stateful,a)??[],y=this.node.attach(t);i?this.update({...this.node.value,[h.annotations]:[y,...f]},!0):(this.parent&&(this.parent.node[h.annotations]=[y,...f]),this.update(this.node.value,!0))}})}return new O(r(h.immer.produce(e.stateless,n)),c(h.immer.produce(e.stateful,n)))}function C(e,t){const n=R(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 O(e.stateless,n)}function G(e){return(t,n)=>async(r=Promise.withResolvers(),c)=>{if(typeof n!="function")return;const o=Symbol("process"),i=n(...c);if(i==null)return void r.resolve();if(typeof i=="function"){const a=i(e.model.current,o);return e.model.current=C(a,o),e.update.rerender(),void r.resolve()}for(;;){const{value:a,done:f}=await i.next();if(f){const he=a(e.model.current,o);e.model.current=C(he,o),e.update.rerender();break}const y=a;e.model.current=y(e.model.current,o),e.update.rerender()}r.resolve()}}function I(e){return e.startsWith("distributed")}class U extends Error{constructor(n,r=null){super(String(r));_(this,b);_(this,E);x(this,b,n),x(this,E,r)}get type(){return D(this,b)}get message(){return D(this,E)||""}}b=new WeakMap,E=new WeakMap;function K(e){return new Promise(t=>setTimeout(t,e))}function V(e){return e?!!(e&&typeof e!="symbol"):Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`)}function X(e){return e instanceof U}function g(e){return JSON.stringify(e)}const Y=Object.freeze(Object.defineProperty({__proto__:null,hash:g,isEventError:X,pk:V,sleep:K},Symbol.toStringTag,{value:"Module"}));function p(e,t){const n=d.useRef(null),r=d.useRef(null);return d.useMemo(()=>{const c=g(t);if(r.current!==c){r.current=c;const o=e();return n.current=o,o}return n.current},t)}function N(e,t){const n=d.useRef(null);d.useEffect(()=>{const r=g(t);if(n.current!==r)return n.current=r,e()},t)}const z=d.createContext({appEmitter:new S});function B(){return d.useContext(z)}function Z(e){return()=>{const t=p(()=>({appEmitter:new S}),[]);return u.jsx(z.Provider,{value:t,children:u.jsx(e,{})})}}function L(e){return p(()=>({controller:{get model(){return e.model.current.stateful},get router(){return e.router.current},queue:[],props:e.options.props,actions:{annotate(t,n){return Q(t,n)},produce(t){return(n,r)=>W(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},props: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 ee(e){return p(()=>{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(([o,i])=>e.dispatchers.attach(o,i)),t):void 0},[])}function te(e){const t=B(),n=G(e);return p(()=>{const r=new S,c=t.appEmitter;return{attach(o,i){const a=String(o);I(a)?c.on(a,n(o,i)):r.on(a,n(o,i))},dispatch(o,i,a){const f=String(o);I(f)?c.emit(f,a,i):r.emit(f,a,i)}}},[])}function ne(){const e=d.useRef(null);return p(()=>({customElement:e}),[])}function re(e){var t,n,r,c;N(()=>{e.dispatchers.dispatch(v.Derive,[]),e.update.rerender()},[e.options.props,(t=e.router.current)==null?void 0:t.location,(n=e.router.current)==null?void 0:n.params,(c=(r=e.router.current)==null?void 0:r.search)==null?void 0:c[0]]),N(()=>(e.dispatchers.dispatch(v.Mount,[]),e.dispatchers.dispatch(v.Node,[e.elements.customElement.current]),()=>e.dispatchers.dispatch(v.Unmount,[])),[])}function se(e){const t=p(()=>e.options.model??{},[]);return d.useRef(new O(t,t))}function oe(){return d.useRef(new Set)}function ue(){return d.useRef(null)}function ce({using:e,children:t}){return w.useInRouterContext()?u.jsx(ie,{using:e,children:t}):t()}function ie({using:e,children:t}){return e.current={navigate:w.useNavigate(),location:w.useLocation(),params:w.useParams(),search:w.useSearchParams()},u.jsx(u.Fragment,{children:t()})}function ae(){const[e,t]=d.useReducer(n=>n+1,0);return p(()=>({hash:e,rerender:t}),[e])}function le({options:e}){const t=B(),n=ae(),r=oe(),c=ne(),o=ue(),i=se({options:e}),a=te({app:t,options:e,update:n,model:i,queue:r}),f=L({app:t,options:e,model:i,dispatchers:a,router:o});return ee({options:e,dispatchers:a,actions:f}),re({options:e,dispatchers:a,elements:c,router:o,update:n}),p(()=>d.createElement(e.name,{ref:c.customElement,style:{display:"contents"},children:u.jsx(ce,{using:o,children:()=>e.view(f.view)})}),[n.hash])}function fe(e){return t=>d.memo(n=>le({options:{...t,name:e.join(""),props:n}}),(n,r)=>g(n)===g(r))}const de={app:Z,module:fe};s.EventError=U,s.Lifecycle=v,s.State=T,s.create=de,s.utils=Y,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
@@ -3,11 +3,13 @@ import { UseDispatchers } from '../dispatchers/types.ts';
3
3
  import { UseElements } from '../elements/types.ts';
4
4
  import { UseRouter } from '../router/types.ts';
5
5
  import { UseOptions } from '../types.ts';
6
+ import { UseUpdate } from '../update/types.ts';
6
7
  import { default as useLifecycles } from './index.ts';
7
8
  export type Props<M extends ModuleDefinition> = {
8
9
  options: UseOptions<M>;
9
10
  elements: UseElements;
10
11
  dispatchers: UseDispatchers;
11
12
  router: UseRouter;
13
+ update: UseUpdate;
12
14
  };
13
15
  export type UseLifecycles = ReturnType<typeof useLifecycles>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chizu",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "type": "module",
5
5
  "main": "./dist/chizu.js",
6
6
  "types": "./dist/index.d.ts",