@shivaedev/react 0.27.0 → 0.29.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 +45 -0
- package/README.md +67 -43
- package/dist/client/actionCompletion.d.ts.map +1 -1
- package/dist/client/actionCompletion.js +3 -2
- package/dist/client/actionCompletion.js.map +1 -1
- package/dist/client/adapters/SessionGeneration.d.ts +14 -0
- package/dist/client/adapters/SessionGeneration.d.ts.map +1 -0
- package/dist/client/adapters/SessionGeneration.js +68 -0
- package/dist/client/adapters/SessionGeneration.js.map +1 -0
- package/dist/client/adapters/SessionHandoff.d.ts +7 -0
- package/dist/client/adapters/SessionHandoff.d.ts.map +1 -0
- package/dist/client/adapters/SessionHandoff.js +27 -0
- package/dist/client/adapters/SessionHandoff.js.map +1 -0
- package/dist/client/authAction.d.ts +0 -7
- package/dist/client/authAction.d.ts.map +1 -1
- package/dist/client/authAction.js +1 -3
- package/dist/client/authAction.js.map +1 -1
- package/dist/client/authQuery.d.ts +3 -2
- package/dist/client/authQuery.d.ts.map +1 -1
- package/dist/client/authQuery.js +2 -2
- package/dist/client/authQuery.js.map +1 -1
- package/dist/client/authSession.d.ts +12 -0
- package/dist/client/authSession.d.ts.map +1 -0
- package/dist/client/authSession.js +13 -0
- package/dist/client/authSession.js.map +1 -0
- package/dist/client/permissionBoundary.d.ts.map +1 -1
- package/dist/client/permissionBoundary.js +2 -2
- package/dist/client/permissionBoundary.js.map +1 -1
- package/dist/client/resultState.d.ts +1 -1
- package/dist/client/resultState.d.ts.map +1 -1
- package/dist/client/resultState.js +7 -5
- package/dist/client/resultState.js.map +1 -1
- package/dist/client/saveState.d.ts +1 -1
- package/dist/client/saveState.d.ts.map +1 -1
- package/dist/client/saveState.js +3 -3
- package/dist/client/saveState.js.map +1 -1
- package/dist/client/sessionBoundary.d.ts +4 -2
- package/dist/client/sessionBoundary.d.ts.map +1 -1
- package/dist/client/sessionBoundary.js +6 -68
- package/dist/client/sessionBoundary.js.map +1 -1
- package/dist/internal/RecheckContext.d.ts +2 -0
- package/dist/internal/RecheckContext.d.ts.map +1 -0
- package/dist/internal/RecheckContext.js +3 -0
- package/dist/internal/RecheckContext.js.map +1 -0
- package/dist/internal/loadedQuery.d.ts +2 -0
- package/dist/internal/loadedQuery.d.ts.map +1 -1
- package/dist/internal/loadedQuery.js +1 -0
- package/dist/internal/loadedQuery.js.map +1 -1
- package/dist/internal/sessionRecheck.d.ts +1 -0
- package/dist/internal/sessionRecheck.d.ts.map +1 -1
- package/dist/internal/sessionRecheck.js +6 -2
- package/dist/internal/sessionRecheck.js.map +1 -1
- package/dist/internal/usePermissionFailure.d.ts.map +1 -1
- package/dist/internal/usePermissionFailure.js +3 -2
- package/dist/internal/usePermissionFailure.js.map +1 -1
- package/dist/permissionGuard.d.ts +2 -0
- package/dist/permissionGuard.d.ts.map +1 -1
- package/dist/permissionGuard.js +1 -1
- package/dist/permissionGuard.js.map +1 -1
- package/dist/queryRoute.d.ts.map +1 -1
- package/dist/queryRoute.js +3 -2
- package/dist/queryRoute.js.map +1 -1
- package/package.json +10 -10
- package/src/client/actionCompletion.ts +3 -2
- package/src/client/adapters/SessionGeneration.ts +93 -0
- package/src/client/adapters/SessionHandoff.ts +36 -0
- package/src/client/authAction.ts +1 -5
- package/src/client/authQuery.ts +4 -3
- package/src/client/authSession.ts +22 -0
- package/src/client/permissionBoundary.ts +2 -2
- package/src/client/resultState.ts +8 -5
- package/src/client/saveState.ts +3 -3
- package/src/client/sessionBoundary.ts +14 -91
- package/src/internal/RecheckContext.ts +3 -0
- package/src/internal/loadedQuery.ts +2 -0
- package/src/internal/sessionRecheck.ts +7 -2
- package/src/internal/usePermissionFailure.ts +3 -2
- package/src/permissionGuard.ts +3 -2
- package/src/queryRoute.ts +3 -2
- package/dist/resumeSignal.d.ts +0 -6
- package/dist/resumeSignal.d.ts.map +0 -1
- package/dist/resumeSignal.js +0 -5
- package/dist/resumeSignal.js.map +0 -1
- package/src/resumeSignal.ts +0 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.29.0",
|
|
4
4
|
"description": "React query, action, editor and session hooks for native Effect atoms",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"README.md"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@shivaedev/access": "0.3.
|
|
49
|
-
"@shivaedev/contract": "0.
|
|
50
|
-
"@shivaedev/errors": "0.
|
|
51
|
-
"@shivaedev/form": "0.
|
|
48
|
+
"@shivaedev/access": "0.3.3",
|
|
49
|
+
"@shivaedev/contract": "0.29.0",
|
|
50
|
+
"@shivaedev/errors": "0.3.0",
|
|
51
|
+
"@shivaedev/form": "0.10.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@effect/atom-react": "4.0.6",
|
|
@@ -56,14 +56,14 @@
|
|
|
56
56
|
"@effect/sql-pg": "4.0.6",
|
|
57
57
|
"@effect/sql-sqlite-node": "4.0.6",
|
|
58
58
|
"@shivaedev/changes": "0.6.0",
|
|
59
|
-
"@shivaedev/identity": "0.
|
|
60
|
-
"@shivaedev/jobs": "0.
|
|
61
|
-
"@shivaedev/quality": "0.
|
|
62
|
-
"@shivaedev/server": "0.
|
|
59
|
+
"@shivaedev/identity": "0.11.1",
|
|
60
|
+
"@shivaedev/jobs": "0.27.0",
|
|
61
|
+
"@shivaedev/quality": "0.50.0",
|
|
62
|
+
"@shivaedev/server": "0.12.0",
|
|
63
63
|
"@shivaedev/service": "0.6.0",
|
|
64
64
|
"@shivaedev/sql": "0.27.0",
|
|
65
65
|
"@shivaedev/test-sql": "0.15.0",
|
|
66
|
-
"@shivaedev/test-story": "0.
|
|
66
|
+
"@shivaedev/test-story": "0.30.0",
|
|
67
67
|
"@tanstack/react-router": "1.170.46",
|
|
68
68
|
"@types/node": "24.19.2",
|
|
69
69
|
"@types/react": "19.3.0",
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { RegistryContext } from "@effect/atom-react";
|
|
2
|
-
import { Effect
|
|
2
|
+
import { Effect } from "effect";
|
|
3
3
|
import type * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import { useContext, useInsertionEffect, useMemo, useRef } from "react";
|
|
5
5
|
import type { AtomServices } from "#fieldRejection.ts";
|
|
6
6
|
import { NoticeContext } from "#internal/NoticeContext.ts";
|
|
7
7
|
import { publishNotice } from "#internal/noticeStore.ts";
|
|
8
|
+
import { requestsRecheck } from "#internal/sessionRecheck.ts";
|
|
8
9
|
import type { NoticeFeedback } from "#notices.ts";
|
|
9
10
|
import { useSessionRecheck } from "./sessionBoundary.ts";
|
|
10
11
|
|
|
@@ -32,7 +33,7 @@ export function useActionSource<TInput, A, E, R, TRuntimeError>(
|
|
|
32
33
|
const lifetime = { active: false, registry };
|
|
33
34
|
function rejected(error: E, input: TInput) {
|
|
34
35
|
return Effect.gen(function* () {
|
|
35
|
-
if (lifetime.active &&
|
|
36
|
+
if (lifetime.active && requestsRecheck(error)) {
|
|
36
37
|
yield* Effect.sync(() => latestRef.current.recheck());
|
|
37
38
|
}
|
|
38
39
|
yield* Effect.sync(() => {
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { RegistryContext } from "@effect/atom-react";
|
|
2
|
+
import type { Duration } from "effect";
|
|
3
|
+
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
+
import { createElement, type ReactNode, useEffect, useState } from "react";
|
|
5
|
+
import { LiveConnection } from "@shivaedev/contract/automaticLive.ts";
|
|
6
|
+
import { mountLive } from "#client/mountLive.ts";
|
|
7
|
+
import { liveBindings } from "#internal/liveBindings.ts";
|
|
8
|
+
import { RecheckContext } from "#internal/RecheckContext.ts";
|
|
9
|
+
import { registerSessionRecheck } from "#internal/sessionRecheck.ts";
|
|
10
|
+
import { useLatestCallback } from "#internal/useLatestCallback.ts";
|
|
11
|
+
|
|
12
|
+
interface Owned<TClient> {
|
|
13
|
+
readonly client: TClient;
|
|
14
|
+
mounted: number;
|
|
15
|
+
readonly registry: AtomRegistry.AtomRegistry;
|
|
16
|
+
}
|
|
17
|
+
function owning<TClient>(connect: (registry: AtomRegistry.AtomRegistry) => TClient, identity: string | undefined): Owned<TClient> {
|
|
18
|
+
const registry = AtomRegistry.make({ "defaultIdleTTL": 400 });
|
|
19
|
+
let client: { readonly value: TClient } | undefined;
|
|
20
|
+
return {
|
|
21
|
+
get client() {
|
|
22
|
+
client ??= { value: connect(registry) };
|
|
23
|
+
for (const binding of liveBindings(client.value)) {
|
|
24
|
+
const session = binding[LiveConnection].identity;
|
|
25
|
+
if (session !== undefined) {
|
|
26
|
+
registry.set(session, identity);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return client.value;
|
|
30
|
+
},
|
|
31
|
+
mounted: 0,
|
|
32
|
+
registry,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
// No cleanup runs when a hidden <Activity> subtree is unmounted, so nodes a hidden render creates must not outlive it.
|
|
36
|
+
function hidden<TClient>(connect: (registry: AtomRegistry.AtomRegistry) => TClient, identity: string | undefined): Owned<TClient> {
|
|
37
|
+
const owned = owning(connect, identity);
|
|
38
|
+
let reaping = false;
|
|
39
|
+
owned.registry.onNodeAdded = () => {
|
|
40
|
+
if (reaping || owned.mounted > 0) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
reaping = true;
|
|
44
|
+
setTimeout(() => {
|
|
45
|
+
reaping = false;
|
|
46
|
+
if (owned.mounted === 0) {
|
|
47
|
+
owned.registry.reset();
|
|
48
|
+
}
|
|
49
|
+
}, 0);
|
|
50
|
+
};
|
|
51
|
+
return owned;
|
|
52
|
+
}
|
|
53
|
+
interface SessionGenerationProps<TSession, TClient> {
|
|
54
|
+
readonly children: (client: TClient) => ReactNode;
|
|
55
|
+
readonly connect: (session: TSession, registry: AtomRegistry.AtomRegistry) => TClient;
|
|
56
|
+
readonly identity: string | undefined;
|
|
57
|
+
readonly liveCoalesceWindow?: Duration.Input | undefined;
|
|
58
|
+
readonly recheck: () => void;
|
|
59
|
+
readonly session: TSession;
|
|
60
|
+
}
|
|
61
|
+
export function SessionGeneration<TSession, TClient>({
|
|
62
|
+
session,
|
|
63
|
+
connect,
|
|
64
|
+
recheck,
|
|
65
|
+
children,
|
|
66
|
+
identity,
|
|
67
|
+
liveCoalesceWindow,
|
|
68
|
+
}: SessionGenerationProps<TSession, TClient>): ReactNode {
|
|
69
|
+
const recheckLatest = useLatestCallback(recheck);
|
|
70
|
+
const [open] = useState(() => (registry: AtomRegistry.AtomRegistry) => connect(session, registry));
|
|
71
|
+
const [owned, own] = useState(() => owning(open, identity));
|
|
72
|
+
useEffect(() => registerSessionRecheck(owned.registry, recheckLatest), [owned, recheckLatest]);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
owned.mounted += 1;
|
|
75
|
+
const stopLive = mountLive(owned.registry, owned.client, liveCoalesceWindow);
|
|
76
|
+
return () => {
|
|
77
|
+
stopLive();
|
|
78
|
+
owned.mounted -= 1;
|
|
79
|
+
queueMicrotask(() => {
|
|
80
|
+
if (owned.mounted > 0) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
owned.registry.dispose();
|
|
84
|
+
own((current) => (current === owned ? hidden(open, identity) : current));
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
}, [identity, owned, liveCoalesceWindow, open]);
|
|
88
|
+
return createElement(
|
|
89
|
+
RecheckContext.Provider,
|
|
90
|
+
{ value: recheckLatest },
|
|
91
|
+
createElement(RegistryContext.Provider, { value: owned.registry }, children(owned.client)),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ReactNode, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
function safeDestination(target: string): string {
|
|
4
|
+
try {
|
|
5
|
+
const url = new URL(target, window.location.origin);
|
|
6
|
+
return (url.protocol === "http:" || url.protocol === "https:") && url.origin === window.location.origin && !url.pathname.startsWith("//")
|
|
7
|
+
? `${url.pathname}${url.search}${url.hash}`
|
|
8
|
+
: "/";
|
|
9
|
+
} catch {
|
|
10
|
+
return "/";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function replaceLocation(href: string): void {
|
|
15
|
+
window.history.replaceState(null, "", href);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function SessionHandoff({
|
|
19
|
+
children,
|
|
20
|
+
redirectTo,
|
|
21
|
+
navigate = replaceLocation,
|
|
22
|
+
}: {
|
|
23
|
+
readonly children?: ReactNode;
|
|
24
|
+
readonly redirectTo: string | undefined;
|
|
25
|
+
readonly navigate: ((href: string) => void) | undefined;
|
|
26
|
+
}): ReactNode {
|
|
27
|
+
const [ready, setReady] = useState(redirectTo === undefined);
|
|
28
|
+
// The signed-out router must finish unmounting before any history listener sees the destination.
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!ready && redirectTo !== undefined) {
|
|
31
|
+
navigate(safeDestination(redirectTo));
|
|
32
|
+
setReady(true);
|
|
33
|
+
}
|
|
34
|
+
}, [ready, redirectTo, navigate]);
|
|
35
|
+
return ready ? children : null;
|
|
36
|
+
}
|
package/src/client/authAction.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
2
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
3
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
|
|
5
|
-
export class AuthActionUnavailable extends Data.TaggedError("AuthActionUnavailable")<{
|
|
6
|
-
readonly cause: unknown;
|
|
7
|
-
}> {}
|
|
8
|
-
|
|
9
5
|
export interface AuthActionResult<A, E> {
|
|
10
6
|
readonly data: A;
|
|
11
7
|
readonly error: E | null;
|
package/src/client/authQuery.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
4
|
-
import {
|
|
4
|
+
import { AuthUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
5
|
+
import type { AuthActionResult } from "./authAction.ts";
|
|
5
6
|
|
|
6
7
|
export function authQuery<TInput, A, E extends object = never>(
|
|
7
8
|
run: (input: TInput, signal: AbortSignal) => Promise<AuthActionResult<A, E>>,
|
|
8
|
-
): (input: TInput) => Atom.Atom<AsyncResult.AsyncResult<A, E |
|
|
9
|
+
): (input: TInput) => Atom.Atom<AsyncResult.AsyncResult<A, E | AuthUnavailable>> {
|
|
9
10
|
return Atom.family((input: TInput) =>
|
|
10
11
|
Atom.make(
|
|
11
12
|
Effect.tryPromise({
|
|
12
|
-
catch: (cause) => new
|
|
13
|
+
catch: (cause) => new AuthUnavailable({ message: String(cause) }),
|
|
13
14
|
try: (signal) => run(input, signal),
|
|
14
15
|
}).pipe(Effect.flatMap((result) => (result.error === null ? Effect.succeed(result.data) : Effect.fail(result.error)))),
|
|
15
16
|
),
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
4
|
+
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
|
+
import type { AuthUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
6
|
+
import type { AuthActionResult } from "./authAction.ts";
|
|
7
|
+
import { authQuery } from "./authQuery.ts";
|
|
8
|
+
|
|
9
|
+
export interface AuthSession<A, E> {
|
|
10
|
+
readonly atom: Atom.Atom<AsyncResult.AsyncResult<A, E | AuthUnavailable>>;
|
|
11
|
+
readonly refresh: Effect.Effect<A, E | AuthUnavailable, AtomRegistry.AtomRegistry>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function authSession<A, E extends object = never>(read: (signal: AbortSignal) => Promise<AuthActionResult<A, E>>): AuthSession<A, E> {
|
|
15
|
+
const atom = authQuery((_: undefined, signal) => read(signal))(undefined);
|
|
16
|
+
const refresh = Effect.gen(function* () {
|
|
17
|
+
const registry = yield* AtomRegistry.AtomRegistry;
|
|
18
|
+
registry.refresh(atom);
|
|
19
|
+
return yield* AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true });
|
|
20
|
+
});
|
|
21
|
+
return { atom, refresh };
|
|
22
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CatchBoundary } from "@tanstack/react-router";
|
|
2
|
-
import { Predicate } from "effect";
|
|
3
2
|
import { createElement, type ReactNode, useContext } from "react";
|
|
4
3
|
import type { Permission } from "@shivaedev/access/permission.ts";
|
|
4
|
+
import { isForbidden } from "@shivaedev/errors/taxonomy.ts";
|
|
5
5
|
import { PermissionContext } from "#internal/PermissionContext.ts";
|
|
6
6
|
import { can, type PermissionSession } from "#permissions.ts";
|
|
7
7
|
|
|
@@ -18,7 +18,7 @@ export function useCan(): (permission: Permission) => boolean {
|
|
|
18
18
|
|
|
19
19
|
function PermissionFailure({ error }: { readonly error: unknown }): ReactNode {
|
|
20
20
|
const context = useContext(PermissionContext);
|
|
21
|
-
if (!
|
|
21
|
+
if (!isForbidden(error)) {
|
|
22
22
|
throw error;
|
|
23
23
|
}
|
|
24
24
|
return context?.fallback;
|
|
@@ -7,6 +7,7 @@ import { useContext, useEffect, useState } from "react";
|
|
|
7
7
|
import { queryCoalesceWindow } from "@shivaedev/contract/queryCoalesceWindow.ts";
|
|
8
8
|
import type { AtomServices } from "#fieldRejection.ts";
|
|
9
9
|
import { acceptLoaderQuery } from "#internal/queryHandoff.ts";
|
|
10
|
+
import { requestsRecheck } from "#internal/sessionRecheck.ts";
|
|
10
11
|
import { usePermissionFailure } from "#internal/usePermissionFailure.ts";
|
|
11
12
|
import { useQueryNotice } from "#internal/useQueryNotice.ts";
|
|
12
13
|
import type { NoticeDescriptor } from "#notices.ts";
|
|
@@ -29,18 +30,16 @@ function state<A, E>(result: AsyncResult.AsyncResult<A, E>, data = AsyncResult.v
|
|
|
29
30
|
result,
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
|
-
export function isUnauthorized<E>(cause: Cause.Cause<E>): boolean {
|
|
33
|
-
return Option.match(Cause.findErrorOption(cause), { onNone: () => false, onSome: (error) => Predicate.isTagged(error, "Unauthorized") });
|
|
34
|
-
}
|
|
35
33
|
function useRecheckOnUnauthorized<A, E>(result: AsyncResult.AsyncResult<A, E>, enabled = true): void {
|
|
36
34
|
const recheck = useSessionRecheck();
|
|
37
35
|
useEffect(() => {
|
|
38
|
-
if (enabled && AsyncResult.isFailure(result) &&
|
|
36
|
+
if (enabled && AsyncResult.isFailure(result) && Option.exists(Cause.findErrorOption(result.cause), requestsRecheck)) {
|
|
39
37
|
recheck();
|
|
40
38
|
}
|
|
41
39
|
}, [enabled, recheck, result]);
|
|
42
40
|
}
|
|
43
41
|
export interface QueryState<A, E> extends ResultState<A, E> {
|
|
42
|
+
readonly denied: boolean;
|
|
44
43
|
readonly refresh: () => void;
|
|
45
44
|
}
|
|
46
45
|
export interface QueryOptions<E = unknown> {
|
|
@@ -82,7 +81,11 @@ export function useQuery<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>, o
|
|
|
82
81
|
useQueryNotice(result, Option.isSome(data), options.notice);
|
|
83
82
|
const refresh = useAtomRefresh(atom);
|
|
84
83
|
usePermissionFailure(AsyncResult.cause(result));
|
|
85
|
-
return {
|
|
84
|
+
return {
|
|
85
|
+
...state(result, data),
|
|
86
|
+
denied: Option.exists(AsyncResult.cause(result), (cause) => Option.exists(Cause.findErrorOption(cause), Predicate.isTagged("QueryReadDenied"))),
|
|
87
|
+
refresh,
|
|
88
|
+
};
|
|
86
89
|
}
|
|
87
90
|
export interface ActionState<TInput, A, E> extends ResultState<A, E> {
|
|
88
91
|
readonly dispatch: (input: TInput) => void;
|
package/src/client/saveState.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { RegistryContext, useAtomValue } from "@effect/atom-react";
|
|
2
|
-
import {
|
|
2
|
+
import { Cause, Option } from "effect";
|
|
3
3
|
import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
4
4
|
import * as Atom from "effect/reactivity/Atom";
|
|
5
5
|
import { useCallback, useContext, useEffect } from "react";
|
|
6
6
|
import { FieldFailure, type Fields, type Form, Invalid } from "@shivaedev/form/shape.ts";
|
|
7
|
-
import {
|
|
7
|
+
import { requestsRecheck } from "#internal/sessionRecheck.ts";
|
|
8
8
|
import { useSessionRecheck } from "./sessionBoundary.ts";
|
|
9
9
|
|
|
10
10
|
const idle = Atom.make(AsyncResult.initial());
|
|
@@ -26,7 +26,7 @@ export function useSaveState<TFields extends Fields, A, E, TRuntimeError>(
|
|
|
26
26
|
const result: AsyncResult.AsyncResult<A, E | TRuntimeError | FieldFailure | Invalid> = useAtomValue(form?.submit ?? idle);
|
|
27
27
|
const dirty = useAtomValue(form?.dirty ?? clean);
|
|
28
28
|
useEffect(() => {
|
|
29
|
-
if (AsyncResult.isFailure(result) &&
|
|
29
|
+
if (AsyncResult.isFailure(result) && Option.exists(Cause.findErrorOption(result.cause), requestsRecheck)) {
|
|
30
30
|
recheck();
|
|
31
31
|
}
|
|
32
32
|
}, [recheck, result]);
|
|
@@ -1,110 +1,27 @@
|
|
|
1
1
|
import { RegistryContext } from "@effect/atom-react";
|
|
2
2
|
import type { Duration } from "effect";
|
|
3
|
-
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
-
import {
|
|
5
|
-
import { LiveConnection } from "@shivaedev/contract/automaticLive.ts";
|
|
3
|
+
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
+
import { createElement, Fragment, type ReactNode, useContext } from "react";
|
|
6
5
|
import { DraftSessionContext } from "@shivaedev/form/client/react.ts";
|
|
7
|
-
import {
|
|
6
|
+
import { SessionGeneration } from "#client/adapters/SessionGeneration.ts";
|
|
7
|
+
import { SessionHandoff } from "#client/adapters/SessionHandoff.ts";
|
|
8
|
+
import { RecheckContext } from "#internal/RecheckContext.ts";
|
|
8
9
|
import { SignOutRegistryContext } from "#internal/SignOutRegistryContext.ts";
|
|
9
|
-
import { registerSessionRecheck } from "#internal/sessionRecheck.ts";
|
|
10
|
-
import { useLatestCallback } from "#internal/useLatestCallback.ts";
|
|
11
|
-
import { mountLive } from "./mountLive.ts";
|
|
12
10
|
import { useDraftOwner } from "./useDraftOwner.ts";
|
|
13
11
|
export interface SessionBoundaryProps<TSession, TClient> {
|
|
14
12
|
readonly children: (client: TClient) => ReactNode;
|
|
15
13
|
readonly connect: (session: TSession, registry: AtomRegistry.AtomRegistry) => TClient;
|
|
16
14
|
readonly identify: (session: TSession) => string;
|
|
17
15
|
readonly liveCoalesceWindow?: Duration.Input | undefined;
|
|
16
|
+
readonly navigate?: ((href: string) => void) | undefined;
|
|
18
17
|
readonly recheck: () => void;
|
|
18
|
+
readonly redirectTo?: string | undefined;
|
|
19
19
|
readonly session: TSession | undefined;
|
|
20
20
|
readonly signedOut?: ReactNode;
|
|
21
21
|
}
|
|
22
|
-
const RecheckContext = createContext<() => void>((): void => undefined);
|
|
23
22
|
export function useSessionRecheck(): () => void {
|
|
24
23
|
return useContext(RecheckContext);
|
|
25
24
|
}
|
|
26
|
-
interface Owned<TClient> {
|
|
27
|
-
readonly client: TClient;
|
|
28
|
-
mounted: number;
|
|
29
|
-
readonly registry: AtomRegistry.AtomRegistry;
|
|
30
|
-
}
|
|
31
|
-
function owning<TClient>(connect: (registry: AtomRegistry.AtomRegistry) => TClient, identity: string | undefined): Owned<TClient> {
|
|
32
|
-
const registry = AtomRegistry.make({ "defaultIdleTTL": 400 });
|
|
33
|
-
let client: { readonly value: TClient } | undefined;
|
|
34
|
-
return {
|
|
35
|
-
get client() {
|
|
36
|
-
client ??= { value: connect(registry) };
|
|
37
|
-
for (const binding of liveBindings(client.value)) {
|
|
38
|
-
const session = binding[LiveConnection].identity;
|
|
39
|
-
if (session !== undefined) {
|
|
40
|
-
registry.set(session, identity);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return client.value;
|
|
44
|
-
},
|
|
45
|
-
mounted: 0,
|
|
46
|
-
registry,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
// No cleanup runs when a hidden <Activity> subtree is unmounted, so nodes a hidden render creates must not outlive it.
|
|
50
|
-
function hidden<TClient>(connect: (registry: AtomRegistry.AtomRegistry) => TClient, identity: string | undefined): Owned<TClient> {
|
|
51
|
-
const owned = owning(connect, identity);
|
|
52
|
-
let reaping = false;
|
|
53
|
-
owned.registry.onNodeAdded = () => {
|
|
54
|
-
if (reaping || owned.mounted > 0) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
reaping = true;
|
|
58
|
-
setTimeout(() => {
|
|
59
|
-
reaping = false;
|
|
60
|
-
if (owned.mounted === 0) {
|
|
61
|
-
owned.registry.reset();
|
|
62
|
-
}
|
|
63
|
-
}, 0);
|
|
64
|
-
};
|
|
65
|
-
return owned;
|
|
66
|
-
}
|
|
67
|
-
interface GenerationProps<TSession, TClient> {
|
|
68
|
-
readonly children: (client: TClient) => ReactNode;
|
|
69
|
-
readonly connect: (session: TSession, registry: AtomRegistry.AtomRegistry) => TClient;
|
|
70
|
-
readonly identity: string | undefined;
|
|
71
|
-
readonly liveCoalesceWindow?: Duration.Input | undefined;
|
|
72
|
-
readonly recheck: () => void;
|
|
73
|
-
readonly session: TSession;
|
|
74
|
-
}
|
|
75
|
-
function Generation<TSession, TClient>({
|
|
76
|
-
session,
|
|
77
|
-
connect,
|
|
78
|
-
recheck,
|
|
79
|
-
children,
|
|
80
|
-
identity,
|
|
81
|
-
liveCoalesceWindow,
|
|
82
|
-
}: GenerationProps<TSession, TClient>): ReactNode {
|
|
83
|
-
const recheckLatest = useLatestCallback(recheck);
|
|
84
|
-
const [open] = useState(() => (registry: AtomRegistry.AtomRegistry) => connect(session, registry));
|
|
85
|
-
const [owned, own] = useState(() => owning(open, identity));
|
|
86
|
-
useEffect(() => registerSessionRecheck(owned.registry, recheckLatest), [owned, recheckLatest]);
|
|
87
|
-
useEffect(() => {
|
|
88
|
-
owned.mounted += 1;
|
|
89
|
-
const stopLive = mountLive(owned.registry, owned.client, liveCoalesceWindow);
|
|
90
|
-
return () => {
|
|
91
|
-
stopLive();
|
|
92
|
-
owned.mounted -= 1;
|
|
93
|
-
queueMicrotask(() => {
|
|
94
|
-
if (owned.mounted > 0) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
owned.registry.dispose();
|
|
98
|
-
own((current) => (current === owned ? hidden(open, identity) : current));
|
|
99
|
-
});
|
|
100
|
-
};
|
|
101
|
-
}, [identity, owned, liveCoalesceWindow, open]);
|
|
102
|
-
return createElement(
|
|
103
|
-
RecheckContext.Provider,
|
|
104
|
-
{ value: recheckLatest },
|
|
105
|
-
createElement(RegistryContext.Provider, { value: owned.registry }, children(owned.client)),
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
25
|
export function SessionBoundary<TSession, TClient>({
|
|
109
26
|
session,
|
|
110
27
|
liveCoalesceWindow,
|
|
@@ -112,6 +29,8 @@ export function SessionBoundary<TSession, TClient>({
|
|
|
112
29
|
connect,
|
|
113
30
|
recheck,
|
|
114
31
|
signedOut,
|
|
32
|
+
redirectTo,
|
|
33
|
+
navigate,
|
|
115
34
|
children,
|
|
116
35
|
}: SessionBoundaryProps<TSession, TClient>): ReactNode {
|
|
117
36
|
const registry = useContext(RegistryContext);
|
|
@@ -125,7 +44,11 @@ export function SessionBoundary<TSession, TClient>({
|
|
|
125
44
|
createElement(
|
|
126
45
|
DraftSessionContext.Provider,
|
|
127
46
|
{ value: current },
|
|
128
|
-
createElement(
|
|
47
|
+
createElement(
|
|
48
|
+
SessionHandoff,
|
|
49
|
+
{ key: id, navigate, redirectTo },
|
|
50
|
+
createElement(SessionGeneration<TSession, TClient>, { children, connect, identity: id, liveCoalesceWindow, recheck, session }),
|
|
51
|
+
),
|
|
129
52
|
),
|
|
130
53
|
);
|
|
131
54
|
}
|
|
@@ -3,6 +3,7 @@ import { type Cause, Option } from "effect";
|
|
|
3
3
|
interface QuerySnapshot<A, E> {
|
|
4
4
|
readonly cause: Option.Option<Cause.Cause<E>>;
|
|
5
5
|
readonly data: Option.Option<A>;
|
|
6
|
+
readonly denied: boolean;
|
|
6
7
|
readonly pending: boolean;
|
|
7
8
|
readonly refresh: () => void;
|
|
8
9
|
}
|
|
@@ -11,6 +12,7 @@ export function loadedQuery<A, E>(query: QuerySnapshot<A, E>, loaded: A) {
|
|
|
11
12
|
return {
|
|
12
13
|
cause: query.cause,
|
|
13
14
|
data: Option.getOrElse(query.data, () => loaded),
|
|
15
|
+
denied: query.denied,
|
|
14
16
|
pending: query.pending,
|
|
15
17
|
refresh: query.refresh,
|
|
16
18
|
refreshing: query.pending,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Predicate } from "effect";
|
|
2
1
|
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
2
|
+
import { RpcClientError } from "effect/rpc/RpcClientError";
|
|
3
|
+
import { isUnauthorized } from "@shivaedev/errors/taxonomy.ts";
|
|
3
4
|
|
|
4
5
|
const rechecks = new WeakMap<AtomRegistry.AtomRegistry, () => void>();
|
|
5
6
|
|
|
@@ -10,8 +11,12 @@ export function registerSessionRecheck(registry: AtomRegistry.AtomRegistry, rech
|
|
|
10
11
|
};
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
export function requestsRecheck(error: unknown, connection = false): boolean {
|
|
15
|
+
return isUnauthorized(error) || (connection && error instanceof RpcClientError);
|
|
16
|
+
}
|
|
17
|
+
|
|
13
18
|
export async function recheckUnauthorized(registry: AtomRegistry.AtomRegistry, error: unknown, connection = false): Promise<void> {
|
|
14
|
-
if (
|
|
19
|
+
if (requestsRecheck(error, connection)) {
|
|
15
20
|
await rechecks.get(registry)?.();
|
|
16
21
|
}
|
|
17
22
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { Cause, Option
|
|
1
|
+
import { Cause, Option } from "effect";
|
|
2
2
|
import { useContext } from "react";
|
|
3
|
+
import { isForbidden } from "@shivaedev/errors/taxonomy.ts";
|
|
3
4
|
import { PermissionContext } from "./PermissionContext.ts";
|
|
4
5
|
|
|
5
6
|
export function usePermissionFailure<E>(cause: Option.Option<Cause.Cause<E>>): void {
|
|
6
7
|
const context = useContext(PermissionContext);
|
|
7
8
|
if (context !== undefined) {
|
|
8
9
|
const failure = Option.flatMap(cause, Cause.findErrorOption);
|
|
9
|
-
if (Option.isSome(failure) &&
|
|
10
|
+
if (Option.isSome(failure) && isForbidden(failure.value)) {
|
|
10
11
|
throw failure.value;
|
|
11
12
|
}
|
|
12
13
|
}
|
package/src/permissionGuard.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { type SignInLocation, signInSearch } from "./signInSearch.ts";
|
|
|
7
7
|
declare module "@tanstack/react-router" {
|
|
8
8
|
interface StaticDataRouteOption {
|
|
9
9
|
readonly permission?: Permission;
|
|
10
|
+
readonly signedIn?: boolean;
|
|
10
11
|
}
|
|
11
12
|
}
|
|
12
13
|
|
|
@@ -17,13 +18,13 @@ export interface PermissionGuardOptions {
|
|
|
17
18
|
|
|
18
19
|
export interface PermissionGuardContext {
|
|
19
20
|
readonly location: SignInLocation;
|
|
20
|
-
readonly matches: readonly { readonly staticData: { readonly permission?: Permission } }[];
|
|
21
|
+
readonly matches: readonly { readonly staticData: { readonly permission?: Permission; readonly signedIn?: boolean } }[];
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export function permissionGuard({ session, signIn }: PermissionGuardOptions): (context: PermissionGuardContext) => void {
|
|
24
25
|
return ({ matches, location }) => {
|
|
25
26
|
const required = matches.flatMap((match) => (match.staticData.permission === undefined ? [] : [match.staticData.permission]));
|
|
26
|
-
if (required.length === 0) {
|
|
27
|
+
if (required.length === 0 && !matches.some((match) => match.staticData.signedIn === true)) {
|
|
27
28
|
return;
|
|
28
29
|
}
|
|
29
30
|
const current = session();
|
package/src/queryRoute.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Predicate } from "effect";
|
|
|
3
3
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
4
4
|
import type * as Atom from "effect/reactivity/Atom";
|
|
5
5
|
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
6
|
+
import { isNotFound } from "@shivaedev/errors/taxonomy.ts";
|
|
6
7
|
import { loadQuery } from "#adapters/loadQuery.ts";
|
|
7
8
|
|
|
8
9
|
export function queryRoute<
|
|
@@ -23,8 +24,8 @@ export function queryRoute<
|
|
|
23
24
|
try {
|
|
24
25
|
return await loadQuery(context.context.registry, query({ context: context.context, params: context.params, search: context.deps }), context);
|
|
25
26
|
} catch (error) {
|
|
26
|
-
if (Predicate.isTagged(error, "
|
|
27
|
-
throw notFound();
|
|
27
|
+
if (isNotFound(error) || Predicate.isTagged(error, "QueryReadDenied")) {
|
|
28
|
+
throw notFound({ data: error });
|
|
28
29
|
}
|
|
29
30
|
throw error;
|
|
30
31
|
}
|
package/dist/resumeSignal.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { type ResumeOptions as ContractOptions, type ResumeSource as ContractSource, type ResumeWindow as ContractWindow } from "@shivaedev/contract/resume.ts";
|
|
2
|
-
export type ResumeSource = ContractSource;
|
|
3
|
-
export type ResumeWindow = ContractWindow;
|
|
4
|
-
export type ResumeOptions = ContractOptions;
|
|
5
|
-
export declare function resumeSignal(options: ResumeOptions): import("effect/reactivity/Atom").Atom<number>;
|
|
6
|
-
//# sourceMappingURL=resumeSignal.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resumeSignal.d.ts","sourceRoot":"","sources":["../src/resumeSignal.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,aAAa,IAAI,eAAe,EACrC,KAAK,YAAY,IAAI,cAAc,EACnC,KAAK,YAAY,IAAI,cAAc,EAEnC,MAAM,+BAA+B,CAAC;AAEvC,MAAM,MAAM,YAAY,GAAG,cAAc,CAAC;AAC1C,MAAM,MAAM,YAAY,GAAG,cAAc,CAAC;AAC1C,MAAM,MAAM,aAAa,GAAG,eAAe,CAAC;AAC5C,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,iDAElD"}
|
package/dist/resumeSignal.js
DELETED
package/dist/resumeSignal.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resumeSignal.js","sourceRoot":"","sources":["../src/resumeSignal.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,YAAY,IAAI,oBAAoB,GACpC,MAAM,+BAA+B,CAAC;AAKvC,MAAM,UAAU,YAAY,CAAC,OAAsB;IAClD,OAAO,oBAAoB,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC"}
|
package/src/resumeSignal.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type ResumeOptions as ContractOptions,
|
|
3
|
-
type ResumeSource as ContractSource,
|
|
4
|
-
type ResumeWindow as ContractWindow,
|
|
5
|
-
resumeSignal as contractResumeSignal,
|
|
6
|
-
} from "@shivaedev/contract/resume.ts";
|
|
7
|
-
|
|
8
|
-
export type ResumeSource = ContractSource;
|
|
9
|
-
export type ResumeWindow = ContractWindow;
|
|
10
|
-
export type ResumeOptions = ContractOptions;
|
|
11
|
-
export function resumeSignal(options: ResumeOptions) {
|
|
12
|
-
return contractResumeSignal(options);
|
|
13
|
-
}
|