chizu 0.2.6 → 0.2.8

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,45 +1,28 @@
1
- var N = Object.defineProperty;
1
+ var J = Object.defineProperty;
2
2
  var k = (t) => {
3
3
  throw TypeError(t);
4
4
  };
5
- var B = (t, e, n) => e in t ? N(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
6
- var y = (t, e, n) => B(t, typeof e != "symbol" ? e + "" : e, n), j = (t, e, n) => e.has(t) || k("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) ? k("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 q } from "react/jsx-runtime";
9
- import x from "eventemitter3";
10
- import * as i from "react";
11
- import { Immer as z } from "immer";
12
- import p from "lodash/get";
13
- import M from "traverse";
14
- import * as f from "react-router-dom";
15
- const A = i.createContext({
16
- appEmitter: new x()
17
- });
18
- function U() {
19
- return i.useContext(A);
20
- }
21
- function F(t) {
22
- return () => {
23
- const e = i.useMemo(
24
- () => ({
25
- appEmitter: new x()
26
- }),
27
- []
28
- );
29
- return /* @__PURE__ */ h(A.Provider, { value: e, children: /* @__PURE__ */ h(t, {}) });
30
- };
31
- }
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";
10
+ import v from "lodash/get";
11
+ import x from "traverse";
12
+ import * as a from "react";
13
+ import P from "eventemitter3";
14
+ import * as h from "react-router-dom";
32
15
  class _ {
33
16
  constructor(e) {
34
17
  this.value = e;
35
18
  }
36
19
  }
37
- class J {
20
+ class T {
38
21
  static Draft(e) {
39
22
  return new _(e);
40
23
  }
41
24
  }
42
- y(J, "Op", {
25
+ w(T, "Op", {
43
26
  Add: 1,
44
27
  Remove: 2,
45
28
  Update: 4,
@@ -48,23 +31,23 @@ y(J, "Op", {
48
31
  });
49
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 || {});
50
33
  const l = {
51
- immer: new z(),
34
+ immer: new F(),
52
35
  annotations: Symbol("annotations")
53
36
  };
54
37
  l.immer.setAutoFreeze(!1);
55
- class S {
38
+ class M {
56
39
  constructor(e, n, r = null) {
57
- y(this, "process");
40
+ w(this, "process");
58
41
  this.value = e, this.operations = n, this.field = r, this.process = null;
59
42
  }
60
43
  attach(e) {
61
44
  return this.process = e, this;
62
45
  }
63
46
  }
64
- function T(t, e = []) {
65
- return new S(t, e);
47
+ function $(t, e = []) {
48
+ return new M(t, e);
66
49
  }
67
- class P {
50
+ class O {
68
51
  constructor(e, n = e) {
69
52
  this.stateless = e, this.stateful = n;
70
53
  }
@@ -78,72 +61,72 @@ function C(t, e = []) {
78
61
  switch (r) {
79
62
  case "is":
80
63
  return (s) => {
81
- const o = R(t, e);
64
+ const o = S(t, e);
82
65
  if (!o) return !1;
83
66
  const u = new Set(
84
- o.flatMap((c) => c.operations)
67
+ o.flatMap((i) => i.operations)
85
68
  );
86
69
  return !!(Array.from(u).reduce(
87
- (c, d) => c | (d ?? 0),
70
+ (i, d) => i | (d ?? 0),
88
71
  0
89
72
  ) & s);
90
73
  };
91
74
  case "pending":
92
- return () => !!R(t, e);
75
+ return () => !!S(t, e);
93
76
  case "draft":
94
77
  return () => {
95
- const s = R(t, e);
96
- if (!s) return p(t, e);
78
+ const s = S(t, e);
79
+ if (!s) return v(t, e);
97
80
  const o = s.flatMap((u) => u.operations).find((u) => u instanceof _);
98
- return o ? o.value : p(t, e);
81
+ return o ? o.value : v(t, e);
99
82
  };
100
83
  }
101
84
  return C(t, [...e, String(r)]);
102
85
  }
103
86
  });
104
87
  }
105
- function R(t, e) {
106
- const r = typeof p(t, e) == "object" ? e : e.slice(0, -1), s = r.length === 0 ? t : p(t, r), o = (s == null ? void 0 : s[l.annotations]) ?? [];
88
+ function S(t, e) {
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]) ?? [];
107
90
  return o.length > 0 ? o : null;
108
91
  }
109
- function $(t, e, n) {
92
+ function Q(t, e, n) {
110
93
  function r(o) {
111
- return M(o).forEach(function() {
94
+ return x(o).forEach(function() {
112
95
  if (this.key === l.annotations) {
113
96
  this.block();
114
97
  return;
115
98
  }
116
- this.node instanceof S && this.update(this.node.value);
99
+ this.node instanceof M && this.update(this.node.value);
117
100
  });
118
101
  }
119
102
  function s(o) {
120
- return M(o).forEach(function() {
103
+ return x(o).forEach(function() {
121
104
  if (this.key === l.annotations) {
122
105
  this.block();
123
106
  return;
124
107
  }
125
- if (this.node instanceof S) {
126
- const u = typeof this.node.value == "object", a = [
108
+ if (this.node instanceof M) {
109
+ const u = typeof this.node.value == "object", c = [
127
110
  ...u ? this.path : this.path.slice(0, -1),
128
111
  l.annotations
129
- ], c = p(t.stateful, a) ?? [], d = this.node.attach(e);
112
+ ], i = v(t.stateful, c) ?? [], d = this.node.attach(e);
130
113
  u ? this.update(
131
114
  {
132
115
  ...this.node.value,
133
- [l.annotations]: [d, ...c]
116
+ [l.annotations]: [d, ...i]
134
117
  },
135
118
  !0
136
- ) : (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));
137
120
  }
138
121
  });
139
122
  }
140
- return new P(
123
+ return new O(
141
124
  r(l.immer.produce(t.stateless, n)),
142
125
  s(l.immer.produce(t.stateful, n))
143
126
  );
144
127
  }
145
- function D(t, e) {
146
- const n = M(t.stateful).forEach(function() {
128
+ function A(t, e) {
129
+ const n = x(t.stateful).forEach(function() {
147
130
  if (this.key === l.annotations) {
148
131
  this.block();
149
132
  return;
@@ -161,10 +144,106 @@ function D(t, e) {
161
144
  );
162
145
  }
163
146
  });
164
- return new P(t.stateless, n);
147
+ return new O(t.stateless, n);
148
+ }
149
+ function W(t) {
150
+ return (e, n) => async (r = Promise.withResolvers(), s) => {
151
+ if (typeof n != "function") return;
152
+ const o = Symbol("process"), u = n(...s);
153
+ if (typeof u == "function") {
154
+ const c = u(t.model.current, o);
155
+ return t.model.current = A(c, o), t.update.rerender(), void r.resolve();
156
+ }
157
+ for (; ; ) {
158
+ const { value: c, done: i } = await u.next();
159
+ if (i) {
160
+ const H = c(t.model.current, o);
161
+ t.model.current = A(H, o), t.update.rerender();
162
+ break;
163
+ }
164
+ const d = c;
165
+ t.model.current = d(t.model.current, o), t.update.rerender();
166
+ }
167
+ r.resolve();
168
+ };
169
+ }
170
+ function N(t) {
171
+ return t.startsWith("distributed");
172
+ }
173
+ var g, y;
174
+ class G extends Error {
175
+ constructor(n, r = null) {
176
+ super(String(r));
177
+ R(this, g);
178
+ R(this, y);
179
+ b(this, g, n), b(this, y, r);
180
+ }
181
+ get type() {
182
+ return E(this, g);
183
+ }
184
+ get message() {
185
+ return E(this, y) || "";
186
+ }
187
+ }
188
+ g = new WeakMap(), y = new WeakMap();
189
+ function K(t) {
190
+ return new Promise((e) => setTimeout(e, t));
191
+ }
192
+ function V(t) {
193
+ return t ? !!(t && typeof t != "symbol") : Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`);
194
+ }
195
+ function X(t) {
196
+ return t instanceof G;
197
+ }
198
+ function j(t) {
199
+ return JSON.stringify(t);
200
+ }
201
+ const gt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
202
+ __proto__: null,
203
+ hash: j,
204
+ isEventError: X,
205
+ pk: V,
206
+ sleep: K
207
+ }, Symbol.toStringTag, { value: "Module" }));
208
+ function f(t, e) {
209
+ const n = a.useRef(null), r = a.useRef(null);
210
+ return a.useMemo(() => {
211
+ const s = j(e);
212
+ if (r.current !== s) {
213
+ r.current = s;
214
+ const o = t();
215
+ return n.current = o, o;
216
+ }
217
+ return n.current;
218
+ }, e);
219
+ }
220
+ function U(t, e) {
221
+ const n = a.useRef(null);
222
+ a.useEffect(() => {
223
+ const r = j(e);
224
+ if (n.current !== r)
225
+ return n.current = r, t();
226
+ }, e);
165
227
  }
166
- function Q(t) {
167
- return i.useMemo(
228
+ const I = a.createContext({
229
+ appEmitter: new P()
230
+ });
231
+ function B() {
232
+ return a.useContext(I);
233
+ }
234
+ function Y(t) {
235
+ return () => {
236
+ const e = f(
237
+ () => ({
238
+ appEmitter: new P()
239
+ }),
240
+ []
241
+ );
242
+ return /* @__PURE__ */ p(I.Provider, { value: e, children: /* @__PURE__ */ p(t, {}) });
243
+ };
244
+ }
245
+ function Z(t) {
246
+ return f(
168
247
  () => ({
169
248
  controller: {
170
249
  get model() {
@@ -174,14 +253,13 @@ function Q(t) {
174
253
  return t.router.current;
175
254
  },
176
255
  queue: [],
177
- handlers: t.options.props,
178
- attributes: t.options.props,
256
+ props: t.options.props,
179
257
  actions: {
180
258
  annotate(e, n) {
181
- return T(e, n);
259
+ return $(e, n);
182
260
  },
183
261
  produce(e) {
184
- return (n, r) => $(n, r, e);
262
+ return (n, r) => Q(n, r, e);
185
263
  },
186
264
  dispatch([e, ...n]) {
187
265
  if (e == null) return Promise.reject();
@@ -197,8 +275,7 @@ function Q(t) {
197
275
  get validate() {
198
276
  return t.model.current.validatable;
199
277
  },
200
- handlers: t.options.props,
201
- attributes: t.options.props,
278
+ props: t.options.props,
202
279
  actions: {
203
280
  dispatch([e, ...n]) {
204
281
  if (e == null) return Promise.reject();
@@ -211,131 +288,84 @@ function Q(t) {
211
288
  []
212
289
  );
213
290
  }
214
- function W(t) {
215
- return i.useMemo(() => {
291
+ function L(t) {
292
+ return f(() => {
216
293
  var r, s;
217
294
  const e = (s = (r = t.options).controller) == null ? void 0 : s.call(r, t.actions.controller);
218
295
  return e ? (Object.entries(e).forEach(([o, u]) => t.dispatchers.attach(o, u)), e) : void 0;
219
296
  }, []);
220
297
  }
221
- function G(t) {
222
- return (e, n) => async (r = Promise.withResolvers(), s) => {
223
- if (typeof n != "function") return;
224
- const o = Symbol("process"), u = n(...s);
225
- if (typeof u == "function") {
226
- const a = u(t.model.current, o);
227
- return t.model.current = D(a, o), t.update.rerender(), void r.resolve();
228
- }
229
- for (; ; ) {
230
- const { value: a, done: c } = await u.next();
231
- if (c) {
232
- const I = a(t.model.current, o);
233
- t.model.current = D(I, o), t.update.rerender();
234
- break;
235
- }
236
- const d = a;
237
- t.model.current = d(t.model.current, o), t.update.rerender();
238
- }
239
- r.resolve();
240
- };
241
- }
242
- function O(t) {
243
- return t.startsWith("distributed");
244
- }
245
- var v, g;
246
- class H extends Error {
247
- constructor(n, r = null) {
248
- super(String(r));
249
- b(this, v);
250
- b(this, g);
251
- E(this, v, n), E(this, g, r);
252
- }
253
- get type() {
254
- return w(this, v);
255
- }
256
- get message() {
257
- return w(this, g) || "";
258
- }
259
- }
260
- v = new WeakMap(), g = new WeakMap();
261
- function K(t) {
262
- const e = U(), n = G(t);
263
- return i.useMemo(() => {
264
- const r = new x(), s = e.appEmitter;
298
+ function tt(t) {
299
+ const e = B(), n = W(t);
300
+ return f(() => {
301
+ const r = new P(), s = e.appEmitter;
265
302
  return {
266
303
  attach(o, u) {
267
- const a = String(o);
268
- O(a) ? s.on(a, n(o, u)) : r.on(a, n(o, u));
269
- },
270
- dispatch(o, u, a) {
271
304
  const c = String(o);
272
- O(c) ? s.emit(c, a, u) : r.emit(c, a, u);
305
+ N(c) ? s.on(c, n(o, u)) : r.on(c, n(o, u));
306
+ },
307
+ dispatch(o, u, c) {
308
+ const i = String(o);
309
+ N(i) ? s.emit(i, c, u) : r.emit(i, c, u);
273
310
  }
274
311
  };
275
312
  }, []);
276
313
  }
277
- function L() {
278
- const t = i.useRef(null);
279
- return i.useMemo(() => ({ customElement: t }), []);
314
+ function et() {
315
+ const t = a.useRef(null);
316
+ return f(() => ({ customElement: t }), []);
280
317
  }
281
- function V(t) {
282
- var n, r, s, o;
283
- const e = i.useRef(!1);
284
- i.useLayoutEffect(() => {
285
- t.dispatchers.dispatch(m.Derive, [
286
- t.options.props,
287
- t.router.current
288
- ]);
318
+ function nt(t) {
319
+ var e, n, r, s;
320
+ U(() => {
321
+ t.dispatchers.dispatch(m.Derive, []);
289
322
  }, [
290
323
  t.options.props,
291
- (n = t.router.current) == null ? void 0 : n.location,
292
- (r = t.router.current) == null ? void 0 : r.params,
293
- (o = (s = t.router.current) == null ? void 0 : s.search) == null ? void 0 : o[0]
294
- ]), i.useLayoutEffect(() => {
295
- if (!e.current)
296
- return e.current = !0, t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
297
- t.elements.customElement.current
298
- ]), () => t.dispatchers.dispatch(m.Unmount, []);
299
- }, []);
324
+ (e = t.router.current) == null ? void 0 : e.location,
325
+ (n = t.router.current) == null ? void 0 : n.params,
326
+ (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, [
328
+ t.elements.customElement.current
329
+ ]), () => t.dispatchers.dispatch(m.Unmount, [])), []);
300
330
  }
301
- function X(t) {
302
- const e = i.useMemo(() => t.options.model ?? {}, []);
303
- return i.useRef(new P(e, e));
331
+ function rt(t) {
332
+ const e = f(() => t.options.model ?? {}, []);
333
+ return a.useRef(new O(e, e));
304
334
  }
305
- function Y() {
306
- return i.useRef(/* @__PURE__ */ new Set());
335
+ function ot() {
336
+ return a.useRef(/* @__PURE__ */ new Set());
307
337
  }
308
- function Z() {
309
- return i.useRef(null);
338
+ function st() {
339
+ return a.useRef(null);
310
340
  }
311
- function tt({ using: t, children: e }) {
312
- return f.useInRouterContext() ? /* @__PURE__ */ h(et, { using: t, children: e }) : e();
341
+ function ut({ using: t, children: e }) {
342
+ return h.useInRouterContext() ? /* @__PURE__ */ p(ct, { using: t, children: e }) : e();
313
343
  }
314
- function et({ using: t, children: e }) {
344
+ function ct({ using: t, children: e }) {
315
345
  return t.current = {
316
- navigate: f.useNavigate(),
317
- location: f.useLocation(),
318
- params: f.useParams(),
319
- search: f.useSearchParams()
320
- }, /* @__PURE__ */ h(q, { children: e() });
346
+ navigate: h.useNavigate(),
347
+ location: h.useLocation(),
348
+ params: h.useParams(),
349
+ search: h.useSearchParams()
350
+ }, /* @__PURE__ */ p(z, { children: e() });
321
351
  }
322
- function nt() {
323
- const [t, e] = i.useReducer((n) => n + 1, 0);
324
- return i.useMemo(() => ({ hash: t, rerender: e }), [t]);
352
+ function it() {
353
+ const [t, e] = a.useReducer((n) => n + 1, 0);
354
+ return f(() => ({ hash: t, rerender: e }), [t]);
325
355
  }
326
- function rt({
356
+ function at({
327
357
  options: t
328
358
  }) {
329
- const e = U(), 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 });
330
- return W({ options: t, dispatchers: a, actions: c }), V({ options: t, dispatchers: a, elements: s, router: o }), i.useMemo(() => i.createElement(t.name, {
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, {
331
361
  ref: s.customElement,
332
362
  style: { display: "contents" },
333
- children: /* @__PURE__ */ h(tt, { using: o, children: () => t.view(c.view) })
363
+ children: /* @__PURE__ */ p(ut, { using: o, children: () => t.view(i.view) })
334
364
  }), [n.hash]);
335
365
  }
336
- function ot(t) {
337
- return (e) => i.memo(
338
- (n) => rt({
366
+ function lt(t) {
367
+ return (e) => a.memo(
368
+ (n) => at({
339
369
  options: {
340
370
  ...e,
341
371
  name: t.join(""),
@@ -345,25 +375,11 @@ function ot(t) {
345
375
  (n, r) => JSON.stringify(n) === JSON.stringify(r)
346
376
  );
347
377
  }
348
- function st(t) {
349
- return new Promise((e) => setTimeout(e, t));
350
- }
351
- function ut(t) {
352
- return t ? !!(t && typeof t != "symbol") : Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`);
353
- }
354
- function it(t) {
355
- return t instanceof H;
356
- }
357
- const ht = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
358
- __proto__: null,
359
- isEventError: it,
360
- pk: ut,
361
- sleep: st
362
- }, Symbol.toStringTag, { value: "Module" })), pt = { app: F, module: ot };
378
+ const yt = { app: Y, module: lt };
363
379
  export {
364
- H as EventError,
380
+ G as EventError,
365
381
  m as Lifecycle,
366
- J as State,
367
- pt as create,
368
- ht as utils
382
+ T as State,
383
+ yt as create,
384
+ gt as utils
369
385
  };
@@ -1 +1 @@
1
- (function(o,u){typeof exports=="object"&&typeof module<"u"?u(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"],u):(o=typeof globalThis<"u"?globalThis:o||self,u(o.Chizu={},o.jsxRuntime,o.EventEmitter3,o.React,o.Immer,o.get,o.Traverse,o.ReactRouterDOM))})(this,function(o,u,l,w,B,v,E,F){"use strict";var de=Object.defineProperty;var U=o=>{throw TypeError(o)};var fe=(o,u,l)=>u in o?de(o,u,{enumerable:!0,configurable:!0,writable:!0,value:l}):o[u]=l;var j=(o,u,l)=>fe(o,typeof u!="symbol"?u+"":u,l),z=(o,u,l)=>u.has(o)||U("Cannot "+l);var O=(o,u,l)=>(z(o,u,"read from private field"),l?l.call(o):u.get(o)),P=(o,u,l)=>u.has(o)?U("Cannot add the same private member more than once"):u instanceof WeakSet?u.add(o):u.set(o,l),D=(o,u,l,w)=>(z(o,u,"write to private field"),w?w.call(o,l):u.set(o,l),l);var g,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 a=k(w),y=k(F),_=a.createContext({appEmitter:new l});function x(){return a.useContext(_)}function J(e){return()=>{const t=a.useMemo(()=>({appEmitter:new l}),[]);return u.jsx(_.Provider,{value:t,children:u.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 B.Immer,annotations:Symbol("annotations")};h.immer.setAutoFreeze(!1);class M{constructor(t,n,r=null){j(this,"process");this.value=t,this.operations=n,this.field=r,this.process=null}attach(t){return this.process=t,this}}function $(e,t=[]){return new M(e,t)}class S{constructor(t,n=t){this.stateless=t,this.stateful=n}get validatable(){return A(this.stateful)}}function A(e,t=[]){return new Proxy(e,{get(n,r){switch(r){case"is":return i=>{const s=R(e,t);if(!s)return!1;const c=new Set(s.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 s=i.flatMap(c=>c.operations).find(c=>c instanceof q);return s?s.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),s=(i==null?void 0:i[h.annotations])??[];return s.length>0?s:null}function L(e,t,n){function r(s){return E(s).forEach(function(){if(this.key===h.annotations){this.block();return}this.node instanceof M&&this.update(this.node.value)})}function i(s){return E(s).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 C(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 $(t,n)},produce(t){return(n,r)=>L(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},handlers:e.options.props,attributes: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 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(([s,c])=>e.dispatchers.attach(s,c)),t):void 0},[])}function G(e){return(t,n)=>async(r=Promise.withResolvers(),i)=>{if(typeof n!="function")return;const s=Symbol("process"),c=n(...i);if(typeof c=="function"){const d=c(e.model.current,s);return e.model.current=C(d,s),e.update.rerender(),void r.resolve()}for(;;){const{value:d,done:f}=await c.next();if(f){const le=d(e.model.current,s);e.model.current=C(le,s),e.update.rerender();break}const p=d;e.model.current=p(e.model.current,s),e.update.rerender()}r.resolve()}}function I(e){return e.startsWith("distributed")}class N extends Error{constructor(n,r=null){super(String(r));P(this,g);P(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(s,c){const d=String(s);I(d)?i.on(d,n(s,c)):r.on(d,n(s,c))},dispatch(s,c,d){const f=String(s);I(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,s;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,(s=(i=e.router.current)==null?void 0:i.search)==null?void 0:s[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()?u.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()},u.jsx(u.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(),s=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:s});return W({options:e,dispatchers:d,actions:f}),V({options:e,dispatchers:d,elements:i,router:s}),a.useMemo(()=>a.createElement(e.name,{ref:i.customElement,style:{display:"contents"},children:u.jsx(ee,{using:s,children:()=>e.view(f.view)})}),[n.hash])}function oe(e){return t=>a.memo(n=>re({options:{...t,name:e.join(""),props:n}}),(n,r)=>JSON.stringify(n)===JSON.stringify(r))}function se(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 N}const ce=Object.freeze(Object.defineProperty({__proto__:null,isEventError:ie,pk:ue,sleep:se},Symbol.toStringTag,{value:"Module"})),ae={app:J,module:oe};o.EventError=N,o.Lifecycle=m,o.State=T,o.create=ae,o.utils=ce,Object.defineProperty(o,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,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,7 +1,7 @@
1
1
  import { EventError } from '../module/renderer/dispatchers/utils.ts';
2
2
  import { Models } from '../module/renderer/model/utils.ts';
3
3
  import { Head } from '../module/renderer/types.ts';
4
- import { Actions, Attributes, Draft, Handlers, Lifecycle, ModuleDefinition, Op, Query, Queue } from '../types/index.ts';
4
+ import { Actions, Draft, Lifecycle, ModuleDefinition, Op, Query, Queue } from '../types/index.ts';
5
5
  import * as Router from "../module/renderer/router/types.ts";
6
6
  export type ControllerActions<M extends ModuleDefinition> = {
7
7
  annotate<T>(value: T, operations?: (Op | Draft<T>)[]): T;
@@ -13,8 +13,7 @@ export type ControllerArgs<M extends ModuleDefinition> = Readonly<{
13
13
  queue: Readonly<Queue<M["Actions"]>>;
14
14
  router: M["Query"] extends NonNullable<Query> ? Readonly<Router.Context<M["Query"]>> : null;
15
15
  actions: Readonly<ControllerActions<M>>;
16
- handlers: Readonly<Handlers<M["Props"]>>;
17
- attributes: Readonly<Attributes<M["Props"]>>;
16
+ props: Readonly<M["Props"]>;
18
17
  }>;
19
18
  export type ActionEvent<M extends ModuleDefinition> = (...args: M["Actions"][number]) => ActionGenerator<M>;
20
19
  type ActionEvents<M extends ModuleDefinition> = {
@@ -24,7 +23,7 @@ export type ActionGenerator<M extends ModuleDefinition> = ((models: Models<M["Mo
24
23
  export type ControllerDefinition<M extends ModuleDefinition> = (controller: ControllerArgs<M>) => ControllerInstance<M>;
25
24
  export type ControllerInstance<M extends ModuleDefinition> = {
26
25
  [Lifecycle.Mount]?(): ActionGenerator<M>;
27
- [Lifecycle.Derive]?(attributes: Attributes<M["Props"]>, routes: null | Router.Context<M["Query"]>): ActionGenerator<M>;
26
+ [Lifecycle.Derive]?(): ActionGenerator<M>;
28
27
  [Lifecycle.Node]?(tree: HTMLElement): ActionGenerator<M>;
29
28
  [Lifecycle.Error]?(error: Error | EventError): ActionGenerator<M>;
30
29
  [Lifecycle.Unmount]?(): ActionGenerator<M>;
@@ -0,0 +1,16 @@
1
+ import * as React from "react";
2
+ /**
3
+ * Optimises the memoisation of a value based on its dependencies.
4
+ *
5
+ * @param factory {() => T}
6
+ * @param dependencies {React.DependencyList}
7
+ * @returns {T}
8
+ */
9
+ export declare function useOptimisedMemo<T>(factory: () => T, dependencies: React.DependencyList): T;
10
+ /**
11
+ * Optimises the execution of an effect based on its dependencies.
12
+ *
13
+ * @param effect {React.EffectCallback}
14
+ * @param dependencies {React.DependencyList}
15
+ */
16
+ export declare function useOptimisedEffect(effect: React.EffectCallback, dependencies: React.DependencyList): void;
@@ -48,18 +48,6 @@ export type ModuleDefinition = {
48
48
  Props: Props;
49
49
  Query: Query;
50
50
  };
51
- type Fns<P extends Props> = {
52
- [K in keyof P]: P[K] extends (...args: any[]) => any ? P[K] : never;
53
- };
54
- type NonFns<P extends Props> = {
55
- [K in keyof P]: P[K] extends (...args: any[]) => any ? never : P[K];
56
- };
57
- export type Handlers<P extends Props> = {
58
- [K in keyof Fns<P> as Fns<P>[K] extends never ? never : K]: Fns<P>[K];
59
- };
60
- export type Attributes<P extends Props> = {
61
- [K in keyof NonFns<P> as NonFns<P>[K] extends never ? never : K]: NonFns<P>[K];
62
- };
63
51
  export type Pk<T> = undefined | Symbol | T;
64
52
  export type Queue<A extends ModuleDefinition["Actions"]> = {
65
53
  name: Head<A>;
@@ -4,3 +4,4 @@ export { default as sleep } from './sleep/index.ts';
4
4
  export declare function pk<T>(): Symbol;
5
5
  export declare function pk<T>(id: Pk<T>): boolean;
6
6
  export declare function isEventError(error: Error | EventError): error is EventError;
7
+ export declare function hash<T>(x: T): string;
@@ -1,5 +1,5 @@
1
1
  import { Validatable } from '../module/renderer/model/types.ts';
2
- import { Attributes, Handlers, ModuleDefinition } from '../types/index.ts';
2
+ import { ModuleDefinition } from '../types/index.ts';
3
3
  import * as React from "react";
4
4
  export type ViewActions<M extends ModuleDefinition> = {
5
5
  dispatch(action: M["Actions"]): Promise<void>;
@@ -8,7 +8,6 @@ export type ViewArgs<M extends ModuleDefinition> = Readonly<{
8
8
  model: Readonly<M["Model"]>;
9
9
  validate: Readonly<Validatable<M["Model"]>>;
10
10
  actions: Readonly<ViewActions<M>>;
11
- handlers: Readonly<Handlers<M["Props"]>>;
12
- attributes: Readonly<Attributes<M["Props"]>>;
11
+ props: Readonly<M["Props"]>;
13
12
  }>;
14
13
  export type ViewDefinition<M extends ModuleDefinition> = (actions: ViewArgs<M>) => React.ReactNode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chizu",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "type": "module",
5
5
  "main": "./dist/chizu.js",
6
6
  "types": "./dist/index.d.ts",
@@ -1 +0,0 @@
1
- export {};