@shivaedev/react 0.51.0 → 0.53.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +73 -5
  3. package/dist/adapters/permissionBoundary.d.ts.map +1 -1
  4. package/dist/adapters/permissionBoundary.js +21 -2
  5. package/dist/adapters/permissionBoundary.js.map +1 -1
  6. package/dist/client/FailureHostContext.d.ts +1 -1
  7. package/dist/client/FailureHostContext.d.ts.map +1 -1
  8. package/dist/client/FailureHostContext.js.map +1 -1
  9. package/dist/client/NoticeContext.d.ts +1 -1
  10. package/dist/client/adapters/SessionGeneration.d.ts +6 -1
  11. package/dist/client/adapters/SessionGeneration.d.ts.map +1 -1
  12. package/dist/client/adapters/SessionGeneration.js +7 -2
  13. package/dist/client/adapters/SessionGeneration.js.map +1 -1
  14. package/dist/client/adapters/SessionSignOut.d.ts +2 -1
  15. package/dist/client/adapters/SessionSignOut.d.ts.map +1 -1
  16. package/dist/client/adapters/SessionSignOut.js +3 -2
  17. package/dist/client/adapters/SessionSignOut.js.map +1 -1
  18. package/dist/client/adapters/useFeedback.d.ts +1 -1
  19. package/dist/client/adapters/useFeedback.d.ts.map +1 -1
  20. package/dist/client/adapters/useFeedback.js +35 -20
  21. package/dist/client/adapters/useFeedback.js.map +1 -1
  22. package/dist/client/adapters/useStoredQueries.d.ts +7 -0
  23. package/dist/client/adapters/useStoredQueries.d.ts.map +1 -0
  24. package/dist/client/adapters/useStoredQueries.js +52 -0
  25. package/dist/client/adapters/useStoredQueries.js.map +1 -0
  26. package/dist/client/adapters/useWarmQueries.d.ts +4 -0
  27. package/dist/client/adapters/useWarmQueries.d.ts.map +1 -0
  28. package/dist/client/adapters/useWarmQueries.js +36 -0
  29. package/dist/client/adapters/useWarmQueries.js.map +1 -0
  30. package/dist/client/authSession.d.ts +10 -2
  31. package/dist/client/authSession.d.ts.map +1 -1
  32. package/dist/client/authSession.js +32 -3
  33. package/dist/client/authSession.js.map +1 -1
  34. package/dist/client/notices.d.ts.map +1 -1
  35. package/dist/client/notices.js +1 -3
  36. package/dist/client/notices.js.map +1 -1
  37. package/dist/client/sessionBoundary.d.ts +5 -2
  38. package/dist/client/sessionBoundary.d.ts.map +1 -1
  39. package/dist/client/sessionBoundary.js +7 -2
  40. package/dist/client/sessionBoundary.js.map +1 -1
  41. package/dist/noticeStore.d.ts +2 -2
  42. package/dist/noticeStore.d.ts.map +1 -1
  43. package/dist/noticeStore.js +4 -3
  44. package/dist/noticeStore.js.map +1 -1
  45. package/package.json +13 -13
  46. package/src/adapters/permissionBoundary.ts +23 -2
  47. package/src/client/FailureHostContext.ts +1 -1
  48. package/src/client/adapters/SessionGeneration.ts +13 -2
  49. package/src/client/adapters/SessionSignOut.ts +4 -1
  50. package/src/client/adapters/useFeedback.ts +22 -10
  51. package/src/client/adapters/useStoredQueries.ts +67 -0
  52. package/src/client/adapters/useWarmQueries.ts +39 -0
  53. package/src/client/authSession.ts +57 -4
  54. package/src/client/notices.ts +1 -3
  55. package/src/client/sessionBoundary.ts +12 -2
  56. package/src/noticeStore.ts +4 -3
@@ -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 type * as AsyncResult from "effect/reactivity/AsyncResult";
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 function authSession<A, E>(read: Effect.Effect<A, E>): AuthSession<A, E> {
12
- const atom = Atom.make(read);
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
+ }
@@ -77,9 +77,7 @@ export function NoticeHost({
77
77
  const report = useMemo(
78
78
  () => (unexpected: UnexpectedFailure) => {
79
79
  const descriptor = wordingRef.current(unexpected);
80
- if (descriptor !== "silent") {
81
- store.publish(descriptor, "failure");
82
- }
80
+ return descriptor === "silent" ? undefined : store.publish(descriptor, "failure");
83
81
  },
84
82
  [store],
85
83
  );
@@ -1,15 +1,17 @@
1
1
  import { RegistryContext } from "@effect/atom-react";
2
- import type { Effect } from "effect";
2
+ import type { Effect, Layer } 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
6
  import { createElement, Fragment, type ReactNode, useContext } from "react";
7
+ import type { QueryStorageProvider } from "@shivaedev/contract/QueryStorage.ts";
7
8
  import type { QueryRetentionOptions } from "@shivaedev/contract/queryRetention.ts";
8
9
  import { DraftSessionContext } from "@shivaedev/form/adapters/react.ts";
9
10
  import { SessionGeneration } from "#client/adapters/SessionGeneration.ts";
10
11
  import { SessionHandoff } from "#client/adapters/SessionHandoff.ts";
11
12
  import { SessionSignOut } from "#client/adapters/SessionSignOut.ts";
12
13
  import { useSessionRetry } from "#client/adapters/useSessionRetry.ts";
14
+ import { useDeviceWipe, useStoredQueryWipes } from "#client/adapters/useStoredQueries.ts";
13
15
  import { RecheckContext } from "#client/RecheckContext.ts";
14
16
  import { SignOutRegistryContext } from "#client/SignOutRegistryContext.ts";
15
17
  import { useDraftOwner } from "#client/useDraftOwner.ts";
@@ -18,6 +20,7 @@ export interface SessionBoundaryProps<TSession, TClient> {
18
20
  readonly connect: (session: TSession, registry: AtomRegistry.AtomRegistry) => TClient;
19
21
  readonly identify: (session: TSession) => string;
20
22
  readonly navigate?: ((href: string) => void) | undefined;
23
+ readonly persist?: Layer.Layer<QueryStorageProvider> | undefined;
21
24
  readonly queryRetention?: QueryRetentionOptions | undefined;
22
25
  readonly recheck: (() => void) | Effect.Effect<unknown, unknown, AtomRegistry.AtomRegistry>;
23
26
  readonly redirectTo?: string | undefined;
@@ -25,6 +28,7 @@ export interface SessionBoundaryProps<TSession, TClient> {
25
28
  readonly session: TSession | undefined;
26
29
  readonly signedOut?: ReactNode;
27
30
  readonly signOut?: Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>> | undefined;
31
+ readonly warm?: ((client: TClient) => readonly Atom.Atom<unknown>[]) | undefined;
28
32
  }
29
33
  export function useSessionRecheck(): () => void {
30
34
  return useContext(RecheckContext);
@@ -40,12 +44,16 @@ export function SessionBoundary<TSession, TClient>({
40
44
  signOut,
41
45
  redirectTo,
42
46
  navigate,
47
+ persist,
48
+ warm,
43
49
  children,
44
50
  }: SessionBoundaryProps<TSession, TClient>): ReactNode {
45
51
  const registry = useContext(RegistryContext);
46
52
  const id = session === undefined ? undefined : identify(session);
47
53
  const current = useDraftOwner(id);
48
54
  const retry = useSessionRetry(recheck, id, runtime);
55
+ useStoredQueryWipes(persist, id);
56
+ const wipe = useDeviceWipe(persist);
49
57
  if (session === undefined) {
50
58
  return createElement(Fragment, null, signedOut);
51
59
  }
@@ -53,9 +61,11 @@ export function SessionBoundary<TSession, TClient>({
53
61
  children,
54
62
  connect,
55
63
  identity: id,
64
+ persist,
56
65
  queryRetention,
57
66
  recheck: retry,
58
67
  session,
68
+ warm,
59
69
  });
60
70
  return createElement(
61
71
  SignOutRegistryContext.Provider,
@@ -66,7 +76,7 @@ export function SessionBoundary<TSession, TClient>({
66
76
  createElement(
67
77
  SessionHandoff,
68
78
  { key: id, navigate, redirectTo },
69
- signOut === undefined ? generation : createElement(SessionSignOut, { action: signOut, recheck: retry }, generation),
79
+ signOut === undefined ? generation : createElement(SessionSignOut, { action: signOut, recheck: retry, wipe }, generation),
70
80
  ),
71
81
  ),
72
82
  );
@@ -24,11 +24,12 @@ export function noticeStore<TMessage = unknown>(registry: AtomRegistry.AtomRegis
24
24
  entries,
25
25
  publish: (descriptor: Pick<NoticeDescriptor<never, TMessage>, "message" | "expiresIn">, kind: NoticeKind, action?: NoticeAction<TMessage>) => {
26
26
  if (!store.active) {
27
- return;
27
+ return undefined;
28
28
  }
29
29
  const entry = noticeEntry(registry, clock, nextId, descriptor, remove, action, kind);
30
30
  nextId += 1;
31
31
  registry.set(entries, [...registry.get(entries), entry]);
32
+ return entry.dismiss;
32
33
  },
33
34
  };
34
35
  return store;
@@ -40,10 +41,10 @@ export function publishNotice<R, E, TMessage = unknown>(
40
41
  kind: NoticeKind,
41
42
  ) {
42
43
  if (!(store?.active && descriptor)) {
43
- return;
44
+ return undefined;
44
45
  }
45
46
  const action = descriptor.action;
46
- store.publish(
47
+ return store.publish(
47
48
  descriptor,
48
49
  kind,
49
50
  action === undefined ? undefined : { atom: runtime.fn(() => action.run).pipe(Atom.setIdleTTL(0)), label: action.label },