small-state-react 0.2.1 → 0.2.3

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/index.cjs CHANGED
@@ -6,12 +6,24 @@ let use_sync_external_store_with_selector = require("use-sync-external-store/wit
6
6
  function readyToUseBoundSend(rtRef) {
7
7
  return (event) => (0, react.useRef)(() => void rtRef.current.send(event)).current;
8
8
  }
9
+ /**
10
+ * @param machine
11
+ * @param {Object} [options]
12
+ * @param {string} [options.id]
13
+ * @param {function(): *} [options.input]
14
+ * @param {function} [options.onPreStart]
15
+ * @returns {RuntimeHandle}
16
+ */
9
17
  function useRuntimeHandle(machine, options) {
10
18
  if (!(0, small_state.isMachine)(machine)) throw new Error(`useRuntimeHandle() expects Machine object.`);
11
19
  const rtRef = (0, react.useRef)(null);
12
20
  if (!rtRef.current) rtRef.current = (0, small_state.makeRuntime)(machine, options);
21
+ const retRef = (0, react.useRef)({});
13
22
  (0, react.useEffect)(() => {
14
- if (rtRef.current.runtimeStatus === "stopped") rtRef.current = (0, small_state.makeRuntime)(machine, options);
23
+ if (rtRef.current.runtimeStatus === "stopped") {
24
+ rtRef.current = (0, small_state.makeRuntime)(machine, options);
25
+ retRef.current = {};
26
+ }
15
27
  const crrRt = rtRef.current;
16
28
  options?.onPreStart?.(crrRt);
17
29
  crrRt.start();
@@ -28,15 +40,20 @@ function useRuntimeHandle(machine, options) {
28
40
  const getSnapshot = (0, react.useCallback)(() => runtime.getSnapshot(), [runtime]);
29
41
  snapshot = (0, react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
30
42
  }
31
- const handle = (0, react.useRef)({}).current;
32
- handle.snapshot = snapshot;
33
- handle.send = (event) => void runtime.send.apply(runtime, [event]);
34
- handle.runtime = runtime;
35
- handle.useBoundSend = readyToUseBoundSend(rtRef);
36
- return handle;
43
+ const ret = retRef.current;
44
+ ret.snapshot = snapshot;
45
+ ret.send = (event) => void runtime.send.apply(runtime, [event]);
46
+ ret.runtime = runtime;
47
+ ret.useBoundSend = readyToUseBoundSend(rtRef);
48
+ return ret;
37
49
  }
38
50
  // src/react/useSelectedSnapshot.ts
39
51
  const DEFAULT_COMPARE = (a, b) => a === b;
52
+ /**
53
+ * @param runtime
54
+ * @param {function} selector
55
+ * @param {function} [compare]
56
+ */
40
57
  function useSelectedSnapshot(runtime, selector, compare = DEFAULT_COMPARE) {
41
58
  const subscribe = (0, react.useCallback)((onStoreChange) => {
42
59
  if (!runtime) return () => {};
package/dist/index.d.cts CHANGED
@@ -1,13 +1,33 @@
1
1
  import { AnyMachine, Runtime, RuntimeOptions, SendFnEvent, Snapshot } from "small-state";
2
2
  type UseBoundSend<M extends AnyMachine> = (event: SendFnEvent<M>) => () => undefined;
3
+ /**
4
+ * @typedef {Object} RuntimeHandle
5
+ * @property {Object} snapshot
6
+ * @property {function(string|{type: string, [key: string]: *}): undefined} send
7
+ * @property {Object} runtime
8
+ * @property {function(string|{type: string, [key: string]: *}): function(): undefined} useBoundSend
9
+ */
3
10
  type RuntimeHandle<M extends AnyMachine> = {
4
11
  snapshot: Snapshot<M>;
5
12
  send: (event: SendFnEvent<M>) => undefined;
6
13
  runtime: Runtime<M>;
7
14
  useBoundSend: UseBoundSend<M>;
8
15
  };
16
+ /**
17
+ * @param machine
18
+ * @param {Object} [options]
19
+ * @param {string} [options.id]
20
+ * @param {function(): *} [options.input]
21
+ * @param {function} [options.onPreStart]
22
+ * @returns {RuntimeHandle}
23
+ */
9
24
  export declare function useRuntimeHandle<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M> & {
10
25
  onPreStart?: (runtime: Runtime<M>) => void;
11
26
  }): RuntimeHandle<M>;
27
+ /**
28
+ * @param runtime
29
+ * @param {function} selector
30
+ * @param {function} [compare]
31
+ */
12
32
  export declare function useSelectedSnapshot<M extends AnyMachine, Sel>(runtime: Runtime<M> | undefined, selector: (snapshot: Snapshot<M>) => Sel, compare?: (current: Sel, next: Sel) => boolean): Sel;
13
33
  export type { RuntimeHandle, UseBoundSend };
package/dist/index.d.mts CHANGED
@@ -1,13 +1,33 @@
1
1
  import { AnyMachine, Runtime, RuntimeOptions, SendFnEvent, Snapshot } from "small-state";
2
2
  type UseBoundSend<M extends AnyMachine> = (event: SendFnEvent<M>) => () => undefined;
3
+ /**
4
+ * @typedef {Object} RuntimeHandle
5
+ * @property {Object} snapshot
6
+ * @property {function(string|{type: string, [key: string]: *}): undefined} send
7
+ * @property {Object} runtime
8
+ * @property {function(string|{type: string, [key: string]: *}): function(): undefined} useBoundSend
9
+ */
3
10
  type RuntimeHandle<M extends AnyMachine> = {
4
11
  snapshot: Snapshot<M>;
5
12
  send: (event: SendFnEvent<M>) => undefined;
6
13
  runtime: Runtime<M>;
7
14
  useBoundSend: UseBoundSend<M>;
8
15
  };
16
+ /**
17
+ * @param machine
18
+ * @param {Object} [options]
19
+ * @param {string} [options.id]
20
+ * @param {function(): *} [options.input]
21
+ * @param {function} [options.onPreStart]
22
+ * @returns {RuntimeHandle}
23
+ */
9
24
  export declare function useRuntimeHandle<M extends AnyMachine>(machine: M, options?: RuntimeOptions<M> & {
10
25
  onPreStart?: (runtime: Runtime<M>) => void;
11
26
  }): RuntimeHandle<M>;
27
+ /**
28
+ * @param runtime
29
+ * @param {function} selector
30
+ * @param {function} [compare]
31
+ */
12
32
  export declare function useSelectedSnapshot<M extends AnyMachine, Sel>(runtime: Runtime<M> | undefined, selector: (snapshot: Snapshot<M>) => Sel, compare?: (current: Sel, next: Sel) => boolean): Sel;
13
33
  export type { RuntimeHandle, UseBoundSend };
package/dist/index.mjs CHANGED
@@ -5,12 +5,24 @@ import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-s
5
5
  function readyToUseBoundSend(rtRef) {
6
6
  return (event) => useRef(() => void rtRef.current.send(event)).current;
7
7
  }
8
+ /**
9
+ * @param machine
10
+ * @param {Object} [options]
11
+ * @param {string} [options.id]
12
+ * @param {function(): *} [options.input]
13
+ * @param {function} [options.onPreStart]
14
+ * @returns {RuntimeHandle}
15
+ */
8
16
  function useRuntimeHandle(machine, options) {
9
17
  if (!isMachine(machine)) throw new Error(`useRuntimeHandle() expects Machine object.`);
10
18
  const rtRef = useRef(null);
11
19
  if (!rtRef.current) rtRef.current = makeRuntime(machine, options);
20
+ const retRef = useRef({});
12
21
  useEffect(() => {
13
- if (rtRef.current.runtimeStatus === "stopped") rtRef.current = makeRuntime(machine, options);
22
+ if (rtRef.current.runtimeStatus === "stopped") {
23
+ rtRef.current = makeRuntime(machine, options);
24
+ retRef.current = {};
25
+ }
14
26
  const crrRt = rtRef.current;
15
27
  options?.onPreStart?.(crrRt);
16
28
  crrRt.start();
@@ -27,15 +39,20 @@ function useRuntimeHandle(machine, options) {
27
39
  const getSnapshot = useCallback(() => runtime.getSnapshot(), [runtime]);
28
40
  snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
29
41
  }
30
- const handle = useRef({}).current;
31
- handle.snapshot = snapshot;
32
- handle.send = (event) => void runtime.send.apply(runtime, [event]);
33
- handle.runtime = runtime;
34
- handle.useBoundSend = readyToUseBoundSend(rtRef);
35
- return handle;
42
+ const ret = retRef.current;
43
+ ret.snapshot = snapshot;
44
+ ret.send = (event) => void runtime.send.apply(runtime, [event]);
45
+ ret.runtime = runtime;
46
+ ret.useBoundSend = readyToUseBoundSend(rtRef);
47
+ return ret;
36
48
  }
37
49
  // src/react/useSelectedSnapshot.ts
38
50
  const DEFAULT_COMPARE = (a, b) => a === b;
51
+ /**
52
+ * @param runtime
53
+ * @param {function} selector
54
+ * @param {function} [compare]
55
+ */
39
56
  function useSelectedSnapshot(runtime, selector, compare = DEFAULT_COMPARE) {
40
57
  const subscribe = useCallback((onStoreChange) => {
41
58
  if (!runtime) return () => {};
package/dist/jotai.cjs CHANGED
@@ -3,6 +3,13 @@ let small_state = require("small-state");
3
3
  let jotai_vanilla = require("jotai/vanilla");
4
4
  // src/jotai/atomWithRuntimeSnapshot.ts
5
5
  const FINALIZE_EVENT = Symbol();
6
+ /**
7
+ * @param machine
8
+ * @param {Object} [options]
9
+ * @param {string} [options.id]
10
+ * @param {function(): *} [options.input]
11
+ * @param {function} [options.onPreStart]
12
+ */
6
13
  function atomWithRuntimeSnapshot(machine, options) {
7
14
  const rtCacheAtom = (0, jotai_vanilla.atom)(null);
8
15
  const snapshotAtom = (0, jotai_vanilla.atom)(null);
package/dist/jotai.d.cts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { AnyMachine, Runtime, RuntimeOptions, SendFnEvent, Snapshot } from "small-state";
2
2
  import { Getter } from "jotai/vanilla";
3
+ /**
4
+ * @param machine
5
+ * @param {Object} [options]
6
+ * @param {string} [options.id]
7
+ * @param {function(): *} [options.input]
8
+ * @param {function} [options.onPreStart]
9
+ */
3
10
  export declare function atomWithRuntimeSnapshot<M extends AnyMachine>(machine: M | ((get: Getter) => M), options?: RuntimeOptions<M> & {
4
11
  onPreStart?: (runtime: Runtime<M>) => void;
5
12
  }): import("jotai/vanilla").WritableAtom<Snapshot<M>, [event: SendFnEvent<M>], void>;
package/dist/jotai.d.mts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { AnyMachine, Runtime, RuntimeOptions, SendFnEvent, Snapshot } from "small-state";
2
2
  import { Getter } from "jotai/vanilla";
3
+ /**
4
+ * @param machine
5
+ * @param {Object} [options]
6
+ * @param {string} [options.id]
7
+ * @param {function(): *} [options.input]
8
+ * @param {function} [options.onPreStart]
9
+ */
3
10
  export declare function atomWithRuntimeSnapshot<M extends AnyMachine>(machine: M | ((get: Getter) => M), options?: RuntimeOptions<M> & {
4
11
  onPreStart?: (runtime: Runtime<M>) => void;
5
12
  }): import("jotai/vanilla").WritableAtom<Snapshot<M>, [event: SendFnEvent<M>], void>;
package/dist/jotai.mjs CHANGED
@@ -2,6 +2,13 @@ import { makeRuntime } from "small-state";
2
2
  import { atom } from "jotai/vanilla";
3
3
  // src/jotai/atomWithRuntimeSnapshot.ts
4
4
  const FINALIZE_EVENT = Symbol();
5
+ /**
6
+ * @param machine
7
+ * @param {Object} [options]
8
+ * @param {string} [options.id]
9
+ * @param {function(): *} [options.input]
10
+ * @param {function} [options.onPreStart]
11
+ */
5
12
  function atomWithRuntimeSnapshot(machine, options) {
6
13
  const rtCacheAtom = atom(null);
7
14
  const snapshotAtom = atom(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "small-state-react",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "React and Jotai bindings for small-state, a lightweight state machine library.",
5
5
  "author": "qyvvy",
6
6
  "license": "MIT",