@shivaedev/react 0.39.0 → 0.40.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 +12 -0
- package/README.md +71 -0
- package/dist/client/adapters/crypto.d.ts +3 -0
- package/dist/client/adapters/crypto.d.ts.map +1 -0
- package/dist/client/adapters/crypto.js +9 -0
- package/dist/client/adapters/crypto.js.map +1 -0
- package/dist/client/adapters/handlerRuntime.d.ts +3 -0
- package/dist/client/adapters/handlerRuntime.d.ts.map +1 -0
- package/dist/client/adapters/handlerRuntime.js +4 -0
- package/dist/client/adapters/handlerRuntime.js.map +1 -0
- package/dist/client/adapters/useClock.d.ts +5 -0
- package/dist/client/adapters/useClock.d.ts.map +1 -0
- package/dist/client/adapters/useClock.js +9 -0
- package/dist/client/adapters/useClock.js.map +1 -0
- package/dist/client/adapters/useNewId.d.ts +5 -0
- package/dist/client/adapters/useNewId.d.ts.map +1 -0
- package/dist/client/adapters/useNewId.js +9 -0
- package/dist/client/adapters/useNewId.js.map +1 -0
- package/dist/client/adapters/usePromiseAction.d.ts +7 -0
- package/dist/client/adapters/usePromiseAction.d.ts.map +1 -0
- package/dist/client/adapters/usePromiseAction.js +16 -0
- package/dist/client/adapters/usePromiseAction.js.map +1 -0
- package/dist/client/adapters/useUndoableChange.d.ts +31 -0
- package/dist/client/adapters/useUndoableChange.d.ts.map +1 -0
- package/dist/client/adapters/useUndoableChange.js +48 -0
- package/dist/client/adapters/useUndoableChange.js.map +1 -0
- package/package.json +17 -17
- package/src/client/adapters/crypto.ts +14 -0
- package/src/client/adapters/handlerRuntime.ts +4 -0
- package/src/client/adapters/useClock.ts +11 -0
- package/src/client/adapters/useNewId.ts +11 -0
- package/src/client/adapters/usePromiseAction.ts +25 -0
- package/src/client/adapters/useUndoableChange.ts +82 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.40.0 - 2026-11-04
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Activate staged updates while hidden once registered blockers release, and expose an application-owned reload action when activation is unavailable or blocked.
|
|
8
|
+
- Provide `useNewId` and `useClock` callbacks backed by atom runtime Crypto and Clock services, plus a Web Crypto client layer.
|
|
9
|
+
- Add `usePromiseAction` to dispatch promise calls with native action state, `UnknownError` failures by default and an optional typed rejection mapper.
|
|
10
|
+
|
|
11
|
+
### Breaking
|
|
12
|
+
|
|
13
|
+
- TypeScript source exports now use `@shivaedev/source` instead of `source`. Replace `source` with `@shivaedev/source` in Node `--conditions`, TypeScript `customConditions`, and Vite/Vitest `resolve.conditions`. Bare `source` selects built JavaScript; no compatibility alias is provided.
|
|
14
|
+
|
|
3
15
|
## 0.39.0 - 2026-11-04
|
|
4
16
|
|
|
5
17
|
### Added
|
package/README.md
CHANGED
|
@@ -564,6 +564,42 @@ A newer dispatch interrupts the earlier command by default. Interrupted work pro
|
|
|
564
564
|
|
|
565
565
|
A typed `Unauthorized` failure asks the session owner to recheck once per failed invocation, even with concurrent commands. The hook does not sign out. Session replacement discards outgoing callbacks and rechecks, including when outgoing work cannot be interrupted.
|
|
566
566
|
|
|
567
|
+
#### Remove with a version-guarded Undo
|
|
568
|
+
|
|
569
|
+
Use `useUndoableChange` from `@shivaedev/react/client/adapters/useUndoableChange.ts` when a versioned command also performs its inverse. Its payload includes `expectedVersion: number`, and its success includes `version: number`. Supply the bound command's `run`, its native `runtime`, and the application's `deployUpdate` as `update`:
|
|
570
|
+
|
|
571
|
+
```tsx
|
|
572
|
+
import { Option } from "effect";
|
|
573
|
+
import { useUndoableChange } from "@shivaedev/react/client/adapters/useUndoableChange.ts";
|
|
574
|
+
|
|
575
|
+
function RemoveNote({ note }: { readonly note: { id: string; version: number } }) {
|
|
576
|
+
const change = useUndoableChange({ ...api.change, runtime: NotesClient.runtime, update }, { describe: (saved) => `Removed ${saved.id}` });
|
|
577
|
+
const notice = Option.getOrUndefined(change.notice);
|
|
578
|
+
return (
|
|
579
|
+
<>
|
|
580
|
+
<button
|
|
581
|
+
disabled={change.pending}
|
|
582
|
+
onClick={() => change.remove({ id: note.id, expectedVersion: note.version, removed: true }, { id: note.id, removed: false })}
|
|
583
|
+
>
|
|
584
|
+
Remove
|
|
585
|
+
</button>
|
|
586
|
+
{notice && (
|
|
587
|
+
<p>
|
|
588
|
+
{notice.message}
|
|
589
|
+
<button disabled={change.undoing} onClick={change.undo}>
|
|
590
|
+
Undo
|
|
591
|
+
</button>
|
|
592
|
+
</p>
|
|
593
|
+
)}
|
|
594
|
+
</>
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
```
|
|
598
|
+
|
|
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
|
+
|
|
601
|
+
`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
|
+
|
|
567
603
|
#### Commands with a pending display value
|
|
568
604
|
|
|
569
605
|
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 refetches:
|
|
@@ -961,6 +997,41 @@ const minutesAgo = Math.floor((now - savedAt) / 60_000);
|
|
|
961
997
|
|
|
962
998
|
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.
|
|
963
999
|
|
|
1000
|
+
#### Ids, time and promises in callbacks
|
|
1001
|
+
|
|
1002
|
+
`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:
|
|
1003
|
+
|
|
1004
|
+
```tsx
|
|
1005
|
+
import { useState } from "react";
|
|
1006
|
+
import { useClock } from "@shivaedev/react/client/adapters/useClock.ts";
|
|
1007
|
+
import { useNewId } from "@shivaedev/react/client/adapters/useNewId.ts";
|
|
1008
|
+
|
|
1009
|
+
const newId = useNewId(client.runtime);
|
|
1010
|
+
const now = useClock(client.runtime);
|
|
1011
|
+
const [id] = useState(newId);
|
|
1012
|
+
const [createdAt] = useState(now);
|
|
1013
|
+
const stamp = () => ({ id: newId(), at: now() });
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
These lines belong inside a component; `client` is the application's atom client. Stories pass a runtime containing their test Clock and seeded Crypto. The hooks use native atom suspense while the runtime builds. The supplied Crypto's UUID generation must be synchronous. Without a runtime, they use a shared native runtime with Effect's default Clock and `clientCrypto`, a Web Crypto Layer from `@shivaedev/react/client/adapters/crypto.ts`. Provide that Layer to an application's runtime when it needs browser Crypto; it uses `crypto.getRandomValues` and `crypto.subtle.digest`.
|
|
1017
|
+
|
|
1018
|
+
`usePromiseAction(() => promise, options?)` from `@shivaedev/react/client/adapters/usePromiseAction.ts` returns the same state as `useAction`, with `dispatch()` taking no input. It runs the callback on dispatch, keeps its atom across rerenders and calls the latest committed callback. A promise rejection or synchronous throw becomes `Cause.UnknownError` by default. Supply `catch` to map the unknown cause into an application's typed failure:
|
|
1019
|
+
|
|
1020
|
+
```tsx
|
|
1021
|
+
import { usePromiseAction } from "@shivaedev/react/client/adapters/usePromiseAction.ts";
|
|
1022
|
+
|
|
1023
|
+
const copy = usePromiseAction(() => navigator.clipboard.writeText(text), {
|
|
1024
|
+
catch: (cause) => new ClipboardDenied({ reason: String(cause) }),
|
|
1025
|
+
});
|
|
1026
|
+
const button = (
|
|
1027
|
+
<button disabled={copy.pending} onClick={() => copy.dispatch()}>
|
|
1028
|
+
Copy
|
|
1029
|
+
</button>
|
|
1030
|
+
);
|
|
1031
|
+
```
|
|
1032
|
+
|
|
1033
|
+
These lines belong inside a component; `text` and `ClipboardDenied` belong to the application. Read the success from `copy.data` and the typed failure from `copy.cause` or `copy.result`. The helper runs through a native action atom and adds no promise cancellation adapter.
|
|
1034
|
+
|
|
964
1035
|
#### Render tests in happy-dom
|
|
965
1036
|
|
|
966
1037
|
Use `@shivaedev/test-story/client/adapters/react.ts` with Vitest's `happy-dom` environment. Install contract and changes for the example's query, command and key declarations, and the testing peers and environment in your app's dev dependencies:
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAU,KAAK,EAAiB,MAAM,QAAQ,CAAC;AAE9D,eAAO,MAAM,YAAY,0CAWxB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Crypto, Effect, Layer, PlatformError } from "effect";
|
|
2
|
+
export const clientCrypto = Layer.succeed(Crypto.Crypto, Crypto.make({
|
|
3
|
+
digest: (algorithm, data) => Effect.tryPromise({
|
|
4
|
+
catch: (cause) => PlatformError.systemError({ _tag: "Unknown", cause, description: "Could not compute digest", method: "digest", module: "Crypto" }),
|
|
5
|
+
try: () => globalThis.crypto.subtle.digest(algorithm, new Uint8Array(data)).then((buffer) => new Uint8Array(buffer)),
|
|
6
|
+
}),
|
|
7
|
+
randomBytes: (size) => globalThis.crypto.getRandomValues(new Uint8Array(size)),
|
|
8
|
+
}));
|
|
9
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../../src/client/adapters/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAE9D,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CACxC,MAAM,CAAC,MAAM,EACb,MAAM,CAAC,IAAI,CAAC;IACX,MAAM,EAAE,CAAC,SAAS,EAAE,IAAI,EAAE,EAAE,CAC3B,MAAM,CAAC,UAAU,CAAC;QACjB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAChB,aAAa,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,WAAW,EAAE,0BAA0B,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;QACnI,GAAG,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;KACpH,CAAC;IACH,WAAW,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;CAC9E,CAAC,CACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlerRuntime.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/handlerRuntime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAG/C,eAAO,MAAM,cAAc,yDAA6B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlerRuntime.js","sourceRoot":"","sources":["../../../src/client/adapters/handlerRuntime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE1D,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Context } from "effect";
|
|
2
|
+
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
export declare function useClock(runtime?: Atom.Atom<AsyncResult.AsyncResult<Context.Context<never>, unknown>>): () => number;
|
|
5
|
+
//# sourceMappingURL=useClock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useClock.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/useClock.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,OAAO,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,KAAK,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAIpD,wBAAgB,QAAQ,CAAC,OAAO,GAAE,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAkB,GAAG,MAAM,MAAM,CAGpI"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useAtomSuspense } from "@effect/atom-react";
|
|
2
|
+
import { Clock, Context } from "effect";
|
|
3
|
+
import { useCallback } from "react";
|
|
4
|
+
import { handlerRuntime } from "#client/adapters/handlerRuntime.ts";
|
|
5
|
+
export function useClock(runtime = handlerRuntime) {
|
|
6
|
+
const clock = Context.get(useAtomSuspense(runtime).value, Clock.Clock);
|
|
7
|
+
return useCallback(() => clock.currentTimeMillisUnsafe(), [clock]);
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=useClock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useClock.js","sourceRoot":"","sources":["../../../src/client/adapters/useClock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAGxC,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,MAAM,UAAU,QAAQ,CAAC,OAAO,GAAwE,cAAc;IACrH,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IACvE,OAAO,WAAW,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,uBAAuB,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;AACpE,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Context, Crypto } from "effect";
|
|
2
|
+
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
export declare function useNewId(runtime?: Atom.Atom<AsyncResult.AsyncResult<Context.Context<Crypto.Crypto>, unknown>>): () => string;
|
|
5
|
+
//# sourceMappingURL=useNewId.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useNewId.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/useNewId.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAU,MAAM,QAAQ,CAAC;AACjD,OAAO,KAAK,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAIpD,wBAAgB,QAAQ,CAAC,OAAO,GAAE,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAkB,GAAG,MAAM,MAAM,CAG5I"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useAtomSuspense } from "@effect/atom-react";
|
|
2
|
+
import { Context, Crypto, Effect } from "effect";
|
|
3
|
+
import { useCallback } from "react";
|
|
4
|
+
import { handlerRuntime } from "#client/adapters/handlerRuntime.ts";
|
|
5
|
+
export function useNewId(runtime = handlerRuntime) {
|
|
6
|
+
const crypto = Context.get(useAtomSuspense(runtime).value, Crypto.Crypto);
|
|
7
|
+
return useCallback(() => Effect.runSync(crypto.randomUUIDv4()), [crypto]);
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=useNewId.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useNewId.js","sourceRoot":"","sources":["../../../src/client/adapters/useNewId.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGjD,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,MAAM,UAAU,QAAQ,CAAC,OAAO,GAAgF,cAAc;IAC7H,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC1E,OAAO,WAAW,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;AAC3E,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Cause } from "effect";
|
|
2
|
+
import { type ActionState } from "#client/resultState.ts";
|
|
3
|
+
export declare function usePromiseAction<A>(run: () => PromiseLike<A>): ActionState<void, A, Cause.UnknownError>;
|
|
4
|
+
export declare function usePromiseAction<A, E>(run: () => PromiseLike<A>, options: {
|
|
5
|
+
readonly catch: (cause: unknown) => E;
|
|
6
|
+
}): ActionState<void, A, E>;
|
|
7
|
+
//# sourceMappingURL=usePromiseAction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePromiseAction.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/usePromiseAction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAU,MAAM,QAAQ,CAAC;AAGvC,OAAO,EAAE,KAAK,WAAW,EAAa,MAAM,wBAAwB,CAAC;AAErE,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC;AACzG,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,CAAC,CAAA;CAAE,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Cause, Effect } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import { useInsertionEffect, useRef, useState } from "react";
|
|
4
|
+
import { useAction } from "#client/resultState.ts";
|
|
5
|
+
export function usePromiseAction(run, options) {
|
|
6
|
+
const latestRef = useRef({ reject: options?.catch, run });
|
|
7
|
+
useInsertionEffect(() => {
|
|
8
|
+
latestRef.current = { reject: options?.catch, run };
|
|
9
|
+
}, [run, options?.catch]);
|
|
10
|
+
const [atom] = useState(() => Atom.fn()(() => Effect.tryPromise({
|
|
11
|
+
catch: (cause) => (latestRef.current.reject ? latestRef.current.reject(cause) : new Cause.UnknownError(cause)),
|
|
12
|
+
try: () => latestRef.current.run(),
|
|
13
|
+
})));
|
|
14
|
+
return useAction(atom);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=usePromiseAction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePromiseAction.js","sourceRoot":"","sources":["../../../src/client/adapters/usePromiseAction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC7D,OAAO,EAAoB,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAIrE,MAAM,UAAU,gBAAgB,CAC/B,GAAyB,EACzB,OAAmD;IAEnD,MAAM,SAAS,GAAG,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;IAC1D,kBAAkB,CAAC,GAAG,EAAE;QACvB,SAAS,CAAC,OAAO,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;IACrD,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;IAC1B,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAC5B,IAAI,CAAC,EAAE,EAAQ,CAAC,GAAG,EAAE,CACpB,MAAM,CAAC,UAAU,CAAC;QACjB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QAC9G,GAAG,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE;KAClC,CAAC,CACF,CACD,CAAC;IACF,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Effect, Option } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import type { AtomServices } from "#AtomServices.ts";
|
|
4
|
+
import type { deployUpdate } from "#client/deployUpdate.ts";
|
|
5
|
+
export interface VersionedChangeCommand<TInput, A, E, R, TRuntimeError> {
|
|
6
|
+
readonly run: (input: TInput) => Effect.Effect<A, E, AtomServices<R>>;
|
|
7
|
+
readonly runtime: Atom.AtomRuntime<R, TRuntimeError>;
|
|
8
|
+
readonly update: Pick<ReturnType<typeof deployUpdate>, "block">;
|
|
9
|
+
}
|
|
10
|
+
export interface UndoableNotice<TInput, A> {
|
|
11
|
+
readonly input: TInput;
|
|
12
|
+
readonly message: string;
|
|
13
|
+
readonly value: A;
|
|
14
|
+
}
|
|
15
|
+
export declare function useUndoableChange<TInput extends {
|
|
16
|
+
readonly expectedVersion: number;
|
|
17
|
+
}, A extends {
|
|
18
|
+
readonly version: number;
|
|
19
|
+
}, E, R, TRuntimeError>(command: VersionedChangeCommand<TInput, A, E, R, TRuntimeError>, options: {
|
|
20
|
+
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
|
+
};
|
|
31
|
+
//# sourceMappingURL=useUndoableChange.d.ts.map
|
|
@@ -0,0 +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,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;;;oBA4CpE,MAAM,WAAW,IAAI,CAAC,MAAM,EAAE,iBAAiB,CAAC;;;;;;EAejE"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { RegistryContext, useAtomValue } from "@effect/atom-react";
|
|
2
|
+
import { Effect, Option } from "effect";
|
|
3
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
import { useContext, useMemo } from "react";
|
|
5
|
+
import { useAction } from "#client/resultState.ts";
|
|
6
|
+
export function useUndoableChange(command, options) {
|
|
7
|
+
const { run, runtime, update } = command;
|
|
8
|
+
const registry = useContext(RegistryContext);
|
|
9
|
+
const owner = useMemo(() => ({ notice: Atom.make(Option.none()), registry, run, runtime, sequence: 0, update }), [run, runtime, update, registry]);
|
|
10
|
+
const execute = useMemo(() => (input) => Effect.acquireUseRelease(owner.update.block, () => owner.run(input), (release) => Effect.sync(release)), [owner]);
|
|
11
|
+
const removeRun = useMemo(() => (request) => execute(request.input), [execute]);
|
|
12
|
+
const undoRun = useMemo(() => (entry) => execute({ ...entry.input, ...entry.inverse, expectedVersion: entry.value.version }), [execute]);
|
|
13
|
+
const removal = useAction(owner.runtime, removeRun, {
|
|
14
|
+
concurrent: true,
|
|
15
|
+
onSuccess: (value, { input, inverse, sequence }) => {
|
|
16
|
+
if (sequence === owner.sequence) {
|
|
17
|
+
owner.registry.set(owner.notice, Option.some({ input, inverse, message: options.describe(value, input), value }));
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
const restoration = useAction(owner.runtime, undoRun, {
|
|
22
|
+
onSuccess: (_value, entry) => {
|
|
23
|
+
if (Option.getOrUndefined(owner.registry.get(owner.notice)) === entry) {
|
|
24
|
+
owner.registry.set(owner.notice, Option.none());
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
const notice = useAtomValue(owner.notice);
|
|
29
|
+
return {
|
|
30
|
+
notice,
|
|
31
|
+
pending: removal.pending || restoration.pending,
|
|
32
|
+
remove: (input, inverse) => {
|
|
33
|
+
owner.sequence += 1;
|
|
34
|
+
removal.dispatch({ input, inverse, sequence: owner.sequence });
|
|
35
|
+
},
|
|
36
|
+
removeResult: removal.result,
|
|
37
|
+
removing: removal.pending,
|
|
38
|
+
undo: () => {
|
|
39
|
+
const current = owner.registry.get(owner.notice);
|
|
40
|
+
if (Option.isSome(current)) {
|
|
41
|
+
restoration.dispatch(current.value);
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
undoing: restoration.pending,
|
|
45
|
+
undoResult: restoration.result,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=useUndoableChange.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useUndoableChange.js","sourceRoot":"","sources":["../../../src/client/adapters/useUndoableChange.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"description": "React query, action, editor and session hooks for native Effect atoms",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -20,14 +20,14 @@
|
|
|
20
20
|
"imports": {
|
|
21
21
|
"#test/*": "./src/test-support/*",
|
|
22
22
|
"#*.ts": {
|
|
23
|
-
"source": "./src/*.ts",
|
|
23
|
+
"@shivaedev/source": "./src/*.ts",
|
|
24
24
|
"types": "./dist/*.d.ts",
|
|
25
25
|
"default": "./dist/*.js"
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"exports": {
|
|
29
29
|
"./*.ts": {
|
|
30
|
-
"source": "./src/*.ts",
|
|
30
|
+
"@shivaedev/source": "./src/*.ts",
|
|
31
31
|
"types": "./dist/*.d.ts",
|
|
32
32
|
"default": "./dist/*.js"
|
|
33
33
|
},
|
|
@@ -45,26 +45,26 @@
|
|
|
45
45
|
"README.md"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@shivaedev/access": "0.
|
|
49
|
-
"@shivaedev/contract": "0.
|
|
50
|
-
"@shivaedev/errors": "0.
|
|
51
|
-
"@shivaedev/form": "0.
|
|
52
|
-
"@shivaedev/identity": "0.
|
|
48
|
+
"@shivaedev/access": "0.5.0",
|
|
49
|
+
"@shivaedev/contract": "0.36.0",
|
|
50
|
+
"@shivaedev/errors": "0.9.0",
|
|
51
|
+
"@shivaedev/form": "0.16.0",
|
|
52
|
+
"@shivaedev/identity": "0.24.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@effect/atom-react": "4.0.6",
|
|
56
56
|
"@effect/platform-node": "4.0.6",
|
|
57
57
|
"@effect/sql-pg": "4.0.6",
|
|
58
58
|
"@effect/sql-sqlite-node": "4.0.6",
|
|
59
|
-
"@shivaedev/changes": "0.
|
|
60
|
-
"@shivaedev/jobs": "0.
|
|
61
|
-
"@shivaedev/quality": "0.
|
|
62
|
-
"@shivaedev/server": "0.
|
|
63
|
-
"@shivaedev/service": "0.
|
|
64
|
-
"@shivaedev/sql": "0.
|
|
65
|
-
"@shivaedev/test-kits": "0.
|
|
66
|
-
"@shivaedev/test-sql": "0.
|
|
67
|
-
"@shivaedev/test-story": "0.
|
|
59
|
+
"@shivaedev/changes": "0.10.0",
|
|
60
|
+
"@shivaedev/jobs": "0.35.0",
|
|
61
|
+
"@shivaedev/quality": "0.60.0",
|
|
62
|
+
"@shivaedev/server": "0.24.0",
|
|
63
|
+
"@shivaedev/service": "0.7.0",
|
|
64
|
+
"@shivaedev/sql": "0.37.0",
|
|
65
|
+
"@shivaedev/test-kits": "0.3.0",
|
|
66
|
+
"@shivaedev/test-sql": "0.20.0",
|
|
67
|
+
"@shivaedev/test-story": "0.38.0",
|
|
68
68
|
"@tanstack/react-router": "1.170.46",
|
|
69
69
|
"@types/node": "24.19.2",
|
|
70
70
|
"@types/react": "19.3.0",
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Crypto, Effect, Layer, PlatformError } from "effect";
|
|
2
|
+
|
|
3
|
+
export const clientCrypto = Layer.succeed(
|
|
4
|
+
Crypto.Crypto,
|
|
5
|
+
Crypto.make({
|
|
6
|
+
digest: (algorithm, data) =>
|
|
7
|
+
Effect.tryPromise({
|
|
8
|
+
catch: (cause) =>
|
|
9
|
+
PlatformError.systemError({ _tag: "Unknown", cause, description: "Could not compute digest", method: "digest", module: "Crypto" }),
|
|
10
|
+
try: () => globalThis.crypto.subtle.digest(algorithm, new Uint8Array(data)).then((buffer) => new Uint8Array(buffer)),
|
|
11
|
+
}),
|
|
12
|
+
randomBytes: (size) => globalThis.crypto.getRandomValues(new Uint8Array(size)),
|
|
13
|
+
}),
|
|
14
|
+
);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useAtomSuspense } from "@effect/atom-react";
|
|
2
|
+
import { Clock, Context } from "effect";
|
|
3
|
+
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
4
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
5
|
+
import { useCallback } from "react";
|
|
6
|
+
import { handlerRuntime } from "#client/adapters/handlerRuntime.ts";
|
|
7
|
+
|
|
8
|
+
export function useClock(runtime: Atom.Atom<AsyncResult.AsyncResult<Context.Context<never>, unknown>> = handlerRuntime): () => number {
|
|
9
|
+
const clock = Context.get(useAtomSuspense(runtime).value, Clock.Clock);
|
|
10
|
+
return useCallback(() => clock.currentTimeMillisUnsafe(), [clock]);
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useAtomSuspense } from "@effect/atom-react";
|
|
2
|
+
import { Context, Crypto, Effect } from "effect";
|
|
3
|
+
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
4
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
5
|
+
import { useCallback } from "react";
|
|
6
|
+
import { handlerRuntime } from "#client/adapters/handlerRuntime.ts";
|
|
7
|
+
|
|
8
|
+
export function useNewId(runtime: Atom.Atom<AsyncResult.AsyncResult<Context.Context<Crypto.Crypto>, unknown>> = handlerRuntime): () => string {
|
|
9
|
+
const crypto = Context.get(useAtomSuspense(runtime).value, Crypto.Crypto);
|
|
10
|
+
return useCallback(() => Effect.runSync(crypto.randomUUIDv4()), [crypto]);
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Cause, Effect } from "effect";
|
|
2
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import { useInsertionEffect, useRef, useState } from "react";
|
|
4
|
+
import { type ActionState, useAction } from "#client/resultState.ts";
|
|
5
|
+
|
|
6
|
+
export function usePromiseAction<A>(run: () => PromiseLike<A>): ActionState<void, A, Cause.UnknownError>;
|
|
7
|
+
export function usePromiseAction<A, E>(run: () => PromiseLike<A>, options: { readonly catch: (cause: unknown) => E }): ActionState<void, A, E>;
|
|
8
|
+
export function usePromiseAction<A, E>(
|
|
9
|
+
run: () => PromiseLike<A>,
|
|
10
|
+
options?: { readonly catch: (cause: unknown) => E },
|
|
11
|
+
): ActionState<void, A, E | Cause.UnknownError> {
|
|
12
|
+
const latestRef = useRef({ reject: options?.catch, run });
|
|
13
|
+
useInsertionEffect(() => {
|
|
14
|
+
latestRef.current = { reject: options?.catch, run };
|
|
15
|
+
}, [run, options?.catch]);
|
|
16
|
+
const [atom] = useState(() =>
|
|
17
|
+
Atom.fn<void>()(() =>
|
|
18
|
+
Effect.tryPromise({
|
|
19
|
+
catch: (cause) => (latestRef.current.reject ? latestRef.current.reject(cause) : new Cause.UnknownError(cause)),
|
|
20
|
+
try: () => latestRef.current.run(),
|
|
21
|
+
}),
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
return useAction(atom);
|
|
25
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { RegistryContext, useAtomValue } from "@effect/atom-react";
|
|
2
|
+
import { Effect, Option } from "effect";
|
|
3
|
+
import * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
import { useContext, useMemo } from "react";
|
|
5
|
+
import type { AtomServices } from "#AtomServices.ts";
|
|
6
|
+
import type { deployUpdate } from "#client/deployUpdate.ts";
|
|
7
|
+
import { useAction } from "#client/resultState.ts";
|
|
8
|
+
|
|
9
|
+
export interface VersionedChangeCommand<TInput, A, E, R, TRuntimeError> {
|
|
10
|
+
readonly run: (input: TInput) => Effect.Effect<A, E, AtomServices<R>>;
|
|
11
|
+
readonly runtime: Atom.AtomRuntime<R, TRuntimeError>;
|
|
12
|
+
readonly update: Pick<ReturnType<typeof deployUpdate>, "block">;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface UndoableNotice<TInput, A> {
|
|
16
|
+
readonly input: TInput;
|
|
17
|
+
readonly message: string;
|
|
18
|
+
readonly value: A;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function useUndoableChange<TInput extends { readonly expectedVersion: number }, A extends { readonly version: number }, E, R, TRuntimeError>(
|
|
22
|
+
command: VersionedChangeCommand<TInput, A, E, R, TRuntimeError>,
|
|
23
|
+
options: { readonly describe: (value: NoInfer<A>, input: NoInfer<TInput>) => string },
|
|
24
|
+
) {
|
|
25
|
+
const { run, runtime, update } = command;
|
|
26
|
+
const registry = useContext(RegistryContext);
|
|
27
|
+
type Entry = UndoableNotice<TInput, A> & { readonly inverse: Omit<TInput, "expectedVersion"> };
|
|
28
|
+
interface Removal {
|
|
29
|
+
readonly input: TInput;
|
|
30
|
+
readonly inverse: Omit<TInput, "expectedVersion">;
|
|
31
|
+
readonly sequence: number;
|
|
32
|
+
}
|
|
33
|
+
const owner = useMemo(
|
|
34
|
+
() => ({ notice: Atom.make(Option.none<Entry>()), registry, run, runtime, sequence: 0, update }),
|
|
35
|
+
[run, runtime, update, registry],
|
|
36
|
+
);
|
|
37
|
+
const execute = useMemo(
|
|
38
|
+
() => (input: TInput) =>
|
|
39
|
+
Effect.acquireUseRelease(
|
|
40
|
+
owner.update.block,
|
|
41
|
+
() => owner.run(input),
|
|
42
|
+
(release) => Effect.sync(release),
|
|
43
|
+
),
|
|
44
|
+
[owner],
|
|
45
|
+
);
|
|
46
|
+
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]);
|
|
48
|
+
const removal = useAction(owner.runtime, removeRun, {
|
|
49
|
+
concurrent: true,
|
|
50
|
+
onSuccess: (value, { input, inverse, sequence }) => {
|
|
51
|
+
if (sequence === owner.sequence) {
|
|
52
|
+
owner.registry.set(owner.notice, Option.some({ input, inverse, message: options.describe(value, input), value }));
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
const restoration = useAction(owner.runtime, undoRun, {
|
|
57
|
+
onSuccess: (_value, entry) => {
|
|
58
|
+
if (Option.getOrUndefined(owner.registry.get(owner.notice)) === entry) {
|
|
59
|
+
owner.registry.set(owner.notice, Option.none());
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
const notice: Option.Option<UndoableNotice<TInput, A>> = useAtomValue(owner.notice);
|
|
64
|
+
return {
|
|
65
|
+
notice,
|
|
66
|
+
pending: removal.pending || restoration.pending,
|
|
67
|
+
remove: (input: TInput, inverse: Omit<TInput, "expectedVersion">) => {
|
|
68
|
+
owner.sequence += 1;
|
|
69
|
+
removal.dispatch({ input, inverse, sequence: owner.sequence });
|
|
70
|
+
},
|
|
71
|
+
removeResult: removal.result,
|
|
72
|
+
removing: removal.pending,
|
|
73
|
+
undo: () => {
|
|
74
|
+
const current = owner.registry.get(owner.notice);
|
|
75
|
+
if (Option.isSome(current)) {
|
|
76
|
+
restoration.dispatch(current.value);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
undoing: restoration.pending,
|
|
80
|
+
undoResult: restoration.result,
|
|
81
|
+
};
|
|
82
|
+
}
|