chizu 0.2.6 → 0.2.7
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 +19 -24
- package/dist/chizu.umd.cjs +1 -1
- package/dist/controller/types.d.ts +3 -4
- package/dist/types/index.d.ts +0 -12
- package/dist/view/types.d.ts +2 -3
- package/package.json +1 -1
package/dist/chizu.js
CHANGED
|
@@ -4,14 +4,14 @@ var k = (t) => {
|
|
|
4
4
|
};
|
|
5
5
|
var B = (t, e, n) => e in t ? N(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
|
|
6
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)),
|
|
7
|
+
var w = (t, e, n) => (j(t, e, "read from private field"), n ? n.call(t) : e.get(t)), E = (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) => (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 q } from "react/jsx-runtime";
|
|
9
9
|
import x from "eventemitter3";
|
|
10
10
|
import * as i from "react";
|
|
11
11
|
import { Immer as z } from "immer";
|
|
12
12
|
import p from "lodash/get";
|
|
13
13
|
import M from "traverse";
|
|
14
|
-
import * as
|
|
14
|
+
import * as d from "react-router-dom";
|
|
15
15
|
const A = i.createContext({
|
|
16
16
|
appEmitter: new x()
|
|
17
17
|
});
|
|
@@ -84,7 +84,7 @@ function C(t, e = []) {
|
|
|
84
84
|
o.flatMap((c) => c.operations)
|
|
85
85
|
);
|
|
86
86
|
return !!(Array.from(u).reduce(
|
|
87
|
-
(c,
|
|
87
|
+
(c, f) => c | (f ?? 0),
|
|
88
88
|
0
|
|
89
89
|
) & s);
|
|
90
90
|
};
|
|
@@ -126,14 +126,14 @@ function $(t, e, n) {
|
|
|
126
126
|
const u = typeof this.node.value == "object", a = [
|
|
127
127
|
...u ? this.path : this.path.slice(0, -1),
|
|
128
128
|
l.annotations
|
|
129
|
-
], c = p(t.stateful, a) ?? [],
|
|
129
|
+
], c = p(t.stateful, a) ?? [], f = this.node.attach(e);
|
|
130
130
|
u ? this.update(
|
|
131
131
|
{
|
|
132
132
|
...this.node.value,
|
|
133
|
-
[l.annotations]: [
|
|
133
|
+
[l.annotations]: [f, ...c]
|
|
134
134
|
},
|
|
135
135
|
!0
|
|
136
|
-
) : (this.parent && (this.parent.node[l.annotations] = [
|
|
136
|
+
) : (this.parent && (this.parent.node[l.annotations] = [f, ...c]), this.update(this.node.value, !0));
|
|
137
137
|
}
|
|
138
138
|
});
|
|
139
139
|
}
|
|
@@ -174,8 +174,7 @@ function Q(t) {
|
|
|
174
174
|
return t.router.current;
|
|
175
175
|
},
|
|
176
176
|
queue: [],
|
|
177
|
-
|
|
178
|
-
attributes: t.options.props,
|
|
177
|
+
props: t.options.props,
|
|
179
178
|
actions: {
|
|
180
179
|
annotate(e, n) {
|
|
181
180
|
return T(e, n);
|
|
@@ -197,8 +196,7 @@ function Q(t) {
|
|
|
197
196
|
get validate() {
|
|
198
197
|
return t.model.current.validatable;
|
|
199
198
|
},
|
|
200
|
-
|
|
201
|
-
attributes: t.options.props,
|
|
199
|
+
props: t.options.props,
|
|
202
200
|
actions: {
|
|
203
201
|
dispatch([e, ...n]) {
|
|
204
202
|
if (e == null) return Promise.reject();
|
|
@@ -233,8 +231,8 @@ function G(t) {
|
|
|
233
231
|
t.model.current = D(I, o), t.update.rerender();
|
|
234
232
|
break;
|
|
235
233
|
}
|
|
236
|
-
const
|
|
237
|
-
t.model.current =
|
|
234
|
+
const f = a;
|
|
235
|
+
t.model.current = f(t.model.current, o), t.update.rerender();
|
|
238
236
|
}
|
|
239
237
|
r.resolve();
|
|
240
238
|
};
|
|
@@ -246,9 +244,9 @@ var v, g;
|
|
|
246
244
|
class H extends Error {
|
|
247
245
|
constructor(n, r = null) {
|
|
248
246
|
super(String(r));
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
247
|
+
E(this, v);
|
|
248
|
+
E(this, g);
|
|
249
|
+
b(this, v, n), b(this, g, r);
|
|
252
250
|
}
|
|
253
251
|
get type() {
|
|
254
252
|
return w(this, v);
|
|
@@ -282,10 +280,7 @@ function V(t) {
|
|
|
282
280
|
var n, r, s, o;
|
|
283
281
|
const e = i.useRef(!1);
|
|
284
282
|
i.useLayoutEffect(() => {
|
|
285
|
-
t.dispatchers.dispatch(m.Derive, [
|
|
286
|
-
t.options.props,
|
|
287
|
-
t.router.current
|
|
288
|
-
]);
|
|
283
|
+
t.dispatchers.dispatch(m.Derive, []);
|
|
289
284
|
}, [
|
|
290
285
|
t.options.props,
|
|
291
286
|
(n = t.router.current) == null ? void 0 : n.location,
|
|
@@ -309,14 +304,14 @@ function Z() {
|
|
|
309
304
|
return i.useRef(null);
|
|
310
305
|
}
|
|
311
306
|
function tt({ using: t, children: e }) {
|
|
312
|
-
return
|
|
307
|
+
return d.useInRouterContext() ? /* @__PURE__ */ h(et, { using: t, children: e }) : e();
|
|
313
308
|
}
|
|
314
309
|
function et({ using: t, children: e }) {
|
|
315
310
|
return t.current = {
|
|
316
|
-
navigate:
|
|
317
|
-
location:
|
|
318
|
-
params:
|
|
319
|
-
search:
|
|
311
|
+
navigate: d.useNavigate(),
|
|
312
|
+
location: d.useLocation(),
|
|
313
|
+
params: d.useParams(),
|
|
314
|
+
search: d.useSearchParams()
|
|
320
315
|
}, /* @__PURE__ */ h(q, { children: e() });
|
|
321
316
|
}
|
|
322
317
|
function nt() {
|
package/dist/chizu.umd.cjs
CHANGED
|
@@ -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,
|
|
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,b,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,b)=>(z(o,u,"write to private field"),b?b.call(o,l):u.set(o,l),l);var g,w;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(b),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 h=(e=>(e.Mount="lifecycle/mount",e.Node="lifecycle/node",e.Derive="lifecycle/derive",e.Error="distributed/lifecycle/error",e.Unmount="lifecycle/unmount",e))(h||{});const m={immer:new B.Immer,annotations:Symbol("annotations")};m.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 c=>{const s=R(e,t);if(!s)return!1;const i=new Set(s.flatMap(f=>f.operations));return!!(Array.from(i).reduce((f,p)=>f|(p??0),0)&c)};case"pending":return()=>!!R(e,t);case"draft":return()=>{const c=R(e,t);if(!c)return v(e,t);const s=c.flatMap(i=>i.operations).find(i=>i 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),c=r.length===0?e:v(e,r),s=(c==null?void 0:c[m.annotations])??[];return s.length>0?s:null}function L(e,t,n){function r(s){return E(s).forEach(function(){if(this.key===m.annotations){this.block();return}this.node instanceof M&&this.update(this.node.value)})}function c(s){return E(s).forEach(function(){if(this.key===m.annotations){this.block();return}if(this.node instanceof M){const i=typeof this.node.value=="object",d=[...i?this.path:this.path.slice(0,-1),m.annotations],f=v(e.stateful,d)??[],p=this.node.attach(t);i?this.update({...this.node.value,[m.annotations]:[p,...f]},!0):(this.parent&&(this.parent.node[m.annotations]=[p,...f]),this.update(this.node.value,!0))}})}return new S(r(m.immer.produce(e.stateless,n)),c(m.immer.produce(e.stateful,n)))}function C(e,t){const n=E(e.stateful).forEach(function(){if(this.key===m.annotations){this.block();return}if(this.node&&this.node[m.annotations]){const r=this.node[m.annotations];this.update({...this.node,[m.annotations]:r.filter(c=>c.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:[],props: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},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 W(e){return a.useMemo(()=>{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(([s,i])=>e.dispatchers.attach(s,i)),t):void 0},[])}function G(e){return(t,n)=>async(r=Promise.withResolvers(),c)=>{if(typeof n!="function")return;const s=Symbol("process"),i=n(...c);if(typeof i=="function"){const d=i(e.model.current,s);return e.model.current=C(d,s),e.update.rerender(),void r.resolve()}for(;;){const{value:d,done:f}=await i.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,w);D(this,g,n),D(this,w,r)}get type(){return O(this,g)}get message(){return O(this,w)||""}}g=new WeakMap,w=new WeakMap;function H(e){const t=x(),n=G(e);return a.useMemo(()=>{const r=new l,c=t.appEmitter;return{attach(s,i){const d=String(s);I(d)?c.on(d,n(s,i)):r.on(d,n(s,i))},dispatch(s,i,d){const f=String(s);I(f)?c.emit(f,d,i):r.emit(f,d,i)}}},[])}function K(){const e=a.useRef(null);return a.useMemo(()=>({customElement:e}),[])}function V(e){var n,r,c,s;const t=a.useRef(!1);a.useLayoutEffect(()=>{e.dispatchers.dispatch(h.Derive,[])},[e.options.props,(n=e.router.current)==null?void 0:n.location,(r=e.router.current)==null?void 0:r.params,(s=(c=e.router.current)==null?void 0:c.search)==null?void 0:s[0]]),a.useLayoutEffect(()=>{if(!t.current)return t.current=!0,e.dispatchers.dispatch(h.Mount,[]),e.dispatchers.dispatch(h.Node,[e.elements.customElement.current]),()=>e.dispatchers.dispatch(h.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(),c=K(),s=Z(),i=X({options:e}),d=H({app:t,options:e,update:n,model:i,queue:r}),f=Q({app:t,options:e,model:i,dispatchers:d,router:s});return W({options:e,dispatchers:d,actions:f}),V({options:e,dispatchers:d,elements:c,router:s}),a.useMemo(()=>a.createElement(e.name,{ref:c.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 ce(e){return e instanceof N}const ie=Object.freeze(Object.defineProperty({__proto__:null,isEventError:ce,pk:ue,sleep:se},Symbol.toStringTag,{value:"Module"})),ae={app:J,module:oe};o.EventError=N,o.Lifecycle=h,o.State=T,o.create=ae,o.utils=ie,Object.defineProperty(o,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,
|
|
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
|
-
|
|
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]?(
|
|
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>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -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>;
|
package/dist/view/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Validatable } from '../module/renderer/model/types.ts';
|
|
2
|
-
import {
|
|
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
|
-
|
|
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;
|