@shivaedev/react 0.41.2 → 0.42.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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.42.0 - 2026-11-05
4
+
5
+ ### Added
6
+
7
+ - Let `useUndoableChange` build an inverse command input from the successful result with `undoInput`, carrying per-row versions or an undo token for batch changes. Existing single-row versioned calls keep working unchanged.
8
+ - Expose the immediate `waiting-for-connection` status through `useConnectionStatus` while a bound command waits, keeping actions pending until delivery or the typed connection timeout.
9
+ - Provides `useDebouncedValue` and cancellable `useDelay` hooks on the atom runtime's Effect Clock. Pending timers count as observed atom work for screen settlement and cancel on unmount.
10
+
11
+ ### Fixed
12
+
13
+ - Editors preserve keys omitted during save normalization, and create forms preserve omissions through reset.
14
+
15
+ ### Breaking
16
+
17
+ - `useConnectionStatus` can return `waiting-for-connection`. Render it as a reconnect notice while the action remains pending.
18
+
3
19
  ## 0.41.2 - 2026-11-04
4
20
 
5
21
  ### Changed
package/README.md CHANGED
@@ -28,7 +28,7 @@ State belongs to an **atom in a registry**. A query hook reads that atom's nativ
28
28
 
29
29
  An **editor** joins a query atom, a form field schema and a save Effect. The query provides the row; `values(row)` converts it to input values; the schema decodes those values before the save runs. form owns the draft and the per-field merge. A **create form** uses the same submission rules without a query and starts a fresh form after success.
30
30
 
31
- Use `useConnectionStatus(client)` from `@shivaedev/react/client/adapters/useConnectionStatus.ts` with the client returned by contract's `makeClient`. It returns one `"live" | "reconnecting"` value across mounted queries from every binding on that client's `Client` service. Status changes debounce for 300 ms on the client runtime's Clock. 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.
31
+ 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.
32
32
 
33
33
  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. Live changed hints coalesce over 20 ms by default. Set `liveCoalesceWindow` on `SessionBoundary` to a native Duration to choose the window for that session, or `0` for immediate per-hint refreshes. A bound query can override that default with `useQuery(atom, { coalesceWindow: "2 seconds" })`. Changing this option applies to subsequent windows without reconnecting or refetching any query; a window already started keeps its deadline. A refetch in flight runs once more after settling when further hints arrive. Cache policy, RPC declarations and transport stay with native Effect and the application; this package supplies their React state and lifecycle boundary.
34
34
 
@@ -598,6 +598,21 @@ function RemoveNote({ note }: { readonly note: { id: string; version: number } }
598
598
 
599
599
  `api`, `NotesClient`, the soft-removal payload and `update` belong to the application. `remove(input, inverse)` captures the inverse domain fields. `undo()` sends those fields through the same command with `expectedVersion` set to the successful removal's returned `version`. The server must enforce that version and declare its typed conflict; a later edit then refuses Undo without overwriting the row. `removeResult` and `undoResult` preserve the native `AsyncResult`, including typed failures. Successful Undo clears its notice; a failed Undo leaves it available.
600
600
 
601
+ For a batch command, supply `undoInput(value, input)` to build the complete inverse payload from its successful result and original input. This overload accepts any command payload and success shape and uses `remove(input)` with no second argument:
602
+
603
+ ```tsx
604
+ const change = useUndoableChange(
605
+ { ...api.editEntries, runtime: DiaryClient.runtime, update },
606
+ {
607
+ describe: () => "Changed diary entries",
608
+ undoInput: (saved) => ({ _tag: "undo" as const, token: saved.undo }),
609
+ },
610
+ );
611
+ change.remove({ _tag: "edit", rows: selectedVersions, scale: 0.5, copies: true });
612
+ ```
613
+
614
+ `api.editEntries`, `DiaryClient` and the payload shape belong to the application. The hook captures the inverse when the command succeeds and submits it unchanged through the same command on Undo. The result can carry a set of row versions with their previous values, or an opaque server-issued undo token; neither requires a top-level `expectedVersion` or `version`. The server owns the transaction: restore each changed row under its own returned version, remove rows the batch created under their own guards, and reject the whole inverse with a typed conflict if any guard is stale. React stories exercise portion scaling, copy removal and stale guards against PostgreSQL; the hook supplies no database undo engine.
615
+
601
616
  `notice` is a native `Option` containing `message`, the submitted `input` and returned `value`. Only the most recently dispatched removal can publish it, even when replies arrive in reverse order. An older Undo cannot clear a newer notice. `removing` and `undoing` describe the two writes, and `pending` includes either. Each invocation holds `update.block` for its whole Effect lifetime, so overlapping writes hold deployment activation until all finish. The hook keeps UI wording and inverse domain data in the app; no `NoticeHost` is needed.
602
617
 
603
618
  #### Commands with a pending display value
@@ -972,7 +987,7 @@ const resume = resumeSignal({ window });
972
987
  const query = Atom.makeRefreshOnSignal(resume)(Atom.make(Effect.succeed("ready")));
973
988
  ```
974
989
 
975
- `resumeSignal` changes on visible `visibilitychange` and visible `online` events. Hidden browser events do not refresh the query. An optional `native` source subscribes a callback and returns its unsubscribe function; its callback also changes the signal. Registry disposal removes the listeners. This module uses the shared [contract resume implementation](https://github.com/ShivaeDev/platform/blob/main/packages/contract/src/resume.ts).
990
+ `resumeSignal` changes on visible `visibilitychange`, `online` and `pageshow` events. Hidden browser events do not refresh the query. An optional `native` source subscribes a callback and returns its unsubscribe function; its callback also changes the signal. Registry disposal removes the listeners. This module uses the shared [contract resume implementation](https://github.com/ShivaeDev/platform/blob/main/packages/contract/src/resume.ts).
976
991
 
977
992
  Use native stale-while-revalidate policy when a resume should refresh only stale data:
978
993
 
@@ -999,6 +1014,31 @@ const minutesAgo = Math.floor((now - savedAt) / 60_000);
999
1014
 
1000
1015
  Without a runtime it uses a native empty atom runtime. Stories supply a runtime with their TestClock service so advancing that clock updates the rendered time.
1001
1016
 
1017
+ #### Debounce and delay on the runtime clock
1018
+
1019
+ `useDebouncedValue(value, duration, runtime?)` from `@shivaedev/react/client/useDebouncedValue.ts` returns the initial value immediately, then retains the previous value until input stops changing for the duration. A changed value or duration restarts the pending window. The timer runs on the atom runtime's Effect `Clock`, and its observed native `AsyncResult` stays waiting throughout that window, so `screenPending()` includes it when settling a story.
1020
+
1021
+ ```tsx
1022
+ import { useDebouncedValue } from "@shivaedev/react/client/useDebouncedValue.ts";
1023
+ import { useDelay } from "@shivaedev/react/client/useDelay.ts";
1024
+
1025
+ const query = useDebouncedValue(search, "300 millis", client.runtime);
1026
+ const press = useDelay("450 millis", client.runtime);
1027
+
1028
+ <button
1029
+ onPointerDown={() => press.start(() => selectRow(id))}
1030
+ onPointerUp={press.cancel}
1031
+ onPointerLeave={press.cancel}
1032
+ onPointerCancel={press.cancel}
1033
+ >
1034
+ Select row
1035
+ </button>;
1036
+ ```
1037
+
1038
+ `useDelay(duration, runtime?)` schedules one callback with `start(callback)` and exposes `pending` and `cancel()`. Starting again replaces the pending callback. Changing the duration cancels it. Unmounting cancels both hooks' timers. Without a runtime, each hook uses a native empty atom runtime with Effect's default Clock. Pass the application's runtime so stories can control the same clock, and cancel a long press on the pointer events appropriate for the application's interaction.
1039
+
1040
+ In a story kit, acquire `watchedClock` from `@shivaedev/test-story/server/watchedClock.ts` and pass it both to the atom runtime's Clock Layer and the screen driver's `clock` option. With `pending: screenPending()` for that registry and React's `act`, typing followed by `settle()` advances registered sleeps inside `act` until the debounce and resulting work finish. Kit verbs need no debounce duration. To check a long-press boundary explicitly, run that clock's `adjust(duration)` inside `act` in a verb that accepts the elapsed duration.
1041
+
1002
1042
  #### Ids, time and promises in callbacks
1003
1043
 
1004
1044
  `useNewId(runtime?)` from `@shivaedev/react/client/adapters/useNewId.ts` returns a UUIDv4 generator. `useClock(runtime?)` from `@shivaedev/react/client/adapters/useClock.ts` returns a `now()` callback that reads epoch milliseconds when called. Both read services from the supplied atom runtime in the current registry and can be used in state initializers and event handlers:
@@ -1,4 +1,5 @@
1
1
  import { Effect, Option } from "effect";
2
+ import type * as AsyncResult from "effect/reactivity/AsyncResult";
2
3
  import * as Atom from "effect/reactivity/Atom";
3
4
  import type { AtomServices } from "#AtomServices.ts";
4
5
  import type { deployUpdate } from "#client/deployUpdate.ts";
@@ -12,20 +13,25 @@ export interface UndoableNotice<TInput, A> {
12
13
  readonly message: string;
13
14
  readonly value: A;
14
15
  }
16
+ export interface UndoableChange<TInput, A, E, TInverse extends readonly unknown[]> {
17
+ readonly notice: Option.Option<UndoableNotice<TInput, A>>;
18
+ readonly pending: boolean;
19
+ readonly remove: (input: TInput, ...inverse: TInverse) => void;
20
+ readonly removeResult: AsyncResult.AsyncResult<A, E>;
21
+ readonly removing: boolean;
22
+ readonly undo: () => void;
23
+ readonly undoing: boolean;
24
+ readonly undoResult: AsyncResult.AsyncResult<A, E>;
25
+ }
26
+ export declare function useUndoableChange<TInput, A, E, R, TRuntimeError>(command: VersionedChangeCommand<TInput, A, E, R, TRuntimeError>, options: {
27
+ readonly describe: (value: NoInfer<A>, input: NoInfer<TInput>) => string;
28
+ readonly undoInput: (value: NoInfer<A>, input: NoInfer<TInput>) => NoInfer<TInput>;
29
+ }): UndoableChange<TInput, A, E | TRuntimeError, []>;
15
30
  export declare function useUndoableChange<TInput extends {
16
31
  readonly expectedVersion: number;
17
32
  }, A extends {
18
33
  readonly version: number;
19
34
  }, E, R, TRuntimeError>(command: VersionedChangeCommand<TInput, A, E, R, TRuntimeError>, options: {
20
35
  readonly describe: (value: NoInfer<A>, input: NoInfer<TInput>) => string;
21
- }): {
22
- notice: Option.Option<UndoableNotice<TInput, A>>;
23
- pending: boolean;
24
- remove: (input: TInput, inverse: Omit<TInput, "expectedVersion">) => void;
25
- removeResult: import("effect/reactivity/AsyncResult").AsyncResult<A, E | TRuntimeError>;
26
- removing: boolean;
27
- undo: () => void;
28
- undoing: boolean;
29
- undoResult: import("effect/reactivity/AsyncResult").AsyncResult<A, E | TRuntimeError>;
30
- };
36
+ }): UndoableChange<TInput, A, E | TRuntimeError, [inverse: Omit<TInput, "expectedVersion">]>;
31
37
  //# sourceMappingURL=useUndoableChange.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useUndoableChange.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/useUndoableChange.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAE/C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAG5D,MAAM,WAAW,sBAAsB,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,aAAa;IACrE,QAAQ,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;IACtE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;IACrD,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,CAAC,OAAO,YAAY,CAAC,EAAE,OAAO,CAAC,CAAC;CAChE;AAED,MAAM,WAAW,cAAc,CAAC,MAAM,EAAE,CAAC;IACxC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CAClB;AAED,wBAAgB,iBAAiB,CAAC,MAAM,SAAS;IAAE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAA;CAAE,EAAE,C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AE,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CACnF,GACC,cAAc,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,aAAa,EAAE,EAAE,CAAC,CAAC;AACpD,wBAAgB,iBAAiB,CAAC,MAAM,SAAS;IAAE,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAA;CAAE,EAAE,CAAC,SAAS;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,EAAE,CAAC,EAAE,CAAC,EAAE,aAAa,EACjJ,OAAO,EAAE,sBAAsB,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,aAAa,CAAC,EAC/D,OAAO,EAAE;IAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK,MAAM,CAAA;CAAE,GACnF,cAAc,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,aAAa,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC"}
@@ -9,12 +9,13 @@ export function useUndoableChange(command, options) {
9
9
  const owner = useMemo(() => ({ notice: Atom.make(Option.none()), registry, run, runtime, sequence: 0, update }), [run, runtime, update, registry]);
10
10
  const execute = useMemo(() => (input) => Effect.acquireUseRelease(owner.update.block, () => owner.run(input), (release) => Effect.sync(release)), [owner]);
11
11
  const removeRun = useMemo(() => (request) => execute(request.input), [execute]);
12
- const undoRun = useMemo(() => (entry) => execute({ ...entry.input, ...entry.inverse, expectedVersion: entry.value.version }), [execute]);
12
+ const undoRun = useMemo(() => (entry) => execute(entry.inverse), [execute]);
13
13
  const removal = useAction(owner.runtime, removeRun, {
14
14
  concurrent: true,
15
15
  onSuccess: (value, { input, inverse, sequence }) => {
16
16
  if (sequence === owner.sequence) {
17
- owner.registry.set(owner.notice, Option.some({ input, inverse, message: options.describe(value, input), value }));
17
+ const undoInput = options.undoInput ? options.undoInput(value, input) : { ...input, ...inverse, expectedVersion: value.version };
18
+ owner.registry.set(owner.notice, Option.some({ input, inverse: undoInput, message: options.describe(value, input), value }));
18
19
  }
19
20
  },
20
21
  });
@@ -1 +1 @@
1
- {"version":3,"file":"useUndoableChange.js","sourceRoot":"","sources":["../../../src/client/adapters/useUndoableChange.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAG5C,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAcnD,MAAM,UAAU,iBAAiB,CAChC,OAA+D,EAC/D,OAAqF;IAErF,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACzC,MAAM,QAAQ,GAAG,UAAU,CAAC,eAAe,CAAC,CAAC;IAO7C,MAAM,KAAK,GAAG,OAAO,CACpB,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAS,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAChG,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,CAChC,CAAC;IACF,MAAM,OAAO,GAAG,OAAO,CACtB,GAAG,EAAE,CAAC,CAAC,KAAa,EAAE,EAAE,CACvB,MAAM,CAAC,iBAAiB,CACvB,KAAK,CAAC,MAAM,CAAC,KAAK,EAClB,GAAG,EAAE,CAAC,KAAK,CAAC,GA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;QACN,MAAM;QACN,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,WAAW,CAAC,OAAO;QAC/C,MAAM,EAAE,CAAC,KAAa,EAAE,OAAyC,EAAE,EAAE;YACpE,KAAK,CAAC,QAAQ,IAAI,CAAC,CAAC;YACpB,OAAO,CAAC,QAAQ,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;QAChE,CAAC;QACD,YAAY,EAAE,OAAO,CAAC,MAAM;QAC5B,QAAQ,EAAE,OAAO,CAAC,OAAO;QACzB,IAAI,EAAE,GAAG,EAAE;YACV,MAAM,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;YACjD,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC5B,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YACrC,CAAC;QACF,CAAC;QACD,OAAO,EAAE,WAAW,CAAC,OAAO;QAC5B,UAAU,EAAE,WAAW,CAAC,MAAM;KAC9B,CAAC;AACH,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { Duration } from "effect";
2
+ import * as Atom from "effect/reactivity/Atom";
3
+ type TimerRuntime = Pick<Atom.AtomRuntime<never, unknown>, "atom">;
4
+ export declare function useDebouncedValue<A>(value: A, duration: Duration.Input, runtime?: TimerRuntime): A;
5
+ export {};
6
+ //# sourceMappingURL=useDebouncedValue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDebouncedValue.d.ts","sourceRoot":"","sources":["../../src/client/useDebouncedValue.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAiB,MAAM,QAAQ,CAAC;AACjD,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAI/C,KAAK,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,CAAC;AAgBnE,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAE,YAA6B,GAAG,CAAC,CAgBlH"}
@@ -0,0 +1,33 @@
1
+ import { RegistryContext, useAtomSuspense } from "@effect/atom-react";
2
+ import { Duration, Effect, Layer } from "effect";
3
+ import * as Atom from "effect/reactivity/Atom";
4
+ import { useContext, useEffect, useState } from "react";
5
+ const defaultRuntime = Atom.runtime(Layer.empty);
6
+ function debounce(value, millis, runtime) {
7
+ const input = Atom.make({ changed: false, millis, value });
8
+ const result = runtime
9
+ .atom((get) => {
10
+ const next = get(input);
11
+ return next.changed ? Effect.as(Effect.sleep(next.millis), next.value) : Effect.succeed(next.value);
12
+ }, { initialValue: value })
13
+ .pipe(Atom.setIdleTTL(0));
14
+ return { input, result };
15
+ }
16
+ export function useDebouncedValue(value, duration, runtime = defaultRuntime) {
17
+ const registry = useContext(RegistryContext);
18
+ const millis = Duration.toMillis(Duration.fromInputUnsafe(duration));
19
+ const [held, hold] = useState(() => ({ ...debounce(value, millis, runtime), registry, runtime }));
20
+ let owner = held;
21
+ if (held.registry !== registry || held.runtime !== runtime) {
22
+ owner = { ...debounce(value, millis, runtime), registry, runtime };
23
+ hold(owner);
24
+ }
25
+ useEffect(() => {
26
+ const previous = registry.get(owner.input);
27
+ if (!Object.is(previous.value, value) || previous.millis !== millis) {
28
+ registry.set(owner.input, { changed: true, millis, value });
29
+ }
30
+ }, [registry, owner, value, millis]);
31
+ return useAtomSuspense(owner.result).value;
32
+ }
33
+ //# sourceMappingURL=useDebouncedValue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDebouncedValue.js","sourceRoot":"","sources":["../../src/client/useDebouncedValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,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I,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,GAAG,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IAClG,IAAI,KAAK,GAAG,IAAI,CAAC;IACjB,IAAI,IAAI,CAAC,QAAQ,KAAK,QAAQ,IAAI,IAAI,CAAC,OAAO,KAAK,OAAO,EAAE,CAAC;QAC5D,KAAK,GAAG,EAAE,GAAG,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;QACnE,IAAI,CAAC,KAAK,CAAC,CAAC;IACb,CAAC;IACD,SAAS,CAAC,GAAG,EAAE;QACd,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,QAAQ,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;YACrE,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;QAC7D,CAAC;IACF,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;IACrC,OAAO,eAAe,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { Duration } from "effect";
2
+ import * as Atom from "effect/reactivity/Atom";
3
+ type TimerRuntime = Pick<Atom.AtomRuntime<never, unknown>, "fn">;
4
+ export declare function useDelay(duration: Duration.Input, runtime?: TimerRuntime): {
5
+ cancel: () => void;
6
+ pending: boolean;
7
+ start: (complete: () => void) => void;
8
+ };
9
+ export {};
10
+ //# sourceMappingURL=useDelay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDelay.d.ts","sourceRoot":"","sources":["../../src/client/useDelay.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAiB,MAAM,QAAQ,CAAC;AACjD,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAI/C,KAAK,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC;AAEjE,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAE,YAA6B;;;sBAOnD,MAAM,IAAI;EAK/C"}
@@ -0,0 +1,16 @@
1
+ import { RegistryContext, useAtomValue } from "@effect/atom-react";
2
+ import { Duration, Effect, Layer } from "effect";
3
+ import * as Atom from "effect/reactivity/Atom";
4
+ import { useCallback, useContext, useEffect, useMemo } from "react";
5
+ const defaultRuntime = Atom.runtime(Layer.empty);
6
+ export function useDelay(duration, runtime = defaultRuntime) {
7
+ const registry = useContext(RegistryContext);
8
+ const millis = Duration.toMillis(Duration.fromInputUnsafe(duration));
9
+ const timer = useMemo(() => runtime.fn((complete) => Effect.andThen(Effect.sleep(millis), Effect.sync(complete))).pipe(Atom.setIdleTTL(0)), [runtime, millis]);
10
+ const start = useCallback((complete) => registry.set(timer, complete), [registry, timer]);
11
+ const result = useAtomValue(timer);
12
+ const cancel = useCallback(() => registry.set(timer, Atom.Interrupt), [registry, timer]);
13
+ useEffect(() => cancel, [cancel]);
14
+ return { cancel, pending: result.waiting, start };
15
+ }
16
+ //# sourceMappingURL=useDelay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useDelay.js","sourceRoot":"","sources":["../../src/client/useDelay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,EA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,CAAC,MAAM,CAAC,CAAC,CAAC;IAClC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;AACnD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/react",
3
- "version": "0.41.2",
3
+ "version": "0.42.0",
4
4
  "description": "React query, action, editor and session hooks for native Effect atoms",
5
5
  "keywords": [
6
6
  "effect"
@@ -46,10 +46,10 @@
46
46
  ],
47
47
  "dependencies": {
48
48
  "@shivaedev/access": "0.5.1",
49
- "@shivaedev/contract": "0.36.1",
49
+ "@shivaedev/contract": "0.37.0",
50
50
  "@shivaedev/errors": "0.9.1",
51
- "@shivaedev/form": "0.17.1",
52
- "@shivaedev/identity": "0.26.1"
51
+ "@shivaedev/form": "0.18.0",
52
+ "@shivaedev/identity": "0.26.2"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@effect/atom-react": "4.0.7",
@@ -57,14 +57,14 @@
57
57
  "@effect/sql-pg": "4.0.7",
58
58
  "@effect/sql-sqlite-node": "4.0.7",
59
59
  "@shivaedev/changes": "0.10.1",
60
- "@shivaedev/jobs": "0.37.0",
61
- "@shivaedev/quality": "0.62.0",
62
- "@shivaedev/server": "0.25.0",
60
+ "@shivaedev/jobs": "0.37.1",
61
+ "@shivaedev/quality": "0.63.0",
62
+ "@shivaedev/server": "0.26.0",
63
63
  "@shivaedev/service": "0.7.1",
64
- "@shivaedev/sql": "0.40.0",
65
- "@shivaedev/test-kits": "0.5.0",
66
- "@shivaedev/test-sql": "0.21.2",
67
- "@shivaedev/test-story": "0.39.2",
64
+ "@shivaedev/sql": "0.41.0",
65
+ "@shivaedev/test-kits": "0.6.0",
66
+ "@shivaedev/test-sql": "0.22.0",
67
+ "@shivaedev/test-story": "0.40.0",
68
68
  "@tanstack/react-router": "1.170.46",
69
69
  "@types/node": "24.19.2",
70
70
  "@types/react": "19.3.0",
@@ -1,5 +1,6 @@
1
1
  import { RegistryContext, useAtomValue } from "@effect/atom-react";
2
2
  import { Effect, Option } from "effect";
3
+ import type * as AsyncResult from "effect/reactivity/AsyncResult";
3
4
  import * as Atom from "effect/reactivity/Atom";
4
5
  import { useContext, useMemo } from "react";
5
6
  import type { AtomServices } from "#AtomServices.ts";
@@ -18,16 +19,41 @@ export interface UndoableNotice<TInput, A> {
18
19
  readonly value: A;
19
20
  }
20
21
 
22
+ export interface UndoableChange<TInput, A, E, TInverse extends readonly unknown[]> {
23
+ readonly notice: Option.Option<UndoableNotice<TInput, A>>;
24
+ readonly pending: boolean;
25
+ readonly remove: (input: TInput, ...inverse: TInverse) => void;
26
+ readonly removeResult: AsyncResult.AsyncResult<A, E>;
27
+ readonly removing: boolean;
28
+ readonly undo: () => void;
29
+ readonly undoing: boolean;
30
+ readonly undoResult: AsyncResult.AsyncResult<A, E>;
31
+ }
32
+
33
+ export function useUndoableChange<TInput, A, E, R, TRuntimeError>(
34
+ command: VersionedChangeCommand<TInput, A, E, R, TRuntimeError>,
35
+ options: {
36
+ readonly describe: (value: NoInfer<A>, input: NoInfer<TInput>) => string;
37
+ readonly undoInput: (value: NoInfer<A>, input: NoInfer<TInput>) => NoInfer<TInput>;
38
+ },
39
+ ): UndoableChange<TInput, A, E | TRuntimeError, []>;
21
40
  export function useUndoableChange<TInput extends { readonly expectedVersion: number }, A extends { readonly version: number }, E, R, TRuntimeError>(
22
41
  command: VersionedChangeCommand<TInput, A, E, R, TRuntimeError>,
23
42
  options: { readonly describe: (value: NoInfer<A>, input: NoInfer<TInput>) => string },
43
+ ): UndoableChange<TInput, A, E | TRuntimeError, [inverse: Omit<TInput, "expectedVersion">]>;
44
+ export function useUndoableChange<TInput, A extends { readonly version: number }, E, R, TRuntimeError>(
45
+ command: VersionedChangeCommand<TInput, A, E, R, TRuntimeError>,
46
+ options: {
47
+ readonly describe: (value: A, input: TInput) => string;
48
+ readonly undoInput?: (value: A, input: TInput) => TInput;
49
+ },
24
50
  ) {
25
51
  const { run, runtime, update } = command;
26
52
  const registry = useContext(RegistryContext);
27
- type Entry = UndoableNotice<TInput, A> & { readonly inverse: Omit<TInput, "expectedVersion"> };
53
+ type Entry = UndoableNotice<TInput, A> & { readonly inverse: TInput };
28
54
  interface Removal {
29
55
  readonly input: TInput;
30
- readonly inverse: Omit<TInput, "expectedVersion">;
56
+ readonly inverse: Omit<TInput, "expectedVersion"> | undefined;
31
57
  readonly sequence: number;
32
58
  }
33
59
  const owner = useMemo(
@@ -44,12 +70,13 @@ export function useUndoableChange<TInput extends { readonly expectedVersion: num
44
70
  [owner],
45
71
  );
46
72
  const removeRun = useMemo(() => (request: Removal) => execute(request.input), [execute]);
47
- const undoRun = useMemo(() => (entry: Entry) => execute({ ...entry.input, ...entry.inverse, expectedVersion: entry.value.version }), [execute]);
73
+ const undoRun = useMemo(() => (entry: Entry) => execute(entry.inverse), [execute]);
48
74
  const removal = useAction(owner.runtime, removeRun, {
49
75
  concurrent: true,
50
76
  onSuccess: (value, { input, inverse, sequence }) => {
51
77
  if (sequence === owner.sequence) {
52
- owner.registry.set(owner.notice, Option.some({ input, inverse, message: options.describe(value, input), value }));
78
+ const undoInput = options.undoInput ? options.undoInput(value, input) : { ...input, ...inverse, expectedVersion: value.version };
79
+ owner.registry.set(owner.notice, Option.some({ input, inverse: undoInput, message: options.describe(value, input), value }));
53
80
  }
54
81
  },
55
82
  });
@@ -64,7 +91,7 @@ export function useUndoableChange<TInput extends { readonly expectedVersion: num
64
91
  return {
65
92
  notice,
66
93
  pending: removal.pending || restoration.pending,
67
- remove: (input: TInput, inverse: Omit<TInput, "expectedVersion">) => {
94
+ remove: (input: TInput, inverse?: Omit<TInput, "expectedVersion">) => {
68
95
  owner.sequence += 1;
69
96
  removal.dispatch({ input, inverse, sequence: owner.sequence });
70
97
  },
@@ -0,0 +1,39 @@
1
+ import { RegistryContext, useAtomSuspense } from "@effect/atom-react";
2
+ import { Duration, Effect, Layer } from "effect";
3
+ import * as Atom from "effect/reactivity/Atom";
4
+ import { useContext, useEffect, useState } from "react";
5
+
6
+ const defaultRuntime = Atom.runtime(Layer.empty);
7
+ type TimerRuntime = Pick<Atom.AtomRuntime<never, unknown>, "atom">;
8
+
9
+ function debounce<A>(value: A, millis: number, runtime: TimerRuntime) {
10
+ const input = Atom.make({ changed: false, millis, value });
11
+ const result = runtime
12
+ .atom(
13
+ (get) => {
14
+ const next = get(input);
15
+ return next.changed ? Effect.as(Effect.sleep(next.millis), next.value) : Effect.succeed(next.value);
16
+ },
17
+ { initialValue: value },
18
+ )
19
+ .pipe(Atom.setIdleTTL(0));
20
+ return { input, result };
21
+ }
22
+
23
+ export function useDebouncedValue<A>(value: A, duration: Duration.Input, runtime: TimerRuntime = defaultRuntime): A {
24
+ const registry = useContext(RegistryContext);
25
+ const millis = Duration.toMillis(Duration.fromInputUnsafe(duration));
26
+ const [held, hold] = useState(() => ({ ...debounce(value, millis, runtime), registry, runtime }));
27
+ let owner = held;
28
+ if (held.registry !== registry || held.runtime !== runtime) {
29
+ owner = { ...debounce(value, millis, runtime), registry, runtime };
30
+ hold(owner);
31
+ }
32
+ useEffect(() => {
33
+ const previous = registry.get(owner.input);
34
+ if (!Object.is(previous.value, value) || previous.millis !== millis) {
35
+ registry.set(owner.input, { changed: true, millis, value });
36
+ }
37
+ }, [registry, owner, value, millis]);
38
+ return useAtomSuspense(owner.result).value;
39
+ }
@@ -0,0 +1,21 @@
1
+ import { RegistryContext, useAtomValue } from "@effect/atom-react";
2
+ import { Duration, Effect, Layer } from "effect";
3
+ import * as Atom from "effect/reactivity/Atom";
4
+ import { useCallback, useContext, useEffect, useMemo } from "react";
5
+
6
+ const defaultRuntime = Atom.runtime(Layer.empty);
7
+ type TimerRuntime = Pick<Atom.AtomRuntime<never, unknown>, "fn">;
8
+
9
+ export function useDelay(duration: Duration.Input, runtime: TimerRuntime = defaultRuntime) {
10
+ const registry = useContext(RegistryContext);
11
+ const millis = Duration.toMillis(Duration.fromInputUnsafe(duration));
12
+ const timer = useMemo(
13
+ () => runtime.fn((complete: () => void) => Effect.andThen(Effect.sleep(millis), Effect.sync(complete))).pipe(Atom.setIdleTTL(0)),
14
+ [runtime, millis],
15
+ );
16
+ const start = useCallback((complete: () => void) => registry.set(timer, complete), [registry, timer]);
17
+ const result = useAtomValue(timer);
18
+ const cancel = useCallback(() => registry.set(timer, Atom.Interrupt), [registry, timer]);
19
+ useEffect(() => cancel, [cancel]);
20
+ return { cancel, pending: result.waiting, start };
21
+ }