chizu 0.2.3 → 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 +16 -16
- package/dist/chizu.js +104 -132
- package/dist/chizu.umd.cjs +1 -1
- package/dist/index.d.ts +5 -7
- package/dist/module/renderer/dispatchers/types.d.ts +0 -2
- package/dist/types/index.d.ts +1 -1
- package/package.json +3 -3
- package/dist/module/renderer/logger/index.d.ts +0 -5
- package/dist/module/renderer/logger/types.d.ts +0 -10
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
|
|
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
|
|
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 – 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
|
|
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
|
|
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 – 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
|
|
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
|
|
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
|
|
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 – 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
|
|
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,51 +1,45 @@
|
|
|
1
1
|
var I = Object.defineProperty;
|
|
2
|
-
var
|
|
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
|
|
7
|
-
var w = (t, e, n) => (
|
|
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
8
|
import { jsx as h, Fragment as B } from "react/jsx-runtime";
|
|
9
|
-
import
|
|
10
|
-
import * as
|
|
9
|
+
import k from "eventemitter3";
|
|
10
|
+
import * as i from "react";
|
|
11
11
|
import { Immer as q } from "immer";
|
|
12
12
|
import p from "lodash/get";
|
|
13
13
|
import M from "traverse";
|
|
14
|
-
import * as
|
|
15
|
-
const O =
|
|
16
|
-
appEmitter: new
|
|
14
|
+
import * as d from "react-router-dom";
|
|
15
|
+
const O = i.createContext({
|
|
16
|
+
appEmitter: new k()
|
|
17
17
|
});
|
|
18
|
-
function
|
|
19
|
-
return
|
|
18
|
+
function A() {
|
|
19
|
+
return i.useContext(O);
|
|
20
20
|
}
|
|
21
21
|
function z(t) {
|
|
22
22
|
return () => {
|
|
23
|
-
const e =
|
|
23
|
+
const e = i.useMemo(
|
|
24
24
|
() => ({
|
|
25
|
-
appEmitter: new
|
|
25
|
+
appEmitter: new k()
|
|
26
26
|
}),
|
|
27
27
|
[]
|
|
28
28
|
);
|
|
29
29
|
return /* @__PURE__ */ h(O.Provider, { value: e, children: /* @__PURE__ */ h(t, {}) });
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
return t;
|
|
34
|
-
}
|
|
35
|
-
function J(t) {
|
|
36
|
-
return t;
|
|
37
|
-
}
|
|
38
|
-
class A {
|
|
32
|
+
class U {
|
|
39
33
|
constructor(e) {
|
|
40
34
|
this.value = e;
|
|
41
35
|
}
|
|
42
36
|
}
|
|
43
|
-
class
|
|
37
|
+
class F {
|
|
44
38
|
static Draft(e) {
|
|
45
|
-
return new
|
|
39
|
+
return new U(e);
|
|
46
40
|
}
|
|
47
41
|
}
|
|
48
|
-
|
|
42
|
+
y(F, "Op", {
|
|
49
43
|
Add: 1,
|
|
50
44
|
Remove: 2,
|
|
51
45
|
Update: 4,
|
|
@@ -60,25 +54,25 @@ const l = {
|
|
|
60
54
|
l.immer.setAutoFreeze(!1);
|
|
61
55
|
class S {
|
|
62
56
|
constructor(e, n, r = null) {
|
|
63
|
-
|
|
57
|
+
y(this, "process");
|
|
64
58
|
this.value = e, this.operations = n, this.field = r, this.process = null;
|
|
65
59
|
}
|
|
66
60
|
attach(e) {
|
|
67
61
|
return this.process = e, this;
|
|
68
62
|
}
|
|
69
63
|
}
|
|
70
|
-
function
|
|
64
|
+
function J(t, e = []) {
|
|
71
65
|
return new S(t, e);
|
|
72
66
|
}
|
|
73
|
-
class
|
|
67
|
+
class x {
|
|
74
68
|
constructor(e, n = e) {
|
|
75
69
|
this.stateless = e, this.stateful = n;
|
|
76
70
|
}
|
|
77
71
|
get validatable() {
|
|
78
|
-
return
|
|
72
|
+
return _(this.stateful);
|
|
79
73
|
}
|
|
80
74
|
}
|
|
81
|
-
function
|
|
75
|
+
function _(t, e = []) {
|
|
82
76
|
return new Proxy(t, {
|
|
83
77
|
get(n, r) {
|
|
84
78
|
switch (r) {
|
|
@@ -87,10 +81,10 @@ function C(t, e = []) {
|
|
|
87
81
|
const o = R(t, e);
|
|
88
82
|
if (!o) return !1;
|
|
89
83
|
const u = new Set(
|
|
90
|
-
o.flatMap((
|
|
84
|
+
o.flatMap((c) => c.operations)
|
|
91
85
|
);
|
|
92
86
|
return !!(Array.from(u).reduce(
|
|
93
|
-
(
|
|
87
|
+
(c, f) => c | (f ?? 0),
|
|
94
88
|
0
|
|
95
89
|
) & s);
|
|
96
90
|
};
|
|
@@ -100,11 +94,11 @@ function C(t, e = []) {
|
|
|
100
94
|
return () => {
|
|
101
95
|
const s = R(t, e);
|
|
102
96
|
if (!s) return p(t, e);
|
|
103
|
-
const o = s.flatMap((u) => u.operations).find((u) => u instanceof
|
|
97
|
+
const o = s.flatMap((u) => u.operations).find((u) => u instanceof U);
|
|
104
98
|
return o ? o.value : p(t, e);
|
|
105
99
|
};
|
|
106
100
|
}
|
|
107
|
-
return
|
|
101
|
+
return _(t, [...e, String(r)]);
|
|
108
102
|
}
|
|
109
103
|
});
|
|
110
104
|
}
|
|
@@ -112,7 +106,7 @@ function R(t, e) {
|
|
|
112
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]) ?? [];
|
|
113
107
|
return o.length > 0 ? o : null;
|
|
114
108
|
}
|
|
115
|
-
function
|
|
109
|
+
function T(t, e, n) {
|
|
116
110
|
function r(o) {
|
|
117
111
|
return M(o).forEach(function() {
|
|
118
112
|
if (this.key === l.annotations) {
|
|
@@ -132,23 +126,23 @@ function L(t, e, n) {
|
|
|
132
126
|
const u = typeof this.node.value == "object", a = [
|
|
133
127
|
...u ? this.path : this.path.slice(0, -1),
|
|
134
128
|
l.annotations
|
|
135
|
-
],
|
|
129
|
+
], c = p(t.stateful, a) ?? [], f = this.node.attach(e);
|
|
136
130
|
u ? this.update(
|
|
137
131
|
{
|
|
138
132
|
...this.node.value,
|
|
139
|
-
[l.annotations]: [
|
|
133
|
+
[l.annotations]: [f, ...c]
|
|
140
134
|
},
|
|
141
135
|
!0
|
|
142
|
-
) : (this.parent && (this.parent.node[l.annotations] = [
|
|
136
|
+
) : (this.parent && (this.parent.node[l.annotations] = [f, ...c]), this.update(this.node.value, !0));
|
|
143
137
|
}
|
|
144
138
|
});
|
|
145
139
|
}
|
|
146
|
-
return new
|
|
140
|
+
return new x(
|
|
147
141
|
r(l.immer.produce(t.stateless, n)),
|
|
148
142
|
s(l.immer.produce(t.stateful, n))
|
|
149
143
|
);
|
|
150
144
|
}
|
|
151
|
-
function
|
|
145
|
+
function $(t, e) {
|
|
152
146
|
const n = M(t.stateful).forEach(function() {
|
|
153
147
|
if (this.key === l.annotations) {
|
|
154
148
|
this.block();
|
|
@@ -167,10 +161,10 @@ function Q(t, e) {
|
|
|
167
161
|
);
|
|
168
162
|
}
|
|
169
163
|
});
|
|
170
|
-
return new
|
|
164
|
+
return new x(t.stateless, n);
|
|
171
165
|
}
|
|
172
|
-
function
|
|
173
|
-
return
|
|
166
|
+
function Q(t) {
|
|
167
|
+
return i.useMemo(
|
|
174
168
|
() => ({
|
|
175
169
|
controller: {
|
|
176
170
|
get model() {
|
|
@@ -184,12 +178,13 @@ function W(t) {
|
|
|
184
178
|
attributes: t.options.props,
|
|
185
179
|
actions: {
|
|
186
180
|
annotate(e, n) {
|
|
187
|
-
return
|
|
181
|
+
return J(e, n);
|
|
188
182
|
},
|
|
189
183
|
produce(e) {
|
|
190
|
-
return (n, r) =>
|
|
184
|
+
return (n, r) => T(n, r, e);
|
|
191
185
|
},
|
|
192
186
|
dispatch([e, ...n]) {
|
|
187
|
+
if (!e) return Promise.reject();
|
|
193
188
|
const r = Promise.withResolvers();
|
|
194
189
|
return t.dispatchers.dispatch(e, n, r), r.promise;
|
|
195
190
|
}
|
|
@@ -206,6 +201,7 @@ function W(t) {
|
|
|
206
201
|
attributes: t.options.props,
|
|
207
202
|
actions: {
|
|
208
203
|
dispatch([e, ...n]) {
|
|
204
|
+
if (!e) return Promise.reject();
|
|
209
205
|
const r = Promise.withResolvers();
|
|
210
206
|
return t.dispatchers.dispatch(e, n, r), r.promise;
|
|
211
207
|
}
|
|
@@ -215,39 +211,39 @@ function W(t) {
|
|
|
215
211
|
[]
|
|
216
212
|
);
|
|
217
213
|
}
|
|
218
|
-
function
|
|
219
|
-
return
|
|
214
|
+
function W(t) {
|
|
215
|
+
return i.useMemo(() => {
|
|
220
216
|
var r, s;
|
|
221
217
|
const e = (s = (r = t.options).controller) == null ? void 0 : s.call(r, t.actions.controller);
|
|
222
218
|
return e ? (Object.entries(e).forEach(([o, u]) => t.dispatchers.attach(o, u)), e) : void 0;
|
|
223
219
|
}, []);
|
|
224
220
|
}
|
|
225
|
-
function
|
|
221
|
+
function G(t) {
|
|
226
222
|
return (e, n) => async (r = Promise.withResolvers(), s) => {
|
|
227
223
|
if (typeof n != "function") return;
|
|
228
224
|
const o = Symbol("process"), u = n(...s);
|
|
229
225
|
for (; ; ) {
|
|
230
|
-
const { value: a, done:
|
|
231
|
-
if (
|
|
232
|
-
const
|
|
233
|
-
t.model.current =
|
|
226
|
+
const { value: a, done: c } = await u.next();
|
|
227
|
+
if (c) {
|
|
228
|
+
const C = a(t.model.current, o);
|
|
229
|
+
t.model.current = $(C, o), t.update.rerender();
|
|
234
230
|
break;
|
|
235
231
|
}
|
|
236
|
-
const
|
|
237
|
-
t.model.current =
|
|
232
|
+
const f = a;
|
|
233
|
+
t.model.current = f(t.model.current, o), t.update.rerender();
|
|
238
234
|
}
|
|
239
235
|
r.resolve();
|
|
240
236
|
};
|
|
241
237
|
}
|
|
242
|
-
function
|
|
238
|
+
function D(t) {
|
|
243
239
|
return t.startsWith("distributed");
|
|
244
240
|
}
|
|
245
241
|
var v, g;
|
|
246
|
-
class
|
|
242
|
+
class H extends Error {
|
|
247
243
|
constructor(n, r = null) {
|
|
248
244
|
super(String(r));
|
|
249
|
-
|
|
250
|
-
|
|
245
|
+
b(this, v);
|
|
246
|
+
b(this, g);
|
|
251
247
|
E(this, v, n), E(this, g, r);
|
|
252
248
|
}
|
|
253
249
|
get type() {
|
|
@@ -258,30 +254,30 @@ class K extends Error {
|
|
|
258
254
|
}
|
|
259
255
|
}
|
|
260
256
|
v = new WeakMap(), g = new WeakMap();
|
|
261
|
-
function
|
|
262
|
-
const e =
|
|
263
|
-
return
|
|
264
|
-
const r = new
|
|
257
|
+
function K(t) {
|
|
258
|
+
const e = A(), n = G(t);
|
|
259
|
+
return i.useMemo(() => {
|
|
260
|
+
const r = new k(), s = e.appEmitter;
|
|
265
261
|
return {
|
|
266
262
|
attach(o, u) {
|
|
267
263
|
const a = String(o);
|
|
268
|
-
|
|
264
|
+
D(a) ? s.on(a, n(o, u)) : r.on(a, n(o, u));
|
|
269
265
|
},
|
|
270
266
|
dispatch(o, u, a) {
|
|
271
|
-
const
|
|
272
|
-
|
|
267
|
+
const c = String(o);
|
|
268
|
+
D(c) ? s.emit(c, a, u) : r.emit(c, a, u);
|
|
273
269
|
}
|
|
274
270
|
};
|
|
275
271
|
}, []);
|
|
276
272
|
}
|
|
277
|
-
function
|
|
278
|
-
const t =
|
|
279
|
-
return
|
|
273
|
+
function L() {
|
|
274
|
+
const t = i.useRef(null);
|
|
275
|
+
return i.useMemo(() => ({ customElement: t }), []);
|
|
280
276
|
}
|
|
281
|
-
function
|
|
277
|
+
function V(t) {
|
|
282
278
|
var n, r, s, o;
|
|
283
|
-
const e =
|
|
284
|
-
|
|
279
|
+
const e = i.useRef(!1);
|
|
280
|
+
i.useLayoutEffect(() => {
|
|
285
281
|
t.dispatchers.dispatch(m.Derive, [
|
|
286
282
|
t.options.props,
|
|
287
283
|
t.router.current
|
|
@@ -291,72 +287,51 @@ function Y(t) {
|
|
|
291
287
|
(n = t.router.current) == null ? void 0 : n.location,
|
|
292
288
|
(r = t.router.current) == null ? void 0 : r.params,
|
|
293
289
|
(o = (s = t.router.current) == null ? void 0 : s.search) == null ? void 0 : o[0]
|
|
294
|
-
]),
|
|
290
|
+
]), i.useLayoutEffect(() => {
|
|
295
291
|
if (!e.current)
|
|
296
292
|
return e.current = !0, t.dispatchers.dispatch(m.Mount, []), t.dispatchers.dispatch(m.Node, [
|
|
297
293
|
t.elements.customElement.current
|
|
298
294
|
]), () => t.dispatchers.dispatch(m.Unmount, []);
|
|
299
295
|
}, []);
|
|
300
296
|
}
|
|
301
|
-
function
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
output(e) {
|
|
305
|
-
const n = t.elements.customElement.current;
|
|
306
|
-
console.groupCollapsed(
|
|
307
|
-
"%cRendered",
|
|
308
|
-
"background: rgb(217, 235, 240); color: rgb(0, 51, 102); border-radius: 2px; padding: 0 5px",
|
|
309
|
-
n
|
|
310
|
-
), console.groupEnd();
|
|
311
|
-
}
|
|
312
|
-
}),
|
|
313
|
-
[]
|
|
314
|
-
);
|
|
297
|
+
function X(t) {
|
|
298
|
+
const e = i.useMemo(() => t.options.model ?? {}, []);
|
|
299
|
+
return i.useRef(new x(e, e));
|
|
315
300
|
}
|
|
316
|
-
function
|
|
317
|
-
|
|
318
|
-
return c.useRef(new k(e, e));
|
|
301
|
+
function Y() {
|
|
302
|
+
return i.useRef(/* @__PURE__ */ new Set());
|
|
319
303
|
}
|
|
320
|
-
function
|
|
321
|
-
return
|
|
322
|
-
}
|
|
323
|
-
function nt() {
|
|
324
|
-
return c.useRef(null);
|
|
304
|
+
function Z() {
|
|
305
|
+
return i.useRef(null);
|
|
325
306
|
}
|
|
326
|
-
function
|
|
327
|
-
return
|
|
307
|
+
function tt({ using: t, children: e }) {
|
|
308
|
+
return d.useInRouterContext() ? /* @__PURE__ */ h(et, { using: t, children: e }) : e();
|
|
328
309
|
}
|
|
329
|
-
function
|
|
310
|
+
function et({ using: t, children: e }) {
|
|
330
311
|
return t.current = {
|
|
331
|
-
navigate:
|
|
332
|
-
location:
|
|
333
|
-
params:
|
|
334
|
-
search:
|
|
312
|
+
navigate: d.useNavigate(),
|
|
313
|
+
location: d.useLocation(),
|
|
314
|
+
params: d.useParams(),
|
|
315
|
+
search: d.useSearchParams()
|
|
335
316
|
}, /* @__PURE__ */ h(B, { children: e() });
|
|
336
317
|
}
|
|
337
|
-
function
|
|
338
|
-
const [t, e] =
|
|
339
|
-
return
|
|
318
|
+
function nt() {
|
|
319
|
+
const [t, e] = i.useReducer((n) => n + 1, 0);
|
|
320
|
+
return i.useMemo(() => ({ hash: t, rerender: e }), [t]);
|
|
340
321
|
}
|
|
341
|
-
function
|
|
322
|
+
function rt({
|
|
342
323
|
options: t
|
|
343
324
|
}) {
|
|
344
|
-
const e =
|
|
345
|
-
|
|
346
|
-
options: t,
|
|
347
|
-
update: n,
|
|
348
|
-
model: u,
|
|
349
|
-
logger: a,
|
|
350
|
-
queue: r
|
|
351
|
-
}), d = W({ app: e, options: t, model: u, dispatchers: i, router: o });
|
|
352
|
-
return G({ options: t, dispatchers: i, actions: d }), Y({ options: t, dispatchers: i, elements: s, router: o }), c.useMemo(() => (a.output({}), c.createElement(t.name, {
|
|
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 });
|
|
326
|
+
return W({ options: t, dispatchers: a, actions: c }), V({ options: t, dispatchers: a, elements: s, router: o }), i.useMemo(() => i.createElement(t.name, {
|
|
353
327
|
ref: s.customElement,
|
|
354
|
-
|
|
355
|
-
|
|
328
|
+
style: { display: "contents" },
|
|
329
|
+
children: /* @__PURE__ */ h(tt, { using: o, children: () => t.view(c.view) })
|
|
330
|
+
}), [n.hash]);
|
|
356
331
|
}
|
|
357
|
-
function
|
|
358
|
-
return (e) =>
|
|
359
|
-
(n) =>
|
|
332
|
+
function ot(t) {
|
|
333
|
+
return (e) => i.memo(
|
|
334
|
+
(n) => rt({
|
|
360
335
|
options: {
|
|
361
336
|
...e,
|
|
362
337
|
name: t.join(""),
|
|
@@ -366,28 +341,25 @@ function ct(t) {
|
|
|
366
341
|
(n, r) => JSON.stringify(n) === JSON.stringify(r)
|
|
367
342
|
);
|
|
368
343
|
}
|
|
369
|
-
function
|
|
370
|
-
return t;
|
|
371
|
-
}
|
|
372
|
-
function at(t) {
|
|
344
|
+
function st(t) {
|
|
373
345
|
return new Promise((e) => setTimeout(e, t));
|
|
374
346
|
}
|
|
375
|
-
function
|
|
347
|
+
function ut(t) {
|
|
376
348
|
return t ? !!(t && typeof t != "symbol") : Symbol(`pk.${Date.now()}.${crypto.randomUUID()}`);
|
|
377
349
|
}
|
|
378
|
-
function
|
|
379
|
-
return t instanceof
|
|
350
|
+
function it(t) {
|
|
351
|
+
return t instanceof H;
|
|
380
352
|
}
|
|
381
|
-
const
|
|
353
|
+
const ht = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
382
354
|
__proto__: null,
|
|
383
|
-
isEventError:
|
|
384
|
-
pk:
|
|
385
|
-
sleep:
|
|
386
|
-
}, Symbol.toStringTag, { value: "Module" })),
|
|
355
|
+
isEventError: it,
|
|
356
|
+
pk: ut,
|
|
357
|
+
sleep: st
|
|
358
|
+
}, Symbol.toStringTag, { value: "Module" })), pt = { app: z, module: ot };
|
|
387
359
|
export {
|
|
388
|
-
|
|
360
|
+
H as EventError,
|
|
389
361
|
m as Lifecycle,
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
362
|
+
F as State,
|
|
363
|
+
pt as create,
|
|
364
|
+
ht as utils
|
|
393
365
|
};
|
package/dist/chizu.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(o
|
|
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/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { default as app } from './app/index.tsx';
|
|
2
|
-
import {
|
|
3
|
-
import { default as model } from './model/index.ts';
|
|
2
|
+
import { ControllerDefinition } from './controller/types.ts';
|
|
4
3
|
import { default as module } from './module/index.tsx';
|
|
5
|
-
import {
|
|
4
|
+
import { ViewArgs, ViewDefinition } from './view/types.ts';
|
|
6
5
|
export { Lifecycle, State } from './types/index.ts';
|
|
7
6
|
export * as utils from './utils/index.ts';
|
|
8
7
|
export declare const create: {
|
|
9
8
|
app: typeof app;
|
|
10
9
|
module: typeof module;
|
|
11
|
-
model: typeof model;
|
|
12
|
-
view: typeof view;
|
|
13
|
-
controller: typeof controller;
|
|
14
10
|
};
|
|
15
11
|
export { EventError } from './module/renderer/dispatchers/utils.ts';
|
|
16
12
|
export type * as Create from './types/index.ts';
|
|
17
|
-
export type { ViewArgs as Args } from './view/types.ts';
|
|
18
13
|
export type { Pk } from './types/index.ts';
|
|
14
|
+
export type { ViewDefinition as View };
|
|
15
|
+
export type { ViewArgs as Within };
|
|
16
|
+
export type { ControllerDefinition as Controller };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { UseApp } from '../../../app/types.ts';
|
|
2
2
|
import { ModuleDefinition } from '../../../types/index.ts';
|
|
3
|
-
import { UseLogger } from '../logger/types.ts';
|
|
4
3
|
import { UseModel } from '../model/types.ts';
|
|
5
4
|
import { UseQueue } from '../queue/types.ts';
|
|
6
5
|
import { UseOptions } from '../types.ts';
|
|
@@ -11,7 +10,6 @@ export type Props<M extends ModuleDefinition> = {
|
|
|
11
10
|
options: UseOptions<M>;
|
|
12
11
|
update: UseUpdate;
|
|
13
12
|
model: UseModel;
|
|
14
|
-
logger: UseLogger;
|
|
15
13
|
queue: UseQueue;
|
|
16
14
|
};
|
|
17
15
|
export type UseDispatchers = ReturnType<typeof useDispatchers>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export declare enum Lifecycle {
|
|
|
28
28
|
Unmount = "lifecycle/unmount"
|
|
29
29
|
}
|
|
30
30
|
export type Model = Record<string, any>;
|
|
31
|
-
export type Actions = [ActionName] | [ActionName, ...ActionPayload];
|
|
31
|
+
export type Actions = [] | [ActionName] | [ActionName, ...ActionPayload];
|
|
32
32
|
export type Props = Record<string, unknown>;
|
|
33
33
|
export type Query = null | string;
|
|
34
34
|
export type Module<T extends {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chizu",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/chizu.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -43,13 +43,13 @@
|
|
|
43
43
|
"jest": "^29.7.0",
|
|
44
44
|
"jest-environment-jsdom": "^29.7.0",
|
|
45
45
|
"lucide-react": "^0.503.0",
|
|
46
|
-
"prettier": "^3.
|
|
46
|
+
"prettier": "^3.5.3",
|
|
47
47
|
"react-dom": "^19.0.0",
|
|
48
48
|
"react-test-renderer": "^19.0.0",
|
|
49
49
|
"rollup-plugin-visualizer": "^5.14.0",
|
|
50
50
|
"ts-jest": "^29.2.6",
|
|
51
51
|
"ts-node": "^10.9.2",
|
|
52
|
-
"typescript": "
|
|
52
|
+
"typescript": "^5.8.3",
|
|
53
53
|
"vite": "^6.0.5",
|
|
54
54
|
"vite-plugin-dts": "^4.5.4",
|
|
55
55
|
"wait-on": "^8.0.3"
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ModuleDefinition } from '../../../types/index.ts';
|
|
2
|
-
import { UseElements } from '../elements/types.ts';
|
|
3
|
-
import { UseOptions } from '../types.ts';
|
|
4
|
-
import { default as useLogger } from './index.ts';
|
|
5
|
-
export type Props<M extends ModuleDefinition> = {
|
|
6
|
-
options: UseOptions<M>;
|
|
7
|
-
elements: UseElements;
|
|
8
|
-
};
|
|
9
|
-
export type UseLogger = ReturnType<typeof useLogger>;
|
|
10
|
-
export type Metrics = {};
|