chizu 0.2.4 → 0.2.5

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