chrono-state-z 1.0.0-z
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/LICENSE +21 -0
- package/README.md +221 -0
- package/build/core/asyncAtom.d.ts +5 -0
- package/build/core/asyncComputed.d.ts +8 -0
- package/build/core/atom.d.ts +5 -0
- package/build/core/batch.d.ts +1 -0
- package/build/core/computed.d.ts +1 -0
- package/build/core/effect.d.ts +2 -0
- package/build/core/effectContext.d.ts +5 -0
- package/build/core/index.d.ts +9 -0
- package/build/core/scheduler.d.ts +5 -0
- package/build/core/transaction.d.ts +1 -0
- package/build/core/watch.d.ts +2 -0
- package/build/devtools/graph.d.ts +22 -0
- package/build/form/createField.d.ts +9 -0
- package/build/form/nestedForm.d.ts +18 -0
- package/build/form/useField.d.ts +5 -0
- package/build/form/useForm.d.ts +12 -0
- package/build/form/validationFSM.d.ts +6 -0
- package/build/index.cjs.js +1 -0
- package/build/index.d.ts +23 -0
- package/build/index.esm.js +1 -0
- package/build/react/DevtoolsReactPanel.d.ts +1 -0
- package/build/react/hooks.d.ts +7 -0
- package/build/runtime/fsm.d.ts +15 -0
- package/build/runtime/intent.d.ts +13 -0
- package/package.json +86 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Delpi.Kye
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# ⚙️ chrono-state-z
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/chrono-state-z)
|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
<a href="https://codesandbox.io/p/sandbox/xt6npl" target="_blank">LIVE EXAMPLE</a>
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
**chrono-state-z** is a **reactive, intent-first runtime**
|
|
11
|
+
with **atoms, computed, async effects, FSM, form engine, and devtools timeline.**
|
|
12
|
+
|
|
13
|
+
> **Logic lives outside React.** React only renders state and emits intents.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## ✨ Why / When to Use
|
|
18
|
+
|
|
19
|
+
- Zero hooks in UI components
|
|
20
|
+
- Complex async flows (login → fetch → redirect)
|
|
21
|
+
- Predictable & testable side effects
|
|
22
|
+
- Headless testing without rendering
|
|
23
|
+
- Architecture-driven reactive design
|
|
24
|
+
- Devtools timeline for replay & debugging
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## 📦 Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install chrono-state-z
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 🧠 Mental Model
|
|
35
|
+
|
|
36
|
+
- Atoms: reactive state units
|
|
37
|
+
- Computed: derived values
|
|
38
|
+
- Effects: middleware-style interceptors
|
|
39
|
+
- FSM / Form: orchestrate async flows & validations
|
|
40
|
+
- Timeline: devtools recording & replay
|
|
41
|
+
- React: purely renders state + emits intents
|
|
42
|
+
|
|
43
|
+
## Examples
|
|
44
|
+
|
|
45
|
+
#### 1️⃣ Core Atom + Computed + Scheduler
|
|
46
|
+
```ts
|
|
47
|
+
import { atom, computed } from 'chrono-state-z'
|
|
48
|
+
|
|
49
|
+
const count = atom(0)
|
|
50
|
+
const double = computed(() => count() * 2)
|
|
51
|
+
|
|
52
|
+
count.set(5)
|
|
53
|
+
console.log(count()) // 5
|
|
54
|
+
console.log(double()) // 10
|
|
55
|
+
|
|
56
|
+
count.set(10)
|
|
57
|
+
console.log(count()) // 10
|
|
58
|
+
console.log(double()) // 20
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### 2️⃣ Async Atom + Async Computed
|
|
62
|
+
```ts
|
|
63
|
+
import { asyncAtom, asyncComputed, atom } from 'chrono-state-z'
|
|
64
|
+
|
|
65
|
+
const value = asyncAtom(async () => {
|
|
66
|
+
await new Promise(r => setTimeout(r, 100))
|
|
67
|
+
return 42
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
value.load().then(v => console.log('AsyncAtom loaded:', v)) // 42
|
|
71
|
+
|
|
72
|
+
const count = atom(2)
|
|
73
|
+
const doubleAsync = asyncComputed(async () => {
|
|
74
|
+
await new Promise(r => setTimeout(r, 50))
|
|
75
|
+
return count() * 2
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
doubleAsync().then(v => console.log('AsyncComputed:', v)) // 4
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### 3️⃣ Batch / Transaction
|
|
82
|
+
```ts
|
|
83
|
+
import { atom, batch } from 'chrono-state-z'
|
|
84
|
+
|
|
85
|
+
const a = atom(1)
|
|
86
|
+
const b = atom(2)
|
|
87
|
+
|
|
88
|
+
batch(() => {
|
|
89
|
+
a.set(10)
|
|
90
|
+
b.set(20)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
console.log(a(), b()) // 10, 20
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
#### 4️⃣ FSM Example
|
|
97
|
+
```ts
|
|
98
|
+
import { createFSM } from 'chrono-state-z'
|
|
99
|
+
|
|
100
|
+
const loginFSM = createFSM({
|
|
101
|
+
initial: { status: 'idle' },
|
|
102
|
+
states: {
|
|
103
|
+
idle: { on: { SUBMIT: ({ state }) => ({ status: 'loading' }) } },
|
|
104
|
+
loading: { on: { SUCCESS: () => ({ status: 'success' }), FAIL: () => ({ status: 'error' }) } },
|
|
105
|
+
success: {},
|
|
106
|
+
error: {},
|
|
107
|
+
},
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
await loginFSM.dispatch('SUBMIT')
|
|
111
|
+
console.log(loginFSM.state().status) // 'loading'
|
|
112
|
+
|
|
113
|
+
await loginFSM.dispatch('SUCCESS')
|
|
114
|
+
console.log(loginFSM.state().status) // 'success'
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### 5️⃣ Form Engine + Validation
|
|
119
|
+
```ts
|
|
120
|
+
import { useForm } from 'chrono-state-z'
|
|
121
|
+
|
|
122
|
+
const loginForm = useForm({
|
|
123
|
+
username: { initial: '', validate: val => val ? null : 'Required' },
|
|
124
|
+
password: { initial: '', validate: val => val ? null : 'Required' },
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
loginForm.fields.username.value.set('admin')
|
|
128
|
+
|
|
129
|
+
if (loginForm.isValid()) {
|
|
130
|
+
console.log(loginForm.values())
|
|
131
|
+
} else {
|
|
132
|
+
console.log('Validation errors exist')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### 6️⃣ React Integration (Headless View)
|
|
138
|
+
```ts
|
|
139
|
+
import React from 'react'
|
|
140
|
+
import { atom, useAtom } from 'chrono-state-z'
|
|
141
|
+
|
|
142
|
+
const count = atom(0)
|
|
143
|
+
|
|
144
|
+
function CounterView({ countAtom }: { countAtom: typeof count }) {
|
|
145
|
+
const value = useAtom(countAtom)
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<div>
|
|
149
|
+
<div>Count: {value}</div>
|
|
150
|
+
<button onClick={() => countAtom.set(countAtom() + 1)}>+</button>
|
|
151
|
+
</div>
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function App() {
|
|
156
|
+
return <CounterView countAtom={count} />
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### 7️⃣ Devtools Timeline & Replay
|
|
162
|
+
```ts
|
|
163
|
+
import { trackNode, schedule } from 'chrono-state-z'
|
|
164
|
+
|
|
165
|
+
const node = trackNode('count')
|
|
166
|
+
|
|
167
|
+
schedule(() => {
|
|
168
|
+
console.log('Replay atom change:', node.value)
|
|
169
|
+
}, 'normal')
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
#### 8️⃣ Effect Subscription
|
|
174
|
+
```ts
|
|
175
|
+
import { effect } from 'chrono-state-z'
|
|
176
|
+
|
|
177
|
+
const count = atom(0)
|
|
178
|
+
|
|
179
|
+
const unsubscribe = effect(() => {
|
|
180
|
+
console.log('Effect triggered:', count())
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
count.set(1) // logs "Effect triggered: 1"
|
|
184
|
+
|
|
185
|
+
// cleanup
|
|
186
|
+
unsubscribe()
|
|
187
|
+
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## 🔍 Comparison / Features
|
|
193
|
+
| Feature | chrono-state-z | Redux | Zustand |
|
|
194
|
+
| -------------------- | -------------- | ----- | ------- |
|
|
195
|
+
| Atoms / Computed | ✅ | ⚠️ | ⚠️ |
|
|
196
|
+
| Async Effects | ✅ | ⚠️ | ⚠️ |
|
|
197
|
+
| FSM / Form Engine | ✅ | ❌ | ❌ |
|
|
198
|
+
| Scheduler / Priority | ✅ | ❌ | ❌ |
|
|
199
|
+
| Headless Testing | ✅ | ⚠️ | ⚠️ |
|
|
200
|
+
| Devtools Timeline | ✅ | ⚠️ | ❌ |
|
|
201
|
+
|
|
202
|
+
---
|
|
203
|
+
|
|
204
|
+
## 🚫 Anti-patterns
|
|
205
|
+
|
|
206
|
+
- Don’t mutate atoms outside runtime
|
|
207
|
+
- Don’t put business logic inside React
|
|
208
|
+
- Always emit intent or use effects for orchestration
|
|
209
|
+
- Don’t bypass scheduler / computed dependencies
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## 🧠 Philosophy
|
|
214
|
+
|
|
215
|
+
- Logic lives outside React
|
|
216
|
+
- Devtools timeline ensures determinism
|
|
217
|
+
- Scheduler + priority enables safe async updates
|
|
218
|
+
|
|
219
|
+
## 📜 License
|
|
220
|
+
|
|
221
|
+
MIT / Delpi
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Priority } from './';
|
|
2
|
+
export declare function asyncComputed<T>(getter: () => Promise<T>, options?: {
|
|
3
|
+
priority?: Priority;
|
|
4
|
+
}): (() => T | undefined) & {
|
|
5
|
+
load: () => Promise<T> | null;
|
|
6
|
+
invalidate: (priority?: Priority) => void;
|
|
7
|
+
node: import("../devtools/graph").GraphNode;
|
|
8
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function batch(fn: () => void): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function computed<T>(getter: () => T): () => T;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./atom";
|
|
2
|
+
export * from './asyncAtom';
|
|
3
|
+
export * from "./computed";
|
|
4
|
+
export * from "./effect";
|
|
5
|
+
export * from "./watch";
|
|
6
|
+
export * from "./batch";
|
|
7
|
+
export { schedule } from "./scheduler";
|
|
8
|
+
export type { Priority } from './scheduler';
|
|
9
|
+
export type { Atom } from "./atom";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function transaction(fn: () => void): void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type NodeType = 'atom' | 'computed' | 'effect';
|
|
2
|
+
export type GraphNode = {
|
|
3
|
+
id: number;
|
|
4
|
+
name?: string;
|
|
5
|
+
type: NodeType;
|
|
6
|
+
value?: any;
|
|
7
|
+
deps: Set<number>;
|
|
8
|
+
highlighted?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export type GraphSnapshotNode = {
|
|
11
|
+
id: number;
|
|
12
|
+
name?: string;
|
|
13
|
+
type: NodeType;
|
|
14
|
+
value?: any;
|
|
15
|
+
deps: number[];
|
|
16
|
+
highlighted?: boolean;
|
|
17
|
+
};
|
|
18
|
+
export declare function trackNode(type: NodeType, name?: string, value?: any): GraphNode;
|
|
19
|
+
export declare function linkNodes(from: GraphNode, to: GraphNode): void;
|
|
20
|
+
export declare function getGraphSnapshot(): GraphSnapshotNode[];
|
|
21
|
+
export declare function highlightNode(id: number): void;
|
|
22
|
+
export declare function clearHighlights(): void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Atom } from "../core";
|
|
2
|
+
export type FieldState<T> = {
|
|
3
|
+
value: T;
|
|
4
|
+
error: string | null;
|
|
5
|
+
};
|
|
6
|
+
export type FieldSchema<T> = {
|
|
7
|
+
initial: T;
|
|
8
|
+
validate?: (val: T) => string | null | Promise<string | null>;
|
|
9
|
+
};
|
|
10
|
+
export declare function createNestedForm<T extends Record<string, any>>(schema: {
|
|
11
|
+
[K in keyof T]: FieldSchema<T[K]>;
|
|
12
|
+
}): {
|
|
13
|
+
fields: { [K in keyof T]: Atom<FieldState<T[K]>>; };
|
|
14
|
+
values: () => T;
|
|
15
|
+
isValid: () => Promise<boolean>;
|
|
16
|
+
setField: <K_1 extends keyof T>(key: K_1, value: T[K_1]) => void;
|
|
17
|
+
submit: () => Promise<T>;
|
|
18
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function useForm<T extends Record<string, any>>(schema: {
|
|
2
|
+
[K in keyof T]: {
|
|
3
|
+
initial: T[K];
|
|
4
|
+
validate?: (val: T[K]) => string | null | Promise<string | null>;
|
|
5
|
+
};
|
|
6
|
+
}): {
|
|
7
|
+
fields: Record<keyof T, any>;
|
|
8
|
+
setField: (key: keyof T, value: any) => void;
|
|
9
|
+
isValid: () => Promise<boolean>;
|
|
10
|
+
values: () => T;
|
|
11
|
+
submit: () => Promise<T>;
|
|
12
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("react"),t=require("react/jsx-runtime");function n(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,Object.freeze(t)}var o=n(e);const r={high:[],normal:[],low:[]};let a=!1;const i=e=>{var t,n,o;for(a=!0;r.high.length||r.normal.length||r.low.length;)if(r.high.length)null===(t=r.high.shift())||void 0===t||t();else if(r.normal.length)null===(n=r.normal.shift())||void 0===n||n();else if(r.low.length){if(void 0!==e&&e.timeRemaining()<1)break;null===(o=r.low.shift())||void 0===o||o()}a=!1,r.low.length>0&&("undefined"!=typeof requestIdleCallback?requestIdleCallback(i):setTimeout(()=>i(),16))};function s(e,t="normal"){r[t].push(e),a||("high"===t?i():"undefined"!=typeof requestIdleCallback?requestIdleCallback(i):setTimeout(()=>i(),16))}const c={activeNode:null,activeEffect:null};let l=0;const u=new Map;function f(e,t,n){const o={id:++l,type:e,name:t,value:n,deps:new Set,highlighted:!1};return u.set(o.id,o),o}function d(e,t){e.deps.add(t.id)}function v(){return Array.from(u.values()).map(e=>({id:e.id,name:e.name,type:e.type,value:e.value,deps:Array.from(e.deps),highlighted:e.highlighted}))}function h(e){let t=e;const n=new Set,o=f("atom"),r=()=>(c.activeNode&&d(c.activeNode,o),c.activeEffect&&n.add(c.activeEffect),t);return r.set=(e,r="normal")=>{Object.is(e,t)||(t=e,o.value=t,n.forEach(e=>s(e,r)))},r}function p(e){let t,n=!0;const o=new Set,r=f("computed"),a=()=>{n||(n=!0,o.forEach(e=>s(e)))};return()=>{if(c.activeNode&&d(c.activeNode,r),c.activeEffect&&o.add(c.activeEffect),n){const o=c.activeNode,i=c.activeEffect;c.activeNode=r,c.activeEffect=a,t=e(),n=!1,c.activeNode=o,c.activeEffect=i}return t}}function m(e,t="normal"){const n=f("effect","Effect"),o=[],r=()=>{try{c.activeNode=n,c.activeEffect=r,e()}catch(e){console.error("[Effect error]",e)}finally{c.activeNode=null,c.activeEffect=null}};return s(r,t),()=>{n.deps.clear(),o.forEach(e=>e())}}function g(e,t,n="normal"){return m(()=>{const n=e();t(n)},n)}const x=new Set;function y(e){e(),x.forEach(e=>s(e)),x.clear()}function w(e){const t=h(e.initial);return{state:t,dispatch:async function(n,o){var r;const a=t(),i=e.states[a.status],s=null===(r=null==i?void 0:i.on)||void 0===r?void 0:r[n];if(s){const e=s({state:a,payload:o}),n=e instanceof Promise?await e:e;y(()=>t.set(n))}}}}const E=w({initial:{status:"idle"},states:{idle:{on:{VALIDATE:()=>({status:"validating"})}},validating:{on:{SUCCESS:()=>({status:"valid"}),ERROR:()=>({status:"invalid"})}},valid:{},invalid:{}}});function b(e){const[,t]=o.useState(0);return o.useEffect(()=>{const n=m(()=>{e(),t(e=>e+1)},"normal");return()=>n()},[e]),e()}exports.DevtoolsReactPanel=function(){const[n,o]=e.useState(v());return e.useEffect(()=>{const e=setInterval(()=>o(v()),100);return()=>clearInterval(e)},[]),t.jsx("div",{style:{fontFamily:"monospace",fontSize:12,background:"#222",color:"#eee",padding:10,maxHeight:300,overflowY:"auto"},children:n.map(e=>t.jsxs("div",{children:["[",e.type,"] ",e.name||"anon"," → deps: ",e.deps.join(", ")]},e.id))})},exports.asyncAtom=function(e){const t=h(void 0);let n=null,o=!1;const r=()=>(n||(n=e().then(e=>(o||t.set(e,"low"),e)),n.finally(()=>n=null)),n);return Object.assign(()=>{const e=t();if(void 0===e)throw r();return e},{set:t.set,load:r,cancel:()=>{o=!0}})},exports.asyncComputed=function(e,t){const n=p(()=>{}),o=f("computed","asyncComputed");let r=null,a=!1,i=null;const l=()=>{if(c.activeNode&&d(c.activeNode,o),r||(a=!1,i=null,r=e().then(e=>(a||n.set(e),e)).catch(e=>{a||(i=e)}).finally(()=>r=null)),i)throw i;if(void 0===n())throw r;return n()};return Object.assign(l,{load:()=>r,invalidate:(e="normal")=>{a=!0,r=null,s(l,(null==t?void 0:t.priority)||e)},node:o})},exports.atom=h,exports.batch=y,exports.clearHighlights=function(){u.forEach(e=>e.highlighted=!1)},exports.computed=p,exports.createFSM=w,exports.createField=function(e,t){const n=h({value:e,touched:!1}),o=p(()=>t?t(n().value):void 0);return{state:n,error:o,setValue:e=>n.set({...n(),value:e}),touch:()=>n.set({...n(),touched:!0})}},exports.createIntentBus=function(){const e=new Map;return{on(t,n){e.set(t,n)},dispatch(t,n){const o=e.get(n.type);o&&o({state:t(),intent:n,setState:t.set})}}},exports.createNestedForm=function(e){const t={};for(const n in e){const o=e[n],r=h({value:o.initial,error:null});o.validate&&g(r,async e=>{try{const t=await o.validate(e.value);y(()=>{r.set({...r(),error:null!=t?t:null})})}catch(e){r.set({...r(),error:e.message})}}),t[n]=r}const n=()=>Object.fromEntries(Object.entries(t).map(([e,t])=>[e,t().value])),o=async()=>(await Promise.all(Object.values(t).map(e=>e().error))).every(e=>null==e);return{fields:t,values:n,isValid:o,setField:(e,n)=>{const o=t[e];o&&y(()=>o.set({...o(),value:n}))},submit:async()=>{if(!await o())throw new Error("Validation failed");return n()}}},exports.effect=m,exports.effectContext=c,exports.getGraphSnapshot=v,exports.highlightNode=function(e){const t=u.get(e);t&&(t.highlighted=!0)},exports.linkNodes=d,exports.schedule=s,exports.scheduleCancelable=function(e,t="normal"){let n=!1;return s(()=>{n||e()},t),()=>{n=!0}},exports.scheduleReactJob=function(t,n="normal"){"low"===n?e.startTransition(t):t()},exports.trackNode=f,exports.transaction=function(e){y(e)},exports.useAtom=b,exports.useBatch=function(){return y},exports.useComputed=function(e){return b(o.useMemo(()=>p(e),[e]))},exports.useEffectReact=function(e,t="normal"){o.useEffect(()=>{const n=m(e,t);return()=>n()},[e,t])},exports.useField=function(e,t){const n=h(e),o=h(null);return t&&g(n,e=>{const n=t(e);o.set(n)}),{value:n,error:o}},exports.useForm=function(e){const t={};for(const n in e){const o=h({value:e[n].initial,error:null});t[n]=o,e[n].validate&&g(o,async t=>{try{const r=await e[n].validate(t.value);y(()=>o.set({...t,error:r}))}catch(e){y(()=>o.set({...t,error:e.message}))}})}const n=async()=>(await Promise.all(Object.values(t).map(e=>e().error))).every(e=>null==e),o=()=>Object.fromEntries(Object.entries(t).map(([e,t])=>[e,t().value]));return{fields:t,setField:(e,n)=>{const o=t[e];o&&y(()=>o.set({...o(),value:n}))},isValid:n,values:o,submit:async()=>{if(!await n())throw new Error("Validation failed");return o()}}},exports.useWatch=function(e,t){o.useEffect(()=>{const n=g(e,t);return()=>null==n?void 0:n()},[e,t])},exports.validationFSM=E,exports.watch=g;
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export * from './core/atom';
|
|
2
|
+
export * from './core/asyncAtom';
|
|
3
|
+
export * from './core/computed';
|
|
4
|
+
export * from './core/asyncComputed';
|
|
5
|
+
export * from './core/effect';
|
|
6
|
+
export * from './core/effectContext';
|
|
7
|
+
export * from './core/watch';
|
|
8
|
+
export * from './core/batch';
|
|
9
|
+
export * from './core/transaction';
|
|
10
|
+
export * from './core/scheduler';
|
|
11
|
+
export * from './runtime/intent';
|
|
12
|
+
export * from './runtime/fsm';
|
|
13
|
+
export * from './form/createField';
|
|
14
|
+
export * from './form/useField';
|
|
15
|
+
export * from './form/useForm';
|
|
16
|
+
export * from './form/nestedForm';
|
|
17
|
+
export * from './form/validationFSM';
|
|
18
|
+
export * from './devtools/graph';
|
|
19
|
+
export * from './react/hooks';
|
|
20
|
+
export { DevtoolsReactPanel } from './react/DevtoolsReactPanel';
|
|
21
|
+
export type { Atom } from './core/atom';
|
|
22
|
+
export type { Priority } from './core/scheduler';
|
|
23
|
+
export type { Intent } from './runtime/intent';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as t from"react";import e,{startTransition as n}from"react";import{jsx as o,jsxs as a}from"react/jsx-runtime";const i={high:[],normal:[],low:[]};let l=!1;const r=t=>{var e,n,o;for(l=!0;i.high.length||i.normal.length||i.low.length;)if(i.high.length)null===(e=i.high.shift())||void 0===e||e();else if(i.normal.length)null===(n=i.normal.shift())||void 0===n||n();else if(i.low.length){if(void 0!==t&&t.timeRemaining()<1)break;null===(o=i.low.shift())||void 0===o||o()}l=!1,i.low.length>0&&("undefined"!=typeof requestIdleCallback?requestIdleCallback(r):setTimeout(()=>r(),16))};function c(t,e="normal"){i[e].push(t),l||("high"===e?r():"undefined"!=typeof requestIdleCallback?requestIdleCallback(r):setTimeout(()=>r(),16))}function s(t,e="normal"){let n=!1;return c(()=>{n||t()},e),()=>{n=!0}}const u={activeNode:null,activeEffect:null};let f=0;const d=new Map;function v(t,e,n){const o={id:++f,type:t,name:e,value:n,deps:new Set,highlighted:!1};return d.set(o.id,o),o}function h(t,e){t.deps.add(e.id)}function m(){return Array.from(d.values()).map(t=>({id:t.id,name:t.name,type:t.type,value:t.value,deps:Array.from(t.deps),highlighted:t.highlighted}))}function p(t){const e=d.get(t);e&&(e.highlighted=!0)}function g(){d.forEach(t=>t.highlighted=!1)}function y(t){let e=t;const n=new Set,o=v("atom"),a=()=>(u.activeNode&&h(u.activeNode,o),u.activeEffect&&n.add(u.activeEffect),e);return a.set=(t,a="normal")=>{Object.is(t,e)||(e=t,o.value=e,n.forEach(t=>c(t,a)))},a}function w(t){const e=y(void 0);let n=null,o=!1;const a=()=>(n||(n=t().then(t=>(o||e.set(t,"low"),t)),n.finally(()=>n=null)),n);return Object.assign(()=>{const t=e();if(void 0===t)throw a();return t},{set:e.set,load:a,cancel:()=>{o=!0}})}function E(t){let e,n=!0;const o=new Set,a=v("computed"),i=()=>{n||(n=!0,o.forEach(t=>c(t)))};return()=>{if(u.activeNode&&h(u.activeNode,a),u.activeEffect&&o.add(u.activeEffect),n){const o=u.activeNode,l=u.activeEffect;u.activeNode=a,u.activeEffect=i,e=t(),n=!1,u.activeNode=o,u.activeEffect=l}return e}}function b(t,e="normal"){const n=v("effect","Effect"),o=[],a=()=>{try{u.activeNode=n,u.activeEffect=a,t()}catch(t){console.error("[Effect error]",t)}finally{u.activeNode=null,u.activeEffect=null}};return c(a,e),()=>{n.deps.clear(),o.forEach(t=>t())}}function N(t,e,n="normal"){return b(()=>{const n=t();e(n)},n)}const j=new Set;function S(t){t(),j.forEach(t=>c(t)),j.clear()}function O(t,e){const n=E(()=>{}),o=v("computed","asyncComputed");let a=null,i=!1,l=null;const r=()=>{if(u.activeNode&&h(u.activeNode,o),a||(i=!1,l=null,a=t().then(t=>(i||n.set(t),t)).catch(t=>{i||(l=t)}).finally(()=>a=null)),l)throw l;if(void 0===n())throw a;return n()};return Object.assign(r,{load:()=>a,invalidate:(t="normal")=>{i=!0,a=null,c(r,(null==e?void 0:e.priority)||t)},node:o})}function C(t){S(t)}function I(){const t=new Map;return{on(e,n){t.set(e,n)},dispatch(e,n){const o=t.get(n.type);o&&o({state:e(),intent:n,setState:e.set})}}}function k(t){const e=y(t.initial);return{state:e,dispatch:async function(n,o){var a;const i=e(),l=t.states[i.status],r=null===(a=null==l?void 0:l.on)||void 0===a?void 0:a[n];if(r){const t=r({state:i,payload:o}),n=t instanceof Promise?await t:t;S(()=>e.set(n))}}}}function V(t,e){const n=y({value:t,touched:!1}),o=E(()=>e?e(n().value):void 0);return{state:n,error:o,setValue:t=>n.set({...n(),value:t}),touch:()=>n.set({...n(),touched:!0})}}function q(t,e){const n=y(t),o=y(null);return e&&N(n,t=>{const n=e(t);o.set(n)}),{value:n,error:o}}function A(t){const e={};for(const n in t){const o=y({value:t[n].initial,error:null});e[n]=o,t[n].validate&&N(o,async e=>{try{const a=await t[n].validate(e.value);S(()=>o.set({...e,error:a}))}catch(t){S(()=>o.set({...e,error:t.message}))}})}const n=async()=>(await Promise.all(Object.values(e).map(t=>t().error))).every(t=>null==t),o=()=>Object.fromEntries(Object.entries(e).map(([t,e])=>[t,e().value]));return{fields:e,setField:(t,n)=>{const o=e[t];o&&S(()=>o.set({...o(),value:n}))},isValid:n,values:o,submit:async()=>{if(!await n())throw new Error("Validation failed");return o()}}}function R(t){const e={};for(const n in t){const o=t[n],a=y({value:o.initial,error:null});o.validate&&N(a,async t=>{try{const e=await o.validate(t.value);S(()=>{a.set({...a(),error:null!=e?e:null})})}catch(t){a.set({...a(),error:t.message})}}),e[n]=a}const n=()=>Object.fromEntries(Object.entries(e).map(([t,e])=>[t,e().value])),o=async()=>(await Promise.all(Object.values(e).map(t=>t().error))).every(t=>null==t);return{fields:e,values:n,isValid:o,setField:(t,n)=>{const o=e[t];o&&S(()=>o.set({...o(),value:n}))},submit:async()=>{if(!await o())throw new Error("Validation failed");return n()}}}const x=k({initial:{status:"idle"},states:{idle:{on:{VALIDATE:()=>({status:"validating"})}},validating:{on:{SUCCESS:()=>({status:"valid"}),ERROR:()=>({status:"invalid"})}},valid:{},invalid:{}}});function F(t,e="normal"){"low"===e?n(t):t()}function M(e){const[,n]=t.useState(0);return t.useEffect(()=>{const t=b(()=>{e(),n(t=>t+1)},"normal");return()=>t()},[e]),e()}function P(e){return M(t.useMemo(()=>E(e),[e]))}function T(e,n){t.useEffect(()=>{const t=N(e,n);return()=>null==t?void 0:t()},[e,n])}function z(){return S}function D(e,n="normal"){t.useEffect(()=>{const t=b(e,n);return()=>t()},[e,n])}function H(){const[t,n]=e.useState(m());return e.useEffect(()=>{const t=setInterval(()=>n(m()),100);return()=>clearInterval(t)},[]),o("div",{style:{fontFamily:"monospace",fontSize:12,background:"#222",color:"#eee",padding:10,maxHeight:300,overflowY:"auto"},children:t.map(t=>a("div",{children:["[",t.type,"] ",t.name||"anon"," → deps: ",t.deps.join(", ")]},t.id))})}export{H as DevtoolsReactPanel,w as asyncAtom,O as asyncComputed,y as atom,S as batch,g as clearHighlights,E as computed,k as createFSM,V as createField,I as createIntentBus,R as createNestedForm,b as effect,u as effectContext,m as getGraphSnapshot,p as highlightNode,h as linkNodes,c as schedule,s as scheduleCancelable,F as scheduleReactJob,v as trackNode,C as transaction,M as useAtom,z as useBatch,P as useComputed,D as useEffectReact,q as useField,A as useForm,T as useWatch,x as validationFSM,N as watch};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function DevtoolsReactPanel(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { batch as coreBatch, Priority } from '../core';
|
|
2
|
+
export declare function scheduleReactJob(job: () => void, priority?: Priority): void;
|
|
3
|
+
export declare function useAtom<T>(getter: () => T): T;
|
|
4
|
+
export declare function useComputed<T>(fn: () => T): T;
|
|
5
|
+
export declare function useWatch<T>(getter: () => T, cb: (val: T) => void): void;
|
|
6
|
+
export declare function useBatch(): typeof coreBatch;
|
|
7
|
+
export declare function useEffectReact(fn: () => void, priority?: Priority): void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Atom } from '../core';
|
|
2
|
+
export declare function createFSM<S extends {
|
|
3
|
+
status: string;
|
|
4
|
+
}>(config: {
|
|
5
|
+
initial: S;
|
|
6
|
+
states: Record<S['status'], {
|
|
7
|
+
on?: Record<string, (ctx: {
|
|
8
|
+
state: S;
|
|
9
|
+
payload?: any;
|
|
10
|
+
}) => S | Promise<S>>;
|
|
11
|
+
}>;
|
|
12
|
+
}): {
|
|
13
|
+
state: Atom<S>;
|
|
14
|
+
dispatch: (type: string, payload?: any) => Promise<void>;
|
|
15
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Atom } from '../core';
|
|
2
|
+
export type Intent<P = any> = {
|
|
3
|
+
type: string;
|
|
4
|
+
payload?: P;
|
|
5
|
+
};
|
|
6
|
+
export declare function createIntentBus<S>(): {
|
|
7
|
+
on<K extends string, P>(type: K, fn: (ctx: {
|
|
8
|
+
state: S;
|
|
9
|
+
intent: Intent<P>;
|
|
10
|
+
setState: (v: S) => void;
|
|
11
|
+
}) => void): void;
|
|
12
|
+
dispatch<P>(state: Atom<S>, intent: Intent<P>): void;
|
|
13
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chrono-state-z",
|
|
3
|
+
"version": "1.0.0-z",
|
|
4
|
+
"description": "Chrono-state-z: Reactive, intent-first state management with FSM, forms, async atoms.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Delpi.Kye",
|
|
7
|
+
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
|
|
10
|
+
"main": "build/index.cjs.js",
|
|
11
|
+
"module": "build/index.esm.js",
|
|
12
|
+
"types": "build/index.d.ts",
|
|
13
|
+
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./build/index.d.ts",
|
|
17
|
+
"import": "./build/index.esm.js",
|
|
18
|
+
"require": "./build/index.cjs.js"
|
|
19
|
+
},
|
|
20
|
+
"./react": {
|
|
21
|
+
"types": "./build/react/index.d.ts",
|
|
22
|
+
"import": "./build/react/index.esm.js",
|
|
23
|
+
"require": "./build/react/index.cjs.js"
|
|
24
|
+
},
|
|
25
|
+
"./devtools": {
|
|
26
|
+
"types": "./build/devtools/index.d.ts",
|
|
27
|
+
"import": "./build/devtools/index.esm.js",
|
|
28
|
+
"require": "./build/devtools/index.cjs.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
"files": ["build"],
|
|
33
|
+
|
|
34
|
+
"scripts": {
|
|
35
|
+
"clean": "rimraf build",
|
|
36
|
+
"build": "rollup -c",
|
|
37
|
+
"cb": "npm run clean && npm run build",
|
|
38
|
+
"prepublishOnly": "npm run cb"
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/delpikye-v/chrono-state.git"
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
"homepage": "https://github.com/delpikye-v/chrono-state#readme",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/delpikye-v/chrono-state/issues"
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
"keywords": [
|
|
52
|
+
"react",
|
|
53
|
+
"reactive",
|
|
54
|
+
"state-management",
|
|
55
|
+
"external-store",
|
|
56
|
+
"intent-runtime",
|
|
57
|
+
"fsm",
|
|
58
|
+
"form-engine",
|
|
59
|
+
"async-atom",
|
|
60
|
+
"computed-state",
|
|
61
|
+
"timeline",
|
|
62
|
+
"devtools",
|
|
63
|
+
"no-hooks",
|
|
64
|
+
"concurrent-safe",
|
|
65
|
+
"preemption"
|
|
66
|
+
],
|
|
67
|
+
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"react": "^18.0.0"
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@rollup/plugin-commonjs": "^25.0.0",
|
|
74
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
75
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
76
|
+
"rimraf": "^5.0.5",
|
|
77
|
+
"rollup": "^4.9.6",
|
|
78
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
79
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
80
|
+
"typescript": "^5.3.3",
|
|
81
|
+
"tslib": "^2.6.2",
|
|
82
|
+
|
|
83
|
+
"react": "^18.2.0",
|
|
84
|
+
"@types/react": "^18.2.43"
|
|
85
|
+
}
|
|
86
|
+
}
|