@ic-reactor/react 3.12.4 → 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 +9 -20
- 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 +4 -1
- package/dist/createMutation.d.ts.map +1 -1
- package/dist/createMutation.js +121 -84
- 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 +9 -2
- package/dist/hooks/createAuthHooks.d.ts.map +1 -1
- package/dist/hooks/createAuthHooks.js +184 -24
- package/dist/hooks/createAuthHooks.js.map +1 -1
- package/dist/hooks/useActorInfiniteQuery.d.ts +36 -8
- 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 +15 -12
- 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 +17 -5
- 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 +428 -21
- 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 +50 -42
- package/src/createInfiniteQuery.ts +120 -28
- package/src/createMutation.ts +213 -132
- 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 +210 -28
- package/src/hooks/useActorInfiniteQuery.ts +156 -55
- package/src/hooks/useActorMethod.ts +295 -92
- package/src/hooks/useActorMutation.ts +42 -30
- package/src/hooks/useActorQuery.ts +43 -10
- package/src/hooks/useActorSuspenseInfiniteQuery.ts +110 -54
- 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 +492 -22
- package/src/utils.ts +387 -3
- package/src/validation.ts +43 -19
package/dist/createMutation.js
CHANGED
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
* @example
|
|
10
10
|
* const transferMutation = createMutation(reactor, {
|
|
11
11
|
* functionName: "transfer",
|
|
12
|
+
* // Refetched before onSuccess runs: a query factory covers every args
|
|
13
|
+
* // instance, a `{ functionName }` every query of that method
|
|
14
|
+
* invalidateQueries: [getBalance, { functionName: "get_history" }],
|
|
12
15
|
* onSuccess: () => console.log("Success!"),
|
|
13
16
|
* })
|
|
14
17
|
*
|
|
@@ -26,13 +29,7 @@
|
|
|
26
29
|
*/
|
|
27
30
|
import { useMutation, } from "@tanstack/react-query";
|
|
28
31
|
import { isCanisterError } from "@ic-reactor/core";
|
|
29
|
-
|
|
30
|
-
// Internal helpers
|
|
31
|
-
// ============================================================================
|
|
32
|
-
/** Invalidate a list of query keys in parallel, filtering out undefineds. */
|
|
33
|
-
async function invalidateAll(queryClient, keys) {
|
|
34
|
-
await Promise.all(keys.map((queryKey) => queryKey ? queryClient.invalidateQueries({ queryKey }) : Promise.resolve()));
|
|
35
|
-
}
|
|
32
|
+
import { invalidateTargets, useMountQueryClient } from "./utils.js";
|
|
36
33
|
// ============================================================================
|
|
37
34
|
// Internal Implementation
|
|
38
35
|
// ============================================================================
|
|
@@ -45,86 +42,49 @@ const createMutationImpl = (reactor, config) => {
|
|
|
45
42
|
*/
|
|
46
43
|
const callFn = (args) => reactor.callMethod({ functionName, args, callConfig });
|
|
47
44
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* Runs the same factory-level chain the hook does — invalidation, then
|
|
51
|
-
* `onSuccess`, or `onCanisterError`/`onError` on failure — so a mutation
|
|
52
|
-
* object behaves the same through both call paths. Only hook-level callbacks
|
|
53
|
-
* are absent, because there is no hook here to supply them.
|
|
54
|
-
*
|
|
55
|
-
* The callbacks get the same `{ client, meta, mutationKey }` context
|
|
56
|
-
* TanStack Query passes them on the hook path.
|
|
57
|
-
*
|
|
58
|
-
* The error is rethrown after the callbacks run, so `await execute(...)`
|
|
59
|
-
* still rejects for the caller.
|
|
45
|
+
* The factory's own `onMutate` result for each call.
|
|
60
46
|
*
|
|
61
|
-
*
|
|
47
|
+
* TanStack Query stores one `onMutate` result per mutation and hands it to
|
|
48
|
+
* every callback. That slot holds the hook's result, which the mutation also
|
|
49
|
+
* exposes as `context`, so this map holds the factory's. Each key is the
|
|
50
|
+
* context object TanStack Query creates for a call and passes to every
|
|
51
|
+
* callback of that call.
|
|
62
52
|
*/
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
meta: effectiveOptions.meta,
|
|
80
|
-
mutationKey: effectiveOptions.mutationKey,
|
|
81
|
-
};
|
|
82
|
-
let result;
|
|
83
|
-
try {
|
|
84
|
-
result = await callFn(args);
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
if (isCanisterError(error)) {
|
|
88
|
-
factoryOnCanisterError?.(error, args);
|
|
89
|
-
}
|
|
90
|
-
// `onMutate` does not run on this path, so there is no result to pass.
|
|
91
|
-
// Awaited like `onSuccess` below and like the hook path, so an async
|
|
92
|
-
// rollback or report finishes before `execute()` rejects.
|
|
93
|
-
await factoryOnError?.(error, args, undefined, context);
|
|
94
|
-
throw error;
|
|
95
|
-
}
|
|
96
|
-
if (factoryInvalidateQueries) {
|
|
97
|
-
await invalidateAll(reactor.queryClient, factoryInvalidateQueries);
|
|
98
|
-
}
|
|
99
|
-
await factoryOnSuccess?.(result, args, undefined, context);
|
|
100
|
-
return result;
|
|
101
|
-
};
|
|
102
|
-
// Hook implementation
|
|
103
|
-
const useMutationHook = (options) => {
|
|
104
|
-
const baseOptions = reactor.getQueryOptions({ functionName });
|
|
53
|
+
const factoryOnMutateResults = new WeakMap();
|
|
54
|
+
/**
|
|
55
|
+
* The `onMutate` result a factory callback receives. A factory without its
|
|
56
|
+
* own `onMutate` gets the hook's, as it always has.
|
|
57
|
+
*/
|
|
58
|
+
const factoryOnMutateResult = (onMutateResult, context) => (factoryOnMutate && context
|
|
59
|
+
? factoryOnMutateResults.get(context)
|
|
60
|
+
: onMutateResult);
|
|
61
|
+
/**
|
|
62
|
+
* The options a mutation of this factory runs with, on both call paths: the
|
|
63
|
+
* factory's config, the hook's options on top when there is a hook, and the
|
|
64
|
+
* callbacks of both levels chained, factory first. `useMutation()` hands
|
|
65
|
+
* them to TanStack Query's `useMutation`, and `execute()` builds a mutation
|
|
66
|
+
* from them in the QueryClient's MutationCache.
|
|
67
|
+
*/
|
|
68
|
+
const mutationOptions = (options) => {
|
|
105
69
|
const { invalidateQueries: hookInvalidateQueries, onCanisterError: hookOnCanisterError, ...restOptions } = options ?? {};
|
|
106
|
-
return
|
|
107
|
-
mutationKey:
|
|
70
|
+
return {
|
|
71
|
+
mutationKey: reactor.getQueryOptions({ functionName }).queryKey,
|
|
108
72
|
...factoryOptions,
|
|
109
73
|
...restOptions,
|
|
110
74
|
// Use callFn (not execute) to avoid double-invalidation:
|
|
111
75
|
// factoryInvalidateQueries are handled in onSuccess below.
|
|
112
76
|
mutationFn: callFn,
|
|
113
|
-
onSuccess: async (
|
|
77
|
+
onSuccess: async (data, variables, onMutateResult, context) => {
|
|
114
78
|
// 1. Factory-level invalidation
|
|
115
|
-
|
|
116
|
-
await invalidateAll(reactor.queryClient, factoryInvalidateQueries);
|
|
117
|
-
}
|
|
79
|
+
await invalidateTargets(reactor, factoryInvalidateQueries, callConfig);
|
|
118
80
|
// 2. Hook-level invalidation
|
|
119
|
-
|
|
120
|
-
await invalidateAll(reactor.queryClient, hookInvalidateQueries);
|
|
121
|
-
}
|
|
81
|
+
await invalidateTargets(reactor, hookInvalidateQueries, callConfig);
|
|
122
82
|
// 3. Factory onSuccess
|
|
123
|
-
await factoryOnSuccess?.(
|
|
83
|
+
await factoryOnSuccess?.(data, variables, factoryOnMutateResult(onMutateResult, context), context);
|
|
124
84
|
// 4. Hook onSuccess
|
|
125
|
-
await restOptions.onSuccess?.(
|
|
85
|
+
await restOptions.onSuccess?.(data, variables, onMutateResult, context);
|
|
126
86
|
},
|
|
127
|
-
onError: async (error, variables,
|
|
87
|
+
onError: async (error, variables, onMutateResult, context) => {
|
|
128
88
|
if (isCanisterError(error)) {
|
|
129
89
|
factoryOnCanisterError?.(error, variables);
|
|
130
90
|
hookOnCanisterError?.(error, variables);
|
|
@@ -132,8 +92,8 @@ const createMutationImpl = (reactor, config) => {
|
|
|
132
92
|
// Awaited in order, like `onSuccess`. TanStack Query holds
|
|
133
93
|
// `onSettled` and the settled state until this promise resolves, so
|
|
134
94
|
// an async `onError` must be part of it.
|
|
135
|
-
await factoryOnError?.(error, variables, context,
|
|
136
|
-
await restOptions.onError?.(error, variables,
|
|
95
|
+
await factoryOnError?.(error, variables, factoryOnMutateResult(onMutateResult, context), context);
|
|
96
|
+
await restOptions.onError?.(error, variables, onMutateResult, context);
|
|
137
97
|
},
|
|
138
98
|
// `onMutate` and `onSettled` are composed like `onSuccess`/`onError`
|
|
139
99
|
// above. They used to arrive through the `...restOptions` spread, so a
|
|
@@ -141,22 +101,99 @@ const createMutationImpl = (reactor, config) => {
|
|
|
141
101
|
// telemetry or logging simply vanished the moment any call site passed
|
|
142
102
|
// its own, with no warning and no type error. Chaining is what a
|
|
143
103
|
// reader who has seen `onSuccess` chain already expects.
|
|
144
|
-
onMutate: async (
|
|
145
|
-
|
|
146
|
-
|
|
104
|
+
onMutate: async (variables, context) => {
|
|
105
|
+
if (factoryOnMutate) {
|
|
106
|
+
const result = await factoryOnMutate(variables, context);
|
|
107
|
+
if (context)
|
|
108
|
+
factoryOnMutateResults.set(context, result);
|
|
109
|
+
}
|
|
110
|
+
// Without a hook-level `onMutate` this is `undefined`, and
|
|
111
|
+
// THookOnMutateResult is then `unknown`.
|
|
112
|
+
return (await restOptions.onMutate?.(variables, context));
|
|
113
|
+
},
|
|
114
|
+
onSettled: async (data, error, variables, onMutateResult, context) => {
|
|
115
|
+
await factoryOnSettled?.(data, error, variables, factoryOnMutateResult(onMutateResult, context), context);
|
|
116
|
+
await restOptions.onSettled?.(data, error, variables, onMutateResult, context);
|
|
147
117
|
},
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Imperative execution for non-React usage.
|
|
122
|
+
*
|
|
123
|
+
* Builds the mutation in the QueryClient's MutationCache and runs it, as
|
|
124
|
+
* TanStack Query's `useMutation` does, with the options the hook path uses
|
|
125
|
+
* minus the hook's own. It used to call the canister and the factory's
|
|
126
|
+
* callbacks itself, so the MutationCache never saw it: its global
|
|
127
|
+
* `onError`, `onSuccess` and `onSettled` did not run, `useIsMutating` did
|
|
128
|
+
* not count it, and neither the factory's `retry` or `networkMode` nor the
|
|
129
|
+
* same options in the QueryClient's mutation defaults applied to the call.
|
|
130
|
+
* The factory's `onMutate` and `onSettled` did not run either.
|
|
131
|
+
*
|
|
132
|
+
* Now the factory's chain runs as it does through `useMutation()`:
|
|
133
|
+
* `onMutate`, then the factory's invalidation and `onSuccess`, or
|
|
134
|
+
* `onCanisterError` and `onError` on failure, then `onSettled`. Each factory
|
|
135
|
+
* callback gets the result of the factory's `onMutate` for this call. Only
|
|
136
|
+
* hook-level callbacks are absent, because there is no hook here to supply
|
|
137
|
+
* them.
|
|
138
|
+
*
|
|
139
|
+
* The QueryClient's mutation defaults apply as well, so a `mutations.retry`
|
|
140
|
+
* there re-sends a failed call here too. Each retry of an update method is
|
|
141
|
+
* a new call the canister runs; set `retry` on the factory (`false`, or
|
|
142
|
+
* `reactorUpdateRetry` to retry only a SysTransient rejection) to decide it
|
|
143
|
+
* per method.
|
|
144
|
+
*
|
|
145
|
+
* It resolves with the method's result. On failure it rejects with the
|
|
146
|
+
* call's error once the callbacks have run, so `await execute(...)` still
|
|
147
|
+
* rejects for the caller.
|
|
148
|
+
*
|
|
149
|
+
* Use this in route loaders, scripts, or server-side code.
|
|
150
|
+
*/
|
|
151
|
+
const execute = async (args) => {
|
|
152
|
+
const { queryClient } = reactor;
|
|
153
|
+
const options = mutationOptions();
|
|
154
|
+
const { onError } = options;
|
|
155
|
+
// What the factory's `onError` or `onCanisterError` throws, if either
|
|
156
|
+
// does. execute() has always rejected with it, and a TanStack Query
|
|
157
|
+
// mutation at the 5.90.2 peer floor does too. Later releases reject with
|
|
158
|
+
// the call's error and report the thrown one as an unhandled rejection,
|
|
159
|
+
// which ends a Node script, so it is caught here and rethrown below.
|
|
160
|
+
let thrownByOnError;
|
|
161
|
+
const mutation = queryClient
|
|
162
|
+
.getMutationCache()
|
|
163
|
+
.build(queryClient, {
|
|
164
|
+
...options,
|
|
165
|
+
onError: async (error, variables, onMutateResult, context) => {
|
|
166
|
+
try {
|
|
167
|
+
await onError?.(error, variables, onMutateResult, context);
|
|
168
|
+
}
|
|
169
|
+
catch (thrown) {
|
|
170
|
+
thrownByOnError = { error: thrown };
|
|
171
|
+
}
|
|
151
172
|
},
|
|
152
|
-
}
|
|
173
|
+
});
|
|
174
|
+
try {
|
|
175
|
+
return await mutation.execute(args);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
throw thrownByOnError ? thrownByOnError.error : error;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
// Hook implementation
|
|
182
|
+
const useMutationHook = (options) => {
|
|
183
|
+
useMountQueryClient(reactor.queryClient);
|
|
184
|
+
return useMutation(mutationOptions(options), reactor.queryClient);
|
|
153
185
|
};
|
|
154
186
|
return { useMutation: useMutationHook, execute };
|
|
155
187
|
};
|
|
156
188
|
// ============================================================================
|
|
157
189
|
// Public Factory Function
|
|
158
190
|
// ============================================================================
|
|
159
|
-
export function createMutation(reactor,
|
|
191
|
+
export function createMutation(reactor,
|
|
192
|
+
// A factory `onError` or `onSettled` gets no `onMutate` result when
|
|
193
|
+
// `onMutate` throws, so the result can also be `undefined`. `onSuccess`
|
|
194
|
+
// shares the type argument. It kept the `undefined` from when `execute()`
|
|
195
|
+
// ran no `onMutate`, although it now always gets a result.
|
|
196
|
+
config) {
|
|
160
197
|
return createMutationImpl(reactor, config);
|
|
161
198
|
}
|
|
162
199
|
//# sourceMappingURL=createMutation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMutation.js","sourceRoot":"","sources":["../src/createMutation.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"createMutation.js","sourceRoot":"","sources":["../src/createMutation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;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EAAE,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,OAAO,EAAE,EAAE;gBAC3D,IAAI,CAAC;oBACH,MAAM,OAAO,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,OAAO,CAAC,CAAA;gBAC5D,CAAC;gBAAC,OAAO,MAAM,EAAE,CAAC;oBAChB,eAAe,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,CAAA;gBACrC,CAAC;YACH,CAAC;SACF,CAAC,CAAA;QACJ,IAAI,CAAC;YACH,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QACrC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,eAAe,CAAC,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAA;QACvD,CAAC;IACH,CAAC,CAAA;IAED,sBAAsB;IACtB,MAAM,eAAe,GAAG,CACtB,OAKC,EACD,EAAE;QACF,mBAAmB,CAAC,OAAO,CAAC,WAAW,CAAC,CAAA;QACxC,OAAO,WAAW,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAA;IACnE,CAAC,CAAA;IAED,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,OAAO,EAAE,CAAA;AAClD,CAAC,CAAA;AAED,+EAA+E;AAC/E,0BAA0B;AAC1B,+EAA+E;AAE/E,MAAM,UAAU,cAAc,CAM5B,OAAoC;AACpC,oEAAoE;AACpE,wEAAwE;AACxE,0EAA0E;AAC1E,2DAA2D;AAC3D,MAKC;IAED,OAAO,kBAAkB,CACvB,OAAO,EACP,MAKC,CACF,CAAA;AACH,CAAC"}
|
package/dist/createQuery.d.ts
CHANGED
|
@@ -24,7 +24,40 @@
|
|
|
24
24
|
* await userQuery.invalidate()
|
|
25
25
|
*/
|
|
26
26
|
import type { Reactor, FunctionName, ReactorArgs, TransformKey } from "@ic-reactor/core";
|
|
27
|
-
import type { QueryFnData, QueryError, QueryConfig, QueryResult, QueryFactoryConfig, NoInfer } from "./types.js";
|
|
27
|
+
import type { QueryFnData, QueryError, QueryConfig, QueryResult, QueryFactoryConfig, SkippableQueryFactoryFn, NoInfer } from "./types.js";
|
|
28
28
|
export declare function createQuery<Service, Transform extends TransformKey, Method extends FunctionName<Service> = FunctionName<Service>, Selected = QueryFnData<Service, Method, Transform>>(reactor: Reactor<Service, Transform>, config: QueryConfig<NoInfer<Service>, Method, Transform, Selected>): QueryResult<QueryFnData<Service, Method, Transform>, Selected, QueryError<Service, Method, Transform>>;
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Create a query factory: a function that takes the method's args and returns
|
|
31
|
+
* the query object for them, the same object for the same args.
|
|
32
|
+
*
|
|
33
|
+
* The function also has `getQueryKey()`, the key prefix every query it
|
|
34
|
+
* returns shares, and `invalidate()`, which invalidates all of them whatever
|
|
35
|
+
* their args. Pass the function itself to a mutation's `invalidateQueries` to
|
|
36
|
+
* refresh every instance after the mutation.
|
|
37
|
+
*
|
|
38
|
+
* It also takes TanStack Query's `skipToken` in place of args, for a
|
|
39
|
+
* component whose args are not known yet, and returns a query with only
|
|
40
|
+
* `useQuery()`, which waits without fetching; see
|
|
41
|
+
* {@link SkippableQueryFactoryFn}.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const getBalance = createQueryFactory(ledger, {
|
|
45
|
+
* functionName: "icrc1_balance_of",
|
|
46
|
+
* })
|
|
47
|
+
*
|
|
48
|
+
* // In a component
|
|
49
|
+
* const { data } = getBalance([{ owner, subaccount: [] }]).useQuery()
|
|
50
|
+
*
|
|
51
|
+
* // In a component whose owner may not be known yet
|
|
52
|
+
* const { data: maybe } = getBalance(
|
|
53
|
+
* owner ? [{ owner, subaccount: [] }] : skipToken
|
|
54
|
+
* ).useQuery()
|
|
55
|
+
*
|
|
56
|
+
* // Refetch every account's balance after a transfer
|
|
57
|
+
* const transfer = createMutation(ledger, {
|
|
58
|
+
* functionName: "icrc1_transfer",
|
|
59
|
+
* invalidateQueries: [getBalance],
|
|
60
|
+
* })
|
|
61
|
+
*/
|
|
62
|
+
export declare function createQueryFactory<Service, Transform extends TransformKey, Method extends FunctionName<Service> = FunctionName<Service>, Selected = QueryFnData<Service, Method, Transform>>(reactor: Reactor<Service, Transform>, config: QueryFactoryConfig<NoInfer<Service>, Method, Transform, Selected>): SkippableQueryFactoryFn<ReactorArgs<Service, Method, Transform>, QueryResult<QueryFnData<Service, Method, Transform>, Selected, QueryError<Service, Method, Transform>>>;
|
|
30
63
|
//# sourceMappingURL=createQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQuery.d.ts","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EACV,OAAO,EACP,YAAY,EACZ,WAAW,EACX,YAAY,EACb,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"createQuery.d.ts","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,KAAK,EACV,OAAO,EACP,YAAY,EACZ,WAAW,EACX,YAAY,EACb,MAAM,kBAAkB,CAAA;AAUzB,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EACV,WAAW,EAGX,WAAW,EACX,kBAAkB,EAClB,uBAAuB,EAEvB,OAAO,EACR,MAAM,YAAY,CAAA;AAwLnB,wBAAgB,WAAW,CACzB,OAAO,EACP,SAAS,SAAS,YAAY,EAC9B,MAAM,SAAS,YAAY,CAAC,OAAO,CAAC,GAAG,YAAY,CAAC,OAAO,CAAC,EAC5D,QAAQ,GAAG,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAElD,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,EACpC,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,CAAC,GACjE,WAAW,CACZ,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EACvC,QAAQ,EACR,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,CACvC,CAKA;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EACP,SAAS,SAAS,YAAY,EAC9B,MAAM,SAAS,YAAY,CAAC,OAAO,CAAC,GAAG,YAAY,CAAC,OAAO,CAAC,EAC5D,QAAQ,GAAG,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAElD,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,EACpC,MAAM,EAAE,kBAAkB,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,CAAC,GACxE,uBAAuB,CACxB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EACvC,WAAW,CACT,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EACvC,QAAQ,EACR,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,CACvC,CACF,CA6DA"}
|
package/dist/createQuery.js
CHANGED
|
@@ -24,33 +24,72 @@
|
|
|
24
24
|
* await userQuery.invalidate()
|
|
25
25
|
*/
|
|
26
26
|
import { useMemo } from "react";
|
|
27
|
-
import { useQuery, } from "@tanstack/react-query";
|
|
28
|
-
import { buildChainedSelect, createBoundedCache } from "./utils.js";
|
|
27
|
+
import { useQuery, skipToken, } from "@tanstack/react-query";
|
|
28
|
+
import { buildChainedSelect, createBoundedCache, pickFetchOptions, queryCacheControls, retryOption, skippedQueryKey, useMountQueryClient, withQueryFactoryMethods, } from "./utils.js";
|
|
29
29
|
// ============================================================================
|
|
30
30
|
// Internal Implementation
|
|
31
31
|
// ============================================================================
|
|
32
|
-
const createQueryImpl = (reactor,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
const createQueryImpl = (reactor,
|
|
33
|
+
// `args` is `skipToken` only for a factory's skipped query, which exposes
|
|
34
|
+
// nothing but `useQuery`; see createQueryFactory.
|
|
35
|
+
config) => {
|
|
36
|
+
const { functionName, args, callConfig, staleTime = 5 * 60 * 1000, select, queryKey: customQueryKey, ...rest } = config;
|
|
37
|
+
const skipped = args === skipToken;
|
|
38
|
+
// `callConfig` goes wherever the hooks send it: to the call and into the
|
|
39
|
+
// key, so a query of another canister or agent has an entry of its own.
|
|
40
|
+
const params = {
|
|
41
|
+
functionName,
|
|
42
|
+
args: skipped ? undefined : args,
|
|
43
|
+
queryKey: customQueryKey,
|
|
44
|
+
callConfig,
|
|
45
|
+
};
|
|
46
|
+
// What the hook observes: the call, or for a query still waiting for its
|
|
47
|
+
// args, an entry of its own under the method's key, with nothing to run;
|
|
48
|
+
// see skippedQueryKey.
|
|
49
|
+
const queryOptions = () => skipped
|
|
50
|
+
? {
|
|
51
|
+
queryKey: skippedQueryKey(reactor.generateQueryKey({ functionName }, callConfig), "query"),
|
|
52
|
+
// Kept as the unique symbol, which an object literal widens.
|
|
53
|
+
queryFn: skipToken,
|
|
54
|
+
retry: undefined,
|
|
55
|
+
}
|
|
56
|
+
: reactor.getQueryOptions(params);
|
|
57
|
+
const getQueryKey = () => reactor.generateQueryKey(params, callConfig);
|
|
36
58
|
// Apply config.select to raw data (shared by fetch, getCacheData, and the hook)
|
|
37
59
|
const applySelect = (raw) => select ? select(raw) : raw;
|
|
60
|
+
// How the query function runs, shared with the hook; see pickFetchOptions.
|
|
61
|
+
const fetchOptions = pickFetchOptions(rest);
|
|
38
62
|
/** Cache-first fetch for use in loaders / route preloading. */
|
|
39
63
|
const fetch = async () => {
|
|
40
|
-
|
|
64
|
+
// Through the reactor rather than straight to the QueryClient: overriding
|
|
65
|
+
// `fetchQuery` in a Reactor subclass is a documented way to add logic to
|
|
66
|
+
// every factory fetch.
|
|
67
|
+
const result = await reactor.fetchQuery(params, fetchOptions);
|
|
41
68
|
return applySelect(result);
|
|
42
69
|
};
|
|
43
70
|
/** Fire-and-forget prefetch — warms the cache without blocking. */
|
|
44
71
|
const prefetch = () => {
|
|
45
72
|
const baseOptions = reactor.getQueryOptions(params);
|
|
46
|
-
|
|
73
|
+
// A sign-in or sign-out cancels a prefetch in flight, and TanStack
|
|
74
|
+
// resolves it all the same: an entry nothing observes was left empty, and
|
|
75
|
+
// one a mounted query shows still held the previous principal's data
|
|
76
|
+
// until that query's refetch landed. It now runs again for the principal
|
|
77
|
+
// signed in, as `fetch()` does. Like `prefetchQuery` it never rejects, so
|
|
78
|
+
// the CallError for a principal that keeps switching is dropped too.
|
|
79
|
+
return reactor.clientManager
|
|
80
|
+
.fetchAcrossIdentitySwitch(() => reactor.queryClient.prefetchQuery({
|
|
81
|
+
...fetchOptions,
|
|
82
|
+
// An update method's default `retry`; see `Reactor.getQueryRetry`.
|
|
83
|
+
...retryOption(fetchOptions.retry, baseOptions.retry),
|
|
47
84
|
queryKey: baseOptions.queryKey,
|
|
48
85
|
queryFn: baseOptions.queryFn,
|
|
49
86
|
staleTime,
|
|
50
|
-
})
|
|
87
|
+
}))
|
|
88
|
+
.catch(() => undefined);
|
|
51
89
|
};
|
|
52
90
|
const useQueryHook = ((options) => {
|
|
53
|
-
|
|
91
|
+
useMountQueryClient(reactor.queryClient);
|
|
92
|
+
const baseOptions = queryOptions();
|
|
54
93
|
// Memoized so the observer's select-result cache can hit; see
|
|
55
94
|
// buildChainedSelect. `select` comes from the factory config and is stable.
|
|
56
95
|
const chainedSelect = useMemo(() => buildChainedSelect(select, options?.select), [options?.select]);
|
|
@@ -61,13 +100,16 @@ const createQueryImpl = (reactor, config) => {
|
|
|
61
100
|
...options,
|
|
62
101
|
queryFn: baseOptions.queryFn,
|
|
63
102
|
select: chainedSelect,
|
|
103
|
+
// The hook's `retry`, else the config's, else an update method's
|
|
104
|
+
// default; see `Reactor.getQueryRetry`.
|
|
105
|
+
...retryOption(options?.retry ?? rest.retry, baseOptions.retry),
|
|
64
106
|
}, reactor.queryClient);
|
|
65
107
|
});
|
|
66
108
|
const invalidate = async () => {
|
|
67
109
|
await reactor.queryClient.invalidateQueries({ queryKey: getQueryKey() });
|
|
68
110
|
};
|
|
69
111
|
const getCacheData = ((selectFn) => {
|
|
70
|
-
const raw = reactor.getQueryData(params);
|
|
112
|
+
const raw = reactor.getQueryData(params, callConfig);
|
|
71
113
|
if (raw === undefined)
|
|
72
114
|
return undefined;
|
|
73
115
|
const selected = applySelect(raw);
|
|
@@ -84,6 +126,7 @@ const createQueryImpl = (reactor, config) => {
|
|
|
84
126
|
getQueryKey,
|
|
85
127
|
getCacheData,
|
|
86
128
|
setData,
|
|
129
|
+
...queryCacheControls(reactor, getQueryKey),
|
|
87
130
|
};
|
|
88
131
|
};
|
|
89
132
|
// ============================================================================
|
|
@@ -95,13 +138,53 @@ export function createQuery(reactor, config) {
|
|
|
95
138
|
// ============================================================================
|
|
96
139
|
// Convenience: Create query with dynamic args
|
|
97
140
|
// ============================================================================
|
|
141
|
+
/**
|
|
142
|
+
* Create a query factory: a function that takes the method's args and returns
|
|
143
|
+
* the query object for them, the same object for the same args.
|
|
144
|
+
*
|
|
145
|
+
* The function also has `getQueryKey()`, the key prefix every query it
|
|
146
|
+
* returns shares, and `invalidate()`, which invalidates all of them whatever
|
|
147
|
+
* their args. Pass the function itself to a mutation's `invalidateQueries` to
|
|
148
|
+
* refresh every instance after the mutation.
|
|
149
|
+
*
|
|
150
|
+
* It also takes TanStack Query's `skipToken` in place of args, for a
|
|
151
|
+
* component whose args are not known yet, and returns a query with only
|
|
152
|
+
* `useQuery()`, which waits without fetching; see
|
|
153
|
+
* {@link SkippableQueryFactoryFn}.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* const getBalance = createQueryFactory(ledger, {
|
|
157
|
+
* functionName: "icrc1_balance_of",
|
|
158
|
+
* })
|
|
159
|
+
*
|
|
160
|
+
* // In a component
|
|
161
|
+
* const { data } = getBalance([{ owner, subaccount: [] }]).useQuery()
|
|
162
|
+
*
|
|
163
|
+
* // In a component whose owner may not be known yet
|
|
164
|
+
* const { data: maybe } = getBalance(
|
|
165
|
+
* owner ? [{ owner, subaccount: [] }] : skipToken
|
|
166
|
+
* ).useQuery()
|
|
167
|
+
*
|
|
168
|
+
* // Refetch every account's balance after a transfer
|
|
169
|
+
* const transfer = createMutation(ledger, {
|
|
170
|
+
* functionName: "icrc1_transfer",
|
|
171
|
+
* invalidateQueries: [getBalance],
|
|
172
|
+
* })
|
|
173
|
+
*/
|
|
98
174
|
export function createQueryFactory(reactor, config) {
|
|
99
175
|
const cache = createBoundedCache();
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
176
|
+
// One skipped query per factory, built on first use. Only its hook is
|
|
177
|
+
// handed out: until the args are known there is no call to make and no
|
|
178
|
+
// entry of their own to read or write.
|
|
179
|
+
let skippedQuery;
|
|
180
|
+
function factory(args) {
|
|
181
|
+
if (args === skipToken) {
|
|
182
|
+
skippedQuery ?? (skippedQuery = {
|
|
183
|
+
useQuery: createQueryImpl(reactor, { ...config, args: skipToken }).useQuery,
|
|
184
|
+
});
|
|
185
|
+
return skippedQuery;
|
|
186
|
+
}
|
|
187
|
+
const key = reactor.generateQueryKey({ functionName: config.functionName, args }, config.callConfig);
|
|
105
188
|
const cacheKey = JSON.stringify(key);
|
|
106
189
|
const existing = cache.get(cacheKey);
|
|
107
190
|
if (existing)
|
|
@@ -112,6 +195,10 @@ export function createQueryFactory(reactor, config) {
|
|
|
112
195
|
});
|
|
113
196
|
cache.set(cacheKey, result);
|
|
114
197
|
return result;
|
|
115
|
-
}
|
|
198
|
+
}
|
|
199
|
+
// The method's own prefix, at the canister and agent the config's
|
|
200
|
+
// `callConfig` names. A config `queryKey` follows the args segment in every
|
|
201
|
+
// instance's key, so it cannot narrow the prefix.
|
|
202
|
+
return withQueryFactoryMethods(factory, reactor, () => reactor.generateQueryKey({ functionName: config.functionName }, config.callConfig));
|
|
116
203
|
}
|
|
117
204
|
//# sourceMappingURL=createQuery.js.map
|
package/dist/createQuery.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQuery.js","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAQH,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC/B,OAAO,EAEL,QAAQ,
|
|
1
|
+
{"version":3,"file":"createQuery.js","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAQH,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC/B,OAAO,EAEL,QAAQ,EACR,SAAS,GAIV,MAAM,uBAAuB,CAAA;AAa9B,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EAChB,kBAAkB,EAClB,WAAW,EACX,eAAe,EACf,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,YAAY,CAAA;AAEnB,+EAA+E;AAC/E,0BAA0B;AAC1B,+EAA+E;AAE/E,MAAM,eAAe,GAAG,CAMtB,OAAoC;AACpC,0EAA0E;AAC1E,kDAAkD;AAClD,MAAkE,EAKlE,EAAE;IAIF,MAAM,EACJ,YAAY,EACZ,IAAI,EACJ,UAAU,EACV,SAAS,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,EACzB,MAAM,EACN,QAAQ,EAAE,cAAc,EACxB,GAAG,IAAI,EACR,GAAG,MAAM,CAAA;IAEV,MAAM,OAAO,GAAG,IAAI,KAAK,SAAS,CAAA;IAElC,yEAAyE;IACzE,wEAAwE;IACxE,MAAM,MAAM,GAAG;QACb,YAAY;QACZ,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC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