@shivaedev/react 0.51.0 → 0.52.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 +25 -2
- package/dist/adapters/permissionBoundary.d.ts.map +1 -1
- package/dist/adapters/permissionBoundary.js +21 -2
- package/dist/adapters/permissionBoundary.js.map +1 -1
- package/dist/client/adapters/SessionGeneration.d.ts +3 -1
- package/dist/client/adapters/SessionGeneration.d.ts.map +1 -1
- package/dist/client/adapters/SessionGeneration.js +4 -1
- package/dist/client/adapters/SessionGeneration.js.map +1 -1
- package/dist/client/adapters/useWarmQueries.d.ts +4 -0
- package/dist/client/adapters/useWarmQueries.d.ts.map +1 -0
- package/dist/client/adapters/useWarmQueries.js +36 -0
- package/dist/client/adapters/useWarmQueries.js.map +1 -0
- package/dist/client/authSession.d.ts +10 -2
- package/dist/client/authSession.d.ts.map +1 -1
- package/dist/client/authSession.js +32 -3
- package/dist/client/authSession.js.map +1 -1
- package/dist/client/sessionBoundary.d.ts +2 -1
- package/dist/client/sessionBoundary.d.ts.map +1 -1
- package/dist/client/sessionBoundary.js +2 -1
- package/dist/client/sessionBoundary.js.map +1 -1
- package/package.json +7 -7
- package/src/adapters/permissionBoundary.ts +23 -2
- package/src/client/adapters/SessionGeneration.ts +6 -0
- package/src/client/adapters/useWarmQueries.ts +39 -0
- package/src/client/authSession.ts +57 -4
- package/src/client/sessionBoundary.ts +3 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.52.0 - 2026-11-20
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- `authSession(read, { remember: browserSessionMemory })` answers at once with the remembered session, marked as refreshing, and checks the session in the background, so `SessionBoundary` renders and the live socket opens while the check is on the wire. The same user keeps the generation with refreshed permissions, another user starts a new one, a signed-out answer wipes the memory before the signed-out screen renders, a failed check keeps the remembered session rendering, and past the remembered expiry the launch waits for the check. With `remember`, the session's value is `RememberedSession | null`.
|
|
8
|
+
- with `remember`, the session has `signOut(request)`, which wipes the memory as soon as the sign-out request succeeds, so a relaunch starts without a remembered session even when the session check after the sign-out never answers.
|
|
9
|
+
- `SessionBoundary` takes `warm`, a function of the session's client that returns bound query atoms, such as `warm={(client) => [client.water.day.query({ date }), client.notes.list.query()]}`. Each listed query is mounted in the session's registry when the generation commits and stays subscribed until sign-out, another user or unmount, so a screen that reads it later renders its current value in its first render, also after a long idle stretch. The list is read again on every render and compared by atom identity: a changed payload opens the new query and hands the old one to retention. A warm query that no screen reads does not hold back `useFirstSettled`. Each listed query holds one subscription on the host for the whole session. Delete components that only mounted queries to keep them warm and list those queries instead.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- `PermissionBoundary` runs the router's guards again when the grants of its session change without a navigation, so a screen that needs a removed grant is replaced by the refusal.
|
|
14
|
+
|
|
3
15
|
## 0.51.0 - 2026-11-20
|
|
4
16
|
|
|
5
17
|
### Added
|
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ An **editor** joins a query atom, a form field schema and a save Effect. The que
|
|
|
26
26
|
|
|
27
27
|
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.
|
|
28
28
|
|
|
29
|
-
Use `useFirstSettled(client)` from `@shivaedev/react/client/adapters/useFirstSettled.ts` with the same client to reveal a session's first screen in one step instead of one loading state per query. It returns `false` until a query mounted on that client has delivered its first success or failure and, with that, no mounted query awaits a first result and no command is in flight. From then on it returns `true` for that client: a later command or a newly mounted query does not turn it back. The signal belongs to the client, so a new session generation whose `connect` makes a new client starts at `false` again. A client whose screen mounts no query stays at `false`; gate only screens that read through the client.
|
|
29
|
+
Use `useFirstSettled(client)` from `@shivaedev/react/client/adapters/useFirstSettled.ts` with the same client to reveal a session's first screen in one step instead of one loading state per query. It returns `false` until a query mounted on that client has delivered its first success or failure and, with that, no mounted query awaits a first result and no command is in flight. From then on it returns `true` for that client: a later command or a newly mounted query does not turn it back. The signal belongs to the client, so a new session generation whose `connect` makes a new client starts at `false` again. A client whose screen mounts no query stays at `false`; gate only screens that read through the client. A query that only the session's [warm list](#keep-queries-warm-for-the-session) mounts does not hold it back while it awaits its first result.
|
|
30
30
|
|
|
31
31
|
A **session generation** is the string returned by `identify(session)`. `SessionBoundary` owns its client, registry and React subtree. Your auth owner supplies the session and decides when to replace or end it. The boundary automatically mounts live connections on bound contract APIs returned by `connect`, directly or as client properties such as `api`. Use a Platform database host and the default shared WebSocket client; the application needs no subscribe handler or `live()` call. The process holding the socket reloads the session and runs the read policy and handler for each subscriber independently. It stores one encoded-result hash per subscribed query in memory and pushes changed values; equal reads acknowledge completion without replacing the screen. Each subscription has at most one rerun in flight and one pending, merging further changes while busy. Other subscriptions run independently in parallel. Backlog overflow triggers a server `Resync`: the server reruns queries itself, and the client sends no follow-up reads. Query results stay in their native registry; RPC declarations and transport stay with native Effect and the application; this package supplies their React state and lifecycle boundary.
|
|
32
32
|
|
|
@@ -88,6 +88,27 @@ Pass the signed-out router as `signedOut` and the requested continuation address
|
|
|
88
88
|
|
|
89
89
|
A query that fails with an `Unauthorized` from `@shivaedev/errors/taxonomy.ts` asks `recheck` to run and keeps its retained data. An action does the same once per failed result, retaining its last successful `data` and the typed failure in `cause`; rerendering that result does not repeat the recheck. A live reconnect requests a check only for a typed `Unauthorized` authentication refusal; dropped sockets and unreachable servers leave the session alone. Pass the auth owner’s session-refresh Effect as `recheck` (for example, `recheck={auth.session.refresh}`). The boundary shares an in-flight check, retries failures after 1, 2, 4 seconds and so on up to 30 seconds, and retries immediately on visible `online` or `visibilitychange` events while a check is pending, restarting an in-flight check too. Session replacement and unmount cancel retries. Supply `runtime` on `SessionBoundary` to run these retries on a native atom runtime, including one carrying a story’s controlled Clock. Existing void callbacks remain supported; they keep their own check and retry policy. Keep retained session data on screen when a refresh exposes a failure, and pass `session={undefined}` only after a definite signed-out answer. The auth owner decides whether the session ends. The boundary also handles React StrictMode's effect replay and preserves component state across hiding and revealing an `<Activity>` subtree, supplying a live registry when revealed.
|
|
90
90
|
|
|
91
|
+
#### Keep queries warm for the session
|
|
92
|
+
|
|
93
|
+
A screen's first visit, and a return after retention let its result go, wait one subscription round trip. Name the queries whose screens should open with current data in `SessionBoundary`'s `warm`, a function of the session's client that returns bound query atoms:
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
<SessionBoundary
|
|
97
|
+
session={session}
|
|
98
|
+
identify={(current) => current.user.id}
|
|
99
|
+
connect={connect}
|
|
100
|
+
recheck={recheck}
|
|
101
|
+
warm={(client) => [client.water.day.query({ date }), client.notes.list.query()]}
|
|
102
|
+
signedOut={<SignIn />}
|
|
103
|
+
>
|
|
104
|
+
{(client) => <Screens client={client} />}
|
|
105
|
+
</SessionBoundary>;
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`connect`, `date`, `SignIn` and `Screens` belong to the application; `date` is its own value, for example derived from `useNow`. The boundary mounts each listed query in the session's registry when the generation commits and keeps it subscribed until the session ends: sign-out, another user or unmount. The host pushes its changes, so a screen that reads it later renders the current value in its first render, with no request of its own. The list is read again on every render and compared by atom identity, and bound query atoms are cached per payload: when `date` changes, the new day's query opens and the previous one is handed to retention like a screen that closed. A warm query that no screen reads does not hold back `useFirstSettled`.
|
|
109
|
+
|
|
110
|
+
Each warm query holds one subscription on the host for the whole session, with its stored result hash and a rerun on each matching change, whether a screen shows it or not. The list must name the payload the screen reads: a query whose payload no screen asks for subscribes for nothing, and that screen stays cold.
|
|
111
|
+
|
|
91
112
|
#### Session-scoped bindings
|
|
92
113
|
|
|
93
114
|
A contract declared with `keyScope: "session"` binds to the registry that owns the session's queries. In the browser, `bind` prefixes every client read and invalidation key of that contract with an opaque id of its registry, so a successful command in one session refreshes none of another session's queries on the page, even when both registries share one runtime. The server adds no prefix: its subscriptions and committed writes use plain keys, and each query's read policy decides what a viewer receives. The [contract guide](https://github.com/ShivaeDev/platform/blob/main/packages/contract/README.md#session-owned-keys) describes the declaration and its handlers. `SessionBoundary` passes that registry to `connect` as its second argument:
|
|
@@ -877,6 +898,8 @@ const showNotes = can(auth.session, ReadNotes);
|
|
|
877
898
|
|
|
878
899
|
`auth` owns the current session and passes it to `App`; `SignIn` updates that session before invoking `onSignedIn`. Use the session's registry for router context and React, as with other query routes. Importing `permissionGuard` augments TanStack's `StaticDataRouteOption`; route options and their inference remain native. The guard returns `void` explicitly, so `beforeLoad` needs no return annotation, including when it reads the session from route context.
|
|
879
900
|
|
|
901
|
+
`PermissionBoundary` runs the guards again when the grants of its `session` change without a navigation, such as a remembered session whose check answers with fewer grants, so a screen that needs a removed grant is replaced by the refusal. The application writes nothing for it; the boundary invalidates the router it renders inside.
|
|
902
|
+
|
|
880
903
|
`InnerWrap` puts `PermissionBoundary` inside the router, and the boundary's fallback can render `Link` and read route context. TanStack types `InnerWrap` as a component that returns an element, so `PermissionShell` leaves its return type to inference; annotated as returning `ReactNode`, it does not compile. Set `defaultErrorComponent: PermissionBoundaryError` so route and loader failures reach the boundary's fallback while TanStack keeps its navigation controller mounted. This lets a refused viewer follow the fallback link without reloading the page. Keep `disableGlobalCatchBoundary: true`; other errors propagate to an enclosing application boundary. A route-specific `errorComponent` owns any failures it consumes.
|
|
881
904
|
|
|
882
905
|
`Forbidden` from `@shivaedev/react/adapters/Forbidden.ts` supplies the no-access heading, explanation and a router link. Set `to` to a route the viewer may open. Optional `title`, `children`, `backLabel` and `className` let the application choose wording and layout. The default heading is “Permission denied” and the link says “Go back”.
|
|
@@ -906,7 +929,7 @@ function SignInButton({ email, password }: { email: string; password: string })
|
|
|
906
929
|
|
|
907
930
|
The example uses `authAction`, identity's `authCall`, `useAction`, `effect/reactivity/AsyncResult` and `effect/Option`, with an application-owned `authClient`. Identity owns the Promise bridge and its typed failures; see [Better Auth client adapters](https://github.com/ShivaeDev/platform/blob/main/packages/identity/README.md#better-auth-client-adapters). For a form's Effect-based `onSubmit`, pass the function returned by `authCall` directly.
|
|
908
931
|
|
|
909
|
-
Use `authSession` from `@shivaedev/react/client/authSession.ts` to wrap identity's session Effect. It returns a native `atom` for `useQuery` and a `refresh` Effect that refreshes that same atom and waits through its loading state. Both expose typed `user.permissions` alongside the provider's existing session and user fields, using the `PermissionSession` schema from `@shivaedev/identity/PermissionSession.ts`. Applications do not decode `get-session` themselves. Missing grants stay optional; malformed grants fail with `AuthUnavailable`. The reader must return a nullable session with a `user` object; use `authQuery` for other payload shapes. Supply Better Auth's public `getSession` call with `disableCookieCache: true` so a mutation reads current user data:
|
|
932
|
+
Use `authSession` from `@shivaedev/react/client/authSession.ts` to wrap identity's session Effect. It returns a native `atom` for `useQuery` and a `refresh` Effect that refreshes that same atom and waits through its loading state. Both expose typed `user.permissions` alongside the provider's existing session and user fields, using the `PermissionSession` schema from `@shivaedev/identity/PermissionSession.ts`. Applications do not decode `get-session` themselves. Missing grants stay optional; malformed grants fail with `AuthUnavailable`. The reader must return a nullable session with a `user` object; use `authQuery` for other payload shapes. Pass `{ remember: browserSessionMemory }` as the second argument to start a returning person's app from the last verified session while the check runs; see [identity's remembered session](https://github.com/ShivaeDev/platform/blob/main/packages/identity/README.md#start-from-the-last-verified-session) for what it keeps and what it means. With `remember`, the session also has `signOut(request)`, which wipes the memory when the sign-out request succeeds. Supply Better Auth's public `getSession` call with `disableCookieCache: true` so a mutation reads current user data:
|
|
910
933
|
|
|
911
934
|
```tsx
|
|
912
935
|
import { Effect } from "effect";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissionBoundary.d.ts","sourceRoot":"","sources":["../../src/adapters/permissionBoundary.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,KAAK,SAAS,
|
|
1
|
+
{"version":3,"file":"permissionBoundary.d.ts","sourceRoot":"","sources":["../../src/adapters/permissionBoundary.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,KAAK,SAAS,EAAiC,MAAM,OAAO,CAAC;AACrF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAElE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0CAA0C,CAAC;AAIlF,MAAM,WAAW,uBAAuB;IACvC,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI,GAAG,SAAS,CAAC;CACvD;AAED,wBAAgB,MAAM,IAAI,CAAC,UAAU,EAAE,UAAU,KAAK,OAAO,CAG5D;AAED,wBAAgB,uBAAuB,CAAC,EAAE,KAAK,EAAE,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CAMzF;AAsBD,wBAAgB,kBAAkB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,uBAAuB,GAAG,SAAS,CAOtG"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CatchBoundary } from "@tanstack/react-router";
|
|
2
|
-
import { createElement, useContext } from "react";
|
|
1
|
+
import { CatchBoundary, useRouter } from "@tanstack/react-router";
|
|
2
|
+
import { createElement, useContext, useEffect, useRef } from "react";
|
|
3
3
|
import { isForbidden } from "@shivaedev/errors/taxonomy.ts";
|
|
4
4
|
import { PermissionContext } from "#adapters/PermissionContext.ts";
|
|
5
5
|
import { can } from "#can.ts";
|
|
@@ -14,7 +14,26 @@ export function PermissionBoundaryError({ error }) {
|
|
|
14
14
|
}
|
|
15
15
|
return context?.fallback;
|
|
16
16
|
}
|
|
17
|
+
function grantKey(session) {
|
|
18
|
+
return (session?.user.permissions ?? [])
|
|
19
|
+
.map((grant) => `${grant.name}:${grant.strength}`)
|
|
20
|
+
.sort()
|
|
21
|
+
.join(",");
|
|
22
|
+
}
|
|
23
|
+
// Route guards read the session when a navigation loads, so a session refreshed in place reruns them here.
|
|
24
|
+
function useGuardRerun(session) {
|
|
25
|
+
const router = useRouter({ warn: false });
|
|
26
|
+
const grants = grantKey(session);
|
|
27
|
+
const guardedRef = useRef(grants);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (guardedRef.current !== grants) {
|
|
30
|
+
guardedRef.current = grants;
|
|
31
|
+
void router?.invalidate();
|
|
32
|
+
}
|
|
33
|
+
}, [router, grants]);
|
|
34
|
+
}
|
|
17
35
|
export function PermissionBoundary({ children, fallback, session }) {
|
|
36
|
+
useGuardRerun(session);
|
|
18
37
|
return createElement(PermissionContext.Provider, { value: { fallback, session } }, createElement(CatchBoundary, { children, errorComponent: PermissionBoundaryError, getResetKey: () => "permission" }));
|
|
19
38
|
}
|
|
20
39
|
//# sourceMappingURL=permissionBoundary.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"permissionBoundary.js","sourceRoot":"","sources":["../../src/adapters/permissionBoundary.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"permissionBoundary.js","sourceRoot":"","sources":["../../src/adapters/permissionBoundary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,aAAa,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAClF,OAAO,EAAE,aAAa,EAAkB,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAErF,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AA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F,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AACtB,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAA2B;IAC1F,aAAa,CAAC,OAAO,CAAC,CAAC;IACvB,OAAO,aAAa,CACnB,iBAAiB,CAAC,QAAQ,EAC1B,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,EAChC,aAAa,CAAC,aAAa,EAAE,EAAE,QAAQ,EAAE,cAAc,EAAE,uBAAuB,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,YAAY,EAAE,CAAC,CACpH,CAAC;AACH,CAAC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
1
2
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
2
3
|
import { type ReactNode } from "react";
|
|
3
4
|
import { type QueryRetentionOptions } from "@shivaedev/contract/queryRetention.ts";
|
|
@@ -8,7 +9,8 @@ interface SessionGenerationProps<TSession, TClient> {
|
|
|
8
9
|
readonly queryRetention?: QueryRetentionOptions | undefined;
|
|
9
10
|
readonly recheck: () => void;
|
|
10
11
|
readonly session: TSession;
|
|
12
|
+
readonly warm?: ((client: TClient) => readonly Atom.Atom<unknown>[]) | undefined;
|
|
11
13
|
}
|
|
12
|
-
export declare function SessionGeneration<TSession, TClient>({ session, connect, recheck, children, identity, queryRetention: retention, }: SessionGenerationProps<TSession, TClient>): ReactNode;
|
|
14
|
+
export declare function SessionGeneration<TSession, TClient>({ session, connect, recheck, children, identity, queryRetention: retention, warm, }: SessionGenerationProps<TSession, TClient>): ReactNode;
|
|
13
15
|
export {};
|
|
14
16
|
//# sourceMappingURL=SessionGeneration.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SessionGeneration.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/SessionGeneration.ts"],"names":[],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GACJ,EAAE,sBAAsB,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,SAAS,CA8BvD"}
|
|
@@ -7,6 +7,7 @@ import { liveBindings } from "@shivaedev/contract/composeBindings.ts";
|
|
|
7
7
|
import { queryRetention } from "@shivaedev/contract/queryRetention.ts";
|
|
8
8
|
import { registerSessionRecheck } from "#adapters/sessionRecheck.ts";
|
|
9
9
|
import { useLatestCallback } from "#adapters/useLatestCallback.ts";
|
|
10
|
+
import { useWarmQueries } from "#client/adapters/useWarmQueries.ts";
|
|
10
11
|
import { mountLive } from "#client/mountLive.ts";
|
|
11
12
|
import { RecheckContext } from "#client/RecheckContext.ts";
|
|
12
13
|
function owning(connection, identity, retention) {
|
|
@@ -47,7 +48,8 @@ function hidden(connection, identity, retention) {
|
|
|
47
48
|
};
|
|
48
49
|
return owned;
|
|
49
50
|
}
|
|
50
|
-
|
|
51
|
+
const NOTHING_WARM = [];
|
|
52
|
+
export function SessionGeneration({ session, connect, recheck, children, identity, queryRetention: retention, warm, }) {
|
|
51
53
|
const recheckLatest = useLatestCallback(recheck);
|
|
52
54
|
const [connection] = useState(() => ({ connect, session }));
|
|
53
55
|
const [owned, own] = useState(() => owning(connection, identity, retention));
|
|
@@ -71,6 +73,7 @@ export function SessionGeneration({ session, connect, recheck, children, identit
|
|
|
71
73
|
});
|
|
72
74
|
};
|
|
73
75
|
}, [identity, owned, connection]);
|
|
76
|
+
useWarmQueries(owned.registry, warm?.(owned.client) ?? NOTHING_WARM);
|
|
74
77
|
return createElement(RecheckContext.Provider, { value: recheckLatest }, createElement(RegistryContext.Provider, { value: owned.registry }, children(owned.client)));
|
|
75
78
|
}
|
|
76
79
|
//# sourceMappingURL=SessionGeneration.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SessionGeneration.js","sourceRoot":"","sources":["../../../src/client/adapters/SessionGeneration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAC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CAAC,CAAC,MAAM,CAAC,UAAU,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;YAC5F,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC;IACH,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC;IAClC,cAAc,CAAC,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,YAAY,CAAC,CAAC;IACrE,OAAO,aAAa,CACnB,cAAc,CAAC,QAAQ,EACvB,EAAE,KAAK,EAAE,aAAa,EAAE,EACxB,aAAa,CAAC,eAAe,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE,EAAE,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAC1F,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
2
|
+
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
3
|
+
export declare function useWarmQueries(registry: AtomRegistry.AtomRegistry, queries: readonly Atom.Atom<unknown>[]): void;
|
|
4
|
+
//# sourceMappingURL=useWarmQueries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWarmQueries.d.ts","sourceRoot":"","sources":["../../../src/client/adapters/useWarmQueries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,IAAI,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,KAAK,YAAY,MAAM,gCAAgC,CAAC;AAiCpE,wBAAgB,cAAc,CAAC,QAAQ,EAAE,YAAY,CAAC,YAAY,EAAE,OAAO,EAAE,SAAS,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,QAIzG"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useEffect, useMemo } from "react";
|
|
2
|
+
import { warmQueries } from "@shivaedev/contract/warmQueries.ts";
|
|
3
|
+
function warmMounts(registry) {
|
|
4
|
+
const mounted = new Map();
|
|
5
|
+
return {
|
|
6
|
+
follow(queries) {
|
|
7
|
+
const listed = new Set(queries);
|
|
8
|
+
if (listed.size !== mounted.size || queries.some((query) => !mounted.has(query))) {
|
|
9
|
+
registry.set(warmQueries, listed);
|
|
10
|
+
}
|
|
11
|
+
for (const query of listed) {
|
|
12
|
+
if (!mounted.has(query)) {
|
|
13
|
+
mounted.set(query, registry.mount(query));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
for (const [query, unmount] of mounted) {
|
|
17
|
+
if (!listed.has(query)) {
|
|
18
|
+
mounted.delete(query);
|
|
19
|
+
unmount();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
release() {
|
|
24
|
+
for (const unmount of mounted.values()) {
|
|
25
|
+
unmount();
|
|
26
|
+
}
|
|
27
|
+
mounted.clear();
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export function useWarmQueries(registry, queries) {
|
|
32
|
+
const warm = useMemo(() => warmMounts(registry), [registry]);
|
|
33
|
+
useEffect(() => () => warm.release(), [warm]);
|
|
34
|
+
useEffect(() => warm.follow(queries), [warm, queries]);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=useWarmQueries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWarmQueries.js","sourceRoot":"","sources":["../../../src/client/adapters/useWarmQueries.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAC;AAEjE,SAAS,UAAU,CAAC,QAAmC;IACtD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAkC,CAAC;IAC1D,OAAO;QACN,MAAM,CAAC,OAAsC;YAC5C,MAAM,MAAM,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;IAC9C,SAAS,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;AACxD,CAAC"}
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
|
-
import
|
|
1
|
+
import { Effect, type Layer } from "effect";
|
|
2
|
+
import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
|
+
import { type RememberedSession, type SessionMemory, type VerifiedSession } from "@shivaedev/identity/client/SessionMemory.ts";
|
|
5
6
|
export interface AuthSession<A, E> {
|
|
6
7
|
readonly atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
|
|
7
8
|
readonly refresh: Effect.Effect<A, E, AtomRegistry.AtomRegistry>;
|
|
8
9
|
}
|
|
10
|
+
export interface RememberedAuthSession<E> extends AuthSession<RememberedSession | null, E> {
|
|
11
|
+
readonly signOut: <A, TError, R>(request: Effect.Effect<A, TError, R>) => Effect.Effect<A, TError, R>;
|
|
12
|
+
}
|
|
13
|
+
export interface AuthSessionOptions {
|
|
14
|
+
readonly remember: Layer.Layer<SessionMemory>;
|
|
15
|
+
}
|
|
9
16
|
export declare function authSession<A, E>(read: Effect.Effect<A, E>): AuthSession<A, E>;
|
|
17
|
+
export declare function authSession<E>(read: Effect.Effect<VerifiedSession | null, E>, options: AuthSessionOptions): RememberedAuthSession<E>;
|
|
10
18
|
//# sourceMappingURL=authSession.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authSession.d.ts","sourceRoot":"","sources":["../../src/client/authSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"authSession.d.ts","sourceRoot":"","sources":["../../src/client/authSession.ts"],"names":[],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AM,CAAC,MAAM,CAAC,eAAe,GAAG,IAAI,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,kBAAkB,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
1
|
+
import { Effect, Option } from "effect";
|
|
2
|
+
import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
2
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
3
4
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
import { forgetSession, recallSession, rememberSession, } from "@shivaedev/identity/client/SessionMemory.ts";
|
|
6
|
+
function refreshing(atom) {
|
|
6
7
|
const refresh = Effect.gen(function* () {
|
|
7
8
|
const registry = yield* AtomRegistry.AtomRegistry;
|
|
8
9
|
registry.refresh(atom);
|
|
@@ -10,4 +11,32 @@ export function authSession(read) {
|
|
|
10
11
|
});
|
|
11
12
|
return { atom, refresh };
|
|
12
13
|
}
|
|
14
|
+
function remembering(read, memory) {
|
|
15
|
+
const recalled = Atom.make(Effect.provide(recallSession, memory));
|
|
16
|
+
const check = Atom.make(read.pipe(Effect.flatMap(rememberSession), Effect.provide(memory)));
|
|
17
|
+
return Atom.readable((get) => {
|
|
18
|
+
const remembered = Option.flatten(AsyncResult.value(get(recalled)));
|
|
19
|
+
const verified = get(check);
|
|
20
|
+
if (Option.isSome(AsyncResult.value(verified)) || Option.isNone(remembered)) {
|
|
21
|
+
return verified;
|
|
22
|
+
}
|
|
23
|
+
if (AsyncResult.isFailure(verified)) {
|
|
24
|
+
return AsyncResult.failure(verified.cause, {
|
|
25
|
+
previousSuccess: Option.some(AsyncResult.success(remembered.value)),
|
|
26
|
+
waiting: verified.waiting,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return AsyncResult.success(remembered.value, { waiting: true });
|
|
30
|
+
}, (refresh) => refresh(check));
|
|
31
|
+
}
|
|
32
|
+
export function authSession(read, options) {
|
|
33
|
+
if (options === undefined) {
|
|
34
|
+
return refreshing(Atom.make(read));
|
|
35
|
+
}
|
|
36
|
+
const forget = Effect.provide(forgetSession, options.remember);
|
|
37
|
+
return {
|
|
38
|
+
...refreshing(remembering(read, options.remember)),
|
|
39
|
+
signOut: (request) => Effect.tap(request, () => forget),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
13
42
|
//# sourceMappingURL=authSession.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authSession.js","sourceRoot":"","sources":["../../src/client/authSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"authSession.js","sourceRoot":"","sources":["../../src/client/authSession.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAc,MAAM,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,KAAK,WAAW,MAAM,+BAA+B,CAAC;AAC7D,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,KAAK,YAAY,MAAM,gCAAgC,CAAC;AAC/D,OAAO,EACN,aAAa,EAEb,aAAa,EACb,eAAe,GAGf,MAAM,6CAA6C,CAAC;AAerD,SAAS,UAAU,CAAO,IAA8C;IACvE,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QACnC,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,YAAY,CAAC,YAAY,CAAC;QAClD,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACvB,OAAO,KAAK,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAC;IAClF,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAC1B,CAAC;AAED,SAAS,WAAW,CAAI,IAA8C,EAAE,MAA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,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACpC,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC/D,OAAO;QACN,GAAG,UAAU,CAAC,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;QAClD,OAAO,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC;KACvD,CAAC;AACH,CAAC"}
|
|
@@ -16,7 +16,8 @@ export interface SessionBoundaryProps<TSession, TClient> {
|
|
|
16
16
|
readonly session: TSession | undefined;
|
|
17
17
|
readonly signedOut?: ReactNode;
|
|
18
18
|
readonly signOut?: Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>> | undefined;
|
|
19
|
+
readonly warm?: ((client: TClient) => readonly Atom.Atom<unknown>[]) | undefined;
|
|
19
20
|
}
|
|
20
21
|
export declare function useSessionRecheck(): () => void;
|
|
21
|
-
export declare function SessionBoundary<TSession, TClient>({ session, runtime, queryRetention, identify, connect, recheck, signedOut, signOut, redirectTo, navigate, children, }: SessionBoundaryProps<TSession, TClient>): ReactNode;
|
|
22
|
+
export declare function SessionBoundary<TSession, TClient>({ session, runtime, queryRetention, identify, connect, recheck, signedOut, signOut, redirectTo, navigate, warm, children, }: SessionBoundaryProps<TSession, TClient>): ReactNode;
|
|
22
23
|
//# sourceMappingURL=sessionBoundary.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sessionBoundary.d.ts","sourceRoot":"","sources":["../../src/client/sessionBoundary.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,KAAK,WAAW,MAA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C,KAAK,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC;IAC5E,QAAQ,CAAC,OAAO,EAAE,QAAQ,GAAG,SAAS,CAAC;IACvC,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC;IAC/B,QAAQ,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IACpF,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,OAAO,KAAK,SAAS,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC;CACjF;AACD,wBAAgB,iBAAiB,IAAI,MAAM,IAAI,CAE9C;AACD,wBAAgB,eAAe,CAAC,QAAQ,EAAE,OAAO,EAAE,EAClD,OAAO,EACP,OAAO,EACP,cAAc,EACd,QAAQ,EACR,OAAO,EACP,OAAO,EACP,SAAS,EACT,OAAO,EACP,UAAU,EACV,QAAQ,EACR,IAAI,EACJ,QAAQ,GACR,EAAE,oBAAoB,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,SAAS,CA8BrD"}
|
|
@@ -11,7 +11,7 @@ import { useDraftOwner } from "#client/useDraftOwner.ts";
|
|
|
11
11
|
export function useSessionRecheck() {
|
|
12
12
|
return useContext(RecheckContext);
|
|
13
13
|
}
|
|
14
|
-
export function SessionBoundary({ session, runtime, queryRetention, identify, connect, recheck, signedOut, signOut, redirectTo, navigate, children, }) {
|
|
14
|
+
export function SessionBoundary({ session, runtime, queryRetention, identify, connect, recheck, signedOut, signOut, redirectTo, navigate, warm, children, }) {
|
|
15
15
|
const registry = useContext(RegistryContext);
|
|
16
16
|
const id = session === undefined ? undefined : identify(session);
|
|
17
17
|
const current = useDraftOwner(id);
|
|
@@ -26,6 +26,7 @@ export function SessionBoundary({ session, runtime, queryRetention, identify, co
|
|
|
26
26
|
queryRetention,
|
|
27
27
|
recheck: retry,
|
|
28
28
|
session,
|
|
29
|
+
warm,
|
|
29
30
|
});
|
|
30
31
|
return createElement(SignOutRegistryContext.Provider, { value: registry }, createElement(DraftSessionContext.Provider, { value: current }, createElement(SessionHandoff, { key: id, navigate, redirectTo }, signOut === undefined ? generation : createElement(SessionSignOut, { action: signOut, recheck: retry }, generation))));
|
|
31
32
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sessionBoundary.js","sourceRoot":"","sources":["../../src/client/sessionBoundary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAKrD,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAkB,UAAU,EAAE,MAAM,OAAO,CAAC;AAE5E,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AACxE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"sessionBoundary.js","sourceRoot":"","sources":["../../src/client/sessionBoundary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAKrD,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAkB,UAAU,EAAE,MAAM,OAAO,CAAC;AAE5E,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AACxE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC1E,OAAO,E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nB,aAAa,CACZ,mBAAmB,CAAC,QAAQ,EAC5B,EAAE,KAAK,EAAE,OAAO,EAAE,EAClB,aAAa,CACZ,cAAc,EACd,EAAE,GAAG,EAAE,EAAE,EAAE,QAAQ,EAAE,UAAU,EAAE,EACjC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,aAAa,CAAC,cAAc,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,UAAU,CAAC,CACnH,CACD,CACD,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.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.6.6",
|
|
49
|
-
"@shivaedev/contract": "0.
|
|
49
|
+
"@shivaedev/contract": "0.52.0",
|
|
50
50
|
"@shivaedev/errors": "0.14.0",
|
|
51
51
|
"@shivaedev/form": "0.20.0",
|
|
52
|
-
"@shivaedev/identity": "0.
|
|
52
|
+
"@shivaedev/identity": "0.35.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@effect/atom-react": "4.1.4",
|
|
@@ -58,13 +58,13 @@
|
|
|
58
58
|
"@effect/sql-sqlite-node": "4.1.4",
|
|
59
59
|
"@shivaedev/changes": "0.14.1",
|
|
60
60
|
"@shivaedev/jobs": "0.43.1",
|
|
61
|
-
"@shivaedev/quality": "0.
|
|
62
|
-
"@shivaedev/server": "0.38.
|
|
61
|
+
"@shivaedev/quality": "0.80.0",
|
|
62
|
+
"@shivaedev/server": "0.38.2",
|
|
63
63
|
"@shivaedev/service": "0.9.2",
|
|
64
64
|
"@shivaedev/sql": "0.52.0",
|
|
65
|
-
"@shivaedev/test-kits": "0.13.
|
|
65
|
+
"@shivaedev/test-kits": "0.13.7",
|
|
66
66
|
"@shivaedev/test-sql": "0.25.5",
|
|
67
|
-
"@shivaedev/test-story": "0.50.
|
|
67
|
+
"@shivaedev/test-story": "0.50.1",
|
|
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,5 @@
|
|
|
1
|
-
import { CatchBoundary } from "@tanstack/react-router";
|
|
2
|
-
import { createElement, type ReactNode, useContext } from "react";
|
|
1
|
+
import { type AnyRouter, CatchBoundary, useRouter } from "@tanstack/react-router";
|
|
2
|
+
import { createElement, type ReactNode, useContext, useEffect, useRef } from "react";
|
|
3
3
|
import type { Permission } from "@shivaedev/access/permission.ts";
|
|
4
4
|
import { isForbidden } from "@shivaedev/errors/taxonomy.ts";
|
|
5
5
|
import type { PermissionSession } from "@shivaedev/identity/PermissionSession.ts";
|
|
@@ -25,7 +25,28 @@ export function PermissionBoundaryError({ error }: { readonly error: unknown }):
|
|
|
25
25
|
return context?.fallback;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
function grantKey(session: PermissionSession | null | undefined) {
|
|
29
|
+
return (session?.user.permissions ?? [])
|
|
30
|
+
.map((grant) => `${grant.name}:${grant.strength}`)
|
|
31
|
+
.sort()
|
|
32
|
+
.join(",");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Route guards read the session when a navigation loads, so a session refreshed in place reruns them here.
|
|
36
|
+
function useGuardRerun(session: PermissionSession | null | undefined) {
|
|
37
|
+
const router: AnyRouter | undefined = useRouter({ warn: false });
|
|
38
|
+
const grants = grantKey(session);
|
|
39
|
+
const guardedRef = useRef(grants);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (guardedRef.current !== grants) {
|
|
42
|
+
guardedRef.current = grants;
|
|
43
|
+
void router?.invalidate();
|
|
44
|
+
}
|
|
45
|
+
}, [router, grants]);
|
|
46
|
+
}
|
|
47
|
+
|
|
28
48
|
export function PermissionBoundary({ children, fallback, session }: PermissionBoundaryProps): ReactNode {
|
|
49
|
+
useGuardRerun(session);
|
|
29
50
|
return createElement(
|
|
30
51
|
PermissionContext.Provider,
|
|
31
52
|
{ value: { fallback, session } },
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { RegistryContext } from "@effect/atom-react";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
3
4
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
5
|
import { createElement, type ReactNode, useEffect, useState } from "react";
|
|
5
6
|
import { LiveConnection } from "@shivaedev/contract/automaticLive.ts";
|
|
@@ -7,6 +8,7 @@ import { liveBindings } from "@shivaedev/contract/composeBindings.ts";
|
|
|
7
8
|
import { type QueryRetentionOptions, queryRetention } from "@shivaedev/contract/queryRetention.ts";
|
|
8
9
|
import { registerSessionRecheck } from "#adapters/sessionRecheck.ts";
|
|
9
10
|
import { useLatestCallback } from "#adapters/useLatestCallback.ts";
|
|
11
|
+
import { useWarmQueries } from "#client/adapters/useWarmQueries.ts";
|
|
10
12
|
import { mountLive } from "#client/mountLive.ts";
|
|
11
13
|
import { RecheckContext } from "#client/RecheckContext.ts";
|
|
12
14
|
|
|
@@ -72,7 +74,9 @@ interface SessionGenerationProps<TSession, TClient> {
|
|
|
72
74
|
readonly queryRetention?: QueryRetentionOptions | undefined;
|
|
73
75
|
readonly recheck: () => void;
|
|
74
76
|
readonly session: TSession;
|
|
77
|
+
readonly warm?: ((client: TClient) => readonly Atom.Atom<unknown>[]) | undefined;
|
|
75
78
|
}
|
|
79
|
+
const NOTHING_WARM: readonly Atom.Atom<unknown>[] = [];
|
|
76
80
|
export function SessionGeneration<TSession, TClient>({
|
|
77
81
|
session,
|
|
78
82
|
connect,
|
|
@@ -80,6 +84,7 @@ export function SessionGeneration<TSession, TClient>({
|
|
|
80
84
|
children,
|
|
81
85
|
identity,
|
|
82
86
|
queryRetention: retention,
|
|
87
|
+
warm,
|
|
83
88
|
}: SessionGenerationProps<TSession, TClient>): ReactNode {
|
|
84
89
|
const recheckLatest = useLatestCallback(recheck);
|
|
85
90
|
const [connection] = useState(() => ({ connect, session }));
|
|
@@ -104,6 +109,7 @@ export function SessionGeneration<TSession, TClient>({
|
|
|
104
109
|
});
|
|
105
110
|
};
|
|
106
111
|
}, [identity, owned, connection]);
|
|
112
|
+
useWarmQueries(owned.registry, warm?.(owned.client) ?? NOTHING_WARM);
|
|
107
113
|
return createElement(
|
|
108
114
|
RecheckContext.Provider,
|
|
109
115
|
{ value: recheckLatest },
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
2
|
+
import type * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
3
|
+
import { useEffect, useMemo } from "react";
|
|
4
|
+
import { warmQueries } from "@shivaedev/contract/warmQueries.ts";
|
|
5
|
+
|
|
6
|
+
function warmMounts(registry: AtomRegistry.AtomRegistry) {
|
|
7
|
+
const mounted = new Map<Atom.Atom<unknown>, () => void>();
|
|
8
|
+
return {
|
|
9
|
+
follow(queries: readonly Atom.Atom<unknown>[]) {
|
|
10
|
+
const listed = new Set(queries);
|
|
11
|
+
if (listed.size !== mounted.size || queries.some((query) => !mounted.has(query))) {
|
|
12
|
+
registry.set(warmQueries, listed);
|
|
13
|
+
}
|
|
14
|
+
for (const query of listed) {
|
|
15
|
+
if (!mounted.has(query)) {
|
|
16
|
+
mounted.set(query, registry.mount(query));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
for (const [query, unmount] of mounted) {
|
|
20
|
+
if (!listed.has(query)) {
|
|
21
|
+
mounted.delete(query);
|
|
22
|
+
unmount();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
release() {
|
|
27
|
+
for (const unmount of mounted.values()) {
|
|
28
|
+
unmount();
|
|
29
|
+
}
|
|
30
|
+
mounted.clear();
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function useWarmQueries(registry: AtomRegistry.AtomRegistry, queries: readonly Atom.Atom<unknown>[]) {
|
|
36
|
+
const warm = useMemo(() => warmMounts(registry), [registry]);
|
|
37
|
+
useEffect(() => () => warm.release(), [warm]);
|
|
38
|
+
useEffect(() => warm.follow(queries), [warm, queries]);
|
|
39
|
+
}
|
|
@@ -1,15 +1,30 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
2
|
-
import
|
|
1
|
+
import { Effect, type Layer, Option } from "effect";
|
|
2
|
+
import * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
3
3
|
import * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
5
|
+
import {
|
|
6
|
+
forgetSession,
|
|
7
|
+
type RememberedSession,
|
|
8
|
+
recallSession,
|
|
9
|
+
rememberSession,
|
|
10
|
+
type SessionMemory,
|
|
11
|
+
type VerifiedSession,
|
|
12
|
+
} from "@shivaedev/identity/client/SessionMemory.ts";
|
|
5
13
|
|
|
6
14
|
export interface AuthSession<A, E> {
|
|
7
15
|
readonly atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>;
|
|
8
16
|
readonly refresh: Effect.Effect<A, E, AtomRegistry.AtomRegistry>;
|
|
9
17
|
}
|
|
10
18
|
|
|
11
|
-
export
|
|
12
|
-
|
|
19
|
+
export interface RememberedAuthSession<E> extends AuthSession<RememberedSession | null, E> {
|
|
20
|
+
readonly signOut: <A, TError, R>(request: Effect.Effect<A, TError, R>) => Effect.Effect<A, TError, R>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface AuthSessionOptions {
|
|
24
|
+
readonly remember: Layer.Layer<SessionMemory>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function refreshing<A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>): AuthSession<A, E> {
|
|
13
28
|
const refresh = Effect.gen(function* () {
|
|
14
29
|
const registry = yield* AtomRegistry.AtomRegistry;
|
|
15
30
|
registry.refresh(atom);
|
|
@@ -17,3 +32,41 @@ export function authSession<A, E>(read: Effect.Effect<A, E>): AuthSession<A, E>
|
|
|
17
32
|
});
|
|
18
33
|
return { atom, refresh };
|
|
19
34
|
}
|
|
35
|
+
|
|
36
|
+
function remembering<E>(read: Effect.Effect<VerifiedSession | null, E>, memory: Layer.Layer<SessionMemory>) {
|
|
37
|
+
const recalled = Atom.make(Effect.provide(recallSession, memory));
|
|
38
|
+
const check = Atom.make(read.pipe(Effect.flatMap(rememberSession), Effect.provide(memory)));
|
|
39
|
+
return Atom.readable(
|
|
40
|
+
(get): AsyncResult.AsyncResult<RememberedSession | null, E> => {
|
|
41
|
+
const remembered = Option.flatten(AsyncResult.value(get(recalled)));
|
|
42
|
+
const verified = get(check);
|
|
43
|
+
if (Option.isSome(AsyncResult.value(verified)) || Option.isNone(remembered)) {
|
|
44
|
+
return verified;
|
|
45
|
+
}
|
|
46
|
+
if (AsyncResult.isFailure(verified)) {
|
|
47
|
+
return AsyncResult.failure(verified.cause, {
|
|
48
|
+
previousSuccess: Option.some(AsyncResult.success(remembered.value)),
|
|
49
|
+
waiting: verified.waiting,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return AsyncResult.success(remembered.value, { waiting: true });
|
|
53
|
+
},
|
|
54
|
+
(refresh) => refresh(check),
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function authSession<A, E>(read: Effect.Effect<A, E>): AuthSession<A, E>;
|
|
59
|
+
export function authSession<E>(read: Effect.Effect<VerifiedSession | null, E>, options: AuthSessionOptions): RememberedAuthSession<E>;
|
|
60
|
+
export function authSession<A extends VerifiedSession | null, E>(
|
|
61
|
+
read: Effect.Effect<A, E>,
|
|
62
|
+
options?: AuthSessionOptions,
|
|
63
|
+
): AuthSession<A, E> | RememberedAuthSession<E> {
|
|
64
|
+
if (options === undefined) {
|
|
65
|
+
return refreshing(Atom.make(read));
|
|
66
|
+
}
|
|
67
|
+
const forget = Effect.provide(forgetSession, options.remember);
|
|
68
|
+
return {
|
|
69
|
+
...refreshing(remembering(read, options.remember)),
|
|
70
|
+
signOut: (request) => Effect.tap(request, () => forget),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -25,6 +25,7 @@ export interface SessionBoundaryProps<TSession, TClient> {
|
|
|
25
25
|
readonly session: TSession | undefined;
|
|
26
26
|
readonly signedOut?: ReactNode;
|
|
27
27
|
readonly signOut?: Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>> | undefined;
|
|
28
|
+
readonly warm?: ((client: TClient) => readonly Atom.Atom<unknown>[]) | undefined;
|
|
28
29
|
}
|
|
29
30
|
export function useSessionRecheck(): () => void {
|
|
30
31
|
return useContext(RecheckContext);
|
|
@@ -40,6 +41,7 @@ export function SessionBoundary<TSession, TClient>({
|
|
|
40
41
|
signOut,
|
|
41
42
|
redirectTo,
|
|
42
43
|
navigate,
|
|
44
|
+
warm,
|
|
43
45
|
children,
|
|
44
46
|
}: SessionBoundaryProps<TSession, TClient>): ReactNode {
|
|
45
47
|
const registry = useContext(RegistryContext);
|
|
@@ -56,6 +58,7 @@ export function SessionBoundary<TSession, TClient>({
|
|
|
56
58
|
queryRetention,
|
|
57
59
|
recheck: retry,
|
|
58
60
|
session,
|
|
61
|
+
warm,
|
|
59
62
|
});
|
|
60
63
|
return createElement(
|
|
61
64
|
SignOutRegistryContext.Provider,
|