@ic-reactor/react 3.12.5 → 3.13.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/README.md +410 -44
- package/dist/auth/auth-client-compat.d.ts +122 -0
- package/dist/auth/auth-client-compat.d.ts.map +1 -0
- package/dist/auth/auth-client-compat.js +162 -0
- package/dist/auth/auth-client-compat.js.map +1 -0
- package/dist/auth/authentication-manager.d.ts +287 -5
- package/dist/auth/authentication-manager.d.ts.map +1 -1
- package/dist/auth/authentication-manager.js +920 -150
- package/dist/auth/authentication-manager.js.map +1 -1
- package/dist/auth/createIdentityAttributeHooks.d.ts.map +1 -1
- package/dist/auth/createIdentityAttributeHooks.js +36 -20
- package/dist/auth/createIdentityAttributeHooks.js.map +1 -1
- package/dist/auth/identity-attributes-manager.d.ts +2 -1
- package/dist/auth/identity-attributes-manager.d.ts.map +1 -1
- package/dist/auth/identity-attributes-manager.js +90 -6
- package/dist/auth/identity-attributes-manager.js.map +1 -1
- package/dist/auth/identity-attributes.d.ts.map +1 -1
- package/dist/auth/identity-attributes.js +57 -0
- package/dist/auth/identity-attributes.js.map +1 -1
- package/dist/auth/local-ii-probe.d.ts +12 -1
- package/dist/auth/local-ii-probe.d.ts.map +1 -1
- package/dist/auth/local-ii-probe.js +22 -3
- package/dist/auth/local-ii-probe.js.map +1 -1
- package/dist/auth/types.d.ts +48 -5
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/createActorHooks.d.ts +7 -18
- package/dist/createActorHooks.d.ts.map +1 -1
- package/dist/createActorHooks.js.map +1 -1
- package/dist/createInfiniteQuery.d.ts +51 -10
- package/dist/createInfiniteQuery.d.ts.map +1 -1
- package/dist/createInfiniteQuery.js +39 -15
- package/dist/createInfiniteQuery.js.map +1 -1
- package/dist/createMutation.d.ts +3 -0
- package/dist/createMutation.d.ts.map +1 -1
- package/dist/createMutation.js +87 -80
- package/dist/createMutation.js.map +1 -1
- package/dist/createQuery.d.ts +35 -2
- package/dist/createQuery.d.ts.map +1 -1
- package/dist/createQuery.js +104 -17
- package/dist/createQuery.js.map +1 -1
- package/dist/createReactorProvider.d.ts +158 -0
- package/dist/createReactorProvider.d.ts.map +1 -0
- package/dist/createReactorProvider.js +256 -0
- package/dist/createReactorProvider.js.map +1 -0
- package/dist/createSuspenseInfiniteQuery.d.ts +16 -9
- package/dist/createSuspenseInfiniteQuery.d.ts.map +1 -1
- package/dist/createSuspenseInfiniteQuery.js +59 -27
- package/dist/createSuspenseInfiniteQuery.js.map +1 -1
- package/dist/createSuspenseQuery.d.ts +23 -2
- package/dist/createSuspenseQuery.d.ts.map +1 -1
- package/dist/createSuspenseQuery.js +68 -21
- package/dist/createSuspenseQuery.js.map +1 -1
- package/dist/defineDisplayReactor.d.ts +43 -0
- package/dist/defineDisplayReactor.d.ts.map +1 -0
- package/dist/defineDisplayReactor.js +42 -0
- package/dist/defineDisplayReactor.js.map +1 -0
- package/dist/defineReactor.d.ts +46 -72
- package/dist/defineReactor.d.ts.map +1 -1
- package/dist/defineReactor.js +11 -176
- package/dist/defineReactor.js.map +1 -1
- package/dist/defineReactorShared.d.ts +84 -0
- package/dist/defineReactorShared.d.ts.map +1 -0
- package/dist/defineReactorShared.js +139 -0
- package/dist/defineReactorShared.js.map +1 -0
- package/dist/hooks/createAuthHooks.d.ts +7 -0
- package/dist/hooks/createAuthHooks.d.ts.map +1 -1
- package/dist/hooks/createAuthHooks.js +180 -20
- package/dist/hooks/createAuthHooks.js.map +1 -1
- package/dist/hooks/useActorInfiniteQuery.d.ts +34 -6
- package/dist/hooks/useActorInfiniteQuery.d.ts.map +1 -1
- package/dist/hooks/useActorInfiniteQuery.js +54 -21
- package/dist/hooks/useActorInfiniteQuery.js.map +1 -1
- package/dist/hooks/useActorMethod.d.ts +37 -4
- package/dist/hooks/useActorMethod.d.ts.map +1 -1
- package/dist/hooks/useActorMethod.js +201 -57
- package/dist/hooks/useActorMethod.js.map +1 -1
- package/dist/hooks/useActorMutation.d.ts +10 -7
- package/dist/hooks/useActorMutation.d.ts.map +1 -1
- package/dist/hooks/useActorMutation.js +14 -13
- package/dist/hooks/useActorMutation.js.map +1 -1
- package/dist/hooks/useActorQuery.d.ts +17 -4
- package/dist/hooks/useActorQuery.d.ts.map +1 -1
- package/dist/hooks/useActorQuery.js +30 -9
- package/dist/hooks/useActorQuery.js.map +1 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +15 -3
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +1 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.js +37 -17
- package/dist/hooks/useActorSuspenseInfiniteQuery.js.map +1 -1
- package/dist/hooks/useActorSuspenseQuery.d.ts +2 -2
- package/dist/hooks/useActorSuspenseQuery.d.ts.map +1 -1
- package/dist/hooks/useActorSuspenseQuery.js +20 -9
- package/dist/hooks/useActorSuspenseQuery.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/ownedAuthentication.d.ts +52 -0
- package/dist/ownedAuthentication.d.ts.map +1 -0
- package/dist/ownedAuthentication.js +49 -0
- package/dist/ownedAuthentication.js.map +1 -0
- package/dist/server.d.ts +21 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +23 -0
- package/dist/server.js.map +1 -0
- package/dist/testing.d.ts +19 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +19 -0
- package/dist/testing.js.map +1 -0
- package/dist/types.d.ts +416 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/utils.d.ts +159 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +301 -1
- package/dist/utils.js.map +1 -1
- package/dist/validation.d.ts +12 -7
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +34 -15
- package/dist/validation.js.map +1 -1
- package/llms.txt +259 -33
- package/package.json +17 -5
- package/src/auth/auth-client-compat.ts +273 -0
- package/src/auth/authentication-manager.ts +918 -96
- package/src/auth/createIdentityAttributeHooks.ts +47 -21
- package/src/auth/identity-attributes-manager.ts +100 -5
- package/src/auth/identity-attributes.ts +75 -0
- package/src/auth/local-ii-probe.ts +29 -3
- package/src/auth/types.ts +49 -6
- package/src/createActorHooks.ts +20 -32
- package/src/createInfiniteQuery.ts +120 -28
- package/src/createMutation.ts +161 -178
- package/src/createQuery.ts +164 -32
- package/src/createReactorProvider.ts +365 -0
- package/src/createSuspenseInfiniteQuery.ts +93 -43
- package/src/createSuspenseQuery.ts +102 -32
- package/src/defineDisplayReactor.ts +62 -0
- package/src/defineReactor.ts +81 -263
- package/src/defineReactorShared.ts +268 -0
- package/src/hooks/createAuthHooks.ts +206 -24
- package/src/hooks/useActorInfiniteQuery.ts +122 -49
- package/src/hooks/useActorMethod.ts +295 -92
- package/src/hooks/useActorMutation.ts +23 -23
- package/src/hooks/useActorQuery.ts +43 -10
- package/src/hooks/useActorSuspenseInfiniteQuery.ts +93 -50
- package/src/hooks/useActorSuspenseQuery.ts +30 -15
- package/src/index.ts +8 -0
- package/src/ownedAuthentication.ts +81 -0
- package/src/server.ts +23 -0
- package/src/testing.ts +18 -0
- package/src/types.ts +463 -14
- package/src/utils.ts +387 -3
- package/src/validation.ts +43 -19
|
@@ -10,6 +10,13 @@ export interface UseAuthReturn {
|
|
|
10
10
|
login: (options?: AuthenticationSignInOptions) => Promise<void>
|
|
11
11
|
logout: (options?: { returnTo?: string }) => Promise<void>
|
|
12
12
|
isAuthenticated: boolean
|
|
13
|
+
/**
|
|
14
|
+
* `true` while a sign-in or sign-out is in progress, and until the session
|
|
15
|
+
* restore the first `useAuth()` starts has settled: on the first render, in
|
|
16
|
+
* a server render and while the stored session is read. A restore that fails
|
|
17
|
+
* settles it too. Show a loading state while it is `true` rather than
|
|
18
|
+
* treating `isAuthenticated: false` as signed out.
|
|
19
|
+
*/
|
|
13
20
|
isAuthenticating: boolean
|
|
14
21
|
/**
|
|
15
22
|
* The signed-in user's principal, or `null` while signed out. A signed-out
|
|
@@ -27,6 +34,31 @@ export interface CreateAuthHooksReturn {
|
|
|
27
34
|
useAuth: () => UseAuthReturn
|
|
28
35
|
}
|
|
29
36
|
|
|
37
|
+
/**
|
|
38
|
+
* The managers whose session a `useAuth()` has already started restoring.
|
|
39
|
+
*
|
|
40
|
+
* Restoring ends in `authenticate()`, which publishes `isAuthenticating: true`
|
|
41
|
+
* and then the restored state. It used to run once per mounted `useAuth()`
|
|
42
|
+
* rather than once per manager, so every consumer that mounted while signed out
|
|
43
|
+
* flipped `isAuthenticating` for the whole app, and one that mounted while a
|
|
44
|
+
* sign-in popup was open cleared `isAuthenticating` before the sign-in ended. A
|
|
45
|
+
* component that hides a `useAuth()` consumer while `isAuthenticating` never
|
|
46
|
+
* settled: each restore unmounted the consumer, and each remount restored
|
|
47
|
+
* again. Both clients answer from memory once loaded, so that loop ran on the
|
|
48
|
+
* microtask queue and the page never painted again.
|
|
49
|
+
*
|
|
50
|
+
* Keyed by manager rather than by `createAuthHooks` call, because reactors that
|
|
51
|
+
* share one `AuthenticationManager` each build their own hooks.
|
|
52
|
+
*/
|
|
53
|
+
const restoredSessions = new WeakSet<AuthenticationManager>()
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* How many `useAuth()` consumers of each manager are mounted, so that a
|
|
57
|
+
* restore can tell a manager that was discarded from one StrictMode released
|
|
58
|
+
* and mounted again; see `useAuth()`.
|
|
59
|
+
*/
|
|
60
|
+
const mountedConsumers = new WeakMap<AuthenticationManager, number>()
|
|
61
|
+
|
|
30
62
|
/**
|
|
31
63
|
* The principal both hooks return.
|
|
32
64
|
*
|
|
@@ -47,6 +79,31 @@ function usePrincipal(
|
|
|
47
79
|
)
|
|
48
80
|
}
|
|
49
81
|
|
|
82
|
+
/**
|
|
83
|
+
* The auth state a server render shows, and so the one hydration shows.
|
|
84
|
+
*
|
|
85
|
+
* `useSyncExternalStore` renders `getServerSnapshot` on the server and again
|
|
86
|
+
* while hydrating, and both renders have to produce the HTML the server sent.
|
|
87
|
+
* The hooks passed the live state for both. A server holds no session, so its
|
|
88
|
+
* HTML is signed out. A component that hydrates after the session is restored,
|
|
89
|
+
* such as one inside a Suspense boundary whose code arrives later, read the
|
|
90
|
+
* restored state and no longer matched that HTML. React then reported a
|
|
91
|
+
* hydration error and threw the boundary's server HTML away to render it again
|
|
92
|
+
* on the client. A constant also keeps a server render from showing whatever a
|
|
93
|
+
* manager shared across requests happens to hold.
|
|
94
|
+
*
|
|
95
|
+
* It is the state the hooks report before the session has been checked: a
|
|
96
|
+
* server cannot know the session, so it renders the "checking" state that a
|
|
97
|
+
* browser shows until its restore settles. Once hydrated, React compares it
|
|
98
|
+
* with the live state and renders again with that.
|
|
99
|
+
*/
|
|
100
|
+
const SERVER_AUTH_STATE: AuthState = Object.freeze({
|
|
101
|
+
identity: null,
|
|
102
|
+
isAuthenticating: true,
|
|
103
|
+
isAuthenticated: false,
|
|
104
|
+
error: undefined,
|
|
105
|
+
})
|
|
106
|
+
|
|
50
107
|
/**
|
|
51
108
|
* Create authentication hooks for managing user sessions with Internet Identity.
|
|
52
109
|
*
|
|
@@ -83,16 +140,71 @@ export const createAuthHooks = (
|
|
|
83
140
|
}
|
|
84
141
|
|
|
85
142
|
const { clientManager } = authentication
|
|
143
|
+
|
|
144
|
+
// The agent state a manager starts in, for the reason given at
|
|
145
|
+
// SERVER_AUTH_STATE: `useAuth()` initializes the agent once hydrated, so a
|
|
146
|
+
// component hydrating later read `isInitialized: true` against server HTML
|
|
147
|
+
// rendered before any initialization. The network is kept: it comes from the
|
|
148
|
+
// agent's host, which initialization does not change.
|
|
149
|
+
const serverAgentState: AgentState = Object.freeze({
|
|
150
|
+
isInitialized: false,
|
|
151
|
+
isInitializing: false,
|
|
152
|
+
error: undefined,
|
|
153
|
+
network: clientManager.network,
|
|
154
|
+
isLocalhost: clientManager.isLocal,
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
// What every consumer hands `useSyncExternalStore`, built once rather than
|
|
158
|
+
// on each render. React unsubscribes and subscribes again whenever the
|
|
159
|
+
// subscribe function changes, and each unsubscribe filters the manager's
|
|
160
|
+
// whole subscriber list. An auth state change re-renders every consumer, so
|
|
161
|
+
// a function per render cost k re-subscriptions and about k²/2 subscriber
|
|
162
|
+
// visits for k consumers: two million for 2,000 `useUserPrincipal()` rows.
|
|
163
|
+
const subscribeAgentState = (callback: () => void) =>
|
|
164
|
+
clientManager.subscribeAgentState(callback)
|
|
165
|
+
const getAgentState = () => clientManager.agentState
|
|
166
|
+
const getServerAgentState = () => serverAgentState
|
|
167
|
+
|
|
168
|
+
// Until the session has been checked, `authState` is the signed-out state
|
|
169
|
+
// the manager starts in, and reporting it as the answer sent signed-in users
|
|
170
|
+
// to the login page (#621). `useAuth()` starts the check from an effect, so
|
|
171
|
+
// on the first render nothing had been checked, yet `isAuthenticating` was
|
|
172
|
+
// false: a guard that redirects when `!isAuthenticating && !isAuthenticated`
|
|
173
|
+
// redirected before the session was ever read. The hooks report
|
|
174
|
+
// `isAuthenticating: true` until the check settles instead. The snapshot is
|
|
175
|
+
// kept per source state, because `useSyncExternalStore` needs the same object
|
|
176
|
+
// back until something changes.
|
|
177
|
+
let unchecked: { source: AuthState; state: AuthState } | undefined
|
|
178
|
+
const subscribeAuthState = (callback: () => void) => {
|
|
179
|
+
const unsubscribeState = authentication.subscribeAuthState(callback)
|
|
180
|
+
const unsubscribeChecked = authentication.subscribeSessionChecked(callback)
|
|
181
|
+
return () => {
|
|
182
|
+
unsubscribeState()
|
|
183
|
+
unsubscribeChecked()
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const getAuthState = () => {
|
|
187
|
+
const state = authentication.authState
|
|
188
|
+
if (authentication.sessionChecked || state.isAuthenticating) return state
|
|
189
|
+
if (unchecked?.source !== state) {
|
|
190
|
+
unchecked = {
|
|
191
|
+
source: state,
|
|
192
|
+
state: Object.freeze({ ...state, isAuthenticating: true }),
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return unchecked.state
|
|
196
|
+
}
|
|
197
|
+
const getServerAuthState = () => SERVER_AUTH_STATE
|
|
198
|
+
|
|
86
199
|
/**
|
|
87
200
|
* Subscribe to agent state changes.
|
|
88
201
|
* Returns the current agent state (agent, isInitialized, etc.)
|
|
89
202
|
*/
|
|
90
203
|
const useAgentState = (): AgentState =>
|
|
91
204
|
useSyncExternalStore(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
() => clientManager.agentState
|
|
205
|
+
subscribeAgentState,
|
|
206
|
+
getAgentState,
|
|
207
|
+
getServerAgentState
|
|
96
208
|
)
|
|
97
209
|
|
|
98
210
|
/**
|
|
@@ -100,17 +212,17 @@ export const createAuthHooks = (
|
|
|
100
212
|
* Returns auth state (isAuthenticated, isAuthenticating, identity, error)
|
|
101
213
|
*/
|
|
102
214
|
const useAuthState = (): AuthState =>
|
|
103
|
-
useSyncExternalStore(
|
|
104
|
-
(callback) => authentication.subscribeAuthState(callback),
|
|
105
|
-
() => authentication.authState,
|
|
106
|
-
// Server snapshot - provide initial state for SSR
|
|
107
|
-
() => authentication.authState
|
|
108
|
-
)
|
|
215
|
+
useSyncExternalStore(subscribeAuthState, getAuthState, getServerAuthState)
|
|
109
216
|
|
|
110
217
|
/**
|
|
111
218
|
* Main authentication hook that provides login/logout methods and auth state.
|
|
112
219
|
* Automatically initializes the session on first use, restoring any previous session.
|
|
113
220
|
*
|
|
221
|
+
* `isAuthenticating` is `true` until that restore has settled, including on
|
|
222
|
+
* the first render and in a server render, so a guard that shows a spinner
|
|
223
|
+
* while `isAuthenticating` never takes the state before the restore for a
|
|
224
|
+
* signed-out user. A restore that fails settles it too.
|
|
225
|
+
*
|
|
114
226
|
* @example
|
|
115
227
|
* function AuthButton() {
|
|
116
228
|
* const { login, logout, isAuthenticated, isAuthenticating } = useAuth()
|
|
@@ -130,24 +242,94 @@ export const createAuthHooks = (
|
|
|
130
242
|
const { isAuthenticated, isAuthenticating, identity, error } =
|
|
131
243
|
useAuthState()
|
|
132
244
|
|
|
133
|
-
//
|
|
245
|
+
// Keeps a StrictMode re-run of the effect below from repeating it.
|
|
134
246
|
const initializedRef = useRef(false)
|
|
135
247
|
|
|
136
|
-
//
|
|
137
|
-
// `prepareClient` also warms up the AuthClient so a later
|
|
138
|
-
// open the identity provider window inside the click handler.
|
|
248
|
+
// Restore the previous session when the first consumer of this manager
|
|
249
|
+
// mounts. `prepareClient` also warms up the AuthClient so a later
|
|
250
|
+
// `login()` can open the identity provider window inside the click handler.
|
|
139
251
|
useEffect(() => {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
authentication
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
.catch(() => undefined)
|
|
252
|
+
mountedConsumers.set(
|
|
253
|
+
authentication,
|
|
254
|
+
(mountedConsumers.get(authentication) ?? 0) + 1
|
|
255
|
+
)
|
|
256
|
+
const unmount = () => {
|
|
257
|
+
mountedConsumers.set(
|
|
258
|
+
authentication,
|
|
259
|
+
(mountedConsumers.get(authentication) ?? 1) - 1
|
|
260
|
+
)
|
|
150
261
|
}
|
|
262
|
+
|
|
263
|
+
if (initializedRef.current && restoredSessions.has(authentication)) {
|
|
264
|
+
return unmount
|
|
265
|
+
}
|
|
266
|
+
const firstRun = !initializedRef.current
|
|
267
|
+
initializedRef.current = true
|
|
268
|
+
|
|
269
|
+
if (restoredSessions.has(authentication)) {
|
|
270
|
+
// Restored already. A live session is still checked again, which
|
|
271
|
+
// notices a delegation that has lapsed since and publishes nothing
|
|
272
|
+
// while it is valid.
|
|
273
|
+
if (firstRun && authentication.authState.isAuthenticated) {
|
|
274
|
+
authentication.authenticate().catch(() => undefined)
|
|
275
|
+
}
|
|
276
|
+
return unmount
|
|
277
|
+
}
|
|
278
|
+
restoredSessions.add(authentication)
|
|
279
|
+
|
|
280
|
+
// A provider that builds its managers per mount disposes the manager
|
|
281
|
+
// from its cleanup. One that unmounted before this restore had built
|
|
282
|
+
// the client still got one, built after `dispose()` and never released,
|
|
283
|
+
// so each quick remount left a live client listening to the page. The
|
|
284
|
+
// restore stops instead once the manager has been disposed since it
|
|
285
|
+
// began and no `useAuth()` of it is mounted, releasing what it built.
|
|
286
|
+
// StrictMode's cleanup disposes the manager too, but mounts this
|
|
287
|
+
// consumer again before the restore looks, and that restore goes on.
|
|
288
|
+
// One that stopped counts as not having run, so a manager mounted again,
|
|
289
|
+
// as `<Activity>` mounts a tree it showed again, restores.
|
|
290
|
+
const releases = authentication.releaseCount
|
|
291
|
+
const abandoned = () => {
|
|
292
|
+
if (
|
|
293
|
+
authentication.releaseCount === releases ||
|
|
294
|
+
(mountedConsumers.get(authentication) ?? 0) > 0
|
|
295
|
+
) {
|
|
296
|
+
return false
|
|
297
|
+
}
|
|
298
|
+
authentication.dispose()
|
|
299
|
+
restoredSessions.delete(authentication)
|
|
300
|
+
return true
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
void authentication
|
|
304
|
+
.prepareClient()
|
|
305
|
+
.catch(() => undefined)
|
|
306
|
+
.then(async () => {
|
|
307
|
+
if (abandoned()) return false
|
|
308
|
+
await clientManager.initialize()
|
|
309
|
+
if (abandoned()) return false
|
|
310
|
+
// A check that has settled already and found no session, such as
|
|
311
|
+
// the one a manager runs over a client handed to its constructor,
|
|
312
|
+
// or a route loader's `authenticate()`, answered what this restore
|
|
313
|
+
// would. Running it again only flashed `isAuthenticating`, and a
|
|
314
|
+
// guard redirected a second time. A live session is still checked,
|
|
315
|
+
// which publishes nothing while it is valid.
|
|
316
|
+
const { isAuthenticated, error } = authentication.authState
|
|
317
|
+
if (authentication.sessionChecked && !isAuthenticated && !error) {
|
|
318
|
+
return true
|
|
319
|
+
}
|
|
320
|
+
await authentication.authenticate().catch(() => undefined)
|
|
321
|
+
return !abandoned()
|
|
322
|
+
})
|
|
323
|
+
// Failures are already reflected in authState/agentState; without
|
|
324
|
+
// this the rejection escapes as an unhandled promise rejection. A
|
|
325
|
+
// restore that failed before it reached `authenticate()`, such as a
|
|
326
|
+
// root key that could not be fetched, has settled all the same.
|
|
327
|
+
.catch(() => true)
|
|
328
|
+
.then((settled) => {
|
|
329
|
+
if (settled) authentication.markSessionChecked()
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
return unmount
|
|
151
333
|
}, [])
|
|
152
334
|
|
|
153
335
|
const principal = usePrincipal(isAuthenticated, identity)
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { useMemo
|
|
1
|
+
import { useMemo } from "react"
|
|
2
2
|
import {
|
|
3
3
|
QueryKey,
|
|
4
4
|
useInfiniteQuery,
|
|
5
|
+
skipToken,
|
|
5
6
|
UseInfiniteQueryResult,
|
|
6
7
|
UseInfiniteQueryOptions,
|
|
7
8
|
InfiniteData,
|
|
9
|
+
type SkipToken,
|
|
8
10
|
} from "@tanstack/react-query"
|
|
9
11
|
import {
|
|
10
12
|
FunctionName,
|
|
@@ -16,7 +18,14 @@ import {
|
|
|
16
18
|
ReactorReturnErr,
|
|
17
19
|
} from "@ic-reactor/core"
|
|
18
20
|
import { CallConfig } from "@icp-sdk/core/agent"
|
|
19
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
callConfigForKey,
|
|
23
|
+
mergeFactoryQueryKey,
|
|
24
|
+
normalizeQueryData,
|
|
25
|
+
retryOption,
|
|
26
|
+
skippedQueryKey,
|
|
27
|
+
useMountQueryClient,
|
|
28
|
+
} from "../utils.js"
|
|
20
29
|
|
|
21
30
|
/**
|
|
22
31
|
* Parameters for useActorInfiniteQuery hook.
|
|
@@ -45,8 +54,27 @@ export interface UseActorInfiniteQueryParameters<
|
|
|
45
54
|
reactor: Reactor<Service, Transform>
|
|
46
55
|
/** The method name to call on the canister */
|
|
47
56
|
functionName: Method
|
|
48
|
-
/**
|
|
49
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Function to get args from page parameter, or TanStack Query's `skipToken`
|
|
59
|
+
* while the args are not known: the query then waits without fetching, in
|
|
60
|
+
* an entry of its own under its method and `queryKey`, which no call's key
|
|
61
|
+
* shares, so it shows no data until the args arrive.
|
|
62
|
+
*/
|
|
63
|
+
getArgs:
|
|
64
|
+
| ((pageParam: TPageParam) => ReactorArgs<Service, Method, Transform>)
|
|
65
|
+
| SkipToken
|
|
66
|
+
/**
|
|
67
|
+
* Narrows what the cache key derives from the call arguments.
|
|
68
|
+
*
|
|
69
|
+
* By default the key is scoped by `getArgs(initialPageParam)`, so two
|
|
70
|
+
* infinite queries on the same method with different arguments stay in
|
|
71
|
+
* separate cache entries. Supply this when those args embed the cursor and
|
|
72
|
+
* only part of them identifies the query — return the stable, serializable
|
|
73
|
+
* portion (typically everything except the pagination field). It is required
|
|
74
|
+
* when `initialPageParam` changes between renders, as `Date.now()` does:
|
|
75
|
+
* without it every render keys a new query.
|
|
76
|
+
*/
|
|
77
|
+
getKeyArgs?: (args: ReactorArgs<Service, Method, Transform>) => unknown
|
|
50
78
|
/** Agent call configuration (effectiveCanisterId, etc.) */
|
|
51
79
|
callConfig?: CallConfig
|
|
52
80
|
/** Custom query key (auto-generated if not provided) */
|
|
@@ -100,10 +128,21 @@ export type UseActorInfiniteQueryResult<
|
|
|
100
128
|
* Hook for executing infinite/paginated query calls on a canister.
|
|
101
129
|
*
|
|
102
130
|
* @example
|
|
103
|
-
* const { data, fetchNextPage, hasNextPage } =
|
|
131
|
+
* const { data, fetchNextPage, hasNextPage } = useReactorInfiniteQuery({
|
|
104
132
|
* reactor,
|
|
105
133
|
* functionName: "getItems",
|
|
106
|
-
* getArgs: (pageParam) => [{ offset: pageParam, limit: 10 }],
|
|
134
|
+
* getArgs: (pageParam) => [{ offset: pageParam, limit: 10 }] as const,
|
|
135
|
+
* initialPageParam: 0,
|
|
136
|
+
* getNextPageParam: (lastPage) => lastPage.nextOffset,
|
|
137
|
+
* })
|
|
138
|
+
*
|
|
139
|
+
* // Wait for the args: no fetch until userId is known
|
|
140
|
+
* const { data } = useReactorInfiniteQuery({
|
|
141
|
+
* reactor,
|
|
142
|
+
* functionName: "getUserItems",
|
|
143
|
+
* getArgs: userId
|
|
144
|
+
* ? (pageParam) => [{ userId, offset: pageParam, limit: 10 }] as const
|
|
145
|
+
* : skipToken,
|
|
107
146
|
* initialPageParam: 0,
|
|
108
147
|
* getNextPageParam: (lastPage) => lastPage.nextOffset,
|
|
109
148
|
* })
|
|
@@ -121,6 +160,7 @@ export const useActorInfiniteQuery = <
|
|
|
121
160
|
reactor,
|
|
122
161
|
functionName,
|
|
123
162
|
getArgs,
|
|
163
|
+
getKeyArgs,
|
|
124
164
|
callConfig,
|
|
125
165
|
queryKey,
|
|
126
166
|
...options
|
|
@@ -137,62 +177,95 @@ export const useActorInfiniteQuery = <
|
|
|
137
177
|
TPageParam,
|
|
138
178
|
Selected
|
|
139
179
|
> => {
|
|
180
|
+
useMountQueryClient(reactor.queryClient)
|
|
181
|
+
|
|
140
182
|
// Always pass queryKey through generateQueryKey so it is merged with the
|
|
141
183
|
// reactor/function identity. Using the custom key verbatim would cause cache
|
|
142
184
|
// collisions if two different actors or methods share the same key string.
|
|
143
|
-
const baseQueryKey = useMemo(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
// while the queryFn already calls the new one.
|
|
167
|
-
reactor.canisterId?.toString(),
|
|
168
|
-
functionName,
|
|
169
|
-
callConfig,
|
|
170
|
-
getArgs,
|
|
171
|
-
options.initialPageParam,
|
|
172
|
-
]
|
|
173
|
-
)
|
|
185
|
+
const baseQueryKey = useMemo(() => {
|
|
186
|
+
// Waiting for its args: an entry of its own under the method and the
|
|
187
|
+
// custom key, which every key the args will give extends; see
|
|
188
|
+
// skippedQueryKey.
|
|
189
|
+
if (getArgs === skipToken) {
|
|
190
|
+
return skippedQueryKey(
|
|
191
|
+
reactor.generateQueryKey(
|
|
192
|
+
{ functionName, queryKey: mergeFactoryQueryKey(queryKey) },
|
|
193
|
+
callConfig
|
|
194
|
+
),
|
|
195
|
+
"infinite"
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
// Fold the call arguments into the key. They live in the `getArgs`
|
|
199
|
+
// closure rather than in the config, so without this two hooks on the
|
|
200
|
+
// same method with different arguments share one cache entry and serve
|
|
201
|
+
// each other's pages. `getKeyArgs` narrows them exactly as it does for the
|
|
202
|
+
// factories, which the bound hook's config type always accepted: it used
|
|
203
|
+
// to be ignored here, so the cursor stayed in the key, and an
|
|
204
|
+
// `initialPageParam` that changes every render (`Date.now()`) keyed a new
|
|
205
|
+
// query on every render — an endless loop of first-page fetches.
|
|
206
|
+
const initialArgs = getArgs(options.initialPageParam)
|
|
207
|
+
const keyArgs = getKeyArgs?.(initialArgs) ?? initialArgs
|
|
174
208
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
async ({ pageParam }: { pageParam: TPageParam }) => {
|
|
178
|
-
const args = getArgs(pageParam)
|
|
179
|
-
const result = await reactor.callMethod({
|
|
209
|
+
return reactor.generateQueryKey(
|
|
210
|
+
{
|
|
180
211
|
functionName,
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
212
|
+
queryKey: mergeFactoryQueryKey(queryKey, undefined, keyArgs),
|
|
213
|
+
},
|
|
214
|
+
callConfig
|
|
215
|
+
)
|
|
216
|
+
}, [
|
|
217
|
+
queryKey,
|
|
218
|
+
reactor,
|
|
219
|
+
// `canisterId` is mutable reactor state that `setCanisterId` can change,
|
|
220
|
+
// while `reactor` itself stays the same object — so it has to be a
|
|
221
|
+
// dependency in its own right or the key stays pinned to the old canister
|
|
222
|
+
// while the queryFn already calls the new one.
|
|
223
|
+
reactor.canisterId?.toString(),
|
|
224
|
+
functionName,
|
|
225
|
+
callConfig,
|
|
226
|
+
getArgs,
|
|
227
|
+
getKeyArgs,
|
|
228
|
+
options.initialPageParam,
|
|
229
|
+
])
|
|
230
|
+
|
|
231
|
+
// Memoize queryFn to prevent recreation on every render
|
|
232
|
+
const queryFn = useMemo(
|
|
233
|
+
() =>
|
|
234
|
+
getArgs === skipToken
|
|
235
|
+
? skipToken
|
|
236
|
+
: async ({
|
|
237
|
+
pageParam,
|
|
238
|
+
queryKey: fetchedKey,
|
|
239
|
+
}: {
|
|
240
|
+
pageParam: TPageParam
|
|
241
|
+
queryKey: QueryKey
|
|
242
|
+
}) => {
|
|
243
|
+
const args = getArgs(pageParam)
|
|
244
|
+
const result = await reactor.callMethod({
|
|
245
|
+
functionName,
|
|
246
|
+
args,
|
|
247
|
+
callConfig: callConfigForKey(fetchedKey, callConfig),
|
|
248
|
+
})
|
|
249
|
+
return normalizeQueryData<
|
|
250
|
+
ReactorReturnOk<Service, Method, Transform>
|
|
251
|
+
>(result as ReactorReturnOk<Service, Method, Transform>)
|
|
252
|
+
},
|
|
188
253
|
[reactor, functionName, getArgs, callConfig]
|
|
189
254
|
)
|
|
190
255
|
|
|
256
|
+
// The method's default `retry`: for an update method, only failures that
|
|
257
|
+
// prove the canister never ran the call; see `Reactor.getQueryRetry`.
|
|
258
|
+
const defaultRetry = useMemo(
|
|
259
|
+
() => reactor.getQueryRetry(functionName, baseQueryKey),
|
|
260
|
+
[reactor, functionName, baseQueryKey]
|
|
261
|
+
)
|
|
262
|
+
|
|
191
263
|
return useInfiniteQuery(
|
|
192
264
|
{
|
|
193
265
|
queryKey: baseQueryKey,
|
|
194
266
|
queryFn,
|
|
195
267
|
...options,
|
|
268
|
+
...retryOption(options.retry, defaultRetry),
|
|
196
269
|
} as any,
|
|
197
270
|
reactor.queryClient
|
|
198
271
|
) as UseActorInfiniteQueryResult<
|