@ic-reactor/react 3.12.5 → 4.0.0-beta.1
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 +236 -423
- package/dist/index.d.ts +259 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +470 -17
- package/dist/index.js.map +1 -1
- package/llms.txt +82 -54
- package/package.json +12 -28
- package/src/index.tsx +611 -0
- package/dist/auth/authentication-manager.d.ts +0 -123
- package/dist/auth/authentication-manager.d.ts.map +0 -1
- package/dist/auth/authentication-manager.js +0 -767
- 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 -106
- package/dist/auth/createIdentityAttributeHooks.js.map +0 -1
- package/dist/auth/identity-attributes-manager.d.ts +0 -26
- package/dist/auth/identity-attributes-manager.d.ts.map +0 -1
- package/dist/auth/identity-attributes-manager.js +0 -107
- 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 -170
- 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 -46
- package/dist/auth/local-ii-probe.d.ts.map +0 -1
- package/dist/auth/local-ii-probe.js +0 -102
- package/dist/auth/local-ii-probe.js.map +0 -1
- package/dist/auth/types.d.ts +0 -179
- 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 -52
- 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 -144
- package/dist/createInfiniteQuery.d.ts.map +0 -1
- package/dist/createInfiniteQuery.js +0 -174
- package/dist/createInfiniteQuery.js.map +0 -1
- package/dist/createMutation.d.ts +0 -30
- package/dist/createMutation.d.ts.map +0 -1
- package/dist/createMutation.js +0 -192
- package/dist/createMutation.js.map +0 -1
- package/dist/createQuery.d.ts +0 -30
- package/dist/createQuery.d.ts.map +0 -1
- package/dist/createQuery.js +0 -117
- package/dist/createQuery.js.map +0 -1
- package/dist/createSuspenseInfiniteQuery.d.ts +0 -147
- package/dist/createSuspenseInfiniteQuery.d.ts.map +0 -1
- package/dist/createSuspenseInfiniteQuery.js +0 -177
- package/dist/createSuspenseInfiniteQuery.js.map +0 -1
- package/dist/createSuspenseQuery.d.ts +0 -25
- package/dist/createSuspenseQuery.d.ts.map +0 -1
- package/dist/createSuspenseQuery.js +0 -111
- package/dist/createSuspenseQuery.js.map +0 -1
- package/dist/defineReactor.d.ts +0 -125
- package/dist/defineReactor.d.ts.map +0 -1
- package/dist/defineReactor.js +0 -180
- package/dist/defineReactor.js.map +0 -1
- package/dist/hooks/createAuthHooks.d.ts +0 -43
- package/dist/hooks/createAuthHooks.d.ts.map +0 -1
- package/dist/hooks/createAuthHooks.js +0 -131
- 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 -39
- package/dist/hooks/useActorInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useActorInfiniteQuery.js +0 -56
- package/dist/hooks/useActorInfiniteQuery.js.map +0 -1
- package/dist/hooks/useActorMethod.d.ts +0 -115
- package/dist/hooks/useActorMethod.d.ts.map +0 -1
- package/dist/hooks/useActorMethod.js +0 -250
- package/dist/hooks/useActorMethod.js.map +0 -1
- package/dist/hooks/useActorMutation.d.ts +0 -48
- package/dist/hooks/useActorMutation.d.ts.map +0 -1
- package/dist/hooks/useActorMutation.js +0 -69
- package/dist/hooks/useActorMutation.js.map +0 -1
- package/dist/hooks/useActorQuery.d.ts +0 -32
- package/dist/hooks/useActorQuery.d.ts.map +0 -1
- package/dist/hooks/useActorQuery.js +0 -46
- package/dist/hooks/useActorQuery.js.map +0 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts +0 -39
- package/dist/hooks/useActorSuspenseInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useActorSuspenseInfiniteQuery.js +0 -56
- 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 -47
- package/dist/hooks/useActorSuspenseQuery.js.map +0 -1
- package/dist/types.d.ts +0 -270
- 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 -51
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -105
- package/dist/utils.js.map +0 -1
- package/dist/validation.d.ts +0 -131
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -125
- package/dist/validation.js.map +0 -1
- package/src/auth/authentication-manager.ts +0 -860
- package/src/auth/constants.ts +0 -32
- package/src/auth/createIdentityAttributeHooks.ts +0 -143
- package/src/auth/identity-attributes-manager.ts +0 -131
- package/src/auth/identity-attributes.ts +0 -270
- package/src/auth/index.ts +0 -7
- package/src/auth/local-ii-probe.ts +0 -147
- package/src/auth/types.ts +0 -200
- package/src/createActorHooks.ts +0 -220
- package/src/createInfiniteQuery.ts +0 -578
- package/src/createMutation.ts +0 -341
- package/src/createQuery.ts +0 -237
- package/src/createSuspenseInfiniteQuery.ts +0 -601
- package/src/createSuspenseQuery.ts +0 -234
- package/src/defineReactor.ts +0 -324
- package/src/hooks/createAuthHooks.ts +0 -189
- package/src/hooks/index.ts +0 -103
- package/src/hooks/useActorInfiniteQuery.ts +0 -205
- package/src/hooks/useActorMethod.ts +0 -507
- package/src/hooks/useActorMutation.ts +0 -205
- package/src/hooks/useActorQuery.ts +0 -124
- package/src/hooks/useActorSuspenseInfiniteQuery.ts +0 -205
- package/src/hooks/useActorSuspenseQuery.ts +0 -132
- package/src/index.ts +0 -23
- package/src/types.ts +0 -499
- package/src/utils.ts +0 -121
- package/src/validation.ts +0 -202
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Infinite Query Factory - Generic wrapper for React Query paginated canister data
|
|
3
|
-
*
|
|
4
|
-
* Creates unified fetch/hook/invalidate functions for any paginated canister method.
|
|
5
|
-
* Works with any Reactor instance.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* const postsQuery = createInfiniteQuery(reactor, {
|
|
9
|
-
* functionName: "get_posts",
|
|
10
|
-
* initialPageParam: 0,
|
|
11
|
-
* getArgs: (cursor) => [{ cursor, limit: 10 }],
|
|
12
|
-
* getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
13
|
-
* })
|
|
14
|
-
*
|
|
15
|
-
* // In component
|
|
16
|
-
* const { data, fetchNextPage, hasNextPage } = postsQuery.useInfiniteQuery()
|
|
17
|
-
*
|
|
18
|
-
* // Flatten all pages
|
|
19
|
-
* const allPosts = data?.pages.flatMap(page => page.posts)
|
|
20
|
-
*
|
|
21
|
-
* // Invalidate cache
|
|
22
|
-
* postsQuery.invalidate()
|
|
23
|
-
*/
|
|
24
|
-
import { useMemo } from "react";
|
|
25
|
-
import { useInfiniteQuery, } from "@tanstack/react-query";
|
|
26
|
-
import { buildChainedSelect, mergeFactoryQueryKey, normalizeQueryData, } from "./utils.js";
|
|
27
|
-
// ============================================================================
|
|
28
|
-
// Internal Implementation
|
|
29
|
-
// ============================================================================
|
|
30
|
-
const createInfiniteQueryImpl = (reactor, config) => {
|
|
31
|
-
const { functionName, callConfig, queryKey: customQueryKey, initialPageParam, getArgs, getKeyArgs, getNextPageParam, getPreviousPageParam, maxPages, staleTime = 5 * 60 * 1000, select, ...rest } = config;
|
|
32
|
-
// Fold the call arguments into the key. Without them every infinite query on
|
|
33
|
-
// a method shares one cache entry, so two lists with different `getArgs` (a
|
|
34
|
-
// different page size, a different filter) serve each other's pages — the
|
|
35
|
-
// arguments live in a closure, not in the config, so nothing else
|
|
36
|
-
// distinguishes them.
|
|
37
|
-
//
|
|
38
|
-
// The first page's args stand for the query's identity; `getKeyArgs` lets a
|
|
39
|
-
// caller drop the cursor field when it is part of those args.
|
|
40
|
-
const getQueryKey = () => {
|
|
41
|
-
const initialArgs = getArgs(initialPageParam);
|
|
42
|
-
const keyArgs = getKeyArgs?.(initialArgs) ?? initialArgs;
|
|
43
|
-
return reactor.generateQueryKey({
|
|
44
|
-
functionName,
|
|
45
|
-
queryKey: mergeFactoryQueryKey(customQueryKey, undefined, keyArgs),
|
|
46
|
-
}, callConfig);
|
|
47
|
-
};
|
|
48
|
-
// Query function - accepts QueryFunctionContext
|
|
49
|
-
const queryFn = async (context) => {
|
|
50
|
-
// pageParam is typed as unknown in QueryFunctionContext, but we know its type
|
|
51
|
-
const pageParam = context.pageParam;
|
|
52
|
-
const args = getArgs(pageParam);
|
|
53
|
-
const result = await reactor.callMethod({
|
|
54
|
-
functionName,
|
|
55
|
-
args,
|
|
56
|
-
callConfig,
|
|
57
|
-
});
|
|
58
|
-
return normalizeQueryData(result);
|
|
59
|
-
};
|
|
60
|
-
// Get infinite query options for fetchInfiniteQuery
|
|
61
|
-
const getInfiniteQueryOptions = () => ({
|
|
62
|
-
queryKey: getQueryKey(),
|
|
63
|
-
queryFn,
|
|
64
|
-
initialPageParam,
|
|
65
|
-
getNextPageParam,
|
|
66
|
-
});
|
|
67
|
-
// Fetch function for loaders (cache-first, fetches first page)
|
|
68
|
-
const fetch = async () => {
|
|
69
|
-
// Check cache first
|
|
70
|
-
const cachedData = reactor.queryClient.getQueryData(getQueryKey());
|
|
71
|
-
if (cachedData !== undefined) {
|
|
72
|
-
return select ? select(cachedData) : cachedData;
|
|
73
|
-
}
|
|
74
|
-
// Fetch if not in cache
|
|
75
|
-
const result = await reactor.queryClient.fetchInfiniteQuery(getInfiniteQueryOptions());
|
|
76
|
-
// Result is already InfiniteData format
|
|
77
|
-
return select ? select(result) : result;
|
|
78
|
-
};
|
|
79
|
-
// Implementation
|
|
80
|
-
const useInfiniteQueryHook = (options) => {
|
|
81
|
-
// Memoized and identity-preserving; see buildChainedSelect for why the
|
|
82
|
-
// function identity matters to the observer's select-result cache.
|
|
83
|
-
const chainedSelect = useMemo(() => buildChainedSelect(select, options?.select), [options?.select]);
|
|
84
|
-
return useInfiniteQuery({
|
|
85
|
-
queryKey: getQueryKey(),
|
|
86
|
-
queryFn,
|
|
87
|
-
initialPageParam,
|
|
88
|
-
getNextPageParam,
|
|
89
|
-
getPreviousPageParam,
|
|
90
|
-
maxPages,
|
|
91
|
-
staleTime,
|
|
92
|
-
...rest,
|
|
93
|
-
...options,
|
|
94
|
-
select: chainedSelect,
|
|
95
|
-
}, reactor.queryClient);
|
|
96
|
-
};
|
|
97
|
-
// Invalidate function
|
|
98
|
-
const invalidate = async () => {
|
|
99
|
-
const queryKey = getQueryKey();
|
|
100
|
-
await reactor.queryClient.invalidateQueries({ queryKey });
|
|
101
|
-
};
|
|
102
|
-
// Get data from cache without fetching
|
|
103
|
-
const getCacheData = (selectFn) => {
|
|
104
|
-
const queryKey = getQueryKey();
|
|
105
|
-
const cachedRawData = reactor.queryClient.getQueryData(queryKey);
|
|
106
|
-
if (cachedRawData === undefined) {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
// Apply config.select to raw cache data
|
|
110
|
-
const selectedData = (select ? select(cachedRawData) : cachedRawData);
|
|
111
|
-
// Apply optional select parameter
|
|
112
|
-
if (selectFn) {
|
|
113
|
-
return selectFn(selectedData);
|
|
114
|
-
}
|
|
115
|
-
return selectedData;
|
|
116
|
-
};
|
|
117
|
-
return {
|
|
118
|
-
fetch,
|
|
119
|
-
useInfiniteQuery: useInfiniteQueryHook,
|
|
120
|
-
invalidate,
|
|
121
|
-
getQueryKey,
|
|
122
|
-
getCacheData,
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
|
-
// ============================================================================
|
|
126
|
-
// Factory Function
|
|
127
|
-
// ============================================================================
|
|
128
|
-
export function createInfiniteQuery(reactor, config) {
|
|
129
|
-
return createInfiniteQueryImpl(reactor, config);
|
|
130
|
-
}
|
|
131
|
-
// ============================================================================
|
|
132
|
-
// Factory with Dynamic Args
|
|
133
|
-
// ============================================================================
|
|
134
|
-
/**
|
|
135
|
-
* Create an infinite query factory that accepts getArgs at call time.
|
|
136
|
-
* Useful when pagination logic varies by context.
|
|
137
|
-
*
|
|
138
|
-
* @template Service - The actor interface type
|
|
139
|
-
* @template Method - The method name on the actor
|
|
140
|
-
* @template Transform - The transformation key (identity, display, etc.)
|
|
141
|
-
* @template TPageParam - The page parameter type
|
|
142
|
-
* @template Selected - The type returned after select transformation
|
|
143
|
-
*
|
|
144
|
-
* @param reactor - The Reactor instance
|
|
145
|
-
* @param config - Infinite query configuration (without getArgs)
|
|
146
|
-
* @returns A function that accepts getArgs and returns an ActorInfiniteQueryResult
|
|
147
|
-
*
|
|
148
|
-
* @example
|
|
149
|
-
* const getPostsQuery = createActorInfiniteQueryFactory(reactor, {
|
|
150
|
-
* functionName: "get_posts",
|
|
151
|
-
* initialPageParam: 0,
|
|
152
|
-
* getKeyArgs: (args) => {
|
|
153
|
-
* const [{ userId }] = args
|
|
154
|
-
* return [{ userId }]
|
|
155
|
-
* },
|
|
156
|
-
* getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
157
|
-
* })
|
|
158
|
-
*
|
|
159
|
-
* // Create query with specific args builder
|
|
160
|
-
* const userPostsQuery = getPostsQuery((cursor) => [{ userId, cursor, limit: 10 }])
|
|
161
|
-
* const { data, fetchNextPage } = userPostsQuery.useInfiniteQuery()
|
|
162
|
-
*/
|
|
163
|
-
export function createInfiniteQueryFactory(reactor, config) {
|
|
164
|
-
const factory = (getArgs) => {
|
|
165
|
-
// `getKeyArgs` and the args-derived key segment are applied by the impl,
|
|
166
|
-
// which now does it for every infinite query rather than only for factories.
|
|
167
|
-
return createInfiniteQueryImpl(reactor, {
|
|
168
|
-
...config,
|
|
169
|
-
getArgs,
|
|
170
|
-
});
|
|
171
|
-
};
|
|
172
|
-
return factory;
|
|
173
|
-
}
|
|
174
|
-
//# sourceMappingURL=createInfiniteQuery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createInfiniteQuery.js","sourceRoot":"","sources":["../src/createInfiniteQuery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAW/B,OAAO,EAEL,gBAAgB,GAOjB,MAAM,uBAAuB,CAAA;AAG9B,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,YAAY,CAAA;AA6NnB,+EAA+E;AAC/E,0BAA0B;AAC1B,+EAA+E;AAE/E,MAAM,uBAAuB,GAAG,CAU9B,OAAoC,EACpC,MAA6E,EAM7E,EAAE;IAKF,MAAM,EACJ,YAAY,EACZ,UAAU,EACV,QAAQ,EAAE,cAAc,EACxB,gBAAgB,EAChB,OAAO,EACP,UAAU,EACV,gBAAgB,EAChB,oBAAoB,EACpB,QAAQ,EACR,SAAS,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,EACzB,MAAM,EACN,GAAG,IAAI,EACR,GAAG,MAAM,CAAA;IAEV,6EAA6E;IAC7E,4EAA4E;IAC5E,0EAA0E;IAC1E,kEAAkE;IAClE,sBAAsB;IACtB,EAAE;IACF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AC,CAAC,CAAC,CAAC,aAAa,CACnC,CAAA;QAEb,kCAAkC;QAClC,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,QAAQ,CAAC,YAAY,CAAC,CAAA;QAC/B,CAAC;QAED,OAAO,YAAY,CAAA;IACrB,CAAC,CAAA;IAED,OAAO;QACL,KAAK;QACL,gBAAgB,EAAE,oBAAoB;QACtC,UAAU;QACV,WAAW;QACX,YAAY;KACb,CAAA;AACH,CAAC,CAAA;AAED,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E,MAAM,UAAU,mBAAmB,CAUjC,OAAoC,EACpC,MAMC;IAOD,OAAO,uBAAuB,CAC5B,OAAO,EACP,MAMC,CACF,CAAA;AACH,CAAC;AAED,+EAA+E;AAC/E,4BAA4B;AAC5B,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,MAAM,UAAU,0BAA0B,CAUxC,OAAoC,EACpC,MAMC;IAED,MAAM,OAAO,GAMT,CACF,OAA2E,EAC3E,EAAE;QACF,yEAAyE;QACzE,6EAA6E;QAC7E,OAAO,uBAAuB,CAM5B,OAAO,EAAE;YACT,GAAI,MAMF;YACF,OAAO;SACR,CAAC,CAAA;IACJ,CAAC,CAAA;IAED,OAAO,OAAO,CAAA;AAChB,CAAC"}
|
package/dist/createMutation.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mutation Factory - Generic wrapper for mutating canister data
|
|
3
|
-
*
|
|
4
|
-
* Creates unified mutation hooks for any canister method.
|
|
5
|
-
* Works with any Reactor instance.
|
|
6
|
-
* Use this when one mutation should support both React hooks and imperative
|
|
7
|
-
* execution outside React.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const transferMutation = createMutation(reactor, {
|
|
11
|
-
* functionName: "transfer",
|
|
12
|
-
* onSuccess: () => console.log("Success!"),
|
|
13
|
-
* })
|
|
14
|
-
*
|
|
15
|
-
* // In component
|
|
16
|
-
* const { mutate, isPending } = transferMutation.useMutation()
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const transferMutation = createMutation(reactor, {
|
|
20
|
-
* functionName: "transfer",
|
|
21
|
-
* onCanisterError: (err) => console.error(err.code),
|
|
22
|
-
* })
|
|
23
|
-
*
|
|
24
|
-
* // Outside React (loader/service/script)
|
|
25
|
-
* await transferMutation.execute([{ to: "aaaaa-aa", amount: "1000" }])
|
|
26
|
-
*/
|
|
27
|
-
import type { Reactor, FunctionName, TransformKey } from "@ic-reactor/core";
|
|
28
|
-
import type { MutationConfig, MutationResult, NoInfer } from "./types.js";
|
|
29
|
-
export declare function createMutation<Service, Transform extends TransformKey, Method extends FunctionName<Service> = FunctionName<Service>, TOnMutateResult = unknown>(reactor: Reactor<Service, Transform>, config: MutationConfig<NoInfer<Service>, Method, Transform, TOnMutateResult | undefined>): MutationResult<Service, Method, Transform>;
|
|
30
|
-
//# sourceMappingURL=createMutation.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createMutation.d.ts","sourceRoot":"","sources":["../src/createMutation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAMH,OAAO,KAAK,EACV,OAAO,EACP,YAAY,EAEZ,YAAY,EAGb,MAAM,kBAAkB,CAAA;AAEzB,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EAEd,OAAO,EACR,MAAM,YAAY,CAAA;AA8QnB,wBAAgB,cAAc,CAC5B,OAAO,EACP,SAAS,SAAS,YAAY,EAC9B,MAAM,SAAS,YAAY,CAAC,OAAO,CAAC,GAAG,YAAY,CAAC,OAAO,CAAC,EAC5D,eAAe,GAAG,OAAO,EAEzB,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,EAGpC,MAAM,EAAE,cAAc,CACpB,OAAO,CAAC,OAAO,CAAC,EAChB,MAAM,EACN,SAAS,EACT,eAAe,GAAG,SAAS,CAC5B,GACA,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,CAU5C"}
|
package/dist/createMutation.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mutation Factory - Generic wrapper for mutating canister data
|
|
3
|
-
*
|
|
4
|
-
* Creates unified mutation hooks for any canister method.
|
|
5
|
-
* Works with any Reactor instance.
|
|
6
|
-
* Use this when one mutation should support both React hooks and imperative
|
|
7
|
-
* execution outside React.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const transferMutation = createMutation(reactor, {
|
|
11
|
-
* functionName: "transfer",
|
|
12
|
-
* onSuccess: () => console.log("Success!"),
|
|
13
|
-
* })
|
|
14
|
-
*
|
|
15
|
-
* // In component
|
|
16
|
-
* const { mutate, isPending } = transferMutation.useMutation()
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const transferMutation = createMutation(reactor, {
|
|
20
|
-
* functionName: "transfer",
|
|
21
|
-
* onCanisterError: (err) => console.error(err.code),
|
|
22
|
-
* })
|
|
23
|
-
*
|
|
24
|
-
* // Outside React (loader/service/script)
|
|
25
|
-
* await transferMutation.execute([{ to: "aaaaa-aa", amount: "1000" }])
|
|
26
|
-
*/
|
|
27
|
-
import { useMutation, } from "@tanstack/react-query";
|
|
28
|
-
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
|
-
}
|
|
36
|
-
// ============================================================================
|
|
37
|
-
// Internal Implementation
|
|
38
|
-
// ============================================================================
|
|
39
|
-
const createMutationImpl = (reactor, config) => {
|
|
40
|
-
const { functionName, callConfig, invalidateQueries: factoryInvalidateQueries, onSuccess: factoryOnSuccess, onCanisterError: factoryOnCanisterError, onError: factoryOnError, onMutate: factoryOnMutate, onSettled: factoryOnSettled, ...factoryOptions } = config;
|
|
41
|
-
/**
|
|
42
|
-
* Raw call without any invalidation logic.
|
|
43
|
-
* Used as mutationFn so that onSuccess handles all post-mutation work
|
|
44
|
-
* and there is no double-invalidation.
|
|
45
|
-
*/
|
|
46
|
-
const callFn = (args) => reactor.callMethod({ functionName, args, callConfig });
|
|
47
|
-
/**
|
|
48
|
-
* The factory's own `onMutate` result for each call through the hook.
|
|
49
|
-
*
|
|
50
|
-
* TanStack Query stores one `onMutate` result per mutation and hands it to
|
|
51
|
-
* every callback. That slot holds the hook's result, which the mutation also
|
|
52
|
-
* exposes as `context`, so this map holds the factory's. Each key is the
|
|
53
|
-
* context object TanStack Query creates for a call and passes to every
|
|
54
|
-
* callback of that call.
|
|
55
|
-
*/
|
|
56
|
-
const factoryOnMutateResults = new WeakMap();
|
|
57
|
-
/**
|
|
58
|
-
* The `onMutate` result a factory callback receives. A factory without its
|
|
59
|
-
* own `onMutate` gets the hook's, as it always has. TanStack Query before
|
|
60
|
-
* 5.89 passes no context, so there a factory callback gets the hook's result
|
|
61
|
-
* too.
|
|
62
|
-
*/
|
|
63
|
-
const factoryOnMutateResult = (onMutateResult, context) => (factoryOnMutate && context
|
|
64
|
-
? factoryOnMutateResults.get(context)
|
|
65
|
-
: onMutateResult);
|
|
66
|
-
/**
|
|
67
|
-
* Imperative execution for non-React usage.
|
|
68
|
-
*
|
|
69
|
-
* Runs the same factory-level chain the hook does — invalidation, then
|
|
70
|
-
* `onSuccess`, or `onCanisterError`/`onError` on failure — so a mutation
|
|
71
|
-
* object behaves the same through both call paths. Only hook-level callbacks
|
|
72
|
-
* are absent, because there is no hook here to supply them.
|
|
73
|
-
*
|
|
74
|
-
* The callbacks get the same `{ client, meta, mutationKey }` context
|
|
75
|
-
* TanStack Query passes them on the hook path.
|
|
76
|
-
*
|
|
77
|
-
* The error is rethrown after the callbacks run, so `await execute(...)`
|
|
78
|
-
* still rejects for the caller.
|
|
79
|
-
*
|
|
80
|
-
* Use this in route loaders, scripts, or server-side code.
|
|
81
|
-
*/
|
|
82
|
-
const execute = async (args) => {
|
|
83
|
-
// TanStack Query builds this for the hook path, and the callback types
|
|
84
|
-
// declare it as always present. None of it is mutation state, so this path
|
|
85
|
-
// builds the same object. Passing `undefined` made a callback that reads
|
|
86
|
-
// `context.client` throw here and nowhere else.
|
|
87
|
-
//
|
|
88
|
-
// The hook path takes `meta` and `mutationKey` from the client's defaulted
|
|
89
|
-
// options, so this runs the same spread through `defaultMutationOptions`.
|
|
90
|
-
// Reading `factoryOptions` alone dropped mutation defaults registered on
|
|
91
|
-
// the QueryClient (`defaultOptions.mutations`, `setMutationDefaults`).
|
|
92
|
-
const effectiveOptions = reactor.queryClient.defaultMutationOptions({
|
|
93
|
-
mutationKey: reactor.getQueryOptions({ functionName }).queryKey,
|
|
94
|
-
...factoryOptions,
|
|
95
|
-
});
|
|
96
|
-
const context = {
|
|
97
|
-
client: reactor.queryClient,
|
|
98
|
-
meta: effectiveOptions.meta,
|
|
99
|
-
mutationKey: effectiveOptions.mutationKey,
|
|
100
|
-
};
|
|
101
|
-
let result;
|
|
102
|
-
try {
|
|
103
|
-
result = await callFn(args);
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
if (isCanisterError(error)) {
|
|
107
|
-
// `catch` drops the type. `callMethod` rejects with this method's
|
|
108
|
-
// CanisterError, the one the hook path narrows to.
|
|
109
|
-
factoryOnCanisterError?.(error, args);
|
|
110
|
-
}
|
|
111
|
-
// `onMutate` does not run on this path, so there is no result to pass.
|
|
112
|
-
// Awaited like `onSuccess` below and like the hook path, so an async
|
|
113
|
-
// rollback or report finishes before `execute()` rejects.
|
|
114
|
-
await factoryOnError?.(error, args, undefined, context);
|
|
115
|
-
throw error;
|
|
116
|
-
}
|
|
117
|
-
if (factoryInvalidateQueries) {
|
|
118
|
-
await invalidateAll(reactor.queryClient, factoryInvalidateQueries);
|
|
119
|
-
}
|
|
120
|
-
await factoryOnSuccess?.(result, args, undefined, context);
|
|
121
|
-
return result;
|
|
122
|
-
};
|
|
123
|
-
// Hook implementation
|
|
124
|
-
const useMutationHook = (options) => {
|
|
125
|
-
const baseOptions = reactor.getQueryOptions({ functionName });
|
|
126
|
-
const { invalidateQueries: hookInvalidateQueries, onCanisterError: hookOnCanisterError, ...restOptions } = options ?? {};
|
|
127
|
-
return useMutation({
|
|
128
|
-
mutationKey: baseOptions.queryKey,
|
|
129
|
-
...factoryOptions,
|
|
130
|
-
...restOptions,
|
|
131
|
-
// Use callFn (not execute) to avoid double-invalidation:
|
|
132
|
-
// factoryInvalidateQueries are handled in onSuccess below.
|
|
133
|
-
mutationFn: callFn,
|
|
134
|
-
onSuccess: async (data, variables, onMutateResult, context) => {
|
|
135
|
-
// 1. Factory-level invalidation
|
|
136
|
-
if (factoryInvalidateQueries) {
|
|
137
|
-
await invalidateAll(reactor.queryClient, factoryInvalidateQueries);
|
|
138
|
-
}
|
|
139
|
-
// 2. Hook-level invalidation
|
|
140
|
-
if (hookInvalidateQueries) {
|
|
141
|
-
await invalidateAll(reactor.queryClient, hookInvalidateQueries);
|
|
142
|
-
}
|
|
143
|
-
// 3. Factory onSuccess
|
|
144
|
-
await factoryOnSuccess?.(data, variables, factoryOnMutateResult(onMutateResult, context), context);
|
|
145
|
-
// 4. Hook onSuccess
|
|
146
|
-
await restOptions.onSuccess?.(data, variables, onMutateResult, context);
|
|
147
|
-
},
|
|
148
|
-
onError: async (error, variables, onMutateResult, context) => {
|
|
149
|
-
if (isCanisterError(error)) {
|
|
150
|
-
factoryOnCanisterError?.(error, variables);
|
|
151
|
-
hookOnCanisterError?.(error, variables);
|
|
152
|
-
}
|
|
153
|
-
// Awaited in order, like `onSuccess`. TanStack Query holds
|
|
154
|
-
// `onSettled` and the settled state until this promise resolves, so
|
|
155
|
-
// an async `onError` must be part of it.
|
|
156
|
-
await factoryOnError?.(error, variables, factoryOnMutateResult(onMutateResult, context), context);
|
|
157
|
-
await restOptions.onError?.(error, variables, onMutateResult, context);
|
|
158
|
-
},
|
|
159
|
-
// `onMutate` and `onSettled` are composed like `onSuccess`/`onError`
|
|
160
|
-
// above. They used to arrive through the `...restOptions` spread, so a
|
|
161
|
-
// hook-level one silently replaced the factory's — factory teardown,
|
|
162
|
-
// telemetry or logging simply vanished the moment any call site passed
|
|
163
|
-
// its own, with no warning and no type error. Chaining is what a
|
|
164
|
-
// reader who has seen `onSuccess` chain already expects.
|
|
165
|
-
onMutate: async (variables, context) => {
|
|
166
|
-
if (factoryOnMutate) {
|
|
167
|
-
const result = await factoryOnMutate(variables, context);
|
|
168
|
-
if (context)
|
|
169
|
-
factoryOnMutateResults.set(context, result);
|
|
170
|
-
}
|
|
171
|
-
// Without a hook-level `onMutate` this is `undefined`, and
|
|
172
|
-
// THookOnMutateResult is then `unknown`.
|
|
173
|
-
return (await restOptions.onMutate?.(variables, context));
|
|
174
|
-
},
|
|
175
|
-
onSettled: async (data, error, variables, onMutateResult, context) => {
|
|
176
|
-
await factoryOnSettled?.(data, error, variables, factoryOnMutateResult(onMutateResult, context), context);
|
|
177
|
-
await restOptions.onSettled?.(data, error, variables, onMutateResult, context);
|
|
178
|
-
},
|
|
179
|
-
}, reactor.queryClient);
|
|
180
|
-
};
|
|
181
|
-
return { useMutation: useMutationHook, execute };
|
|
182
|
-
};
|
|
183
|
-
// ============================================================================
|
|
184
|
-
// Public Factory Function
|
|
185
|
-
// ============================================================================
|
|
186
|
-
export function createMutation(reactor,
|
|
187
|
-
// `execute()` runs the factory's `onSuccess` and `onError` without running
|
|
188
|
-
// its `onMutate`, so their `onMutate` result can also be `undefined`.
|
|
189
|
-
config) {
|
|
190
|
-
return createMutationImpl(reactor, config);
|
|
191
|
-
}
|
|
192
|
-
//# sourceMappingURL=createMutation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createMutation.js","sour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ACF,EACD,OAAO,CAAC,WAAW,CACpB,CAAA;IACH,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,2EAA2E;AAC3E,sEAAsE;AACtE,MAKC;IAED,OAAO,kBAAkB,CACvB,OAAO,EACP,MAKC,CACF,CAAA;AACH,CAAC"}
|
package/dist/createQuery.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Query Factory - Generic wrapper for React Query-based canister data
|
|
3
|
-
*
|
|
4
|
-
* Creates unified fetch/hook/invalidate functions for any canister method.
|
|
5
|
-
* Works with any Reactor instance.
|
|
6
|
-
* Use this when the same read operation must work both inside React components
|
|
7
|
-
* and outside React (loaders, actions, services, tests).
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const userQuery = createQuery(todoManager, {
|
|
11
|
-
* functionName: "get_user",
|
|
12
|
-
* select: (result) => result.user,
|
|
13
|
-
* })
|
|
14
|
-
*
|
|
15
|
-
* // In component
|
|
16
|
-
* const { data: user } = userQuery.useQuery()
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const userQuery = createQuery(todoManager, { functionName: "get_user", args: ["alice"] })
|
|
20
|
-
*
|
|
21
|
-
* // Outside React (loader/service/script)
|
|
22
|
-
* await userQuery.fetch()
|
|
23
|
-
* const cached = userQuery.getCacheData()
|
|
24
|
-
* await userQuery.invalidate()
|
|
25
|
-
*/
|
|
26
|
-
import type { Reactor, FunctionName, ReactorArgs, TransformKey } from "@ic-reactor/core";
|
|
27
|
-
import type { QueryFnData, QueryError, QueryConfig, QueryResult, QueryFactoryConfig, NoInfer } from "./types.js";
|
|
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
|
-
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>): (args: ReactorArgs<Service, Method, Transform>) => QueryResult<QueryFnData<Service, Method, Transform>, Selected, QueryError<Service, Method, Transform>>;
|
|
30
|
-
//# sourceMappingURL=createQuery.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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;AAQzB,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EACV,WAAW,EAEX,WAAW,EACX,kBAAkB,EAClB,OAAO,EACR,MAAM,YAAY,CAAA;AA0HnB,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,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,CACD,IAAI,EAAE,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,KAC1C,WAAW,CACd,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EACvC,QAAQ,EACR,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,CACvC,CA8BA"}
|
package/dist/createQuery.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Query Factory - Generic wrapper for React Query-based canister data
|
|
3
|
-
*
|
|
4
|
-
* Creates unified fetch/hook/invalidate functions for any canister method.
|
|
5
|
-
* Works with any Reactor instance.
|
|
6
|
-
* Use this when the same read operation must work both inside React components
|
|
7
|
-
* and outside React (loaders, actions, services, tests).
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const userQuery = createQuery(todoManager, {
|
|
11
|
-
* functionName: "get_user",
|
|
12
|
-
* select: (result) => result.user,
|
|
13
|
-
* })
|
|
14
|
-
*
|
|
15
|
-
* // In component
|
|
16
|
-
* const { data: user } = userQuery.useQuery()
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const userQuery = createQuery(todoManager, { functionName: "get_user", args: ["alice"] })
|
|
20
|
-
*
|
|
21
|
-
* // Outside React (loader/service/script)
|
|
22
|
-
* await userQuery.fetch()
|
|
23
|
-
* const cached = userQuery.getCacheData()
|
|
24
|
-
* await userQuery.invalidate()
|
|
25
|
-
*/
|
|
26
|
-
import { useMemo } from "react";
|
|
27
|
-
import { useQuery, } from "@tanstack/react-query";
|
|
28
|
-
import { buildChainedSelect, createBoundedCache } from "./utils.js";
|
|
29
|
-
// ============================================================================
|
|
30
|
-
// Internal Implementation
|
|
31
|
-
// ============================================================================
|
|
32
|
-
const createQueryImpl = (reactor, config) => {
|
|
33
|
-
const { functionName, args, staleTime = 5 * 60 * 1000, select, queryKey: customQueryKey, ...rest } = config;
|
|
34
|
-
const params = { functionName, args, queryKey: customQueryKey };
|
|
35
|
-
const getQueryKey = () => reactor.generateQueryKey(params);
|
|
36
|
-
// Apply config.select to raw data (shared by fetch, getCacheData, and the hook)
|
|
37
|
-
const applySelect = (raw) => select ? select(raw) : raw;
|
|
38
|
-
/** Cache-first fetch for use in loaders / route preloading. */
|
|
39
|
-
const fetch = async () => {
|
|
40
|
-
const result = await reactor.fetchQuery(params);
|
|
41
|
-
return applySelect(result);
|
|
42
|
-
};
|
|
43
|
-
/** Fire-and-forget prefetch — warms the cache without blocking. */
|
|
44
|
-
const prefetch = () => {
|
|
45
|
-
const baseOptions = reactor.getQueryOptions(params);
|
|
46
|
-
return reactor.queryClient.prefetchQuery({
|
|
47
|
-
queryKey: baseOptions.queryKey,
|
|
48
|
-
queryFn: baseOptions.queryFn,
|
|
49
|
-
staleTime,
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
const useQueryHook = ((options) => {
|
|
53
|
-
const baseOptions = reactor.getQueryOptions(params);
|
|
54
|
-
// Memoized so the observer's select-result cache can hit; see
|
|
55
|
-
// buildChainedSelect. `select` comes from the factory config and is stable.
|
|
56
|
-
const chainedSelect = useMemo(() => buildChainedSelect(select, options?.select), [options?.select]);
|
|
57
|
-
return useQuery({
|
|
58
|
-
queryKey: baseOptions.queryKey,
|
|
59
|
-
staleTime,
|
|
60
|
-
...rest,
|
|
61
|
-
...options,
|
|
62
|
-
queryFn: baseOptions.queryFn,
|
|
63
|
-
select: chainedSelect,
|
|
64
|
-
}, reactor.queryClient);
|
|
65
|
-
});
|
|
66
|
-
const invalidate = async () => {
|
|
67
|
-
await reactor.queryClient.invalidateQueries({ queryKey: getQueryKey() });
|
|
68
|
-
};
|
|
69
|
-
const getCacheData = ((selectFn) => {
|
|
70
|
-
const raw = reactor.getQueryData(params);
|
|
71
|
-
if (raw === undefined)
|
|
72
|
-
return undefined;
|
|
73
|
-
const selected = applySelect(raw);
|
|
74
|
-
return selectFn ? selectFn(selected) : selected;
|
|
75
|
-
});
|
|
76
|
-
const setData = (updater) => {
|
|
77
|
-
return reactor.queryClient.setQueryData(getQueryKey(), updater);
|
|
78
|
-
};
|
|
79
|
-
return {
|
|
80
|
-
fetch,
|
|
81
|
-
prefetch,
|
|
82
|
-
useQuery: useQueryHook,
|
|
83
|
-
invalidate,
|
|
84
|
-
getQueryKey,
|
|
85
|
-
getCacheData,
|
|
86
|
-
setData,
|
|
87
|
-
};
|
|
88
|
-
};
|
|
89
|
-
// ============================================================================
|
|
90
|
-
// Public Factory Function
|
|
91
|
-
// ============================================================================
|
|
92
|
-
export function createQuery(reactor, config) {
|
|
93
|
-
return createQueryImpl(reactor, config);
|
|
94
|
-
}
|
|
95
|
-
// ============================================================================
|
|
96
|
-
// Convenience: Create query with dynamic args
|
|
97
|
-
// ============================================================================
|
|
98
|
-
export function createQueryFactory(reactor, config) {
|
|
99
|
-
const cache = createBoundedCache();
|
|
100
|
-
return (args) => {
|
|
101
|
-
const key = reactor.generateQueryKey({
|
|
102
|
-
functionName: config.functionName,
|
|
103
|
-
args,
|
|
104
|
-
});
|
|
105
|
-
const cacheKey = JSON.stringify(key);
|
|
106
|
-
const existing = cache.get(cacheKey);
|
|
107
|
-
if (existing)
|
|
108
|
-
return existing;
|
|
109
|
-
const result = createQueryImpl(reactor, {
|
|
110
|
-
...config,
|
|
111
|
-
args,
|
|
112
|
-
});
|
|
113
|
-
cache.set(cacheKey, result);
|
|
114
|
-
return result;
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
//# sourceMappingURL=createQuery.js.map
|
package/dist/createQuery.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,GAGT,MAAM,uBAAuB,CAAA;AAU9B,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAEnE,+EAA+E;AAC/E,0BAA0B;AAC1B,+EAA+E;AAE/E,MAAM,eAAe,GAAG,CAMtB,OAAoC,EACpC,MAAyD,EAKzD,EAAE;IAIF,MAAM,EACJ,YAAY,EACZ,IAAI,EACJ,SAAS,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,EACzB,MAAM,EACN,QAAQ,EAAE,cAAc,EACxB,GAAG,IAAI,EACR,GAAG,MAAM,CAAA;IAEV,MAAM,MAAM,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,cAAc,EAAE,CAAA;IAE/D,MAAM,WAAW,GAAG,GAAa,EAAE,CAAC,OAAO,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAA;IAEpE,gFAAgF;IAChF,MAAM,WAAW,GAAG,CAAC,GAAU,EAAY,EAAE,CAC3C,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,GAA2B,CAAA;IAErD,+DAA+D;IAC/D,MAAM,KAAK,GAAG,KAAK,IAAuB,EAAE;QAC1C,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAA;QAC/C,OAAO,WAAW,CAAC,MAAM,CAAC,CAAA;IAC5B,CAAC,CAAA;IAED,mEAAmE;IACnE,MAAM,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