@ic-reactor/react 3.13.0 → 4.0.0-beta.2
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/README.md +237 -787
- package/dist/index.d.ts +260 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +471 -23
- package/dist/index.js.map +1 -1
- package/llms.txt +82 -278
- package/package.json +11 -39
- package/src/index.tsx +612 -0
- package/dist/auth/auth-client-compat.d.ts +0 -122
- package/dist/auth/auth-client-compat.d.ts.map +0 -1
- package/dist/auth/auth-client-compat.js +0 -162
- package/dist/auth/auth-client-compat.js.map +0 -1
- package/dist/auth/authentication-manager.d.ts +0 -405
- package/dist/auth/authentication-manager.d.ts.map +0 -1
- package/dist/auth/authentication-manager.js +0 -1537
- package/dist/auth/authentication-manager.js.map +0 -1
- package/dist/auth/constants.d.ts +0 -24
- package/dist/auth/constants.d.ts.map +0 -1
- package/dist/auth/constants.js +0 -24
- package/dist/auth/constants.js.map +0 -1
- package/dist/auth/createIdentityAttributeHooks.d.ts +0 -14
- package/dist/auth/createIdentityAttributeHooks.d.ts.map +0 -1
- package/dist/auth/createIdentityAttributeHooks.js +0 -122
- package/dist/auth/createIdentityAttributeHooks.js.map +0 -1
- package/dist/auth/identity-attributes-manager.d.ts +0 -27
- package/dist/auth/identity-attributes-manager.d.ts.map +0 -1
- package/dist/auth/identity-attributes-manager.js +0 -191
- package/dist/auth/identity-attributes-manager.js.map +0 -1
- package/dist/auth/identity-attributes.d.ts +0 -19
- package/dist/auth/identity-attributes.d.ts.map +0 -1
- package/dist/auth/identity-attributes.js +0 -227
- package/dist/auth/identity-attributes.js.map +0 -1
- package/dist/auth/index.d.ts +0 -8
- package/dist/auth/index.d.ts.map +0 -1
- package/dist/auth/index.js +0 -8
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/local-ii-probe.d.ts +0 -57
- package/dist/auth/local-ii-probe.d.ts.map +0 -1
- package/dist/auth/local-ii-probe.js +0 -121
- package/dist/auth/local-ii-probe.js.map +0 -1
- package/dist/auth/types.d.ts +0 -222
- package/dist/auth/types.d.ts.map +0 -1
- package/dist/auth/types.js +0 -2
- package/dist/auth/types.js.map +0 -1
- package/dist/createActorHooks.d.ts +0 -41
- package/dist/createActorHooks.d.ts.map +0 -1
- package/dist/createActorHooks.js +0 -17
- package/dist/createActorHooks.js.map +0 -1
- package/dist/createInfiniteQuery.d.ts +0 -185
- package/dist/createInfiniteQuery.d.ts.map +0 -1
- package/dist/createInfiniteQuery.js +0 -198
- package/dist/createInfiniteQuery.js.map +0 -1
- package/dist/createMutation.d.ts +0 -33
- package/dist/createMutation.d.ts.map +0 -1
- package/dist/createMutation.js +0 -199
- package/dist/createMutation.js.map +0 -1
- package/dist/createQuery.d.ts +0 -63
- package/dist/createQuery.d.ts.map +0 -1
- package/dist/createQuery.js +0 -204
- package/dist/createQuery.js.map +0 -1
- package/dist/createReactorProvider.d.ts +0 -158
- package/dist/createReactorProvider.d.ts.map +0 -1
- package/dist/createReactorProvider.js +0 -256
- package/dist/createReactorProvider.js.map +0 -1
- package/dist/createSuspenseInfiniteQuery.d.ts +0 -154
- package/dist/createSuspenseInfiniteQuery.d.ts.map +0 -1
- package/dist/createSuspenseInfiniteQuery.js +0 -209
- package/dist/createSuspenseInfiniteQuery.js.map +0 -1
- package/dist/createSuspenseQuery.d.ts +0 -46
- package/dist/createSuspenseQuery.d.ts.map +0 -1
- package/dist/createSuspenseQuery.js +0 -158
- package/dist/createSuspenseQuery.js.map +0 -1
- package/dist/defineDisplayReactor.d.ts +0 -43
- package/dist/defineDisplayReactor.d.ts.map +0 -1
- package/dist/defineDisplayReactor.js +0 -42
- package/dist/defineDisplayReactor.js.map +0 -1
- package/dist/defineReactor.d.ts +0 -99
- package/dist/defineReactor.d.ts.map +0 -1
- package/dist/defineReactor.js +0 -15
- package/dist/defineReactor.js.map +0 -1
- package/dist/defineReactorShared.d.ts +0 -84
- package/dist/defineReactorShared.d.ts.map +0 -1
- package/dist/defineReactorShared.js +0 -139
- package/dist/defineReactorShared.js.map +0 -1
- package/dist/hooks/createAuthHooks.d.ts +0 -50
- package/dist/hooks/createAuthHooks.d.ts.map +0 -1
- package/dist/hooks/createAuthHooks.js +0 -291
- package/dist/hooks/createAuthHooks.js.map +0 -1
- package/dist/hooks/index.d.ts +0 -21
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/index.js +0 -24
- package/dist/hooks/index.js.map +0 -1
- package/dist/hooks/useActorInfiniteQuery.d.ts +0 -67
- package/dist/hooks/useActorInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useActorInfiniteQuery.js +0 -89
- package/dist/hooks/useActorInfiniteQuery.js.map +0 -1
- package/dist/hooks/useActorMethod.d.ts +0 -148
- package/dist/hooks/useActorMethod.d.ts.map +0 -1
- package/dist/hooks/useActorMethod.js +0 -394
- package/dist/hooks/useActorMethod.js.map +0 -1
- package/dist/hooks/useActorMutation.d.ts +0 -51
- package/dist/hooks/useActorMutation.d.ts.map +0 -1
- package/dist/hooks/useActorMutation.js +0 -70
- package/dist/hooks/useActorMutation.js.map +0 -1
- package/dist/hooks/useActorQuery.d.ts +0 -45
- package/dist/hooks/useActorQuery.d.ts.map +0 -1
- package/dist/hooks/useActorQuery.js +0 -67
- package/dist/hooks/useActorQuery.js.map +0 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +0 -51
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.js +0 -76
- package/dist/hooks/useActorSuspenseInfiniteQuery.js.map +0 -1
- package/dist/hooks/useActorSuspenseQuery.d.ts +0 -32
- package/dist/hooks/useActorSuspenseQuery.d.ts.map +0 -1
- package/dist/hooks/useActorSuspenseQuery.js +0 -58
- package/dist/hooks/useActorSuspenseQuery.js.map +0 -1
- package/dist/ownedAuthentication.d.ts +0 -52
- package/dist/ownedAuthentication.d.ts.map +0 -1
- package/dist/ownedAuthentication.js +0 -49
- package/dist/ownedAuthentication.js.map +0 -1
- package/dist/server.d.ts +0 -21
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -23
- package/dist/server.js.map +0 -1
- package/dist/testing.d.ts +0 -19
- package/dist/testing.d.ts.map +0 -1
- package/dist/testing.js +0 -19
- package/dist/testing.js.map +0 -1
- package/dist/types.d.ts +0 -671
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -5
- package/dist/types.js.map +0 -1
- package/dist/utils.d.ts +0 -207
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -405
- package/dist/utils.js.map +0 -1
- package/dist/validation.d.ts +0 -136
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -144
- package/dist/validation.js.map +0 -1
- package/src/auth/auth-client-compat.ts +0 -273
- package/src/auth/authentication-manager.ts +0 -1682
- package/src/auth/constants.ts +0 -32
- package/src/auth/createIdentityAttributeHooks.ts +0 -169
- package/src/auth/identity-attributes-manager.ts +0 -226
- package/src/auth/identity-attributes.ts +0 -345
- package/src/auth/index.ts +0 -7
- package/src/auth/local-ii-probe.ts +0 -173
- package/src/auth/types.ts +0 -243
- package/src/createActorHooks.ts +0 -208
- package/src/createInfiniteQuery.ts +0 -670
- package/src/createMutation.ts +0 -324
- package/src/createQuery.ts +0 -369
- package/src/createReactorProvider.ts +0 -365
- package/src/createSuspenseInfiniteQuery.ts +0 -651
- package/src/createSuspenseQuery.ts +0 -304
- package/src/defineDisplayReactor.ts +0 -62
- package/src/defineReactor.ts +0 -142
- package/src/defineReactorShared.ts +0 -268
- package/src/hooks/createAuthHooks.ts +0 -371
- package/src/hooks/index.ts +0 -103
- package/src/hooks/useActorInfiniteQuery.ts +0 -278
- package/src/hooks/useActorMethod.ts +0 -710
- package/src/hooks/useActorMutation.ts +0 -205
- package/src/hooks/useActorQuery.ts +0 -157
- package/src/hooks/useActorSuspenseInfiniteQuery.ts +0 -248
- package/src/hooks/useActorSuspenseQuery.ts +0 -147
- package/src/index.ts +0 -31
- package/src/ownedAuthentication.ts +0 -81
- package/src/server.ts +0 -23
- package/src/testing.ts +0 -18
- package/src/types.ts +0 -948
- package/src/utils.ts +0 -505
- package/src/validation.ts +0 -226
|
@@ -1,365 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reactor Provider Factory - builds an app's reactors once per mounted tree.
|
|
3
|
-
*
|
|
4
|
-
* A server renders every request through the same module graph, so a reactor
|
|
5
|
-
* built at module scope is one cache shared by every visitor: query keys carry
|
|
6
|
-
* no caller principal, and one request's caller-scoped result can be served to
|
|
7
|
-
* the next. The setup belongs inside the render tree instead, built in a
|
|
8
|
-
* `useState` initializer and handed down through context. Apps wrote that
|
|
9
|
-
* provider by hand (examples/nextjs had one of 93 lines) and then forwarded
|
|
10
|
-
* each hook out of the context with an `as any` cast, because a hook that is
|
|
11
|
-
* generic over the method name has no parameter tuple a typed wrapper could
|
|
12
|
-
* name.
|
|
13
|
-
*
|
|
14
|
-
* `createReactorProvider` is that provider. `useReactor()` returns what the
|
|
15
|
-
* factory built, typed as the factory's return, so every hook on it keeps its
|
|
16
|
-
* own generic signature and needs no forwarder.
|
|
17
|
-
*/
|
|
18
|
-
import {
|
|
19
|
-
createContext,
|
|
20
|
-
createElement,
|
|
21
|
-
useContext,
|
|
22
|
-
useEffect,
|
|
23
|
-
useState,
|
|
24
|
-
} from "react"
|
|
25
|
-
import type { ReactElement, ReactNode } from "react"
|
|
26
|
-
import { QueryClientProvider } from "@tanstack/react-query"
|
|
27
|
-
import type { QueryClient } from "@tanstack/react-query"
|
|
28
|
-
import type { AuthenticationManager } from "./auth/authentication-manager.js"
|
|
29
|
-
import {
|
|
30
|
-
authenticationOf,
|
|
31
|
-
collectAuthentication,
|
|
32
|
-
} from "./ownedAuthentication.js"
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Props of the provider {@link createReactorProvider} returns: `children`, and
|
|
36
|
-
* the props its factory takes.
|
|
37
|
-
*/
|
|
38
|
-
export type ReactorProviderProps<TProps extends object = {}> = TProps & {
|
|
39
|
-
children?: ReactNode
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Options for {@link createReactorProvider}. */
|
|
43
|
-
export interface CreateReactorProviderOptions {
|
|
44
|
-
/**
|
|
45
|
-
* Render a `QueryClientProvider` for the QueryClient the built value uses,
|
|
46
|
-
* so that `useQueryClient()`, React Query Devtools and `HydrationBoundary`
|
|
47
|
-
* below the provider read the cache the reactor hooks fill. The reactor
|
|
48
|
-
* hooks do not need it: each binds to its reactor's own QueryClient.
|
|
49
|
-
*
|
|
50
|
-
* Rendered only when the value holds exactly one QueryClient, found on the
|
|
51
|
-
* value and on its own properties: a `defineReactor` result, a reactor or a
|
|
52
|
-
* `ClientManager` brings its manager's, and a `QueryClient` counts as
|
|
53
|
-
* itself. Below the provider it takes the place of an outer
|
|
54
|
-
* `QueryClientProvider`, so a plain `useQuery` there caches in the
|
|
55
|
-
* reactors' QueryClient too. Pass `false` to leave that context to a
|
|
56
|
-
* provider of your own.
|
|
57
|
-
*
|
|
58
|
-
* @default true
|
|
59
|
-
*/
|
|
60
|
-
queryClientProvider?: boolean
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** What {@link createReactorProvider} returns. */
|
|
64
|
-
export interface CreateReactorProviderReturn<
|
|
65
|
-
TValue extends object,
|
|
66
|
-
TProps extends object = {},
|
|
67
|
-
> {
|
|
68
|
-
/**
|
|
69
|
-
* Builds the value once per mount and provides it to `useReactor`.
|
|
70
|
-
*
|
|
71
|
-
* Its props, other than `children`, are passed to the factory when it
|
|
72
|
-
* mounts and are not read again. To build a new value, give the provider a
|
|
73
|
-
* new `key`: React then unmounts the old tree, which releases the old value,
|
|
74
|
-
* and mounts a new one.
|
|
75
|
-
*/
|
|
76
|
-
ReactorProvider: (props: ReactorProviderProps<TProps>) => ReactElement
|
|
77
|
-
/**
|
|
78
|
-
* The value the nearest `ReactorProvider` built, or one of its properties
|
|
79
|
-
* when given that property's name. A hook: call it in a component or a
|
|
80
|
-
* custom hook below the provider. It throws when there is none.
|
|
81
|
-
*/
|
|
82
|
-
useReactor: {
|
|
83
|
-
<TKey extends keyof TValue>(key: TKey): TValue[TKey]
|
|
84
|
-
// Last, so that `ReturnType<typeof useReactor>` is the whole value.
|
|
85
|
-
(): TValue
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Creates a provider that builds an app's reactors once per mounted tree, and
|
|
91
|
-
* a `useReactor()` hook that returns what it built, fully typed.
|
|
92
|
-
*
|
|
93
|
-
* This is the setup a server-rendered app needs. The factory runs in a
|
|
94
|
-
* `useState` initializer, once per mounted tree, and a server render is a tree
|
|
95
|
-
* of its own: each request builds its own `ClientManager`, `QueryClient`,
|
|
96
|
-
* reactors and `AuthenticationManager`, so no cache or identity is shared
|
|
97
|
-
* between visitors. The first render needs no effect, so the server's HTML is
|
|
98
|
-
* complete, and the browser builds a fresh value from the same props to
|
|
99
|
-
* hydrate it. In a client-only app it works the same, one value per mount.
|
|
100
|
-
*
|
|
101
|
-
* The factory returns anything: a `defineReactor` or `defineDisplayReactor`
|
|
102
|
-
* result, a record of them, reactors and managers built by hand, query and
|
|
103
|
-
* mutation objects from `createQuery` or `createMutation`. `useReactor()`
|
|
104
|
-
* returns it with the factory's own return type, so its hooks keep their
|
|
105
|
-
* generic signatures. `useReactor("todo")` returns one property of it.
|
|
106
|
-
*
|
|
107
|
-
* When the tree unmounts, the provider disposes each `AuthenticationManager`
|
|
108
|
-
* built for the value, releasing the Internet Identity client it built: those
|
|
109
|
-
* constructed while the factory ran, and the one a `defineReactor` result in
|
|
110
|
-
* the value, or among its own properties, builds on first use (a result that
|
|
111
|
-
* never used authentication builds none). A manager built elsewhere and
|
|
112
|
-
* handed in, such as an app-wide one passed as `authentication` or as a
|
|
113
|
-
* prop, is left to whoever built it. `dispose()` only forgets the client, and
|
|
114
|
-
* the next sign-in builds a new one, so this is safe under StrictMode, which
|
|
115
|
-
* runs the cleanup and then the effect again on the same value.
|
|
116
|
-
*
|
|
117
|
-
* It also renders a `QueryClientProvider` for the value's QueryClient; see
|
|
118
|
-
* {@link CreateReactorProviderOptions.queryClientProvider}.
|
|
119
|
-
*
|
|
120
|
-
* A suspense hook below the provider may suspend its first render: React
|
|
121
|
-
* renders the same element again when the data arrives, and the provider
|
|
122
|
-
* reuses the value that first render built, whether the Suspense boundary
|
|
123
|
-
* sits above the provider or, while hydrating, there is none. A provider that
|
|
124
|
-
* a transition mounts (`startTransition`, a client-side navigation) can be
|
|
125
|
-
* rendered from a new element on each retry and build a new value each time,
|
|
126
|
-
* so give its suspending components a `<Suspense>` boundary inside it.
|
|
127
|
-
*
|
|
128
|
-
* Call `createReactorProvider` at module scope: it builds nothing itself,
|
|
129
|
-
* only the context, and every mounted provider builds its own value. In the
|
|
130
|
-
* Next.js App Router that module needs `"use client"`, like any module with
|
|
131
|
-
* hooks; a server component renders the provider from there.
|
|
132
|
-
*
|
|
133
|
-
* @param factory - Builds the value. It receives the provider's props other
|
|
134
|
-
* than `children`, and runs once per mounted provider.
|
|
135
|
-
* @param options - See {@link CreateReactorProviderOptions}.
|
|
136
|
-
*
|
|
137
|
-
* @example One canister, with Internet Identity
|
|
138
|
-
* ```tsx
|
|
139
|
-
* "use client"
|
|
140
|
-
* import { createReactorProvider, defineReactor } from "@ic-reactor/react"
|
|
141
|
-
* import { canisterId, idlFactory, type _SERVICE } from "./declarations/todo"
|
|
142
|
-
*
|
|
143
|
-
* export const { ReactorProvider, useReactor } = createReactorProvider(() =>
|
|
144
|
-
* defineReactor<_SERVICE>({ name: "todo", idlFactory, canisterId })
|
|
145
|
-
* )
|
|
146
|
-
*
|
|
147
|
-
* function Todos() {
|
|
148
|
-
* const { useActorQuery, useAuth } = useReactor()
|
|
149
|
-
* const { isAuthenticated } = useAuth()
|
|
150
|
-
* const { data } = useActorQuery({ functionName: "getAllTodos" })
|
|
151
|
-
* return <p>{isAuthenticated ? data?.length : "Sign in"}</p>
|
|
152
|
-
* }
|
|
153
|
-
*
|
|
154
|
-
* // app/layout.tsx (a server component)
|
|
155
|
-
* // <ReactorProvider>{children}</ReactorProvider>
|
|
156
|
-
* ```
|
|
157
|
-
*
|
|
158
|
-
* @example Several canisters sharing one agent and one sign-in
|
|
159
|
-
* ```tsx
|
|
160
|
-
* export const { ReactorProvider, useReactor } = createReactorProvider(() => {
|
|
161
|
-
* const backend = defineReactor<Backend>({
|
|
162
|
-
* name: "backend",
|
|
163
|
-
* idlFactory: backendIdl,
|
|
164
|
-
* canisterId: backendId,
|
|
165
|
-
* })
|
|
166
|
-
* const ledger = defineDisplayReactor<Ledger>({
|
|
167
|
-
* name: "ledger",
|
|
168
|
-
* idlFactory: ledgerIdl,
|
|
169
|
-
* canisterId: ledgerId,
|
|
170
|
-
* authentication: backend.authentication,
|
|
171
|
-
* })
|
|
172
|
-
* return { backend, ledger }
|
|
173
|
-
* })
|
|
174
|
-
*
|
|
175
|
-
* function Balance() {
|
|
176
|
-
* const { data } = useReactor("ledger").useActorQuery({
|
|
177
|
-
* functionName: "icrc1_total_supply",
|
|
178
|
-
* })
|
|
179
|
-
* return <span>{data}</span>
|
|
180
|
-
* }
|
|
181
|
-
* ```
|
|
182
|
-
*
|
|
183
|
-
* @example Props for the factory, and a new key to build again
|
|
184
|
-
* ```tsx
|
|
185
|
-
* export const { ReactorProvider: LedgerProvider, useReactor: useLedger } =
|
|
186
|
-
* createReactorProvider(({ canisterId }: { canisterId: string }) =>
|
|
187
|
-
* defineDisplayReactor<Ledger>({ name: "ledger", idlFactory, canisterId })
|
|
188
|
-
* )
|
|
189
|
-
*
|
|
190
|
-
* // Read once per mount: the key makes a new canister a new tree.
|
|
191
|
-
* <LedgerProvider key={canisterId} canisterId={canisterId}>
|
|
192
|
-
* <TokenPage />
|
|
193
|
-
* </LedgerProvider>
|
|
194
|
-
* ```
|
|
195
|
-
*/
|
|
196
|
-
export function createReactorProvider<
|
|
197
|
-
TValue extends object,
|
|
198
|
-
// `| undefined` so that a factory whose props are optional, as in
|
|
199
|
-
// `({ host = "..." }: { host?: string } = {})`, still types them.
|
|
200
|
-
TProps extends object | undefined = {},
|
|
201
|
-
>(
|
|
202
|
-
factory: (props: TProps) => TValue,
|
|
203
|
-
options: CreateReactorProviderOptions = {}
|
|
204
|
-
): CreateReactorProviderReturn<TValue, NonNullable<TProps>> {
|
|
205
|
-
const { queryClientProvider = true } = options
|
|
206
|
-
const ReactorContext = createContext<TValue | null>(null)
|
|
207
|
-
|
|
208
|
-
// What a browser render built and has not committed yet, by the props
|
|
209
|
-
// object that render received. React throws away the state of a tree that
|
|
210
|
-
// suspends before it first commits, and renders it again, from the same
|
|
211
|
-
// element, once the promise settles. A value built afresh for that render
|
|
212
|
-
// came with an empty QueryClient, so a suspense query below, with no
|
|
213
|
-
// Suspense boundary between it and this provider, fetched again, suspended
|
|
214
|
-
// again and rebuilt the value in an endless loop of canister calls. The
|
|
215
|
-
// render that follows reuses the value instead, and so does StrictMode's
|
|
216
|
-
// second call of the initializer, which would otherwise build a value only
|
|
217
|
-
// to drop it. A server keeps nothing here: it never commits, and a
|
|
218
|
-
// module-scope element rendered for each request would hand one request's
|
|
219
|
-
// value to the next.
|
|
220
|
-
const uncommitted = new WeakMap<object, Built<TValue>>()
|
|
221
|
-
|
|
222
|
-
function ReactorProvider(
|
|
223
|
-
providerProps: ReactorProviderProps<NonNullable<TProps>>
|
|
224
|
-
): ReactElement {
|
|
225
|
-
// Once per mounted tree. A server renders each request as a tree of its
|
|
226
|
-
// own, so each request builds its own value and nothing it caches is seen
|
|
227
|
-
// by another. The props are read here only; a new `key` builds again.
|
|
228
|
-
const [built] = useState(() => {
|
|
229
|
-
const pending = uncommitted.get(providerProps)
|
|
230
|
-
if (pending) return pending
|
|
231
|
-
const { children: _children, ...props } = providerProps
|
|
232
|
-
const { value, built: authentication } = collectAuthentication(() =>
|
|
233
|
-
factory(props as unknown as TProps)
|
|
234
|
-
)
|
|
235
|
-
const next: Built<TValue> = {
|
|
236
|
-
value,
|
|
237
|
-
authentication,
|
|
238
|
-
queryClient: queryClientProvider ? soleQueryClient(value) : undefined,
|
|
239
|
-
}
|
|
240
|
-
if (!isServer()) {
|
|
241
|
-
next.pendingProps = providerProps
|
|
242
|
-
uncommitted.set(providerProps, next)
|
|
243
|
-
}
|
|
244
|
-
return next
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
// Releases the Internet Identity clients of the managers built for this
|
|
248
|
-
// value once this tree unmounts. A v10 client listens to the page until
|
|
249
|
-
// it is disposed, so each remount would otherwise leave one behind.
|
|
250
|
-
// dispose() only forgets the client: StrictMode runs this cleanup and then
|
|
251
|
-
// the effect again on the same value, and the next sign-in builds a new
|
|
252
|
-
// one. A server runs no effects, and its managers build no client.
|
|
253
|
-
useEffect(() => {
|
|
254
|
-
// Committed: the value is this mount's, and a later mount of the same
|
|
255
|
-
// element builds its own.
|
|
256
|
-
if (built.pendingProps) {
|
|
257
|
-
uncommitted.delete(built.pendingProps)
|
|
258
|
-
built.pendingProps = undefined
|
|
259
|
-
}
|
|
260
|
-
return () => disposeAuthentication(built)
|
|
261
|
-
}, [built])
|
|
262
|
-
|
|
263
|
-
const provided = createElement(
|
|
264
|
-
ReactorContext.Provider,
|
|
265
|
-
{ value: built.value },
|
|
266
|
-
providerProps.children
|
|
267
|
-
)
|
|
268
|
-
return built.queryClient
|
|
269
|
-
? createElement(
|
|
270
|
-
QueryClientProvider,
|
|
271
|
-
{ client: built.queryClient },
|
|
272
|
-
provided
|
|
273
|
-
)
|
|
274
|
-
: provided
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
function useReactor<TKey extends keyof TValue>(key: TKey): TValue[TKey]
|
|
278
|
-
function useReactor(): TValue
|
|
279
|
-
function useReactor(key?: keyof TValue) {
|
|
280
|
-
const value = useContext(ReactorContext)
|
|
281
|
-
if (value === null) {
|
|
282
|
-
throw new Error(
|
|
283
|
-
"[ic-reactor] useReactor() was called outside its <ReactorProvider>. " +
|
|
284
|
-
"Render the component below the ReactorProvider returned by the " +
|
|
285
|
-
"same createReactorProvider() call: each call has a context of its own."
|
|
286
|
-
)
|
|
287
|
-
}
|
|
288
|
-
return key === undefined ? value : value[key]
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
return { ReactorProvider, useReactor }
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/** What a provider built, and until it commits, the props it built it from. */
|
|
295
|
-
interface Built<TValue> {
|
|
296
|
-
value: TValue
|
|
297
|
-
/** The managers constructed while the factory ran. */
|
|
298
|
-
authentication: AuthenticationManager[]
|
|
299
|
-
queryClient: QueryClient | undefined
|
|
300
|
-
pendingProps?: object
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Whether this render runs on a server, as TanStack Query decides it: Deno
|
|
305
|
-
* defines `window` without being a browser.
|
|
306
|
-
*/
|
|
307
|
-
const isServer = () => typeof window === "undefined" || "Deno" in globalThis
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* The value and each of its own data properties: where a built value keeps
|
|
311
|
-
* what it built. Accessors are skipped rather than read, because reading
|
|
312
|
-
* one can build what it returns: a `defineReactor` result's `authentication`
|
|
313
|
-
* builds the manager on first read.
|
|
314
|
-
*/
|
|
315
|
-
function partsOf(value: object): object[] {
|
|
316
|
-
const parts = [value]
|
|
317
|
-
for (const descriptor of Object.values(
|
|
318
|
-
Object.getOwnPropertyDescriptors(value)
|
|
319
|
-
)) {
|
|
320
|
-
const part: unknown = descriptor.value
|
|
321
|
-
if (typeof part === "object" && part !== null) parts.push(part)
|
|
322
|
-
}
|
|
323
|
-
return parts
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
function isQueryClient(candidate: unknown): candidate is QueryClient {
|
|
327
|
-
const client = candidate as Partial<QueryClient> | null | undefined
|
|
328
|
-
return (
|
|
329
|
-
typeof client?.getQueryCache === "function" &&
|
|
330
|
-
typeof client.mount === "function"
|
|
331
|
-
)
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* The QueryClient the value uses, when it uses exactly one. A reactor, a
|
|
336
|
-
* `ClientManager` and a `defineReactor` result each bring theirs as
|
|
337
|
-
* `queryClient`.
|
|
338
|
-
*/
|
|
339
|
-
function soleQueryClient(value: object): QueryClient | undefined {
|
|
340
|
-
const clients = new Set<QueryClient>()
|
|
341
|
-
for (const part of partsOf(value)) {
|
|
342
|
-
const client = isQueryClient(part)
|
|
343
|
-
? part
|
|
344
|
-
: (part as { queryClient?: unknown }).queryClient
|
|
345
|
-
if (isQueryClient(client)) clients.add(client)
|
|
346
|
-
}
|
|
347
|
-
if (clients.size !== 1) return undefined
|
|
348
|
-
const [client] = clients
|
|
349
|
-
return client
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Disposes each `AuthenticationManager` built for the value, once each: those
|
|
354
|
-
* constructed while the factory ran, and the one each `defineReactor` result
|
|
355
|
-
* in the value or among its own properties has built since. A manager built
|
|
356
|
-
* elsewhere and handed in is left to whoever built it.
|
|
357
|
-
*/
|
|
358
|
-
function disposeAuthentication({ value, authentication }: Built<object>): void {
|
|
359
|
-
const managers = new Set(authentication)
|
|
360
|
-
for (const part of partsOf(value)) {
|
|
361
|
-
const owned = authenticationOf(part)
|
|
362
|
-
if (owned) managers.add(owned)
|
|
363
|
-
}
|
|
364
|
-
for (const manager of managers) manager.dispose()
|
|
365
|
-
}
|