@shivaedev/react 0.48.0 → 0.48.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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.48.1 - 2026-11-18
4
+
5
+ ### Changed
6
+
7
+ - Update `@shivaedev/contract` from 0.45.0 to 0.46.0.
8
+ - Update `@shivaedev/identity` from 0.32.1 to 0.32.2.
9
+
3
10
  ## 0.48.0 - 2026-11-18
4
11
 
5
12
  ### Added
package/README.md CHANGED
@@ -625,34 +625,31 @@ change.remove({ _tag: "edit", rows: selectedVersions, scale: 0.5, copies: true }
625
625
 
626
626
  #### Commands with a pending display value
627
627
 
628
- Use `usePendingCommand` for a command acting on a query row, such as a list toggle. Give each row a stable action atom with the existing bound command, so the command's declared invalidation keys still drive server reruns:
628
+ Use `usePendingCommand` for a command acting on a query row, such as a list toggle. Pass the bound command with its client's runtime, the row's current server value and the display value an input asks for:
629
629
 
630
630
  ```tsx
631
- import * as Atom from "effect/reactivity/Atom";
632
631
  import { usePendingCommand } from "@shivaedev/react/client/pendingCommand.ts";
633
632
 
634
- const toggle = Atom.family((id: number) => ItemsClient.runtime.fn((input: { readonly enabled: boolean }) => api.toggle.run({ id, ...input })));
635
- const list = api.list.query();
636
-
637
633
  function EnabledToggle({ row }: { readonly row: { readonly id: number; readonly enabled: boolean } }) {
638
- const command = usePendingCommand(toggle(row.id), {
639
- queries: [list],
640
- value: row.enabled,
641
- wanted: (input) => input.enabled,
642
- });
634
+ const command = usePendingCommand({ ...api.toggle, runtime: ItemsClient.runtime }, { value: row.enabled, wanted: (input) => input.enabled });
643
635
  return (
644
- <button aria-pressed={command.value} disabled={command.pending} onClick={() => command.dispatch({ enabled: !command.value })} type="button">
636
+ <button
637
+ aria-pressed={command.value}
638
+ disabled={command.pending}
639
+ onClick={() => command.dispatch({ enabled: !command.value, id: row.id })}
640
+ type="button"
641
+ >
645
642
  Enabled
646
643
  </button>
647
644
  );
648
645
  }
649
646
  ```
650
647
 
651
- Each row needs its own action atom because the native atom owns its current result, including failures. `Atom.family` keeps an independent action atom stable for each row ID. This lets one row fail while another keeps its requested display through the shared list refetch. Do not share one action atom across independently controlled rows.
648
+ The example assumes a bound `api.toggle` whose `run` accepts `{ id, enabled }` and its native `ItemsClient`. `run` is the command's identity and must stay the same function between renders, as for `useAction`. Each use of the hook owns its action state, so one row can fail while another keeps its requested display.
652
649
 
653
- The example assumes a bound `api.toggle.run` accepting `{ id, enabled }`, a bound list query and their native `ItemsClient`. `queries` lists the query atoms whose next results the display waits for; it does not add invalidations. Include every relevant query and keep the action and query atom identities stable. `value` is the current server value; `wanted(input)` computes the requested display value.
650
+ `command.value` changes on dispatch and holds the requested value until the command is [reflected](https://github.com/ShivaeDev/platform/blob/main/packages/contract/README.md#when-a-commands-change-is-on-the-screen): every live query open on the connection shows the server's state after it. A switch backed by a live query therefore does not fall back to the old server value between the command's reply and the server's update, and a command that changes nothing frees the switch without an update. `pending` covers that whole interval, and `result`, `data` and `cause` settle when it ends. After the hold ends, `value` follows the server again, even if it differs from the requested value. A command failure immediately returns to the server value and remains typed in `result` and `cause`, just as with `useAction`. This binding holds a display value; it applies no optimistic update and does not write to the query cache.
654
651
 
655
- `command.value` changes on dispatch and holds the requested value until the command has succeeded, no watched query is waiting, and at least one watched query has delivered a settled Result it did not have at dispatch. A reload and a live query's pushed Result both count, so a live switch does not fall back to the old server value between the command's response and the push. `pending` covers that whole interval. After the hold ends, `value` follows the server again, even if it differs from the requested value. A command failure immediately returns to the server value and remains typed in `result` and `cause`, just as with `useAction`. The other action fields describe the native command result, so `result` can already be successful while `pending` still waits for refetches. This binding holds a display value; it does not write to the query cache.
652
+ Queries the client reloads itself, such as those of a client made with `live: false`, are not part of a command's reflection. List them as `queries`, an array that keeps its identity between renders, and the hold also lasts while any of them is reloading; `result` can then already be successful while `pending` still waits. `queries` adds no invalidations.
656
653
 
657
654
  #### Trace an action across RPCs
658
655
 
@@ -1,13 +1,19 @@
1
+ import { type Effect } from "effect";
1
2
  import * as AsyncResult from "effect/reactivity/AsyncResult";
2
3
  import * as Atom from "effect/reactivity/Atom";
4
+ import type { AtomServices } from "#AtomServices.ts";
3
5
  import { type ActionState } from "#client/resultState.ts";
6
+ export interface PendingCommand<TInput, A, E, R, TRuntimeError> {
7
+ readonly run: (input: TInput) => Effect.Effect<A, E, AtomServices<R>>;
8
+ readonly runtime: Atom.AtomRuntime<R, TRuntimeError>;
9
+ }
4
10
  export interface PendingCommandOptions<TInput, TValue> {
5
- readonly queries: readonly Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>>[];
11
+ readonly queries?: readonly Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>>[];
6
12
  readonly value: TValue;
7
13
  readonly wanted: (input: TInput) => TValue;
8
14
  }
9
15
  export interface PendingCommandState<TInput, A, E, TValue> extends ActionState<TInput, A, E> {
10
16
  readonly value: TValue;
11
17
  }
12
- export declare function usePendingCommand<TInput, A, E, TValue>(atom: Atom.AtomResultFn<TInput, A, E>, options: PendingCommandOptions<TInput, TValue>): PendingCommandState<TInput, A, E, TValue>;
18
+ export declare function usePendingCommand<TInput, A, E, R, TRuntimeError, TValue>(command: PendingCommand<TInput, A, E, R, TRuntimeError>, options: PendingCommandOptions<NoInfer<TInput>, TValue>): PendingCommandState<TInput, A, E | TRuntimeError, TValue>;
13
19
  //# sourceMappingURL=pendingCommand.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pendingCommand.d.ts","sourceRoot":"","sources":["../../src/client/pendingCommand.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAC7D,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAE/C,OAAO,EAAE,KAAK,WAAW,EAAa,MAAM,wBAAwB,CAAC;AAErE,MAAM,WAAW,qBAAqB,CAAC,MAAM,EAAE,MAAM;IACpD,QAAQ,CAAC,OAAO,E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AAY,CAAC,CAAC,CAAC,CAAC,CAAC;IACtE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;CACrD;AACD,MAAM,WAAW,qBAAqB,CAAC,MAAM,EAAE,MAAM;IACpD,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;IACnF,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;CAC3C;AACD,MAAM,WAAW,mBAAmB,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,CAAE,SAAQ,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;IAC3F,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB;AAID,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,aAAa,EAAE,MAAM,EACvE,OAAO,EAAE,cAAc,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,aAAa,CAAC,EACvD,OAAO,EAAE,qBAAqB,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,GACrD,mBAAmB,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,aAAa,EAAE,MAAM,CAAC,CAwB3D"}
@@ -3,27 +3,28 @@ import { Option } from "effect";
3
3
  import * as AsyncResult from "effect/reactivity/AsyncResult";
4
4
  import * as Atom from "effect/reactivity/Atom";
5
5
  import { useContext, useMemo, useState } from "react";
6
+ import { untilReflected } from "@shivaedev/contract/commandSignal.ts";
6
7
  import { useAction } from "#client/resultState.ts";
7
- export function usePendingCommand(atom, options) {
8
- const action = useAction(atom);
8
+ const NO_QUERIES = [];
9
+ export function usePendingCommand(command, options) {
10
+ const { run, runtime } = command;
11
+ const { queries = NO_QUERIES, value, wanted } = options;
12
+ // The reply arrives before the live queries the command changed are updated, so the action ends when the command is reflected.
13
+ const reflected = useMemo(() => (input) => untilReflected(run(input)), [run]);
14
+ const action = useAction(runtime, reflected);
9
15
  const registry = useContext(RegistryContext);
10
- const { queries, value, wanted } = options;
11
- const watched = useMemo(() => Atom.make((get) => queries.map((query) => get(query))), [queries]);
12
- const results = useAtomValue(watched);
13
- const [held, hold] = useState({ atom, before: results, registry, value: Option.none() });
14
- const sameOwner = held.atom === atom && held.registry === registry;
16
+ const refreshing = useAtomValue(useMemo(() => Atom.make((get) => queries.some((query) => get(query).waiting)), [queries]));
17
+ const [held, hold] = useState({ reflected, registry, value: Option.none() });
18
+ const sameOwner = held.reflected === reflected && held.registry === registry;
15
19
  const failed = AsyncResult.isFailure(action.result) && !action.pending;
16
- const refreshing = results.some((result) => result.waiting);
17
- // A live query answers with a pushed Result instead of a reload, so the hold waits for any new settled Result.
18
- const answered = results.some((result, index) => !result.waiting && result !== held.before[index]);
19
- const pending = sameOwner && Option.isSome(held.value) && !failed && (action.pending || refreshing || !answered);
20
+ const pending = sameOwner && Option.isSome(held.value) && !failed && (action.pending || refreshing);
20
21
  if (!sameOwner || (Option.isSome(held.value) && !pending)) {
21
- hold({ atom, before: results, registry, value: Option.none() });
22
+ hold({ reflected, registry, value: Option.none() });
22
23
  }
23
24
  return {
24
25
  ...action,
25
26
  dispatch: (input) => {
26
- hold({ atom, before: registry.get(watched), registry, value: Option.some(wanted(input)) });
27
+ hold({ reflected, registry, value: Option.some(wanted(input)) });
27
28
  action.dispatch(input);
28
29
  },
29
30
  pending: action.pending || pending,
@@ -1 +1 @@
1
- {"version":3,"file":"pendingCommand.js","sourceRoot":"","sources":["../../src/client/pendingCommand.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE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}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/react",
3
- "version": "0.48.0",
3
+ "version": "0.48.1",
4
4
  "description": "React query, action, editor and session hooks for native Effect atoms",
5
5
  "keywords": [
6
6
  "effect"
@@ -46,25 +46,25 @@
46
46
  ],
47
47
  "dependencies": {
48
48
  "@shivaedev/access": "0.6.3",
49
- "@shivaedev/contract": "0.45.0",
49
+ "@shivaedev/contract": "0.46.0",
50
50
  "@shivaedev/errors": "0.12.0",
51
51
  "@shivaedev/form": "0.19.3",
52
- "@shivaedev/identity": "0.32.1"
52
+ "@shivaedev/identity": "0.32.2"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@effect/atom-react": "4.1.2",
56
56
  "@effect/platform-node": "4.1.2",
57
57
  "@effect/sql-pg": "4.1.2",
58
58
  "@effect/sql-sqlite-node": "4.1.2",
59
- "@shivaedev/changes": "0.12.0",
60
- "@shivaedev/jobs": "0.42.3",
61
- "@shivaedev/quality": "0.78.1",
62
- "@shivaedev/server": "0.34.2",
59
+ "@shivaedev/changes": "0.13.0",
60
+ "@shivaedev/jobs": "0.42.4",
61
+ "@shivaedev/quality": "0.78.2",
62
+ "@shivaedev/server": "0.35.0",
63
63
  "@shivaedev/service": "0.9.1",
64
- "@shivaedev/sql": "0.48.0",
65
- "@shivaedev/test-kits": "0.13.0",
66
- "@shivaedev/test-sql": "0.25.1",
67
- "@shivaedev/test-story": "0.47.2",
64
+ "@shivaedev/sql": "0.49.0",
65
+ "@shivaedev/test-kits": "0.13.1",
66
+ "@shivaedev/test-sql": "0.25.2",
67
+ "@shivaedev/test-story": "0.47.3",
68
68
  "@tanstack/react-router": "1.170.46",
69
69
  "@types/node": "24.19.2",
70
70
  "@types/react": "19.3.0",
@@ -1,12 +1,18 @@
1
1
  import { RegistryContext, useAtomValue } from "@effect/atom-react";
2
- import { Option } from "effect";
2
+ import { type Effect, Option } from "effect";
3
3
  import * as AsyncResult from "effect/reactivity/AsyncResult";
4
4
  import * as Atom from "effect/reactivity/Atom";
5
5
  import { useContext, useMemo, useState } from "react";
6
+ import { untilReflected } from "@shivaedev/contract/commandSignal.ts";
7
+ import type { AtomServices } from "#AtomServices.ts";
6
8
  import { type ActionState, useAction } from "#client/resultState.ts";
7
9
 
10
+ export interface PendingCommand<TInput, A, E, R, TRuntimeError> {
11
+ readonly run: (input: TInput) => Effect.Effect<A, E, AtomServices<R>>;
12
+ readonly runtime: Atom.AtomRuntime<R, TRuntimeError>;
13
+ }
8
14
  export interface PendingCommandOptions<TInput, TValue> {
9
- readonly queries: readonly Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>>[];
15
+ readonly queries?: readonly Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>>[];
10
16
  readonly value: TValue;
11
17
  readonly wanted: (input: TInput) => TValue;
12
18
  }
@@ -14,29 +20,30 @@ export interface PendingCommandState<TInput, A, E, TValue> extends ActionState<T
14
20
  readonly value: TValue;
15
21
  }
16
22
 
17
- export function usePendingCommand<TInput, A, E, TValue>(
18
- atom: Atom.AtomResultFn<TInput, A, E>,
19
- options: PendingCommandOptions<TInput, TValue>,
20
- ): PendingCommandState<TInput, A, E, TValue> {
21
- const action = useAction(atom);
23
+ const NO_QUERIES: readonly Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>>[] = [];
24
+
25
+ export function usePendingCommand<TInput, A, E, R, TRuntimeError, TValue>(
26
+ command: PendingCommand<TInput, A, E, R, TRuntimeError>,
27
+ options: PendingCommandOptions<NoInfer<TInput>, TValue>,
28
+ ): PendingCommandState<TInput, A, E | TRuntimeError, TValue> {
29
+ const { run, runtime } = command;
30
+ const { queries = NO_QUERIES, value, wanted } = options;
31
+ // The reply arrives before the live queries the command changed are updated, so the action ends when the command is reflected.
32
+ const reflected = useMemo(() => (input: TInput) => untilReflected(run(input)), [run]);
33
+ const action = useAction(runtime, reflected);
22
34
  const registry = useContext(RegistryContext);
23
- const { queries, value, wanted } = options;
24
- const watched = useMemo(() => Atom.make((get) => queries.map((query) => get(query))), [queries]);
25
- const results = useAtomValue(watched);
26
- const [held, hold] = useState({ atom, before: results, registry, value: Option.none<TValue>() });
27
- const sameOwner = held.atom === atom && held.registry === registry;
35
+ const refreshing = useAtomValue(useMemo(() => Atom.make((get) => queries.some((query) => get(query).waiting)), [queries]));
36
+ const [held, hold] = useState({ reflected, registry, value: Option.none<TValue>() });
37
+ const sameOwner = held.reflected === reflected && held.registry === registry;
28
38
  const failed = AsyncResult.isFailure(action.result) && !action.pending;
29
- const refreshing = results.some((result) => result.waiting);
30
- // A live query answers with a pushed Result instead of a reload, so the hold waits for any new settled Result.
31
- const answered = results.some((result, index) => !result.waiting && result !== held.before[index]);
32
- const pending = sameOwner && Option.isSome(held.value) && !failed && (action.pending || refreshing || !answered);
39
+ const pending = sameOwner && Option.isSome(held.value) && !failed && (action.pending || refreshing);
33
40
  if (!sameOwner || (Option.isSome(held.value) && !pending)) {
34
- hold({ atom, before: results, registry, value: Option.none() });
41
+ hold({ reflected, registry, value: Option.none() });
35
42
  }
36
43
  return {
37
44
  ...action,
38
45
  dispatch: (input) => {
39
- hold({ atom, before: registry.get(watched), registry, value: Option.some(wanted(input)) });
46
+ hold({ reflected, registry, value: Option.some(wanted(input)) });
40
47
  action.dispatch(input);
41
48
  },
42
49
  pending: action.pending || pending,