@shivaedev/react 0.47.3 → 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,22 @@
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
+
10
+ ## 0.48.0 - 2026-11-18
11
+
12
+ ### Added
13
+
14
+ - `useFirstSettled(client)` from `@shivaedev/react/client/adapters/useFirstSettled.ts` tells an application when a session's first screen is ready. It returns `false` until a query mounted on the client has delivered its first success or failure and no mounted query awaits a first result and no command is in flight, then stays `true` for that client through later commands and newly mounted queries. A new session generation whose `connect` makes a new client starts at `false` again, and a client that mounts no query stays at `false`.
15
+
16
+ ### Changed
17
+
18
+ - `useAction(runtime, run)` with a `run` function written inline in the component fails its next render with an error that names `useAction`, says `run` is the action's identity and gives the fix: pass a bound command such as `api.rename.run`, or wrap the function in `useCallback` or `useMemo`. Such a component used to fail with React's `Too many re-renders`. Replacing a mounted control's command with another stable function works as before.
19
+
3
20
  ## 0.47.3 - 2026-11-11
4
21
 
5
22
  ### Changed
package/README.md CHANGED
@@ -32,6 +32,8 @@ An **editor** joins a query atom, a form field schema and a save Effect. The que
32
32
 
33
33
  Use `useConnectionStatus(client)` from `@shivaedev/react/client/adapters/useConnectionStatus.ts` with the client returned by contract's `makeClient`. It returns one `"live" | "reconnecting" | "waiting-for-connection"` value across mounted queries from every binding on that client's `Client` service. Live subscription status changes debounce for 300 ms on the client runtime's Clock. A bound command held for reconnection reports `"waiting-for-connection"` immediately, so the application can show Reconnecting while keeping the action pending. Contract holds unsent commands for up to 10 seconds and returns a typed `RpcClientError` on timeout; commands already sent are never replayed. The default socket probes Ping/Pong on browser or native resume and reconnects after 2.5 seconds without a Pong. A query refusal is separate from connectivity. Visible browser `online` and `visibilitychange` signals interrupt the live retry delay and reconnect immediately with a fresh backoff schedule.
34
34
 
35
+ Use `useFirstSettled(client)` from `@shivaedev/react/client/adapters/useFirstSettled.ts` with the same client to reveal a session's first screen in one step instead of one loading state per query. It returns `false` until a query mounted on that client has delivered its first success or failure and, with that, no mounted query awaits a first result and no command is in flight. From then on it returns `true` for that client: a later command or a newly mounted query does not turn it back. The signal belongs to the client, so a new session generation whose `connect` makes a new client starts at `false` again. A client whose screen mounts no query stays at `false`; gate only screens that read through the client.
36
+
35
37
  A **session generation** is the string returned by `identify(session)`. `SessionBoundary` owns its client, registry and React subtree. Your auth owner supplies the session and decides when to replace or end it. The boundary automatically mounts live connections on bound contract APIs returned by `connect`, directly or as client properties such as `api`. Use a Platform database host and the default shared WebSocket client; the application needs no subscribe handler or `live()` call. The process holding the socket reloads the session and runs the read policy and handler for each subscriber independently. It stores one encoded-result hash per subscribed query in memory and pushes changed values; equal reads acknowledge completion without replacing the screen. Each subscription has at most one rerun in flight and one pending, merging further changes while busy. Other subscriptions run independently in parallel. Backlog overflow triggers a server `Resync`: the server reruns queries itself, and the client sends no follow-up reads. Query results stay in their native registry; RPC declarations and transport stay with native Effect and the application; this package supplies their React state and lifecycle boundary.
36
38
 
37
39
  An explicit query refresh keeps the current data visible while reporting pending until the new subscription result arrives. Retryable connection failures keep that data visible during reconnection. Render read denials before retained data; replacing or ending the session disposes its registry.
@@ -562,7 +564,7 @@ const rename = useAction(ReportsClient.runtime, api.rename.run, {
562
564
 
563
565
  `ReportsClient` and `api` belong to the application. Input, success, typed failure and runtime requirements are inferred from the command. Use `notices` for the feedback path above. The optional synchronous `onSuccess(value, input)` and `onFailure(error, input)` callbacks are also available for application completion work. They run inside each invocation using the latest committed callbacks and that invocation's input and outcome. Rerendering a result does not replay completion, and an abandoned render cannot replace the committed callback. A callback that throws becomes a native defect; it does not call `onFailure` as a domain rejection.
564
566
 
565
- The command's identity is the native action atom, or the `run` function in the runtime overload. Pass a stable bound function such as `api.rename.run`; memoize wrappers with `useCallback` or `useMemo`. Changing the command on a mounted control keeps `pending` true until its outgoing request settles. Its result, data and cause stay with the outgoing command: a refusal or success from Launch never appears on a replacement Resume control. Resume dispatches its own function and input. Completion callbacks for the outgoing runtime command stop when it is replaced; callbacks for the same command still use the latest committed props.
567
+ The command's identity is the native action atom, or the `run` function in the runtime overload. Pass a stable bound function such as `api.rename.run`; memoize wrappers with `useCallback` or `useMemo`. A `run` written inline in the component is a new function on every render: its next render fails with an error that names `useAction`, says `run` must stay the same function and gives this fix, where React alone reports only `Too many re-renders`. Changing the command on a mounted control keeps `pending` true until its outgoing request settles. Its result, data and cause stay with the outgoing command: a refusal or success from Launch never appears on a replacement Resume control. Resume dispatches its own function and input. Completion callbacks for the outgoing runtime command stop when it is replaced; callbacks for the same command still use the latest committed props.
566
568
 
567
569
  The hook calls the supplied Effect once. A bound contract command still owns invalidation, and completion does not wait for server query reruns and their pushed results. Undo payloads, expected versions and presentation text stay in the application.
568
570
 
@@ -623,34 +625,31 @@ change.remove({ _tag: "edit", rows: selectedVersions, scale: 0.5, copies: true }
623
625
 
624
626
  #### Commands with a pending display value
625
627
 
626
- 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:
627
629
 
628
630
  ```tsx
629
- import * as Atom from "effect/reactivity/Atom";
630
631
  import { usePendingCommand } from "@shivaedev/react/client/pendingCommand.ts";
631
632
 
632
- const toggle = Atom.family((id: number) => ItemsClient.runtime.fn((input: { readonly enabled: boolean }) => api.toggle.run({ id, ...input })));
633
- const list = api.list.query();
634
-
635
633
  function EnabledToggle({ row }: { readonly row: { readonly id: number; readonly enabled: boolean } }) {
636
- const command = usePendingCommand(toggle(row.id), {
637
- queries: [list],
638
- value: row.enabled,
639
- wanted: (input) => input.enabled,
640
- });
634
+ const command = usePendingCommand({ ...api.toggle, runtime: ItemsClient.runtime }, { value: row.enabled, wanted: (input) => input.enabled });
641
635
  return (
642
- <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
+ >
643
642
  Enabled
644
643
  </button>
645
644
  );
646
645
  }
647
646
  ```
648
647
 
649
- 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.
650
649
 
651
- 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.
652
651
 
653
- `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.
654
653
 
655
654
  #### Trace an action across RPCs
656
655
 
@@ -1 +1 @@
1
- {"version":3,"file":"actionCompletion.d.ts","sourceRoot":"","sources":["../../src/client/actionCompletion.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAGpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAIzD,MAAM,WAAW,aAAa,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,KAAK;IACrD,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;IAC9B,QAAQ,CAAC,OAAO,CAAC,EAAE,cAAc,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACvD,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACvD;AAED,wBAAgB,eAAe,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,aAAa,EAC7D,MAAM,EAAE,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,aAAa,C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AAG,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,aAAa,CAAC,EAC5E,GAAG,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,EAC1E,OAAO,EAAE,aAAa,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GACrC,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,aAAa,CAAC,CA4DjD"}
@@ -2,10 +2,12 @@ import { RegistryContext } from "@effect/atom-react";
2
2
  import { Effect } from "effect";
3
3
  import { useContext, useInsertionEffect, useMemo, useRef } from "react";
4
4
  import { isUnauthorized } from "@shivaedev/errors/taxonomy.ts";
5
+ import { useStableRun } from "#client/adapters/useStableRun.ts";
5
6
  import { NoticeContext } from "#client/NoticeContext.ts";
6
7
  import { useSessionRecheck } from "#client/sessionBoundary.ts";
7
8
  import { publishNotice } from "#noticeStore.ts";
8
9
  export function useActionSource(source, run, options) {
10
+ useStableRun(run);
9
11
  const registry = useContext(RegistryContext);
10
12
  const recheck = useSessionRecheck();
11
13
  const notices = useContext(NoticeContext);
@@ -1 +1 @@
1
- {"version":3,"file":"actionCompletion.js","sourceRoot":"","sources":["../../src/client/actionCompletion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAE/D,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,E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MAAM,IAAI,GAAG,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QAC1E,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IAC3B,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;IACjD,kBAAkB,CAAC,GAAG,EAAE;QACvB,KAAK,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC;QAC7B,OAAO,GAAG,EAAE;YACX,KAAK,CAAC,QAAQ,CAAC,MAAM,GAAG,KAAK,CAAC;QAC/B,CAAC,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IACZ,OAAO,KAAK,CAAC,IAAI,CAAC;AACnB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { type ActiveClient } from "@shivaedev/contract/clientActivity.ts";
2
+ export declare function useFirstSettled(client: ActiveClient): boolean;
3
+ //# sourceMappingURL=useFirstSettled.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFirstSettled.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/useFirstSettled.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,YAAY,EAAkB,MAAM,uCAAuC,CAAC;AAE1F,wBAAgB,eAAe,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAE7D"}
@@ -0,0 +1,6 @@
1
+ import { useAtomValue } from "@effect/atom-react";
2
+ import { clientActivity } from "@shivaedev/contract/clientActivity.ts";
3
+ export function useFirstSettled(client) {
4
+ return useAtomValue(clientActivity(client).firstSettled);
5
+ }
6
+ //# sourceMappingURL=useFirstSettled.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useFirstSettled.js","sourceRoot":"","sources":["../../../src/client/adapters/useFirstSettled.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAqB,cAAc,EAAE,MAAM,uCAAuC,CAAC;AAE1F,MAAM,UAAU,eAAe,CAAC,MAAoB;IACnD,OAAO,YAAY,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function useStableRun(run: unknown): void;
2
+ //# sourceMappingURL=useStableRun.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStableRun.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/useStableRun.ts"],"names":[],"mappings":"AAKA,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,QAiBxC"}
@@ -0,0 +1,20 @@
1
+ import { useInsertionEffect, useRef, useState } from "react";
2
+ // A deliberate swap changes `run` once and commits; React itself stops a component after 25 uncommitted renders without naming the hook.
3
+ const UNCOMMITTED_CHANGES = 10;
4
+ export function useStableRun(run) {
5
+ // State, unlike a ref, is discarded with an abandoned render, so only renders React repeats before one commit are counted.
6
+ const [seen, see] = useState({ changes: 0, run });
7
+ const committedRef = useRef(seen);
8
+ useInsertionEffect(() => {
9
+ committedRef.current = seen;
10
+ }, [seen]);
11
+ if (seen.run === run) {
12
+ return;
13
+ }
14
+ const changes = seen === committedRef.current ? 1 : seen.changes + 1;
15
+ if (changes === UNCOMMITTED_CHANGES) {
16
+ throw new Error(`useAction received a new \`run\` function on ${UNCOMMITTED_CHANGES} renders in a row. \`run\` is the action's identity and must stay the same function between renders: pass a bound command such as \`api.rename.run\` directly, or wrap your function in \`useCallback\` or \`useMemo\`.`);
17
+ }
18
+ see({ changes, run });
19
+ }
20
+ //# sourceMappingURL=useStableRun.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStableRun.js","sourceRoot":"","sources":["../../../src/client/adapters/useStableRun.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE7D,yIAAyI;AACzI,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B,MAAM,UAAU,YAAY,CAAC,GAAY;IACxC,2HAA2H;IAC3H,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IAClD,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAClC,kBAAkB,CAAC,GAAG,EAAE;QACvB,YAAY,CAAC,OAAO,GAAG,IAAI,CAAC;IAC7B,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IACX,IAAI,IAAI,CAAC,GAAG,KAAK,GAAG,EAAE,CAAC;QACtB,OAAO;IACR,CAAC;IACD,MAAM,OAAO,GAAG,IAAI,KAAK,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;IACrE,IAAI,OAAO,KAAK,mBAAmB,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CACd,gDAAgD,mBAAmB,yNAAyN,CAC5R,CAAC;IACH,CAAC;IACD,GAAG,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;AACvB,CAAC"}
@@ -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,EAAE,SAAS,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;IAClF,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;AAED,wBA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,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,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAC7D,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACtD,OAAO,EAAoB,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAWrE,MAAM,UAAU,iBAAiB,CAChC,IAAqC,EACrC,OAA8C;IAE9C,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,QAAQ,GAAG,UAAU,CAAC,eAAe,CAAC,CAAC;IAC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,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,UAAU,CAAC,CAAC;IACpG,IAAI,CAAC,SAAS,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3D,IAAI,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACrD,CAAC;IACD,OAAO;QACN,GAAG,MAAM;QACT,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE;YACnB,IAAI,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;YACjE,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACxB,CAAC;QACD,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,OAAO;QAClC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK;KAClE,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/react",
3
- "version": "0.47.3",
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.44.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.0"
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.2",
61
- "@shivaedev/quality": "0.78.0",
62
- "@shivaedev/server": "0.34.1",
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.47.0",
65
- "@shivaedev/test-kits": "0.12.1",
66
- "@shivaedev/test-sql": "0.25.0",
67
- "@shivaedev/test-story": "0.47.1",
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",
@@ -4,6 +4,7 @@ import type * as Atom from "effect/reactivity/Atom";
4
4
  import { useContext, useInsertionEffect, useMemo, useRef } from "react";
5
5
  import { isUnauthorized } from "@shivaedev/errors/taxonomy.ts";
6
6
  import type { AtomServices } from "#AtomServices.ts";
7
+ import { useStableRun } from "#client/adapters/useStableRun.ts";
7
8
  import { NoticeContext } from "#client/NoticeContext.ts";
8
9
  import type { NoticeFeedback } from "#client/notices.ts";
9
10
  import { useSessionRecheck } from "#client/sessionBoundary.ts";
@@ -21,6 +22,7 @@ export function useActionSource<TInput, A, E, R, TRuntimeError>(
21
22
  run: ((input: TInput) => Effect.Effect<A, E, AtomServices<R>>) | undefined,
22
23
  options: ActionOptions<TInput, A, E, R>,
23
24
  ): Atom.AtomResultFn<TInput, A, E | TRuntimeError> {
25
+ useStableRun(run);
24
26
  const registry = useContext(RegistryContext);
25
27
  const recheck = useSessionRecheck();
26
28
  const notices = useContext(NoticeContext);
@@ -0,0 +1,6 @@
1
+ import { useAtomValue } from "@effect/atom-react";
2
+ import { type ActiveClient, clientActivity } from "@shivaedev/contract/clientActivity.ts";
3
+
4
+ export function useFirstSettled(client: ActiveClient): boolean {
5
+ return useAtomValue(clientActivity(client).firstSettled);
6
+ }
@@ -0,0 +1,23 @@
1
+ import { useInsertionEffect, useRef, useState } from "react";
2
+
3
+ // A deliberate swap changes `run` once and commits; React itself stops a component after 25 uncommitted renders without naming the hook.
4
+ const UNCOMMITTED_CHANGES = 10;
5
+
6
+ export function useStableRun(run: unknown) {
7
+ // State, unlike a ref, is discarded with an abandoned render, so only renders React repeats before one commit are counted.
8
+ const [seen, see] = useState({ changes: 0, run });
9
+ const committedRef = useRef(seen);
10
+ useInsertionEffect(() => {
11
+ committedRef.current = seen;
12
+ }, [seen]);
13
+ if (seen.run === run) {
14
+ return;
15
+ }
16
+ const changes = seen === committedRef.current ? 1 : seen.changes + 1;
17
+ if (changes === UNCOMMITTED_CHANGES) {
18
+ throw new Error(
19
+ `useAction received a new \`run\` function on ${UNCOMMITTED_CHANGES} renders in a row. \`run\` is the action's identity and must stay the same function between renders: pass a bound command such as \`api.rename.run\` directly, or wrap your function in \`useCallback\` or \`useMemo\`.`,
20
+ );
21
+ }
22
+ see({ changes, run });
23
+ }
@@ -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,