chrono-state-z 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # ⚙️ chrono-state-z
1
+ # ⏱️ chrono-state-z
2
2
 
3
3
  [![NPM](https://img.shields.io/npm/v/chrono-state-z.svg)](https://www.npmjs.com/package/chrono-state-z) ![Downloads](https://img.shields.io/npm/dt/chrono-state-z.svg)
4
4
 
@@ -6,11 +6,9 @@
6
6
 
7
7
  ---
8
8
 
9
- **chrono-state-z** is a **reactive, intent-first state runtime**
10
- designed to keep **business logic outside React**.
9
+ **chrono-state-z** is a **reactive, intent-first state runtime** designed to keep **business logic outside React**.
11
10
 
12
- It provides **atoms, computed values, async state, effects, scheduling**,
13
- with a **headless core** and **thin React bindings**.
11
+ It provides **atoms, computed values, async state, effects, scheduling**, with a **headless core** and **thin React bindings**.
14
12
 
15
13
  > **React renders state. Logic lives elsewhere.**
16
14
 
@@ -43,7 +41,7 @@ Use chrono-state-z when you need:
43
41
  ## 📦 Installation
44
42
 
45
43
  ```bash
46
- npm install react ## for react
44
+ # npm install react ## for react
47
45
  npm install intentx-core-z chrono-state-z
48
46
  ```
49
47
 
@@ -208,7 +206,7 @@ import { computed, useComputed } from 'chrono-state-z'
208
206
  const total = computed(() => price() * qty())
209
207
 
210
208
  function TotalView() {
211
- const value = useComputed(() => total())
209
+ const value = useComputed(total)
212
210
  return <div>Total: {value}</div>
213
211
  }
214
212
  ```
@@ -218,8 +216,7 @@ function TotalView() {
218
216
  ### useAtomSelector
219
217
 
220
218
  ```tsx
221
- import { atom, useAtomSelector } from 'chrono-state-z'
222
-
219
+ // atom() returns a callable reactive value
223
220
  const user = atom({ id: 1, name: 'Alice', age: 20 })
224
221
 
225
222
  function Username() {
@@ -234,11 +231,13 @@ function Username() {
234
231
 
235
232
  ```tsx
236
233
  import { useStore } from 'chrono-state-z'
234
+ import type { Store } from 'chrono-state-z'
237
235
 
238
- function StoreView({ store }) {
236
+ function StoreView({ store }: { store: Store<any> }) {
239
237
  const state = useStore(store)
240
- return <pre>{JSON.stringify(state)}</pre>
238
+ return <pre>{JSON.stringify(state, null, 2)}</pre>
241
239
  }
240
+
242
241
  ```
243
242
 
244
243
  ---
@@ -246,13 +245,22 @@ function StoreView({ store }) {
246
245
  ### useStoreSelector
247
246
 
248
247
  ```tsx
248
+ // Only re-renders when saving changes, not the whole store.
249
249
  import { useStoreSelector } from 'chrono-state-z'
250
250
 
251
251
  function SavingBadge({ store }) {
252
252
  const saving = useStoreSelector(store, s => s.saving)
253
+ // const saving = useStoreSelector(
254
+ // store,
255
+ // s => s.meta,
256
+ // shallowEqual
257
+ // )
253
258
  return saving ? 'Saving...' : 'Idle'
254
259
  }
260
+
255
261
  ```
262
+ - `selector` should be pure and stable.
263
+ - If you need dynamic selection, memoize the selector.
256
264
 
257
265
  ---
258
266
 
@@ -261,28 +269,46 @@ function SavingBadge({ store }) {
261
269
  ```ts
262
270
  import { useWatch } from 'chrono-state-z'
263
271
 
264
- useWatch(
265
- () => user(),
266
- (u) => {
267
- if (u?.role === 'admin') redirect('/admin')
268
- }
269
- )
272
+ function AuthGuard() {
273
+ useWatch(
274
+ () => user(),
275
+ (u) => {
276
+ if (u?.role === 'admin') {
277
+ redirect('/admin')
278
+ }
279
+ }
280
+ )
281
+
282
+ return null
283
+ }
284
+
285
+ ```
286
+
287
+ ### AsyncAtom
288
+ ```ts
289
+ const user = asyncAtom(fetchUser)
290
+
291
+ // load explicitly
292
+ await user.load()
293
+
294
+ // read value (throws / suspends if not ready)
295
+ const u = user()
270
296
  ```
271
297
 
272
298
  ---
273
299
 
274
- ## 🧩 Architecture Example
300
+ ## 🧩 Architecture Pattern
275
301
 
276
302
  ```ts
277
303
  import { asyncAtom, computed } from 'chrono-state-z'
278
304
 
279
305
  export function createUserLogic() {
280
306
  const user = asyncAtom(fetchUser)
281
- const username = computed(() => user()?.name ?? 'Guest')
307
+ const name = computed(() => user()?.name ?? 'Guest')
282
308
 
283
309
  return {
284
310
  user,
285
- username,
311
+ name,
286
312
  reload: () => user.invalidate()
287
313
  }
288
314
  }
@@ -291,15 +317,16 @@ export function createUserLogic() {
291
317
  ```tsx
292
318
  function UserView({ logic }) {
293
319
  const user = useAtom(logic.user)
294
- const name = useComputed(() => logic.username())
320
+ const name = useComputed(() => logic.name())
295
321
 
296
322
  return (
297
- <div>
298
- <h3>Hello {name}</h3>
323
+ <>
324
+ <div>Hello {name}</div>
299
325
  <button onClick={logic.reload}>Reload</button>
300
- </div>
326
+ </>
301
327
  )
302
328
  }
329
+
303
330
  ```
304
331
 
305
332
  ---
@@ -337,4 +364,4 @@ function UserView({ logic }) {
337
364
 
338
365
  ## 📜 License
339
366
 
340
- MIT / Delpi
367
+ MIT
@@ -6,4 +6,4 @@ export type AsyncAtom<T> = {
6
6
  cancel(): void;
7
7
  invalidate(p?: Priority): void;
8
8
  };
9
- export declare function asyncAtom<T>(fetcher: () => Promise<T>): AsyncAtom<T>;
9
+ export declare function asyncAtom<T>(fetcher: (signal?: AbortSignal) => Promise<T>): AsyncAtom<T>;
@@ -3,4 +3,4 @@ export type AsyncComputed<T> = {
3
3
  (): T;
4
4
  invalidate(p?: Priority): void;
5
5
  };
6
- export declare function asyncComputed<T>(getter: () => Promise<T>, priority?: Priority): AsyncComputed<T>;
6
+ export declare function asyncComputed<T>(getter: (signal?: AbortSignal) => Promise<T>, priority?: Priority): AsyncComputed<T>;
@@ -1,11 +1,3 @@
1
- import type { IntentHandler, Scope } from "intentx-core-z";
2
- export type Subscriber = () => void;
3
- export type Store<S extends object> = {
4
- scope: Scope;
5
- state(): S;
6
- setState(fn: (s: S) => void): void;
7
- subscribe(fn: Subscriber): () => void;
8
- emit(type: string, payload?: any): Promise<void>;
9
- on(type: string, handler: IntentHandler<S>): () => void;
10
- };
1
+ import type { Scope } from "intentx-core-z";
2
+ import { Store } from "./store";
11
3
  export declare function createStore<S extends object>(initial: S, scope?: Scope): Store<S>;
@@ -5,10 +5,6 @@ export { asyncComputed } from "./asyncComputed";
5
5
  export { effect } from "./effect";
6
6
  export { watch } from "./watch";
7
7
  export { createStore } from "./createStore";
8
- export type { Store } from "./createStore";
9
- export { createScope, defaultScope } from "./scope";
10
- export type { Scope } from "./scope";
11
- export { transaction } from "./transaction";
12
- export { createSelector } from "./selector";
8
+ export { selectAtom, createSelector } from "./selector";
9
+ export type { Store, Subscriber } from "./store";
13
10
  export { factoryAtom } from "./factoryAtom";
14
- export { createTestRuntime } from "./testRuntime";
@@ -1 +1,7 @@
1
- export declare function createSelector<T, R>(select: (state: T) => R, isEqual?: (a: R, b: R) => boolean): (state: T) => R;
1
+ export type EqualityFn<T> = (a: T, b: T) => boolean;
2
+ export declare function createSelector<S, R>(select: (state: S) => R, isEqual?: EqualityFn<R>): (state: S) => R;
3
+ export declare function selectAtom<T, R>(atom: () => T, selector: (value: T) => R, onChange: (value: R) => void, options?: {
4
+ isEqual?: (a: R, b: R) => boolean;
5
+ immediate?: boolean;
6
+ }): () => void;
7
+ export declare function watchSelector<S, R>(getState: () => S, subscribe: (fn: () => void) => () => void, selector: (state: S) => R, onChange: (value: R) => void, isEqual?: EqualityFn<R>): () => void;
@@ -1,9 +1,12 @@
1
- import type { IntentHandler } from "intentx-core-z";
1
+ import type { IntentHandler, Scope } from "intentx-core-z";
2
+ import { EqualityFn } from "./selector";
2
3
  export type Subscriber = () => void;
3
4
  export type Store<S extends object> = {
5
+ scope: Scope;
4
6
  state(): S;
5
7
  setState(fn: (s: S) => void): void;
6
8
  subscribe(fn: Subscriber): () => void;
7
- emit(intent: string, payload?: any): Promise<void>;
8
- on(intent: string, handler: IntentHandler<S>): () => void;
9
+ emit(type: string, payload?: any): Promise<void>;
10
+ on(type: string, handler: IntentHandler<S>): () => void;
11
+ watch<R>(selector: (state: S) => R, onChange: (value: R) => void, isEqual?: EqualityFn<R>): () => void;
9
12
  };
@@ -1 +1 @@
1
- "use strict";var e=require("intentx-core-z"),t=require("react");function n(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,Object.freeze(t)}var r=n(t);const c={activeEffect:null};function o(t){let n=t;const r=new Set,o=new Set,u=()=>(c.activeEffect&&o.add(c.activeEffect),n);return u.set=(t,c="normal")=>{Object.is(t,n)||(n=t,o.forEach(t=>e.schedule(t,c)),r.forEach(e=>e()))},u.subscribe=e=>(r.add(e),()=>r.delete(e)),u}function u(t){let n,r=!0;const o=new Set,u=()=>{r||(r=!0,o.forEach(t=>e.schedule(t)))};return()=>{if(c.activeEffect&&o.add(c.activeEffect),r){const e=c.activeEffect;c.activeEffect=u,n=t(),r=!1,c.activeEffect=e}return n}}function s(t,n="normal"){let r=null,o=!1;const u=()=>{if(!o){null==r||r();try{c.activeEffect=u;const e=t();"function"==typeof e&&(r=e)}finally{c.activeEffect=null}}};return e.schedule(u,n),()=>{o=!0,null==r||r(),r=null}}function l(e,t,n="normal"){return s(()=>{t(e())},n)}const a=e.createScope("default");function f(e){return r.useSyncExternalStore(t=>{let n=e();return s(()=>{const r=e();Object.is(n,r)||(n=r,t())})},e,e)}Object.defineProperty(exports,"createScope",{enumerable:!0,get:function(){return e.createScope}}),Object.defineProperty(exports,"transaction",{enumerable:!0,get:function(){return e.batch}}),exports.asyncAtom=function(t){const n=o(null);let r=null,c=!1;const u=()=>(r||(c=!1,r=t().then(e=>(c||n.set(e),e)).finally(()=>{r=null})),r),s=()=>{c=!0,r=null},l=()=>{const e=n();if(null===e)throw u();return e};return l.set=n.set,l.load=u,l.cancel=s,l.invalidate=(t="normal")=>{s(),e.schedule(()=>{try{u()}catch{}},t)},l},exports.asyncComputed=function(t,n="normal"){const r=o(void 0);let c=null,u=!1,s=null;const l=()=>(c||(u=!1,s=null,c=t().then(e=>(u||r.set(e,n),e)).catch(e=>{throw u||(s=e),e}).finally(()=>{c=null})),c),a=()=>{const e=r();if(s)throw s;if(void 0===e)throw l();return e};return a.invalidate=(t=n)=>{u=!0,c=null,e.schedule(()=>{try{l()}catch{}},t)},a},exports.atom=o,exports.computed=u,exports.createSelector=function(e,t=Object.is){let n,r=!1;return c=>{const o=e(c);return r&&t(n,o)?n:(r=!0,n=o,o)}},exports.createStore=function(t,n=a){let r=t;const c=new Set,o=e.createIntentBus((e,t)=>{const n=new AbortController;return{payload:e,scope:t,state:r,signal:n.signal,setState(e){e(r),c.forEach(e=>e())},emit:(e,n)=>o.emit(e,n,t)}});return{scope:n,state:()=>r,setState(e){e(r),c.forEach(e=>e())},subscribe:e=>(c.add(e),()=>c.delete(e)),emit:(e,t)=>o.emit(e,t,n),on:(e,t)=>o.on(e,t,n)}},exports.createTestRuntime=function(){let e=[];return{schedule(t){e.push(t)},flushEffects(){for(;e.length;)e.shift()()}}},exports.defaultScope=a,exports.effect=s,exports.factoryAtom=function(e){const t=new Map;return n=>{let r=t.get(n);return r||(r=e(n),t.set(n,r)),r}},exports.scheduleReactJob=function(e,n="normal"){"low"===n?t.startTransition(e):e()},exports.useAtom=f,exports.useBatch=function(){return e.batch},exports.useComputed=function(e){return f(r.useMemo(()=>u(e),[e]))},exports.useEffectReact=function(e,t="normal"){r.useEffect(()=>{const n=s(e,t);return()=>n()},[e,t])},exports.useFactoryAtom=function(e,t){return f(e(t))},exports.useStore=function(e){return t.useSyncExternalStore(e.subscribe,e.state,e.state)},exports.useStoreSelector=function(e,n,r=Object.is){const c=t.useRef(null),o=()=>{const t=n(e.state()),o=c.current;return null!==o&&r(o,t)?o:(c.current=t,t)};return t.useSyncExternalStore(e.subscribe,o,o)},exports.useWatch=function(e,t){r.useEffect(()=>{const n=l(e,t);return()=>null==n?void 0:n()},[e,t])},exports.watch=l;
1
+ "use strict";var t=require("intentx-core-z"),e=require("react");function n(t){var e=Object.create(null);return t&&Object.keys(t).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}}),e.default=t,Object.freeze(e)}var r=n(e);const o={activeEffect:null};function c(e){let n=e;const r=new Set,c=new Set,u=()=>(o.activeEffect&&c.add(o.activeEffect),n);return u.set=(e,o="normal")=>{Object.is(e,n)||(n=e,c.forEach(e=>t.schedule(e,o)),r.forEach(e=>t.schedule(e,o)))},u.subscribe=t=>(r.add(t),()=>r.delete(t)),u}function u(e){let n,r=!0;const c=new Set,u=()=>{r||(r=!0,c.forEach(e=>t.schedule(e)))};return()=>{if(o.activeEffect&&c.add(o.activeEffect),r){const t=o.activeEffect;o.activeEffect=u,n=e(),r=!1,o.activeEffect=t}return n}}function l(e,n="normal"){let r=null,c=!1;const u=()=>{if(!c){null==r||r();try{o.activeEffect=u;const t=e();"function"==typeof t&&(r=t)}finally{o.activeEffect=null}}};return t.schedule(u,n),()=>{c=!0,null==r||r(),r=null}}function s(t,e,n="normal"){return l(()=>{e(t())},n)}function a(t,e=Object.is){let n,r=!1;return o=>{const c=t(o);return r&&e(n,c)?n:(r=!0,n=c,c)}}const i=t.createScope("default");function f(t){return r.useSyncExternalStore(e=>{let n=t();return l(()=>{const r=t();Object.is(n,r)||(n=r,e())})},t,t)}Object.defineProperty(exports,"createScope",{enumerable:!0,get:function(){return t.createScope}}),exports.asyncAtom=function(e){const n=c(null);let r=null,o=null;const u=()=>(r||(o=new AbortController,r=e(o.signal).then(t=>((null==o?void 0:o.signal.aborted)||n.set(t),t)).finally(()=>{r=null})),r),l=()=>{null==o||o.abort(),o=null,r=null},s=()=>{const t=n();if(null===t)throw u();return t};return s.set=n.set,s.load=u,s.cancel=l,s.invalidate=(e="normal")=>{l(),t.schedule(()=>{try{u()}catch{}},e)},s},exports.asyncComputed=function(e,n="normal"){const r=c(void 0);let o=null,u=null,l=null;const s=()=>(o||(u=new AbortController,l=null,o=e(u.signal).then(t=>((null==u?void 0:u.signal.aborted)||r.set(t,n),t)).catch(t=>{throw(null==u?void 0:u.signal.aborted)||(l=t),t}).finally(()=>{o=null})),o),a=()=>{const t=r();if(l)throw l;if(void 0===t)throw s();return t};return a.invalidate=(e=n)=>{null==u||u.abort(),u=null,o=null,t.schedule(()=>{try{s()}catch{}},e)},a},exports.atom=c,exports.computed=u,exports.createSelector=a,exports.createStore=function(e,n=i){let r=e;const o=new Set,c=t.createIntentBus((t,e)=>{const n=new AbortController;return{payload:t,scope:e,state:r,signal:n.signal,setState(t){t(r),o.forEach(t=>t())},emit:(t,n)=>c.emit(t,n,e)}});return{scope:n,state:()=>r,setState(t){t(r),o.forEach(t=>t())},subscribe:t=>(o.add(t),()=>o.delete(t)),emit:(t,e)=>c.emit(t,e,n),on:(t,e)=>c.on(t,e,n),watch:(t,e,n)=>function(t,e,n,r,o=Object.is){const c=a(n,o);let u=!1;const l=()=>{u||r(c(t()))};l();const s=e(l);return()=>{u=!0,s()}}(()=>r,t=>(o.add(t),()=>o.delete(t)),t,e,n)}},exports.effect=l,exports.factoryAtom=function(t){const e=new Map;return n=>{let r=e.get(n);return r||(r=t(n),e.set(n,r)),r}},exports.scheduleReactJob=function(t,n="normal"){"low"===n?e.startTransition(t):t()},exports.selectAtom=function(t,e,n,r){var o;const c=null!==(o=null==r?void 0:r.isEqual)&&void 0!==o?o:Object.is;let u;return l(()=>{const o=e(t());void 0!==u&&c(u,o)||(u=o,((null==r?void 0:r.immediate)||void 0!==u)&&n(o))})},exports.useAtom=f,exports.useAtomSelector=function(t,e,n=Object.is){const o=r.useRef(null),c=()=>{const r=e(t()),c=o.current;return null!==c&&n(c,r)?c:(o.current=r,r)};return r.useSyncExternalStore(e=>l(()=>{t(),e()}),c,c)},exports.useBatch=function(){return t.batch},exports.useComputed=function(t){return f(r.useMemo(()=>u(t),[t]))},exports.useEffectReact=function(t,e="normal"){r.useEffect(()=>{const n=l(t,e);return()=>n()},[t,e])},exports.useFactoryAtom=function(t,e){return f(t(e))},exports.useStore=function(t){return e.useSyncExternalStore(t.subscribe,t.state,t.state)},exports.useStoreSelector=function(t,e,n=Object.is){const o=r.useMemo(()=>a(e,n),[e,n]),c=r.useCallback(()=>o(t.state()),[t,o]);return r.useSyncExternalStore(t.subscribe,c,c)},exports.useWatch=function(t,e){r.useEffect(()=>{const n=s(t,e);return()=>null==n?void 0:n()},[t,e])},exports.watch=s;
package/build/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export { createScope, Scope } from "intentx-core-z";
1
2
  export * from './core';
2
3
  export * from './react';
@@ -1 +1 @@
1
- import{schedule as t,createScope as n,createIntentBus as e,batch as r}from"intentx-core-z";export{createScope,batch as transaction}from"intentx-core-z";import*as c from"react";import o,{startTransition as u}from"react";const l={activeEffect:null};function f(n){let e=n;const r=new Set,c=new Set,o=()=>(l.activeEffect&&c.add(l.activeEffect),e);return o.set=(n,o="normal")=>{Object.is(n,e)||(e=n,c.forEach(n=>t(n,o)),r.forEach(t=>t()))},o.subscribe=t=>(r.add(t),()=>r.delete(t)),o}function i(n){let e,r=!0;const c=new Set,o=()=>{r||(r=!0,c.forEach(n=>t(n)))};return()=>{if(l.activeEffect&&c.add(l.activeEffect),r){const t=l.activeEffect;l.activeEffect=o,e=n(),r=!1,l.activeEffect=t}return e}}function s(n){const e=f(null);let r=null,c=!1;const o=()=>(r||(c=!1,r=n().then(t=>(c||e.set(t),t)).finally(()=>{r=null})),r),u=()=>{c=!0,r=null},l=()=>{const t=e();if(null===t)throw o();return t};return l.set=e.set,l.load=o,l.cancel=u,l.invalidate=(n="normal")=>{u(),t(()=>{try{o()}catch{}},n)},l}function a(n,e="normal"){const r=f(void 0);let c=null,o=!1,u=null;const l=()=>(c||(o=!1,u=null,c=n().then(t=>(o||r.set(t,e),t)).catch(t=>{throw o||(u=t),t}).finally(()=>{c=null})),c),i=()=>{const t=r();if(u)throw u;if(void 0===t)throw l();return t};return i.invalidate=(n=e)=>{o=!0,c=null,t(()=>{try{l()}catch{}},n)},i}function E(n,e="normal"){let r=null,c=!1;const o=()=>{if(!c){null==r||r();try{l.activeEffect=o;const t=n();"function"==typeof t&&(r=t)}finally{l.activeEffect=null}}};return t(o,e),()=>{c=!0,null==r||r(),r=null}}function h(t,n,e="normal"){return E(()=>{n(t())},e)}const d=n("default");function m(t,n=d){let r=t;const c=new Set,o=e((t,n)=>{const e=new AbortController;return{payload:t,scope:n,state:r,signal:e.signal,setState(t){t(r),c.forEach(t=>t())},emit:(t,e)=>o.emit(t,e,n)}});return{scope:n,state:()=>r,setState(t){t(r),c.forEach(t=>t())},subscribe:t=>(c.add(t),()=>c.delete(t)),emit:(t,e)=>o.emit(t,e,n),on:(t,e)=>o.on(t,e,n)}}function v(t,n=Object.is){let e,r=!1;return c=>{const o=t(c);return r&&n(e,o)?e:(r=!0,e=o,o)}}function b(t){const n=new Map;return e=>{let r=n.get(e);return r||(r=t(e),n.set(e,r)),r}}function S(){let t=[];return{schedule(n){t.push(n)},flushEffects(){for(;t.length;)t.shift()()}}}function p(t){return c.useSyncExternalStore(n=>{let e=t();return E(()=>{const r=t();Object.is(e,r)||(e=r,n())})},t,t)}function w(t){return p(c.useMemo(()=>i(t),[t]))}function y(t,n){return p(t(n))}function x(t){return o.useSyncExternalStore(t.subscribe,t.state,t.state)}function g(t,n,e=Object.is){const r=o.useRef(null),c=()=>{const c=n(t.state()),o=r.current;return null!==o&&e(o,c)?o:(r.current=c,c)};return o.useSyncExternalStore(t.subscribe,c,c)}function j(t,n){c.useEffect(()=>{const e=h(t,n);return()=>null==e?void 0:e()},[t,n])}function O(t,n="normal"){c.useEffect(()=>{const e=E(t,n);return()=>e()},[t,n])}function z(){return r}function M(t,n="normal"){"low"===n?u(t):t()}export{s as asyncAtom,a as asyncComputed,f as atom,i as computed,v as createSelector,m as createStore,S as createTestRuntime,d as defaultScope,E as effect,b as factoryAtom,M as scheduleReactJob,p as useAtom,z as useBatch,w as useComputed,O as useEffectReact,y as useFactoryAtom,x as useStore,g as useStoreSelector,j as useWatch,h as watch};
1
+ import{schedule as t,createScope as n,createIntentBus as e,batch as r}from"intentx-core-z";export{createScope}from"intentx-core-z";import*as o from"react";import l,{startTransition as c}from"react";const u={activeEffect:null};function i(n){let e=n;const r=new Set,o=new Set,l=()=>(u.activeEffect&&o.add(u.activeEffect),e);return l.set=(n,l="normal")=>{Object.is(n,e)||(e=n,o.forEach(n=>t(n,l)),r.forEach(n=>t(n,l)))},l.subscribe=t=>(r.add(t),()=>r.delete(t)),l}function s(n){let e,r=!0;const o=new Set,l=()=>{r||(r=!0,o.forEach(n=>t(n)))};return()=>{if(u.activeEffect&&o.add(u.activeEffect),r){const t=u.activeEffect;u.activeEffect=l,e=n(),r=!1,u.activeEffect=t}return e}}function a(n){const e=i(null);let r=null,o=null;const l=()=>(r||(o=new AbortController,r=n(o.signal).then(t=>((null==o?void 0:o.signal.aborted)||e.set(t),t)).finally(()=>{r=null})),r),c=()=>{null==o||o.abort(),o=null,r=null},u=()=>{const t=e();if(null===t)throw l();return t};return u.set=e.set,u.load=l,u.cancel=c,u.invalidate=(n="normal")=>{c(),t(()=>{try{l()}catch{}},n)},u}function f(n,e="normal"){const r=i(void 0);let o=null,l=null,c=null;const u=()=>(o||(l=new AbortController,c=null,o=n(l.signal).then(t=>((null==l?void 0:l.signal.aborted)||r.set(t,e),t)).catch(t=>{throw(null==l?void 0:l.signal.aborted)||(c=t),t}).finally(()=>{o=null})),o),s=()=>{const t=r();if(c)throw c;if(void 0===t)throw u();return t};return s.invalidate=(n=e)=>{null==l||l.abort(),l=null,o=null,t(()=>{try{u()}catch{}},n)},s}function d(n,e="normal"){let r=null,o=!1;const l=()=>{if(!o){null==r||r();try{u.activeEffect=l;const t=n();"function"==typeof t&&(r=t)}finally{u.activeEffect=null}}};return t(l,e),()=>{o=!0,null==r||r(),r=null}}function b(t,n,e="normal"){return d(()=>{n(t())},e)}function v(t,n=Object.is){let e,r=!1;return o=>{const l=t(o);return r&&n(e,l)?e:(r=!0,e=l,l)}}function m(t,n,e,r){var o;const l=null!==(o=null==r?void 0:r.isEqual)&&void 0!==o?o:Object.is;let c;return d(()=>{const o=n(t());void 0!==c&&l(c,o)||(c=o,((null==r?void 0:r.immediate)||void 0!==c)&&e(o))})}const E=n("default");function h(t,n=E){let r=t;const o=new Set,l=e((t,n)=>{const e=new AbortController;return{payload:t,scope:n,state:r,signal:e.signal,setState(t){t(r),o.forEach(t=>t())},emit:(t,e)=>l.emit(t,e,n)}});return{scope:n,state:()=>r,setState(t){t(r),o.forEach(t=>t())},subscribe:t=>(o.add(t),()=>o.delete(t)),emit:(t,e)=>l.emit(t,e,n),on:(t,e)=>l.on(t,e,n),watch:(t,n,e)=>function(t,n,e,r,o=Object.is){const l=v(e,o);let c=!1;const u=()=>{c||r(l(t()))};u();const i=n(u);return()=>{c=!0,i()}}(()=>r,t=>(o.add(t),()=>o.delete(t)),t,n,e)}}function S(t){const n=new Map;return e=>{let r=n.get(e);return r||(r=t(e),n.set(e,r)),r}}function w(t){return o.useSyncExternalStore(n=>{let e=t();return d(()=>{const r=t();Object.is(e,r)||(e=r,n())})},t,t)}function y(t,n,e=Object.is){const r=o.useRef(null),l=()=>{const o=n(t()),l=r.current;return null!==l&&e(l,o)?l:(r.current=o,o)};return o.useSyncExternalStore(n=>d(()=>{t(),n()}),l,l)}function p(){return r}function g(t){return w(o.useMemo(()=>s(t),[t]))}function x(t,n="normal"){o.useEffect(()=>{const e=d(t,n);return()=>e()},[t,n])}function j(t,n){return w(t(n))}function O(t){return l.useSyncExternalStore(t.subscribe,t.state,t.state)}function C(t,n,e=Object.is){const r=o.useMemo(()=>v(n,e),[n,e]),l=o.useCallback(()=>r(t.state()),[t,r]);return o.useSyncExternalStore(t.subscribe,l,l)}function A(t,n){o.useEffect(()=>{const e=b(t,n);return()=>null==e?void 0:e()},[t,n])}function M(t,n="normal"){"low"===n?c(t):t()}export{a as asyncAtom,f as asyncComputed,i as atom,s as computed,v as createSelector,h as createStore,d as effect,S as factoryAtom,M as scheduleReactJob,m as selectAtom,w as useAtom,y as useAtomSelector,p as useBatch,g as useComputed,x as useEffectReact,j as useFactoryAtom,O as useStore,C as useStoreSelector,A as useWatch,b as watch};
@@ -1,9 +1,10 @@
1
1
  export { useAtom } from './useAtom';
2
+ export { useAtomSelector } from './useAtomSelector';
3
+ export { useBatch } from './useBatch';
2
4
  export { useComputed } from './useComputed';
5
+ export { useEffectReact } from './useEffectReact';
3
6
  export { useFactoryAtom } from './useFactoryAtom';
4
7
  export { useStore } from './useStore';
5
8
  export { useStoreSelector } from './useStoreSelector';
6
9
  export { useWatch } from './useWatch';
7
- export { useEffectReact } from './useEffectReact';
8
- export { useBatch } from './useBatch';
9
10
  export { scheduleReactJob } from './schedule';
@@ -1,4 +1,4 @@
1
- export declare function useStoreSelector<S, T>(store: {
1
+ export declare function useStoreSelector<S, R>(store: {
2
2
  state(): S;
3
3
  subscribe(fn: () => void): () => void;
4
- }, selector: (state: S) => T, isEqual?: (a: T, b: T) => boolean): T;
4
+ }, selector: (state: S) => R, isEqual?: (a: R, b: R) => boolean): R;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "chrono-state-z",
3
- "version": "2.0.1",
4
- "description": "Reactive, intent-first state runtime with atoms, computed, async state, effects, and FSM. Logic-first, React-agnostic.",
3
+ "version": "2.1.0",
4
+ "description": "Intent-first, logic-centric reactive state runtime with atoms, computed, async state, effects, and deterministic scheduling. React-agnostic core.",
5
5
  "license": "MIT",
6
6
  "author": "Delpi.Kye",
7
7
  "sideEffects": false,
@@ -51,25 +51,25 @@
51
51
  },
52
52
 
53
53
  "keywords": [
54
- "react",
55
54
  "state-management",
56
55
  "reactive",
57
56
  "atom",
58
57
  "computed",
59
58
  "async-state",
60
- "external-store",
61
59
  "intent",
62
60
  "intent-first",
63
61
  "logic-first",
64
- "headless",
65
- "fsm",
66
62
  "effects",
67
- "scheduler"
63
+ "scheduler",
64
+ "headless",
65
+ "react",
66
+ "react-hooks",
67
+ "react-18"
68
68
  ],
69
69
 
70
70
  "peerDependencies": {
71
71
  "react": "^18.0.0",
72
- "intentx-core-z": "^1.0.0"
72
+ "intentx-core-z": "^2.1.0"
73
73
  },
74
74
 
75
75
  "devDependencies": {
@@ -1,4 +0,0 @@
1
- import { Scope } from "intentx-core-z";
2
- export { createScope } from "intentx-core-z";
3
- export type { Scope } from "intentx-core-z";
4
- export declare const defaultScope: Scope;
@@ -1,6 +0,0 @@
1
- type Job = () => void;
2
- export declare function createTestRuntime(): {
3
- schedule(job: Job): void;
4
- flushEffects(): void;
5
- };
6
- export {};
@@ -1,4 +0,0 @@
1
- export declare function useScopedStore<S>(store: {
2
- state(): S;
3
- subscribe(fn: () => void): () => void;
4
- }): S;