chizu 0.2.11 → 0.2.12

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,16 +1,16 @@
1
1
  var q = Object.defineProperty;
2
- var k = (t) => {
2
+ var O = (t) => {
3
3
  throw TypeError(t);
4
4
  };
5
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);
6
+ var E = (t, e, n) => z(t, typeof e != "symbol" ? e + "" : e, n), A = (t, e, n) => e.has(t) || O("Cannot " + n);
7
+ var R = (t, e, n) => (A(t, e, "read from private field"), n ? n.call(t) : e.get(t)), b = (t, e, n) => e.has(t) ? O("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, n), S = (t, e, n, r) => (A(t, e, "write to private field"), r ? r.call(t, n) : e.set(t, n), n);
8
8
  import { jsx as p, Fragment as F } from "react/jsx-runtime";
9
9
  import { Immer as T } from "immer";
10
10
  import v from "lodash/get";
11
- import M from "traverse";
11
+ import x from "traverse";
12
12
  import * as a from "react";
13
- import j from "eventemitter3";
13
+ import D from "eventemitter3";
14
14
  import * as h from "react-router-dom";
15
15
  class C {
16
16
  constructor(e) {
@@ -35,7 +35,7 @@ const l = {
35
35
  annotations: Symbol("annotations")
36
36
  };
37
37
  l.immer.setAutoFreeze(!1);
38
- class P {
38
+ class M {
39
39
  constructor(e, n, r = null) {
40
40
  E(this, "process");
41
41
  this.value = e, this.operations = n, this.field = r, this.process = null;
@@ -45,9 +45,9 @@ class P {
45
45
  }
46
46
  }
47
47
  function J(t, e = []) {
48
- return new P(t, e);
48
+ return new M(t, e);
49
49
  }
50
- class D {
50
+ class k {
51
51
  constructor(e, n = e) {
52
52
  this.stateless = e, this.stateful = n;
53
53
  }
@@ -61,21 +61,21 @@ function I(t, e = []) {
61
61
  switch (r) {
62
62
  case "is":
63
63
  return (s) => {
64
- const o = x(t, e);
64
+ const o = P(t, e);
65
65
  if (!o) return !1;
66
66
  const u = new Set(
67
67
  o.flatMap((i) => i.operations)
68
68
  );
69
69
  return !!(Array.from(u).reduce(
70
- (i, d) => i | (d ?? 0),
70
+ (i, f) => i | (f ?? 0),
71
71
  0
72
72
  ) & s);
73
73
  };
74
74
  case "pending":
75
- return () => !!x(t, e);
75
+ return () => !!P(t, e);
76
76
  case "draft":
77
77
  return () => {
78
- const s = x(t, e);
78
+ const s = P(t, e);
79
79
  if (!s) return v(t, e);
80
80
  const o = s.flatMap((u) => u.operations).find((u) => u instanceof C);
81
81
  return o ? o.value : v(t, e);
@@ -85,48 +85,48 @@ function I(t, e = []) {
85
85
  }
86
86
  });
87
87
  }
88
- function x(t, e) {
88
+ function P(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 M(o).forEach(function() {
94
+ return x(o).forEach(function() {
95
95
  if (this.key === l.annotations) {
96
96
  this.block();
97
97
  return;
98
98
  }
99
- this.node instanceof P && this.update(this.node.value);
99
+ this.node instanceof M && this.update(this.node.value);
100
100
  });
101
101
  }
102
102
  function s(o) {
103
- return M(o).forEach(function() {
103
+ return x(o).forEach(function() {
104
104
  if (this.key === l.annotations) {
105
105
  this.block();
106
106
  return;
107
107
  }
108
- if (this.node instanceof P) {
108
+ if (this.node instanceof M) {
109
109
  const u = typeof this.node.value == "object", c = [
110
110
  ...u ? this.path : this.path.slice(0, -1),
111
111
  l.annotations
112
- ], i = v(t.stateful, c) ?? [], d = this.node.attach(e);
112
+ ], i = v(t.stateful, c) ?? [], f = this.node.attach(e);
113
113
  u ? this.update(
114
114
  {
115
115
  ...this.node.value,
116
- [l.annotations]: [d, ...i]
116
+ [l.annotations]: [f, ...i]
117
117
  },
118
118
  !0
119
- ) : (this.parent && (this.parent.node[l.annotations] = [d, ...i]), this.update(this.node.value, !0));
119
+ ) : (this.parent && (this.parent.node[l.annotations] = [f, ...i]), this.update(this.node.value, !0));
120
120
  }
121
121
  });
122
122
  }
123
- return new D(
123
+ return new k(
124
124
  r(l.immer.produce(t.stateless, n)),
125
125
  s(l.immer.produce(t.stateful, n))
126
126
  );
127
127
  }
128
- function A(t, e) {
129
- const n = M(t.stateful).forEach(function() {
128
+ function U(t, e) {
129
+ const n = x(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 D(t.stateless, n);
147
+ return new k(t.stateless, n);
148
148
  }
149
149
  function W(t) {
150
150
  return (e, n) => async (r = Promise.withResolvers(), s) => {
@@ -154,22 +154,22 @@ function W(t) {
154
154
  return void r.resolve();
155
155
  if (typeof u == "function") {
156
156
  const c = u(t.model.current, o);
157
- return t.model.current = A(c, o), t.update.rerender(), void r.resolve();
157
+ return t.model.current = U(c, o), t.update.rerender(), void r.resolve();
158
158
  }
159
159
  for (; ; ) {
160
160
  const { value: c, done: i } = await u.next();
161
161
  if (i) {
162
162
  const N = c(t.model.current, o);
163
- t.model.current = A(N, o), t.update.rerender();
163
+ t.model.current = U(N, o), t.update.rerender();
164
164
  break;
165
165
  }
166
- const d = c;
167
- t.model.current = d(t.model.current, o), t.update.rerender();
166
+ const f = c;
167
+ t.model.current = f(t.model.current, o), t.update.rerender();
168
168
  }
169
169
  r.resolve();
170
170
  };
171
171
  }
172
- function U(t) {
172
+ function _(t) {
173
173
  return t.startsWith("distributed");
174
174
  }
175
175
  var y, w;
@@ -200,14 +200,14 @@ function X(t) {
200
200
  function g(t) {
201
201
  return JSON.stringify(t);
202
202
  }
203
- const gt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
203
+ const yt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
204
204
  __proto__: null,
205
205
  hash: g,
206
206
  isEventError: X,
207
207
  pk: V,
208
208
  sleep: K
209
209
  }, Symbol.toStringTag, { value: "Module" }));
210
- function f(t, e) {
210
+ function d(t, e) {
211
211
  const n = a.useRef(null), r = a.useRef(null);
212
212
  return a.useMemo(() => {
213
213
  const s = g(e);
@@ -219,7 +219,7 @@ function f(t, e) {
219
219
  return n.current;
220
220
  }, e);
221
221
  }
222
- function _(t, e) {
222
+ function j(t, e) {
223
223
  const n = a.useRef(null);
224
224
  a.useEffect(() => {
225
225
  const r = g(e);
@@ -228,16 +228,16 @@ function _(t, e) {
228
228
  }, e);
229
229
  }
230
230
  const B = a.createContext({
231
- appEmitter: new j()
231
+ appEmitter: new D()
232
232
  });
233
233
  function H() {
234
234
  return a.useContext(B);
235
235
  }
236
236
  function Y(t) {
237
237
  return () => {
238
- const e = f(
238
+ const e = d(
239
239
  () => ({
240
- appEmitter: new j()
240
+ appEmitter: new D()
241
241
  }),
242
242
  []
243
243
  );
@@ -245,14 +245,14 @@ function Y(t) {
245
245
  };
246
246
  }
247
247
  function Z(t) {
248
- return f(
248
+ return d(
249
249
  () => ({
250
250
  controller: {
251
251
  get model() {
252
252
  return t.model.current.stateful;
253
253
  },
254
254
  get props() {
255
- return t.options.props;
255
+ return t.props.current;
256
256
  },
257
257
  get router() {
258
258
  return t.router.current;
@@ -277,7 +277,7 @@ function Z(t) {
277
277
  return t.model.current.stateless;
278
278
  },
279
279
  get props() {
280
- return t.options.props;
280
+ return t.props.current;
281
281
  },
282
282
  get validate() {
283
283
  return t.model.current.validatable;
@@ -295,7 +295,7 @@ function Z(t) {
295
295
  );
296
296
  }
297
297
  function L(t) {
298
- return f(() => {
298
+ return d(() => {
299
299
  var r, s;
300
300
  const e = (s = (r = t.options).controller) == null ? void 0 : s.call(r, t.actions.controller);
301
301
  return e ? (Object.entries(e).forEach(([o, u]) => t.dispatchers.attach(o, u)), e) : void 0;
@@ -303,51 +303,57 @@ function L(t) {
303
303
  }
304
304
  function tt(t) {
305
305
  const e = H(), n = W(t);
306
- return f(() => {
307
- const r = new j(), s = e.appEmitter;
306
+ return d(() => {
307
+ const r = new D(), s = e.appEmitter;
308
308
  return {
309
309
  attach(o, u) {
310
310
  const c = String(o);
311
- U(c) ? s.on(c, n(o, u)) : r.on(c, n(o, u));
311
+ _(c) ? s.on(c, n(o, u)) : r.on(c, n(o, u));
312
312
  },
313
313
  dispatch(o, u, c) {
314
314
  const i = String(o);
315
- U(i) ? s.emit(i, c, u) : r.emit(i, c, u);
315
+ _(i) ? s.emit(i, c, u) : r.emit(i, c, u);
316
316
  }
317
317
  };
318
318
  }, []);
319
319
  }
320
320
  function et() {
321
321
  const t = a.useRef(null);
322
- return f(() => ({ customElement: t }), []);
322
+ return d(() => ({ customElement: t }), []);
323
323
  }
324
324
  function nt(t) {
325
325
  var e, n, r, s;
326
- _(() => {
327
- t.dispatchers.dispatch(m.Derive, []), t.update.rerender();
326
+ j(() => {
327
+ t.dispatchers.dispatch(m.Derive, []);
328
328
  }, [
329
329
  t.options.props,
330
330
  (e = t.router.current) == null ? void 0 : e.location,
331
331
  (n = t.router.current) == null ? void 0 : n.params,
332
332
  (s = (r = t.router.current) == null ? void 0 : r.search) == null ? void 0 : s[0]
333
- ]), _(() => (t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
333
+ ]), j(() => (t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
334
334
  t.elements.customElement.current
335
335
  ]), () => t.dispatchers.dispatch(m.Unmount, [])), []);
336
336
  }
337
337
  function rt(t) {
338
- const e = f(() => t.options.model ?? {}, []);
339
- return a.useRef(new D(e, e));
338
+ const e = d(() => t.options.model ?? {}, []);
339
+ return a.useRef(new k(e, e));
340
340
  }
341
- function ot() {
342
- return a.useRef(/* @__PURE__ */ new Set());
341
+ function ot(t) {
342
+ const e = a.useRef(t.options.props);
343
+ return j(() => {
344
+ e.current = t.options.props, t.update.rerender();
345
+ }, [t.options.props]), e;
343
346
  }
344
347
  function st() {
345
- return a.useRef(null);
348
+ return a.useRef(/* @__PURE__ */ new Set());
346
349
  }
347
- function ut({ using: t, children: e }) {
348
- return h.useInRouterContext() ? /* @__PURE__ */ p(ct, { using: t, children: e }) : e();
350
+ function ut() {
351
+ return a.useRef(null);
349
352
  }
350
353
  function ct({ using: t, children: e }) {
354
+ return h.useInRouterContext() ? /* @__PURE__ */ p(it, { using: t, children: e }) : e();
355
+ }
356
+ function it({ using: t, children: e }) {
351
357
  return t.current = {
352
358
  navigate: h.useNavigate(),
353
359
  location: h.useLocation(),
@@ -355,23 +361,30 @@ function ct({ using: t, children: e }) {
355
361
  search: h.useSearchParams()
356
362
  }, /* @__PURE__ */ p(F, { children: e() });
357
363
  }
358
- function it() {
364
+ function at() {
359
365
  const [t, e] = a.useReducer((n) => n + 1, 0);
360
- return f(() => ({ hash: t, rerender: e }), [t]);
366
+ return d(() => ({ hash: t, rerender: e }), [t]);
361
367
  }
362
- function at({
368
+ function lt({
363
369
  options: t
364
370
  }) {
365
- 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 });
366
- return L({ options: t, dispatchers: c, actions: i }), nt({ options: t, dispatchers: c, elements: s, router: o, update: n }), f(() => a.createElement(t.name, {
371
+ const e = H(), n = at(), r = st(), s = et(), o = ut(), u = ot({ options: t, update: n }), c = rt({ options: t }), i = tt({ app: e, options: t, update: n, model: c, queue: r }), f = Z({
372
+ app: e,
373
+ options: t,
374
+ model: c,
375
+ dispatchers: i,
376
+ router: o,
377
+ props: u
378
+ });
379
+ return L({ options: t, dispatchers: i, actions: f }), nt({ options: t, dispatchers: i, elements: s, router: o, update: n }), d(() => a.createElement(t.name, {
367
380
  ref: s.customElement,
368
381
  style: { display: "contents" },
369
- children: /* @__PURE__ */ p(ut, { using: o, children: () => t.view(i.view) })
382
+ children: /* @__PURE__ */ p(ct, { using: o, children: () => t.view(f.view) })
370
383
  }), [n.hash]);
371
384
  }
372
- function lt(t) {
385
+ function ft(t) {
373
386
  return (e) => a.memo(
374
- (n) => at({
387
+ (n) => lt({
375
388
  options: {
376
389
  ...e,
377
390
  name: t.join(""),
@@ -381,11 +394,11 @@ function lt(t) {
381
394
  (n, r) => g(n) === g(r)
382
395
  );
383
396
  }
384
- const yt = { app: Y, module: lt };
397
+ const wt = { app: Y, module: ft };
385
398
  export {
386
399
  G as EventError,
387
400
  m as Lifecycle,
388
401
  $ as State,
389
- yt as create,
390
- gt as utils
402
+ wt as create,
403
+ yt as utils
391
404
  };
@@ -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,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($),g=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 w(e){return JSON.stringify(e)}const Y=Object.freeze(Object.defineProperty({__proto__:null,hash:w,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=w(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=w(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 props(){return e.options.props},get router(){return e.router.current},queue:[],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 props(){return e.options.props},get validate(){return e.model.current.validatable},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 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,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)=>w(n)===w(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"})});
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,a,p,E,$,S,J){"use strict";var pe=Object.defineProperty;var H=s=>{throw TypeError(s)};var ve=(s,u,a)=>u in s?pe(s,u,{enumerable:!0,configurable:!0,writable:!0,value:a}):s[u]=a;var D=(s,u,a)=>ve(s,typeof u!="symbol"?u+"":u,a),F=(s,u,a)=>u.has(s)||H("Cannot "+a);var _=(s,u,a)=>(F(s,u,"read from private field"),a?a.call(s):u.get(s)),x=(s,u,a)=>u.has(s)?H("Cannot add the same private member more than once"):u instanceof WeakSet?u.add(s):u.set(s,a),k=(s,u,a,p)=>(F(s,u,"write to private field"),p?p.call(s,a):u.set(s,a),a);var b,R;function q(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=q($),g=q(J);class T{constructor(t){this.value=t}}class A{static Draft(t){return new T(t)}}D(A,"Op",{Add:1,Remove:2,Update:4,Move:8,Replace:16});var y=(e=>(e.Mount="lifecycle/mount",e.Node="lifecycle/node",e.Derive="lifecycle/derive",e.Error="distributed/lifecycle/error",e.Unmount="lifecycle/unmount",e))(y||{});const h={immer:new a.Immer,annotations:Symbol("annotations")};h.immer.setAutoFreeze(!1);class j{constructor(t,n,r=null){D(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 P{constructor(t,n=t){this.stateless=t,this.stateful=n}get validatable(){return C(this.stateful)}}function C(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,m)=>f|(m??0),0)&c)};case"pending":return()=>!!O(e,t);case"draft":return()=>{const c=O(e,t);if(!c)return p(e,t);const o=c.flatMap(i=>i.operations).find(i=>i instanceof T);return o?o.value:p(e,t)}}return C(e,[...t,String(r)])}})}function O(e,t){const r=typeof p(e,t)=="object"?t:t.slice(0,-1),c=r.length===0?e:p(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 E(o).forEach(function(){if(this.key===h.annotations){this.block();return}this.node instanceof j&&this.update(this.node.value)})}function c(o){return E(o).forEach(function(){if(this.key===h.annotations){this.block();return}if(this.node instanceof j){const i=typeof this.node.value=="object",l=[...i?this.path:this.path.slice(0,-1),h.annotations],f=p(e.stateful,l)??[],m=this.node.attach(t);i?this.update({...this.node.value,[h.annotations]:[m,...f]},!0):(this.parent&&(this.parent.node[h.annotations]=[m,...f]),this.update(this.node.value,!0))}})}return new P(r(h.immer.produce(e.stateless,n)),c(h.immer.produce(e.stateful,n)))}function I(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 P(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 l=i(e.model.current,o);return e.model.current=I(l,o),e.update.rerender(),void r.resolve()}for(;;){const{value:l,done:f}=await i.next();if(f){const me=l(e.model.current,o);e.model.current=I(me,o),e.update.rerender();break}const m=l;e.model.current=m(e.model.current,o),e.update.rerender()}r.resolve()}}function U(e){return e.startsWith("distributed")}class N extends Error{constructor(n,r=null){super(String(r));x(this,b);x(this,R);k(this,b,n),k(this,R,r)}get type(){return _(this,b)}get message(){return _(this,R)||""}}b=new WeakMap,R=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 N}function w(e){return JSON.stringify(e)}const Y=Object.freeze(Object.defineProperty({__proto__:null,hash:w,isEventError:X,pk:V,sleep:K},Symbol.toStringTag,{value:"Module"}));function v(e,t){const n=d.useRef(null),r=d.useRef(null);return d.useMemo(()=>{const c=w(t);if(r.current!==c){r.current=c;const o=e();return n.current=o,o}return n.current},t)}function M(e,t){const n=d.useRef(null);d.useEffect(()=>{const r=w(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=v(()=>({appEmitter:new S}),[]);return u.jsx(z.Provider,{value:t,children:u.jsx(e,{})})}}function L(e){return v(()=>({controller:{get model(){return e.model.current.stateful},get props(){return e.props.current},get router(){return e.router.current},queue:[],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 props(){return e.props.current},get validate(){return e.model.current.validatable},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 v(()=>{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 v(()=>{const r=new S,c=t.appEmitter;return{attach(o,i){const l=String(o);U(l)?c.on(l,n(o,i)):r.on(l,n(o,i))},dispatch(o,i,l){const f=String(o);U(f)?c.emit(f,l,i):r.emit(f,l,i)}}},[])}function ne(){const e=d.useRef(null);return v(()=>({customElement:e}),[])}function re(e){var t,n,r,c;M(()=>{e.dispatchers.dispatch(y.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]]),M(()=>(e.dispatchers.dispatch(y.Mount,[]),e.dispatchers.dispatch(y.Node,[e.elements.customElement.current]),()=>e.dispatchers.dispatch(y.Unmount,[])),[])}function se(e){const t=v(()=>e.options.model??{},[]);return d.useRef(new P(t,t))}function oe(e){const t=d.useRef(e.options.props);return M(()=>{t.current=e.options.props,e.update.rerender()},[e.options.props]),t}function ue(){return d.useRef(new Set)}function ce(){return d.useRef(null)}function ie({using:e,children:t}){return g.useInRouterContext()?u.jsx(ae,{using:e,children:t}):t()}function ae({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 le(){const[e,t]=d.useReducer(n=>n+1,0);return v(()=>({hash:e,rerender:t}),[e])}function fe({options:e}){const t=B(),n=le(),r=ue(),c=ne(),o=ce(),i=oe({options:e,update:n}),l=se({options:e}),f=te({app:t,options:e,update:n,model:l,queue:r}),m=L({app:t,options:e,model:l,dispatchers:f,router:o,props:i});return ee({options:e,dispatchers:f,actions:m}),re({options:e,dispatchers:f,elements:c,router:o,update:n}),v(()=>d.createElement(e.name,{ref:c.customElement,style:{display:"contents"},children:u.jsx(ie,{using:o,children:()=>e.view(m.view)})}),[n.hash])}function de(e){return t=>d.memo(n=>fe({options:{...t,name:e.join(""),props:n}}),(n,r)=>w(n)===w(r))}const he={app:Z,module:de};s.EventError=N,s.Lifecycle=y,s.State=A,s.create=he,s.utils=Y,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
@@ -4,6 +4,7 @@ import { ModuleDefinition } from '../../../types/index.ts';
4
4
  import { ViewArgs } from '../../../view/types.ts';
5
5
  import { UseDispatchers } from '../dispatchers/types.ts';
6
6
  import { UseModel } from '../model/types.ts';
7
+ import { UseProps } from '../props/types.ts';
7
8
  import { UseRouter } from '../router/types.ts';
8
9
  import { UseOptions } from '../types.ts';
9
10
  export type Props<M extends ModuleDefinition> = {
@@ -12,6 +13,7 @@ export type Props<M extends ModuleDefinition> = {
12
13
  model: UseModel;
13
14
  dispatchers: UseDispatchers;
14
15
  router: UseRouter;
16
+ props: UseProps;
15
17
  };
16
18
  export type UseActions<M extends ModuleDefinition> = {
17
19
  controller: ControllerArgs<M>;
@@ -1,2 +1,2 @@
1
- import { default as useElements } from '.';
1
+ import { default as useElements } from './index.ts';
2
2
  export type UseElements = ReturnType<typeof useElements>;
@@ -0,0 +1,4 @@
1
+ import { ModuleDefinition } from '../../../types/index.ts';
2
+ import { Props } from './types.ts';
3
+ import * as React from "react";
4
+ export default function useProps<M extends ModuleDefinition>(props: Props<M>): React.RefObject<Readonly<M["Props"]>>;
@@ -0,0 +1,9 @@
1
+ import { ModuleDefinition } from '../../../types/index.ts';
2
+ import { UseOptions } from '../types.ts';
3
+ import { UseUpdate } from '../update/types.ts';
4
+ import { default as useProps } from './index.ts';
5
+ export type Props<M extends ModuleDefinition> = {
6
+ options: UseOptions<M>;
7
+ update: UseUpdate;
8
+ };
9
+ export type UseProps = ReturnType<typeof useProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chizu",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "type": "module",
5
5
  "main": "./dist/chizu.js",
6
6
  "types": "./dist/index.d.ts",