chizu 0.2.8 → 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;
1
+ var q = Object.defineProperty;
2
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), D = (t, e, n) => e.has(t) || k("Cannot " + n);
7
- var E = (t, e, n) => (D(t, e, "read from private field"), n ? n.call(t) : e.get(t)), R = (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), b = (t, e, n, r) => (D(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,37 +31,37 @@ 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
67
  o.flatMap((i) => i.operations)
@@ -72,40 +72,40 @@ function C(t, e = []) {
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) {
108
+ if (this.node instanceof P) {
109
109
  const u = typeof this.node.value == "object", c = [
110
110
  ...u ? this.path : this.path.slice(0, -1),
111
111
  l.annotations
@@ -120,13 +120,13 @@ function Q(t, e, n) {
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,12 +144,14 @@ 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) => {
151
151
  if (typeof n != "function") return;
152
152
  const o = Symbol("process"), u = n(...s);
153
+ if (u == null)
154
+ return void r.resolve();
153
155
  if (typeof u == "function") {
154
156
  const c = u(t.model.current, o);
155
157
  return t.model.current = A(c, o), t.update.rerender(), void r.resolve();
@@ -157,8 +159,8 @@ function W(t) {
157
159
  for (; ; ) {
158
160
  const { value: c, done: i } = await u.next();
159
161
  if (i) {
160
- const H = c(t.model.current, o);
161
- t.model.current = A(H, o), t.update.rerender();
162
+ const N = c(t.model.current, o);
163
+ t.model.current = A(N, o), t.update.rerender();
162
164
  break;
163
165
  }
164
166
  const d = c;
@@ -167,25 +169,25 @@ function W(t) {
167
169
  r.resolve();
168
170
  };
169
171
  }
170
- function N(t) {
172
+ function U(t) {
171
173
  return t.startsWith("distributed");
172
174
  }
173
- var g, y;
175
+ var y, w;
174
176
  class G extends Error {
175
177
  constructor(n, r = null) {
176
178
  super(String(r));
177
- R(this, g);
178
- R(this, y);
179
- 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);
180
182
  }
181
183
  get type() {
182
- return E(this, g);
184
+ return R(this, y);
183
185
  }
184
186
  get message() {
185
- return E(this, y) || "";
187
+ return R(this, w) || "";
186
188
  }
187
189
  }
188
- g = new WeakMap(), y = new WeakMap();
190
+ y = new WeakMap(), w = new WeakMap();
189
191
  function K(t) {
190
192
  return new Promise((e) => setTimeout(e, t));
191
193
  }
@@ -195,12 +197,12 @@ function V(t) {
195
197
  function X(t) {
196
198
  return t instanceof G;
197
199
  }
198
- function j(t) {
200
+ function g(t) {
199
201
  return JSON.stringify(t);
200
202
  }
201
203
  const gt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
202
204
  __proto__: null,
203
- hash: j,
205
+ hash: g,
204
206
  isEventError: X,
205
207
  pk: V,
206
208
  sleep: K
@@ -208,7 +210,7 @@ const gt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
208
210
  function f(t, e) {
209
211
  const n = a.useRef(null), r = a.useRef(null);
210
212
  return a.useMemo(() => {
211
- const s = j(e);
213
+ const s = g(e);
212
214
  if (r.current !== s) {
213
215
  r.current = s;
214
216
  const o = t();
@@ -217,29 +219,29 @@ function f(t, e) {
217
219
  return n.current;
218
220
  }, e);
219
221
  }
220
- function U(t, e) {
222
+ function _(t, e) {
221
223
  const n = a.useRef(null);
222
224
  a.useEffect(() => {
223
- const r = j(e);
225
+ const r = g(e);
224
226
  if (n.current !== r)
225
227
  return n.current = r, t();
226
228
  }, e);
227
229
  }
228
- const I = a.createContext({
229
- appEmitter: new P()
230
+ const B = a.createContext({
231
+ appEmitter: new j()
230
232
  });
231
- function B() {
232
- return a.useContext(I);
233
+ function H() {
234
+ return a.useContext(B);
233
235
  }
234
236
  function Y(t) {
235
237
  return () => {
236
238
  const e = f(
237
239
  () => ({
238
- appEmitter: new P()
240
+ appEmitter: new j()
239
241
  }),
240
242
  []
241
243
  );
242
- return /* @__PURE__ */ p(I.Provider, { value: e, children: /* @__PURE__ */ p(t, {}) });
244
+ return /* @__PURE__ */ p(B.Provider, { value: e, children: /* @__PURE__ */ p(t, {}) });
243
245
  };
244
246
  }
245
247
  function Z(t) {
@@ -256,7 +258,7 @@ function Z(t) {
256
258
  props: t.options.props,
257
259
  actions: {
258
260
  annotate(e, n) {
259
- return $(e, n);
261
+ return J(e, n);
260
262
  },
261
263
  produce(e) {
262
264
  return (n, r) => Q(n, r, e);
@@ -296,17 +298,17 @@ function L(t) {
296
298
  }, []);
297
299
  }
298
300
  function tt(t) {
299
- const e = B(), n = W(t);
301
+ const e = H(), n = W(t);
300
302
  return f(() => {
301
- const r = new P(), s = e.appEmitter;
303
+ const r = new j(), s = e.appEmitter;
302
304
  return {
303
305
  attach(o, u) {
304
306
  const c = String(o);
305
- N(c) ? s.on(c, n(o, u)) : r.on(c, n(o, u));
307
+ U(c) ? s.on(c, n(o, u)) : r.on(c, n(o, u));
306
308
  },
307
309
  dispatch(o, u, c) {
308
310
  const i = String(o);
309
- N(i) ? s.emit(i, c, u) : r.emit(i, c, u);
311
+ U(i) ? s.emit(i, c, u) : r.emit(i, c, u);
310
312
  }
311
313
  };
312
314
  }, []);
@@ -317,20 +319,20 @@ function et() {
317
319
  }
318
320
  function nt(t) {
319
321
  var e, n, r, s;
320
- U(() => {
321
- t.dispatchers.dispatch(m.Derive, []);
322
+ _(() => {
323
+ t.dispatchers.dispatch(m.Derive, []), t.update.rerender();
322
324
  }, [
323
325
  t.options.props,
324
326
  (e = t.router.current) == null ? void 0 : e.location,
325
327
  (n = t.router.current) == null ? void 0 : n.params,
326
328
  (s = (r = t.router.current) == null ? void 0 : r.search) == null ? void 0 : s[0]
327
- ]), U(() => (t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
329
+ ]), _(() => (t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
328
330
  t.elements.customElement.current
329
331
  ]), () => t.dispatchers.dispatch(m.Unmount, [])), []);
330
332
  }
331
333
  function rt(t) {
332
334
  const e = f(() => t.options.model ?? {}, []);
333
- return a.useRef(new O(e, e));
335
+ return a.useRef(new D(e, e));
334
336
  }
335
337
  function ot() {
336
338
  return a.useRef(/* @__PURE__ */ new Set());
@@ -347,7 +349,7 @@ function ct({ using: t, children: e }) {
347
349
  location: h.useLocation(),
348
350
  params: h.useParams(),
349
351
  search: h.useSearchParams()
350
- }, /* @__PURE__ */ p(z, { children: e() });
352
+ }, /* @__PURE__ */ p(F, { children: e() });
351
353
  }
352
354
  function it() {
353
355
  const [t, e] = a.useReducer((n) => n + 1, 0);
@@ -356,8 +358,8 @@ function it() {
356
358
  function at({
357
359
  options: t
358
360
  }) {
359
- const e = B(), 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 });
360
- return L({ options: t, dispatchers: c, actions: i }), nt({ options: t, dispatchers: c, 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, {
361
363
  ref: s.customElement,
362
364
  style: { display: "contents" },
363
365
  children: /* @__PURE__ */ p(ut, { using: o, children: () => t.view(i.view) })
@@ -372,14 +374,14 @@ function lt(t) {
372
374
  props: n
373
375
  }
374
376
  }),
375
- (n, r) => JSON.stringify(n) === JSON.stringify(r)
377
+ (n, r) => g(n) === g(r)
376
378
  );
377
379
  }
378
380
  const yt = { app: Y, module: lt };
379
381
  export {
380
382
  G as EventError,
381
383
  m as Lifecycle,
382
- T as State,
384
+ $ as State,
383
385
  yt as create,
384
386
  gt as utils
385
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),k=(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 x(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=x(F),g=x($);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(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);k(this,w,n),k(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"})});
@@ -19,7 +19,7 @@ export type ActionEvent<M extends ModuleDefinition> = (...args: M["Actions"][num
19
19
  type ActionEvents<M extends ModuleDefinition> = {
20
20
  [K in Head<M["Actions"]>]: (payload: Payload<M["Actions"], K>) => ActionGenerator<M>;
21
21
  };
22
- 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>;
22
+ export type ActionGenerator<M extends ModuleDefinition> = void | ((models: Models<M["Model"]>, process: Symbol) => Models<M["Model"]>) | AsyncGenerator<(models: Models<M["Model"]>, process: Symbol) => Models<M["Model"]>, (models: Models<M["Model"]>, process: Symbol) => Models<M["Model"]>, unknown>;
23
23
  export type ControllerDefinition<M extends ModuleDefinition> = (controller: ControllerArgs<M>) => ControllerInstance<M>;
24
24
  export type ControllerInstance<M extends ModuleDefinition> = {
25
25
  [Lifecycle.Mount]?(): ActionGenerator<M>;
@@ -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.8",
3
+ "version": "0.2.10",
4
4
  "type": "module",
5
5
  "main": "./dist/chizu.js",
6
6
  "types": "./dist/index.d.ts",