small-state-react 0.1.0 → 0.1.1

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.ja.md CHANGED
@@ -10,15 +10,16 @@ npm i small-state-react
10
10
 
11
11
  ## Usage
12
12
 
13
- ### './small-state-react'
13
+ ### Import from "./small-state-react"
14
14
 
15
15
  #### `useRuntime(machine, options?)`
16
16
 
17
- `<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M>) => {snapshot, send, runtime}`
17
+ `<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M> & { onPreStart?: (runtime: Runtime<M>) => void }) => {snapshot, send, runtime}`
18
18
 
19
19
  - マシンを受け取り、ランタイムとして start させ、 `{snapshot, send, runtime}` のオブジェクトを返す。
20
- - マシンは参照が変わると作り直されるので makeMachine をインラインで定義するのでなくいったん変数に束縛しそれを指定する必要がある。
21
- - 逆にオプションはインラインで指定してもランタイムが作り直されないことに注意。
20
+ - オプション引数は一番最初の呼び出しに与えられた引数のみが使われる。インラインで指定されて参照が変わっても同一の値と見なすためである。
21
+ - オプション `onPreStart` はランタイムが start される直前に実行されるフック関数であり、マシンから作られたランタイムを引数に受け取る。
22
+ - `.onEmitted` や `.subscribe` を実行するのに使うと良い。
22
23
 
23
24
 
24
25
  #### `useSelectedSnapshot(runtime, selector, compare?)`
@@ -30,12 +31,14 @@ npm i small-state-react
30
31
  - 引数 compare は selection の旧値と新値を比較する関数である。 true なら同一と見なし再レンダリングを抑制する。 false なら更新を反映する。省略時は `===` による同一性比較である。
31
32
 
32
33
 
33
- ### './small-state-react/jotai'
34
+ ### Import from "./small-state-react/jotai"
34
35
 
35
36
  #### `atomWithRuntimeSnapshot(machine, options?)`
36
37
 
37
- `<M extends AnyMachine>(machine: M | ((get:Getter)=>M), options?: RuntimeOptions<M>) => WritableAtom<Snapshot<M>, [event: SendFnEvent<M>], void>`
38
+ `<M extends AnyMachine>(machine: M | ((get:Getter)=>M), options?: RuntimeOptions<M> & { onPreStart?: (runtime: Runtime<M>) => void }) => WritableAtom<Snapshot<M>, [event: SendFnEvent<M>], void>`
38
39
 
39
40
  - 指定したマシンについての atom を返す。
40
41
  - これはどこかでマウントされたときランタイム化され起動される。どこにもマウントされなくなったとき、このランタイムは破棄される。
41
42
  - `useAtom` に渡して戻ってくる値は `[snapshot, send]` である。
43
+ - オプション `onPreStart` はランタイムが start される直前に実行されるフック関数であり、マシンから作られたランタイムを引数に受け取る。
44
+ - `.onEmitted` や `.subscribe` を実行するのに使うと良い。
package/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # small-state-react
2
2
 
3
+ This README.md was automatically translated from the Japanese version and may contain inaccuracies. For accurate information, please refer to [the Japanese version](https://github.com/qyvvy/small-state-react/blob/main/README.ja.md).
4
+
5
+ ---
6
+
3
7
  React custom hooks for [small\-state](https://github.com/qyvvy/small-state), and React extensions for [Jotai](https://github.com/pmndrs/jotai).
4
8
 
5
9
 
@@ -11,15 +15,16 @@ npm i small-state-react
11
15
 
12
16
  ## Usage
13
17
 
14
- ### './small-state-react'
18
+ ### Import from "./small-state-react"
15
19
 
16
20
  #### `useRuntime(machine, options?)`
17
21
 
18
- `<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M>) => {snapshot, send, runtime}`
22
+ `<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M> & { onPreStart?: (runtime: Runtime<M>) => void }) => {snapshot, send, runtime}`
19
23
 
20
24
  - Accepts a machine, starts it as a Runtime, and returns an `{snapshot, send, runtime}` object.
21
- - Since the machine is recreated if its reference changes, you must bind makeMachine to a variable and pass it, rather than defining it inline.
22
- - Note that options, conversely, will not cause the runtime to be recreated even if specified inline.
25
+ - Only the options passed to the initial call are used. Even if the options are specified inline and their references change between renders, they are treated as the same values.
26
+ - The `onPreStart` option is a hook function that is called immediately before the runtime starts. It receives `runtime` as an argument.
27
+ - This is useful for registering `.onEmitted` or `.subscribe` handlers.
23
28
 
24
29
 
25
30
  #### `useSelectedSnapshot(runtime, selector, compare?)`
@@ -31,7 +36,7 @@ npm i small-state-react
31
36
  - The compare argument is a function that compares the old and new values of the selection. If true, they are considered identical and re-rendering is suppressed. If false, the update is applied. If omitted, identity comparison (===) is used.
32
37
 
33
38
 
34
- ### './small-state-react/jotai'
39
+ ### Import from "./small-state-react/jotai"
35
40
 
36
41
  #### `atomWithRuntimeSnapshot(machine, options?)`
37
42
 
package/dist/index.cjs CHANGED
@@ -5,18 +5,20 @@ let use_sync_external_store_with_selector = require("use-sync-external-store/wit
5
5
  // src/react/useRuntime.ts
6
6
  function useRuntime(machine, options) {
7
7
  if (!(0, small_state.isMachine)(machine)) throw new Error(`useRuntime() expects Machine object.`);
8
- const runtime = (0, react.useMemo)(() => (0, small_state.makeRuntime)(machine, options), [machine]);
8
+ const qOptions = (0, react.useMemo)(() => options, []);
9
+ const runtime = (0, react.useMemo)(() => (0, small_state.makeRuntime)(machine, qOptions), [machine, qOptions]);
9
10
  const subscribe = (0, react.useCallback)((onStoreChange) => {
10
11
  return runtime.subscribe(onStoreChange, onStoreChange);
11
12
  }, [runtime]);
12
13
  const getSnapshot = (0, react.useCallback)(() => runtime?.getSnapshot(), [runtime]);
13
14
  const snapshot = (0, react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
14
15
  (0, react.useEffect)(() => {
16
+ qOptions?.onPreStart?.(runtime);
15
17
  runtime.start();
16
18
  return () => {
17
19
  runtime.stop();
18
20
  };
19
- }, [runtime]);
21
+ }, [runtime, qOptions]);
20
22
  return {
21
23
  snapshot,
22
24
  send: runtime.send.bind(runtime),
package/dist/index.d.cts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { AnyMachine, Runtime, RuntimeOptions, Snapshot } from "small-state";
2
- export declare function useRuntime<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M>): {
2
+ export declare function useRuntime<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M> & {
3
+ onPreStart?: (runtime: Runtime<M>) => void;
4
+ }): {
3
5
  snapshot: import("small-state").Snapshot<M>;
4
- send: (event: import("small-state").SendFnEvent<M>) => import("small-state").Runtime<M>;
5
- runtime: import("small-state").Runtime<M>;
6
+ send: (event: import("small-state").SendFnEvent<M>) => Runtime<M>;
7
+ runtime: Runtime<M>;
6
8
  };
7
9
  export declare function useSelectedSnapshot<M extends AnyMachine, Sel>(runtime: Runtime<M> | undefined, selector: (snapshot: Snapshot<M>) => Sel, compare?: (current: Sel, next: Sel) => boolean): Sel;
package/dist/index.d.mts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { AnyMachine, Runtime, RuntimeOptions, Snapshot } from "small-state";
2
- export declare function useRuntime<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M>): {
2
+ export declare function useRuntime<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M> & {
3
+ onPreStart?: (runtime: Runtime<M>) => void;
4
+ }): {
3
5
  snapshot: import("small-state").Snapshot<M>;
4
- send: (event: import("small-state").SendFnEvent<M>) => import("small-state").Runtime<M>;
5
- runtime: import("small-state").Runtime<M>;
6
+ send: (event: import("small-state").SendFnEvent<M>) => Runtime<M>;
7
+ runtime: Runtime<M>;
6
8
  };
7
9
  export declare function useSelectedSnapshot<M extends AnyMachine, Sel>(runtime: Runtime<M> | undefined, selector: (snapshot: Snapshot<M>) => Sel, compare?: (current: Sel, next: Sel) => boolean): Sel;
package/dist/index.mjs CHANGED
@@ -4,18 +4,20 @@ import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-s
4
4
  // src/react/useRuntime.ts
5
5
  function useRuntime(machine, options) {
6
6
  if (!isMachine(machine)) throw new Error(`useRuntime() expects Machine object.`);
7
- const runtime = useMemo(() => makeRuntime(machine, options), [machine]);
7
+ const qOptions = useMemo(() => options, []);
8
+ const runtime = useMemo(() => makeRuntime(machine, qOptions), [machine, qOptions]);
8
9
  const subscribe = useCallback((onStoreChange) => {
9
10
  return runtime.subscribe(onStoreChange, onStoreChange);
10
11
  }, [runtime]);
11
12
  const getSnapshot = useCallback(() => runtime?.getSnapshot(), [runtime]);
12
13
  const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
13
14
  useEffect(() => {
15
+ qOptions?.onPreStart?.(runtime);
14
16
  runtime.start();
15
17
  return () => {
16
18
  runtime.stop();
17
19
  };
18
- }, [runtime]);
20
+ }, [runtime, qOptions]);
19
21
  return {
20
22
  snapshot,
21
23
  send: runtime.send.bind(runtime),
package/dist/jotai.cjs CHANGED
@@ -30,6 +30,7 @@ function atomWithRuntimeSnapshot(machine, options) {
30
30
  set(rtCacheAtom, rt);
31
31
  const sync = () => set(snapshotAtom, rt.getSnapshot());
32
32
  const unsubscribe = rt.subscribe(sync, sync);
33
+ options?.onPreStart?.(rt);
33
34
  rt.start();
34
35
  sync();
35
36
  return unsubscribe;
package/dist/jotai.d.cts CHANGED
@@ -1,3 +1,5 @@
1
- import { AnyMachine, RuntimeOptions, SendFnEvent, Snapshot } from "small-state";
1
+ import { AnyMachine, Runtime, RuntimeOptions, SendFnEvent, Snapshot } from "small-state";
2
2
  import { Getter } from "jotai/vanilla";
3
- export declare function atomWithRuntimeSnapshot<M extends AnyMachine>(machine: M | ((get: Getter) => M), options?: RuntimeOptions<M>): import("jotai/vanilla").WritableAtom<Snapshot<M>, [event: SendFnEvent<M>], void>;
3
+ export declare function atomWithRuntimeSnapshot<M extends AnyMachine>(machine: M | ((get: Getter) => M), options?: RuntimeOptions<M> & {
4
+ onPreStart?: (runtime: Runtime<M>) => void;
5
+ }): import("jotai/vanilla").WritableAtom<Snapshot<M>, [event: SendFnEvent<M>], void>;
package/dist/jotai.d.mts CHANGED
@@ -1,3 +1,5 @@
1
- import { AnyMachine, RuntimeOptions, SendFnEvent, Snapshot } from "small-state";
1
+ import { AnyMachine, Runtime, RuntimeOptions, SendFnEvent, Snapshot } from "small-state";
2
2
  import { Getter } from "jotai/vanilla";
3
- export declare function atomWithRuntimeSnapshot<M extends AnyMachine>(machine: M | ((get: Getter) => M), options?: RuntimeOptions<M>): import("jotai/vanilla").WritableAtom<Snapshot<M>, [event: SendFnEvent<M>], void>;
3
+ export declare function atomWithRuntimeSnapshot<M extends AnyMachine>(machine: M | ((get: Getter) => M), options?: RuntimeOptions<M> & {
4
+ onPreStart?: (runtime: Runtime<M>) => void;
5
+ }): import("jotai/vanilla").WritableAtom<Snapshot<M>, [event: SendFnEvent<M>], void>;
package/dist/jotai.mjs CHANGED
@@ -29,6 +29,7 @@ function atomWithRuntimeSnapshot(machine, options) {
29
29
  set(rtCacheAtom, rt);
30
30
  const sync = () => set(snapshotAtom, rt.getSnapshot());
31
31
  const unsubscribe = rt.subscribe(sync, sync);
32
+ options?.onPreStart?.(rt);
32
33
  rt.start();
33
34
  sync();
34
35
  return unsubscribe;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "small-state-react",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "React and Jotai bindings for small-state, a lightweight state machine library.",
5
5
  "author": "qyvvy",
6
6
  "license": "MIT",