@effector-tanstack-query/core 0.1.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 +40 -0
- package/dist/createBaseQuery.cjs +208 -0
- package/dist/createBaseQuery.cjs.map +1 -0
- package/dist/createBaseQuery.d.cts +114 -0
- package/dist/createBaseQuery.d.ts +114 -0
- package/dist/createBaseQuery.js +204 -0
- package/dist/createBaseQuery.js.map +1 -0
- package/dist/createInfiniteQuery.cjs +193 -0
- package/dist/createInfiniteQuery.cjs.map +1 -0
- package/dist/createInfiniteQuery.d.cts +8 -0
- package/dist/createInfiniteQuery.d.ts +8 -0
- package/dist/createInfiniteQuery.js +191 -0
- package/dist/createInfiniteQuery.js.map +1 -0
- package/dist/createInvalidate.cjs +37 -0
- package/dist/createInvalidate.cjs.map +1 -0
- package/dist/createInvalidate.d.cts +50 -0
- package/dist/createInvalidate.d.ts +50 -0
- package/dist/createInvalidate.js +35 -0
- package/dist/createInvalidate.js.map +1 -0
- package/dist/createMutation.cjs +177 -0
- package/dist/createMutation.cjs.map +1 -0
- package/dist/createMutation.d.cts +7 -0
- package/dist/createMutation.d.ts +7 -0
- package/dist/createMutation.js +175 -0
- package/dist/createMutation.js.map +1 -0
- package/dist/createQuery.cjs +98 -0
- package/dist/createQuery.cjs.map +1 -0
- package/dist/createQuery.d.cts +8 -0
- package/dist/createQuery.d.ts +8 -0
- package/dist/createQuery.js +96 -0
- package/dist/createQuery.js.map +1 -0
- package/dist/index.cjs +36 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/queryClient.cjs +20 -0
- package/dist/queryClient.cjs.map +1 -0
- package/dist/queryClient.d.cts +15 -0
- package/dist/queryClient.d.ts +15 -0
- package/dist/queryClient.js +17 -0
- package/dist/queryClient.js.map +1 -0
- package/dist/resolve.cjs +37 -0
- package/dist/resolve.cjs.map +1 -0
- package/dist/resolve.d.cts +17 -0
- package/dist/resolve.d.ts +17 -0
- package/dist/resolve.js +33 -0
- package/dist/resolve.js.map +1 -0
- package/dist/types.cjs +4 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +209 -0
- package/dist/types.d.ts +209 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +60 -0
- package/src/createBaseQuery.ts +428 -0
- package/src/createInfiniteQuery.ts +291 -0
- package/src/createInvalidate.ts +104 -0
- package/src/createMutation.ts +271 -0
- package/src/createQuery.ts +155 -0
- package/src/index.ts +17 -0
- package/src/queryClient.ts +23 -0
- package/src/resolve.ts +50 -0
- package/src/types.ts +270 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { createEvent, createStore, scopeBind, attach, sample } from 'effector';
|
|
2
|
+
import { InfiniteQueryObserver } from '@tanstack/query-core';
|
|
3
|
+
import { warnMissingName, createBaseQuery, sidConfig } from './createBaseQuery.js';
|
|
4
|
+
import { resolveReactiveRefetchInterval } from './resolve.js';
|
|
5
|
+
|
|
6
|
+
// src/createInfiniteQuery.ts
|
|
7
|
+
function createInfiniteQuery(arg1, arg2) {
|
|
8
|
+
const [explicitClient, options] = parseInfiniteArgs(arg1, arg2);
|
|
9
|
+
const { queryKey, enabled, name, ...restOptions } = options;
|
|
10
|
+
if (!name) warnMissingName("createInfiniteQuery");
|
|
11
|
+
const reactiveRefetchInterval = resolveReactiveRefetchInterval(
|
|
12
|
+
restOptions.refetchInterval
|
|
13
|
+
);
|
|
14
|
+
if (reactiveRefetchInterval) {
|
|
15
|
+
delete restOptions.refetchInterval;
|
|
16
|
+
}
|
|
17
|
+
const base = createBaseQuery(
|
|
18
|
+
explicitClient,
|
|
19
|
+
{ queryKey, enabled, name, reactiveRefetchInterval },
|
|
20
|
+
{
|
|
21
|
+
createObserver: (qc, { queryKey: key, enabled: isEnabled }) => new InfiniteQueryObserver(qc, {
|
|
22
|
+
...restOptions,
|
|
23
|
+
queryKey: key,
|
|
24
|
+
enabled: isEnabled
|
|
25
|
+
}),
|
|
26
|
+
setupExtras: () => {
|
|
27
|
+
const hasNextPageUpdated = createEvent();
|
|
28
|
+
const hasPreviousPageUpdated = createEvent();
|
|
29
|
+
const isFetchingNextPageUpdated = createEvent();
|
|
30
|
+
const isFetchingPreviousPageUpdated = createEvent();
|
|
31
|
+
const isFetchNextPageErrorUpdated = createEvent();
|
|
32
|
+
const isFetchPreviousPageErrorUpdated = createEvent();
|
|
33
|
+
const $hasNextPage = createStore(false, {
|
|
34
|
+
...sidConfig(name, "$hasNextPage")
|
|
35
|
+
}).on(hasNextPageUpdated, (_, v) => v);
|
|
36
|
+
const $hasPreviousPage = createStore(false, {
|
|
37
|
+
...sidConfig(name, "$hasPreviousPage")
|
|
38
|
+
}).on(hasPreviousPageUpdated, (_, v) => v);
|
|
39
|
+
const $isFetchingNextPage = createStore(false, {
|
|
40
|
+
...sidConfig(name, "$isFetchingNextPage")
|
|
41
|
+
}).on(isFetchingNextPageUpdated, (_, v) => v);
|
|
42
|
+
const $isFetchingPreviousPage = createStore(false, {
|
|
43
|
+
...sidConfig(name, "$isFetchingPreviousPage")
|
|
44
|
+
}).on(isFetchingPreviousPageUpdated, (_, v) => v);
|
|
45
|
+
const $isFetchNextPageError = createStore(false, {
|
|
46
|
+
...sidConfig(name, "$isFetchNextPageError")
|
|
47
|
+
}).on(isFetchNextPageErrorUpdated, (_, v) => v);
|
|
48
|
+
const $isFetchPreviousPageError = createStore(false, {
|
|
49
|
+
...sidConfig(name, "$isFetchPreviousPageError")
|
|
50
|
+
}).on(isFetchPreviousPageErrorUpdated, (_, v) => v);
|
|
51
|
+
const fetchNextPage = createEvent();
|
|
52
|
+
const fetchPreviousPage = createEvent();
|
|
53
|
+
return {
|
|
54
|
+
stores: {
|
|
55
|
+
$hasNextPage,
|
|
56
|
+
$hasPreviousPage,
|
|
57
|
+
$isFetchingNextPage,
|
|
58
|
+
$isFetchingPreviousPage,
|
|
59
|
+
$isFetchNextPageError,
|
|
60
|
+
$isFetchPreviousPageError,
|
|
61
|
+
fetchNextPage,
|
|
62
|
+
fetchPreviousPage
|
|
63
|
+
},
|
|
64
|
+
// Wire fetchNextPage / fetchPreviousPage as scope-aware effects via
|
|
65
|
+
// attach over $observer — same pattern as the rest of createBaseQuery.
|
|
66
|
+
setupEffects: ({ $observer }) => {
|
|
67
|
+
const fetchNextPageFx = attach({
|
|
68
|
+
source: $observer,
|
|
69
|
+
effect: (observer) => {
|
|
70
|
+
if (!observer) return;
|
|
71
|
+
observer.fetchNextPage();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
sample({ clock: fetchNextPage, target: fetchNextPageFx });
|
|
75
|
+
const fetchPreviousPageFx = attach({
|
|
76
|
+
source: $observer,
|
|
77
|
+
effect: (observer) => {
|
|
78
|
+
if (!observer) return;
|
|
79
|
+
observer.fetchPreviousPage();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
sample({ clock: fetchPreviousPage, target: fetchPreviousPageFx });
|
|
83
|
+
},
|
|
84
|
+
bindDispatcher: () => {
|
|
85
|
+
const dispatchHasNextPage = scopeBind(hasNextPageUpdated, {
|
|
86
|
+
safe: true
|
|
87
|
+
});
|
|
88
|
+
const dispatchHasPreviousPage = scopeBind(hasPreviousPageUpdated, {
|
|
89
|
+
safe: true
|
|
90
|
+
});
|
|
91
|
+
const dispatchIsFetchingNextPage = scopeBind(
|
|
92
|
+
isFetchingNextPageUpdated,
|
|
93
|
+
{ safe: true }
|
|
94
|
+
);
|
|
95
|
+
const dispatchIsFetchingPreviousPage = scopeBind(
|
|
96
|
+
isFetchingPreviousPageUpdated,
|
|
97
|
+
{ safe: true }
|
|
98
|
+
);
|
|
99
|
+
const dispatchIsFetchNextPageError = scopeBind(
|
|
100
|
+
isFetchNextPageErrorUpdated,
|
|
101
|
+
{ safe: true }
|
|
102
|
+
);
|
|
103
|
+
const dispatchIsFetchPreviousPageError = scopeBind(
|
|
104
|
+
isFetchPreviousPageErrorUpdated,
|
|
105
|
+
{ safe: true }
|
|
106
|
+
);
|
|
107
|
+
return (result2) => {
|
|
108
|
+
dispatchHasNextPage(result2.hasNextPage);
|
|
109
|
+
dispatchHasPreviousPage(result2.hasPreviousPage);
|
|
110
|
+
dispatchIsFetchingNextPage(result2.isFetchingNextPage);
|
|
111
|
+
dispatchIsFetchingPreviousPage(result2.isFetchingPreviousPage);
|
|
112
|
+
dispatchIsFetchNextPageError(result2.isFetchNextPageError);
|
|
113
|
+
dispatchIsFetchPreviousPageError(result2.isFetchPreviousPageError);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
const prefetch = createEvent();
|
|
121
|
+
const prefetchFx = attach({
|
|
122
|
+
source: {
|
|
123
|
+
qc: base.$queryClient,
|
|
124
|
+
key: base.$resolvedKey,
|
|
125
|
+
enabled: base.$enabled
|
|
126
|
+
},
|
|
127
|
+
effect: ({ qc, key, enabled: enabled2 }) => {
|
|
128
|
+
if (!qc || !enabled2) return;
|
|
129
|
+
return qc.fetchInfiniteQuery({
|
|
130
|
+
...restOptions,
|
|
131
|
+
queryKey: key
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
sample({ clock: prefetch, target: prefetchFx });
|
|
136
|
+
const result = {
|
|
137
|
+
$data: base.$data,
|
|
138
|
+
$error: base.$error,
|
|
139
|
+
$status: base.$status,
|
|
140
|
+
$isPending: base.$isPending,
|
|
141
|
+
$isFetching: base.$isFetching,
|
|
142
|
+
$isSuccess: base.$isSuccess,
|
|
143
|
+
$isError: base.$isError,
|
|
144
|
+
$isPlaceholderData: base.$isPlaceholderData,
|
|
145
|
+
$fetchStatus: base.$fetchStatus,
|
|
146
|
+
$hasNextPage: base.$hasNextPage,
|
|
147
|
+
$hasPreviousPage: base.$hasPreviousPage,
|
|
148
|
+
$isFetchingNextPage: base.$isFetchingNextPage,
|
|
149
|
+
$isFetchingPreviousPage: base.$isFetchingPreviousPage,
|
|
150
|
+
$isFetchNextPageError: base.$isFetchNextPageError,
|
|
151
|
+
$isFetchPreviousPageError: base.$isFetchPreviousPageError,
|
|
152
|
+
$observer: base.$observer,
|
|
153
|
+
$queryClient: base.$queryClient,
|
|
154
|
+
fetchNextPage: base.fetchNextPage,
|
|
155
|
+
fetchPreviousPage: base.fetchPreviousPage,
|
|
156
|
+
refresh: base.refresh,
|
|
157
|
+
prefetch,
|
|
158
|
+
mounted: base.mounted,
|
|
159
|
+
unmounted: base.unmounted
|
|
160
|
+
};
|
|
161
|
+
Object.defineProperty(result, "__createObserver", {
|
|
162
|
+
enumerable: false,
|
|
163
|
+
value: (qc, init) => new InfiniteQueryObserver(qc, {
|
|
164
|
+
...restOptions,
|
|
165
|
+
queryKey: init.queryKey,
|
|
166
|
+
enabled: init.enabled
|
|
167
|
+
})
|
|
168
|
+
});
|
|
169
|
+
Object.defineProperty(result, "__resolvedKey", {
|
|
170
|
+
enumerable: false,
|
|
171
|
+
value: base.$resolvedKey
|
|
172
|
+
});
|
|
173
|
+
Object.defineProperty(result, "__enabled", {
|
|
174
|
+
enumerable: false,
|
|
175
|
+
value: base.$enabled
|
|
176
|
+
});
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
function parseInfiniteArgs(arg1, arg2) {
|
|
180
|
+
if (arg2 !== void 0) {
|
|
181
|
+
return [arg1, arg2];
|
|
182
|
+
}
|
|
183
|
+
return [
|
|
184
|
+
null,
|
|
185
|
+
arg1
|
|
186
|
+
];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export { createInfiniteQuery };
|
|
190
|
+
//# sourceMappingURL=createInfiniteQuery.js.map
|
|
191
|
+
//# sourceMappingURL=createInfiniteQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/createInfiniteQuery.ts"],"names":["result","enabled"],"mappings":";;;;;;AA2CO,SAAS,mBAAA,C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A,OAAO,CAAC,MAAqB,IAAI,CAAA;AAAA,EACnC;AACA,EAAA,OAAO;AAAA,IACL,IAAA;AAAA,IACA;AAAA,GACF;AACF","file":"createInfiniteQuery.js","sourcesContent":["import { attach, createEvent, createStore, sample, scopeBind } from 'effector'\nimport { InfiniteQueryObserver } from '@tanstack/query-core'\nimport type {\n InfiniteData,\n QueryClient,\n QueryKey,\n} from '@tanstack/query-core'\nimport { createBaseQuery, sidConfig, warnMissingName } from './createBaseQuery'\nimport { resolveReactiveRefetchInterval } from './resolve'\nimport type {\n CreateInfiniteQueryOptions,\n InfiniteQueryResult,\n} from './types'\n\ntype Observer<TQueryFnData, TError, TData, TPageParam> = InfiniteQueryObserver<\n TQueryFnData,\n TError,\n TData,\n QueryKey,\n TPageParam\n>\n\ntype ObserverResult<TQueryFnData, TError, TData, TPageParam> = ReturnType<\n Observer<TQueryFnData, TError, TData, TPageParam>['getCurrentResult']\n>\n\nexport function createInfiniteQuery<\n TQueryFnData = unknown,\n TError = Error,\n TPageParam = unknown,\n TData = InfiniteData<TQueryFnData, TPageParam>,\n>(\n options: CreateInfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData>,\n): InfiniteQueryResult<TData, TError, TPageParam>\nexport function createInfiniteQuery<\n TQueryFnData = unknown,\n TError = Error,\n TPageParam = unknown,\n TData = InfiniteData<TQueryFnData, TPageParam>,\n>(\n queryClient: QueryClient,\n options: CreateInfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData>,\n): InfiniteQueryResult<TData, TError, TPageParam>\nexport function createInfiniteQuery<\n TQueryFnData = unknown,\n TError = Error,\n TPageParam = unknown,\n TData = InfiniteData<TQueryFnData, TPageParam>,\n>(\n arg1:\n | QueryClient\n | CreateInfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData>,\n arg2?: CreateInfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData>,\n): InfiniteQueryResult<TData, TError, TPageParam> {\n const [explicitClient, options] = parseInfiniteArgs<\n TQueryFnData,\n TError,\n TPageParam,\n TData\n >(arg1, arg2)\n const { queryKey, enabled, name, ...restOptions } = options\n\n if (!name) warnMissingName('createInfiniteQuery')\n\n const reactiveRefetchInterval = resolveReactiveRefetchInterval(\n (restOptions as { refetchInterval?: unknown }).refetchInterval,\n )\n if (reactiveRefetchInterval) {\n delete (restOptions as { refetchInterval?: unknown }).refetchInterval\n }\n\n const base = createBaseQuery<\n TData,\n TError,\n ObserverResult<TQueryFnData, TError, TData, TPageParam>,\n Observer<TQueryFnData, TError, TData, TPageParam>,\n {\n $hasNextPage: ReturnType<typeof createStore<boolean>>\n $hasPreviousPage: ReturnType<typeof createStore<boolean>>\n $isFetchingNextPage: ReturnType<typeof createStore<boolean>>\n $isFetchingPreviousPage: ReturnType<typeof createStore<boolean>>\n $isFetchNextPageError: ReturnType<typeof createStore<boolean>>\n $isFetchPreviousPageError: ReturnType<typeof createStore<boolean>>\n fetchNextPage: ReturnType<typeof createEvent<void>>\n fetchPreviousPage: ReturnType<typeof createEvent<void>>\n }\n >(\n explicitClient,\n { queryKey, enabled, name, reactiveRefetchInterval },\n {\n createObserver: (qc, { queryKey: key, enabled: isEnabled }) =>\n new InfiniteQueryObserver<\n TQueryFnData,\n TError,\n TData,\n QueryKey,\n TPageParam\n >(qc, {\n ...restOptions,\n queryKey: key,\n enabled: isEnabled,\n } as any),\n setupExtras: () => {\n const hasNextPageUpdated = createEvent<boolean>()\n const hasPreviousPageUpdated = createEvent<boolean>()\n const isFetchingNextPageUpdated = createEvent<boolean>()\n const isFetchingPreviousPageUpdated = createEvent<boolean>()\n const isFetchNextPageErrorUpdated = createEvent<boolean>()\n const isFetchPreviousPageErrorUpdated = createEvent<boolean>()\n\n const $hasNextPage = createStore(false, {\n ...sidConfig(name, '$hasNextPage'),\n }).on(hasNextPageUpdated, (_, v) => v)\n const $hasPreviousPage = createStore(false, {\n ...sidConfig(name, '$hasPreviousPage'),\n }).on(hasPreviousPageUpdated, (_, v) => v)\n const $isFetchingNextPage = createStore(false, {\n ...sidConfig(name, '$isFetchingNextPage'),\n }).on(isFetchingNextPageUpdated, (_, v) => v)\n const $isFetchingPreviousPage = createStore(false, {\n ...sidConfig(name, '$isFetchingPreviousPage'),\n }).on(isFetchingPreviousPageUpdated, (_, v) => v)\n const $isFetchNextPageError = createStore(false, {\n ...sidConfig(name, '$isFetchNextPageError'),\n }).on(isFetchNextPageErrorUpdated, (_, v) => v)\n const $isFetchPreviousPageError = createStore(false, {\n ...sidConfig(name, '$isFetchPreviousPageError'),\n }).on(isFetchPreviousPageErrorUpdated, (_, v) => v)\n\n const fetchNextPage = createEvent<void>()\n const fetchPreviousPage = createEvent<void>()\n\n return {\n stores: {\n $hasNextPage,\n $hasPreviousPage,\n $isFetchingNextPage,\n $isFetchingPreviousPage,\n $isFetchNextPageError,\n $isFetchPreviousPageError,\n fetchNextPage,\n fetchPreviousPage,\n },\n // Wire fetchNextPage / fetchPreviousPage as scope-aware effects via\n // attach over $observer — same pattern as the rest of createBaseQuery.\n setupEffects: ({ $observer }) => {\n const fetchNextPageFx = attach({\n source: $observer,\n effect: (observer) => {\n if (!observer) return\n observer.fetchNextPage()\n },\n })\n sample({ clock: fetchNextPage, target: fetchNextPageFx })\n\n const fetchPreviousPageFx = attach({\n source: $observer,\n effect: (observer) => {\n if (!observer) return\n observer.fetchPreviousPage()\n },\n })\n sample({ clock: fetchPreviousPage, target: fetchPreviousPageFx })\n },\n bindDispatcher: () => {\n const dispatchHasNextPage = scopeBind(hasNextPageUpdated, {\n safe: true,\n })\n const dispatchHasPreviousPage = scopeBind(hasPreviousPageUpdated, {\n safe: true,\n })\n const dispatchIsFetchingNextPage = scopeBind(\n isFetchingNextPageUpdated,\n { safe: true },\n )\n const dispatchIsFetchingPreviousPage = scopeBind(\n isFetchingPreviousPageUpdated,\n { safe: true },\n )\n const dispatchIsFetchNextPageError = scopeBind(\n isFetchNextPageErrorUpdated,\n { safe: true },\n )\n const dispatchIsFetchPreviousPageError = scopeBind(\n isFetchPreviousPageErrorUpdated,\n { safe: true },\n )\n\n return (result) => {\n dispatchHasNextPage(result.hasNextPage)\n dispatchHasPreviousPage(result.hasPreviousPage)\n dispatchIsFetchingNextPage(result.isFetchingNextPage)\n dispatchIsFetchingPreviousPage(result.isFetchingPreviousPage)\n dispatchIsFetchNextPageError(result.isFetchNextPageError)\n dispatchIsFetchPreviousPageError(result.isFetchPreviousPageError)\n }\n },\n }\n },\n },\n )\n\n // See createQuery.prefetch — same contract, but uses fetchInfiniteQuery so\n // the first page is fetched + cached on the server.\n const prefetch = createEvent<void>()\n const prefetchFx = attach({\n source: {\n qc: base.$queryClient,\n key: base.$resolvedKey,\n enabled: base.$enabled,\n },\n effect: ({ qc, key, enabled }) => {\n if (!qc || !enabled) return\n return qc.fetchInfiniteQuery({\n ...restOptions,\n queryKey: key,\n } as any)\n },\n })\n sample({ clock: prefetch, target: prefetchFx })\n\n const result: InfiniteQueryResult<TData, TError, TPageParam> = {\n $data: base.$data,\n $error: base.$error,\n $status: base.$status,\n $isPending: base.$isPending,\n $isFetching: base.$isFetching,\n $isSuccess: base.$isSuccess,\n $isError: base.$isError,\n $isPlaceholderData: base.$isPlaceholderData,\n $fetchStatus: base.$fetchStatus,\n $hasNextPage: base.$hasNextPage,\n $hasPreviousPage: base.$hasPreviousPage,\n $isFetchingNextPage: base.$isFetchingNextPage,\n $isFetchingPreviousPage: base.$isFetchingPreviousPage,\n $isFetchNextPageError: base.$isFetchNextPageError,\n $isFetchPreviousPageError: base.$isFetchPreviousPageError,\n $observer: base.$observer,\n $queryClient: base.$queryClient,\n fetchNextPage: base.fetchNextPage,\n fetchPreviousPage: base.fetchPreviousPage,\n refresh: base.refresh,\n prefetch,\n mounted: base.mounted,\n unmounted: base.unmounted,\n }\n\n Object.defineProperty(result, '__createObserver', {\n enumerable: false,\n value: (qc: QueryClient, init: { queryKey: any; enabled: boolean }) =>\n new InfiniteQueryObserver<\n TQueryFnData,\n TError,\n TData,\n QueryKey,\n TPageParam\n >(qc, {\n ...restOptions,\n queryKey: init.queryKey,\n enabled: init.enabled,\n } as any),\n })\n Object.defineProperty(result, '__resolvedKey', {\n enumerable: false,\n value: base.$resolvedKey,\n })\n Object.defineProperty(result, '__enabled', {\n enumerable: false,\n value: base.$enabled,\n })\n\n return result\n}\n\nfunction parseInfiniteArgs<TQueryFnData, TError, TPageParam, TData>(\n arg1:\n | QueryClient\n | CreateInfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData>,\n arg2?: CreateInfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData>,\n): [\n QueryClient | null,\n CreateInfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData>,\n] {\n if (arg2 !== undefined) {\n return [arg1 as QueryClient, arg2]\n }\n return [\n null,\n arg1 as CreateInfiniteQueryOptions<TQueryFnData, TError, TPageParam, TData>,\n ]\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var effector = require('effector');
|
|
4
|
+
var queryClient_cjs = require('./queryClient.cjs');
|
|
5
|
+
var resolve_cjs = require('./resolve.cjs');
|
|
6
|
+
|
|
7
|
+
// src/createInvalidate.ts
|
|
8
|
+
function createInvalidate(arg1, arg2) {
|
|
9
|
+
const [explicitClient, options] = parseArgs(arg1, arg2);
|
|
10
|
+
const { queryKey, exact, refetchType, type } = options;
|
|
11
|
+
const $effectiveClient = explicitClient ? effector.createStore(explicitClient, {
|
|
12
|
+
serialize: "ignore"
|
|
13
|
+
}) : queryClient_cjs.$queryClient;
|
|
14
|
+
const $resolvedKey = resolve_cjs.resolveKey(queryKey);
|
|
15
|
+
const invalidate = effector.createEvent();
|
|
16
|
+
const invalidateFx = effector.attach({
|
|
17
|
+
source: { qc: $effectiveClient, key: $resolvedKey },
|
|
18
|
+
effect: ({ qc, key }) => {
|
|
19
|
+
if (!qc) return;
|
|
20
|
+
const filters = { queryKey: key };
|
|
21
|
+
if (exact !== void 0) filters.exact = exact;
|
|
22
|
+
if (refetchType !== void 0) filters.refetchType = refetchType;
|
|
23
|
+
if (type !== void 0) filters.type = type;
|
|
24
|
+
return qc.invalidateQueries(filters);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
effector.sample({ clock: invalidate, target: invalidateFx });
|
|
28
|
+
return invalidate;
|
|
29
|
+
}
|
|
30
|
+
function parseArgs(arg1, arg2) {
|
|
31
|
+
if (arg2 !== void 0) return [arg1, arg2];
|
|
32
|
+
return [null, arg1];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
exports.createInvalidate = createInvalidate;
|
|
36
|
+
//# sourceMappingURL=createInvalidate.cjs.map
|
|
37
|
+
//# sourceMappingURL=createInvalidate.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/createInvalidate.ts"],"names":["createStore","$queryClient","resolveKey","createEvent","attach","sample"],"mappings":";;;;;;;AA4DO,SAAS,gBAAA,CACd,MACA,IAAA,EACqB;AACrB,EAAA,MAAM,CAAC,cAAA,EAAgB,OAAO,CAAA,GAAI,SAAA,CAAU,MAAM,IAAI,CAAA;AACtD,EAAA,MAAM,EAAE,QAAA,EAAU,KAAA,EAAO,WAAA,EAAa,MAAK,GAAI,OAAA;AAK/C,EAAA,MAAM,gBAAA,GAA8C,cAAA,GAChDA,oBAAA,CAAY,cAAA,EAAsC;AAAA,IAChD,SAAA,EAAW;AAAA,GACZ,CAAA,GACDC,4BAAA;AAEJ,EAAA,MAAM,YAAA,GAAeC,uBAAW,QAAQ,CAAA;AAExC,EAAA,MAAM,aAAaC,oBAAA,EAAkB;AAErC,EAAA,MAAM,eAAeC,eAAA,CAAO;AAAA,IAC1B,MAAA,EAAQ,EAAE,EAAA,EAAI,gBAAA,EAAkB,KAAK,YAAA,EAAa;AAAA,IAClD,MAAA,EAAQ,CAAC,EAAE,EAAA,EAAI,KAAI,KAAM;AACvB,MAAA,IAAI,CAAC,EAAA,EAAI;AACT,MAAA,MAAM,OAAA,GAAkC,EAAE,QAAA,EAAU,GAAA,EAAI;AACxD,MAAA,IAAI,KAAA,KAAU,MAAA,EAAW,OAAA,CAAQ,KAAA,GAAQ,KAAA;AACzC,MAAA,IAAI,WAAA,KAAgB,MAAA,EAAW,OAAA,CAAQ,WAAA,GAAc,WAAA;AACrD,MAAA,IAAI,IAAA,KAAS,MAAA,EAAW,OAAA,CAAQ,IAAA,GAAO,IAAA;AACvC,MAAA,OAAO,EAAA,CAAG,kBAAkB,OAAO,CAAA;AAAA,IACrC;AAAA,GACD,CAAA;AAED,EAAAC,eAAA,CAAO,EAAE,KAAA,EAAO,UAAA,EAAY,MAAA,EAAQ,cAAc,CAAA;AAElD,EAAA,OAAO,UAAA;AACT;AAEA,SAAS,SAAA,CACP,MACA,IAAA,EAC+C;AAC/C,EAAA,IAAI,IAAA,KAAS,MAAA,EAAW,OAAO,CAAC,MAAqB,IAAI,CAAA;AACzD,EAAA,OAAO,CAAC,MAAM,IAA+B,CAAA;AAC/C","file":"createInvalidate.cjs","sourcesContent":["import { attach, createEvent, createStore, sample } from 'effector'\nimport type { EventCallable, Store } from 'effector'\nimport type {\n InvalidateQueryFilters,\n QueryClient,\n} from '@tanstack/query-core'\nimport { $queryClient } from './queryClient'\nimport { resolveKey } from './resolve'\nimport type { EffectorQueryKey } from './types'\n\nexport interface CreateInvalidateOptions {\n /**\n * Key (or key prefix) to invalidate. Supports the same reactive shape as\n * `createQuery.queryKey` — drop a `Store` anywhere in the array and the\n * invalidate event will resolve it on every call, so dynamic keys \"just\n * work\".\n */\n queryKey: EffectorQueryKey\n /**\n * Forwarded to `queryClient.invalidateQueries({ exact })`. With `exact: true`\n * only queries whose key *exactly* matches are invalidated; otherwise the\n * key is treated as a prefix.\n */\n exact?: boolean\n /**\n * Forwarded to `queryClient.invalidateQueries({ refetchType })`. Controls\n * which observers refetch immediately after invalidation. Defaults to\n * `'active'` (TanStack Query's own default).\n */\n refetchType?: 'active' | 'inactive' | 'all' | 'none'\n /**\n * Forwarded to `queryClient.invalidateQueries({ type })`. Filters which\n * queries match — `'active'`, `'inactive'`, `'all'`.\n */\n type?: 'active' | 'inactive' | 'all'\n}\n\n/**\n * Builds an effector event that invalidates a query (or a key prefix) on the\n * resolved `QueryClient`. Useful when you want to invalidate a query\n * declaratively from a `sample` (e.g. on a mutation's `finished.success`)\n * without writing a one-off `attach` every time.\n *\n * @example Static key\n * const invalidateFavorites = createInvalidate({ queryKey: ['favorites'] })\n * sample({ clock: addFavorite.finished.success, target: invalidateFavorites })\n *\n * @example Reactive key — uses current $userId at invocation time\n * const invalidateUser = createInvalidate({ queryKey: ['user', $userId] })\n *\n * @example Explicit client (same back-compat overload as the other factories)\n * const invalidateFavorites = createInvalidate(queryClient, { queryKey: ['favorites'] })\n */\nexport function createInvalidate(\n options: CreateInvalidateOptions,\n): EventCallable<void>\nexport function createInvalidate(\n queryClient: QueryClient,\n options: CreateInvalidateOptions,\n): EventCallable<void>\nexport function createInvalidate(\n arg1: QueryClient | CreateInvalidateOptions,\n arg2?: CreateInvalidateOptions,\n): EventCallable<void> {\n const [explicitClient, options] = parseArgs(arg1, arg2)\n const { queryKey, exact, refetchType, type } = options\n\n // Same locking semantics as the other factories: explicit client freezes\n // the store, default flows through global $queryClient (and respects\n // fork({ values: [[$queryClient, qc]] }) for per-scope isolation).\n const $effectiveClient: Store<QueryClient | null> = explicitClient\n ? createStore(explicitClient as QueryClient | null, {\n serialize: 'ignore',\n })\n : $queryClient\n\n const $resolvedKey = resolveKey(queryKey)\n\n const invalidate = createEvent<void>()\n\n const invalidateFx = attach({\n source: { qc: $effectiveClient, key: $resolvedKey },\n effect: ({ qc, key }) => {\n if (!qc) return\n const filters: InvalidateQueryFilters = { queryKey: key }\n if (exact !== undefined) filters.exact = exact\n if (refetchType !== undefined) filters.refetchType = refetchType\n if (type !== undefined) filters.type = type\n return qc.invalidateQueries(filters)\n },\n })\n\n sample({ clock: invalidate, target: invalidateFx })\n\n return invalidate\n}\n\nfunction parseArgs(\n arg1: QueryClient | CreateInvalidateOptions,\n arg2?: CreateInvalidateOptions,\n): [QueryClient | null, CreateInvalidateOptions] {\n if (arg2 !== undefined) return [arg1 as QueryClient, arg2]\n return [null, arg1 as CreateInvalidateOptions]\n}\n"]}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { EventCallable } from 'effector';
|
|
2
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
3
|
+
import { EffectorQueryKey } from './types.cjs';
|
|
4
|
+
|
|
5
|
+
interface CreateInvalidateOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Key (or key prefix) to invalidate. Supports the same reactive shape as
|
|
8
|
+
* `createQuery.queryKey` — drop a `Store` anywhere in the array and the
|
|
9
|
+
* invalidate event will resolve it on every call, so dynamic keys "just
|
|
10
|
+
* work".
|
|
11
|
+
*/
|
|
12
|
+
queryKey: EffectorQueryKey;
|
|
13
|
+
/**
|
|
14
|
+
* Forwarded to `queryClient.invalidateQueries({ exact })`. With `exact: true`
|
|
15
|
+
* only queries whose key *exactly* matches are invalidated; otherwise the
|
|
16
|
+
* key is treated as a prefix.
|
|
17
|
+
*/
|
|
18
|
+
exact?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Forwarded to `queryClient.invalidateQueries({ refetchType })`. Controls
|
|
21
|
+
* which observers refetch immediately after invalidation. Defaults to
|
|
22
|
+
* `'active'` (TanStack Query's own default).
|
|
23
|
+
*/
|
|
24
|
+
refetchType?: 'active' | 'inactive' | 'all' | 'none';
|
|
25
|
+
/**
|
|
26
|
+
* Forwarded to `queryClient.invalidateQueries({ type })`. Filters which
|
|
27
|
+
* queries match — `'active'`, `'inactive'`, `'all'`.
|
|
28
|
+
*/
|
|
29
|
+
type?: 'active' | 'inactive' | 'all';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds an effector event that invalidates a query (or a key prefix) on the
|
|
33
|
+
* resolved `QueryClient`. Useful when you want to invalidate a query
|
|
34
|
+
* declaratively from a `sample` (e.g. on a mutation's `finished.success`)
|
|
35
|
+
* without writing a one-off `attach` every time.
|
|
36
|
+
*
|
|
37
|
+
* @example Static key
|
|
38
|
+
* const invalidateFavorites = createInvalidate({ queryKey: ['favorites'] })
|
|
39
|
+
* sample({ clock: addFavorite.finished.success, target: invalidateFavorites })
|
|
40
|
+
*
|
|
41
|
+
* @example Reactive key — uses current $userId at invocation time
|
|
42
|
+
* const invalidateUser = createInvalidate({ queryKey: ['user', $userId] })
|
|
43
|
+
*
|
|
44
|
+
* @example Explicit client (same back-compat overload as the other factories)
|
|
45
|
+
* const invalidateFavorites = createInvalidate(queryClient, { queryKey: ['favorites'] })
|
|
46
|
+
*/
|
|
47
|
+
declare function createInvalidate(options: CreateInvalidateOptions): EventCallable<void>;
|
|
48
|
+
declare function createInvalidate(queryClient: QueryClient, options: CreateInvalidateOptions): EventCallable<void>;
|
|
49
|
+
|
|
50
|
+
export { type CreateInvalidateOptions, createInvalidate };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { EventCallable } from 'effector';
|
|
2
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
3
|
+
import { EffectorQueryKey } from './types.js';
|
|
4
|
+
|
|
5
|
+
interface CreateInvalidateOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Key (or key prefix) to invalidate. Supports the same reactive shape as
|
|
8
|
+
* `createQuery.queryKey` — drop a `Store` anywhere in the array and the
|
|
9
|
+
* invalidate event will resolve it on every call, so dynamic keys "just
|
|
10
|
+
* work".
|
|
11
|
+
*/
|
|
12
|
+
queryKey: EffectorQueryKey;
|
|
13
|
+
/**
|
|
14
|
+
* Forwarded to `queryClient.invalidateQueries({ exact })`. With `exact: true`
|
|
15
|
+
* only queries whose key *exactly* matches are invalidated; otherwise the
|
|
16
|
+
* key is treated as a prefix.
|
|
17
|
+
*/
|
|
18
|
+
exact?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Forwarded to `queryClient.invalidateQueries({ refetchType })`. Controls
|
|
21
|
+
* which observers refetch immediately after invalidation. Defaults to
|
|
22
|
+
* `'active'` (TanStack Query's own default).
|
|
23
|
+
*/
|
|
24
|
+
refetchType?: 'active' | 'inactive' | 'all' | 'none';
|
|
25
|
+
/**
|
|
26
|
+
* Forwarded to `queryClient.invalidateQueries({ type })`. Filters which
|
|
27
|
+
* queries match — `'active'`, `'inactive'`, `'all'`.
|
|
28
|
+
*/
|
|
29
|
+
type?: 'active' | 'inactive' | 'all';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds an effector event that invalidates a query (or a key prefix) on the
|
|
33
|
+
* resolved `QueryClient`. Useful when you want to invalidate a query
|
|
34
|
+
* declaratively from a `sample` (e.g. on a mutation's `finished.success`)
|
|
35
|
+
* without writing a one-off `attach` every time.
|
|
36
|
+
*
|
|
37
|
+
* @example Static key
|
|
38
|
+
* const invalidateFavorites = createInvalidate({ queryKey: ['favorites'] })
|
|
39
|
+
* sample({ clock: addFavorite.finished.success, target: invalidateFavorites })
|
|
40
|
+
*
|
|
41
|
+
* @example Reactive key — uses current $userId at invocation time
|
|
42
|
+
* const invalidateUser = createInvalidate({ queryKey: ['user', $userId] })
|
|
43
|
+
*
|
|
44
|
+
* @example Explicit client (same back-compat overload as the other factories)
|
|
45
|
+
* const invalidateFavorites = createInvalidate(queryClient, { queryKey: ['favorites'] })
|
|
46
|
+
*/
|
|
47
|
+
declare function createInvalidate(options: CreateInvalidateOptions): EventCallable<void>;
|
|
48
|
+
declare function createInvalidate(queryClient: QueryClient, options: CreateInvalidateOptions): EventCallable<void>;
|
|
49
|
+
|
|
50
|
+
export { type CreateInvalidateOptions, createInvalidate };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createStore, createEvent, attach, sample } from 'effector';
|
|
2
|
+
import { $queryClient } from './queryClient.js';
|
|
3
|
+
import { resolveKey } from './resolve.js';
|
|
4
|
+
|
|
5
|
+
// src/createInvalidate.ts
|
|
6
|
+
function createInvalidate(arg1, arg2) {
|
|
7
|
+
const [explicitClient, options] = parseArgs(arg1, arg2);
|
|
8
|
+
const { queryKey, exact, refetchType, type } = options;
|
|
9
|
+
const $effectiveClient = explicitClient ? createStore(explicitClient, {
|
|
10
|
+
serialize: "ignore"
|
|
11
|
+
}) : $queryClient;
|
|
12
|
+
const $resolvedKey = resolveKey(queryKey);
|
|
13
|
+
const invalidate = createEvent();
|
|
14
|
+
const invalidateFx = attach({
|
|
15
|
+
source: { qc: $effectiveClient, key: $resolvedKey },
|
|
16
|
+
effect: ({ qc, key }) => {
|
|
17
|
+
if (!qc) return;
|
|
18
|
+
const filters = { queryKey: key };
|
|
19
|
+
if (exact !== void 0) filters.exact = exact;
|
|
20
|
+
if (refetchType !== void 0) filters.refetchType = refetchType;
|
|
21
|
+
if (type !== void 0) filters.type = type;
|
|
22
|
+
return qc.invalidateQueries(filters);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
sample({ clock: invalidate, target: invalidateFx });
|
|
26
|
+
return invalidate;
|
|
27
|
+
}
|
|
28
|
+
function parseArgs(arg1, arg2) {
|
|
29
|
+
if (arg2 !== void 0) return [arg1, arg2];
|
|
30
|
+
return [null, arg1];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { createInvalidate };
|
|
34
|
+
//# sourceMappingURL=createInvalidate.js.map
|
|
35
|
+
//# sourceMappingURL=createInvalidate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/createInvalidate.ts"],"names":[],"mappings":";;;;;AA4DO,SAAS,gBAAA,CACd,MACA,IAAA,EACqB;AACrB,EAAA,MAAM,CAAC,cAAA,EAAgB,OAAO,CAAA,GAAI,SAAA,CAAU,MAAM,IAAI,CAAA;AACtD,EAAA,MAAM,EAAE,QAAA,EAAU,KAAA,EAAO,WAAA,EAAa,MAAK,GAAI,OAAA;AAK/C,EAAA,MAAM,gBAAA,GAA8C,cAAA,GAChD,WAAA,CAAY,cAAA,EAAsC;AAAA,IAChD,SAAA,EAAW;AAAA,GACZ,CAAA,GACD,YAAA;AAEJ,EAAA,MAAM,YAAA,GAAe,WAAW,QAAQ,CAAA;AAExC,EAAA,MAAM,aAAa,WAAA,EAAkB;AAErC,EAAA,MAAM,eAAe,MAAA,CAAO;AAAA,IAC1B,MAAA,EAAQ,EAAE,EAAA,EAAI,gBAAA,EAAkB,KAAK,YAAA,EAAa;AAAA,IAClD,MAAA,EAAQ,CAAC,EAAE,EAAA,EAAI,KAAI,KAAM;AACvB,MAAA,IAAI,CAAC,EAAA,EAAI;AACT,MAAA,MAAM,OAAA,GAAkC,EAAE,QAAA,EAAU,GAAA,EAAI;AACxD,MAAA,IAAI,KAAA,KAAU,MAAA,EAAW,OAAA,CAAQ,KAAA,GAAQ,KAAA;AACzC,MAAA,IAAI,WAAA,KAAgB,MAAA,EAAW,OAAA,CAAQ,WAAA,GAAc,WAAA;AACrD,MAAA,IAAI,IAAA,KAAS,MAAA,EAAW,OAAA,CAAQ,IAAA,GAAO,IAAA;AACvC,MAAA,OAAO,EAAA,CAAG,kBAAkB,OAAO,CAAA;AAAA,IACrC;AAAA,GACD,CAAA;AAED,EAAA,MAAA,CAAO,EAAE,KAAA,EAAO,UAAA,EAAY,MAAA,EAAQ,cAAc,CAAA;AAElD,EAAA,OAAO,UAAA;AACT;AAEA,SAAS,SAAA,CACP,MACA,IAAA,EAC+C;AAC/C,EAAA,IAAI,IAAA,KAAS,MAAA,EAAW,OAAO,CAAC,MAAqB,IAAI,CAAA;AACzD,EAAA,OAAO,CAAC,MAAM,IAA+B,CAAA;AAC/C","file":"createInvalidate.js","sourcesContent":["import { attach, createEvent, createStore, sample } from 'effector'\nimport type { EventCallable, Store } from 'effector'\nimport type {\n InvalidateQueryFilters,\n QueryClient,\n} from '@tanstack/query-core'\nimport { $queryClient } from './queryClient'\nimport { resolveKey } from './resolve'\nimport type { EffectorQueryKey } from './types'\n\nexport interface CreateInvalidateOptions {\n /**\n * Key (or key prefix) to invalidate. Supports the same reactive shape as\n * `createQuery.queryKey` — drop a `Store` anywhere in the array and the\n * invalidate event will resolve it on every call, so dynamic keys \"just\n * work\".\n */\n queryKey: EffectorQueryKey\n /**\n * Forwarded to `queryClient.invalidateQueries({ exact })`. With `exact: true`\n * only queries whose key *exactly* matches are invalidated; otherwise the\n * key is treated as a prefix.\n */\n exact?: boolean\n /**\n * Forwarded to `queryClient.invalidateQueries({ refetchType })`. Controls\n * which observers refetch immediately after invalidation. Defaults to\n * `'active'` (TanStack Query's own default).\n */\n refetchType?: 'active' | 'inactive' | 'all' | 'none'\n /**\n * Forwarded to `queryClient.invalidateQueries({ type })`. Filters which\n * queries match — `'active'`, `'inactive'`, `'all'`.\n */\n type?: 'active' | 'inactive' | 'all'\n}\n\n/**\n * Builds an effector event that invalidates a query (or a key prefix) on the\n * resolved `QueryClient`. Useful when you want to invalidate a query\n * declaratively from a `sample` (e.g. on a mutation's `finished.success`)\n * without writing a one-off `attach` every time.\n *\n * @example Static key\n * const invalidateFavorites = createInvalidate({ queryKey: ['favorites'] })\n * sample({ clock: addFavorite.finished.success, target: invalidateFavorites })\n *\n * @example Reactive key — uses current $userId at invocation time\n * const invalidateUser = createInvalidate({ queryKey: ['user', $userId] })\n *\n * @example Explicit client (same back-compat overload as the other factories)\n * const invalidateFavorites = createInvalidate(queryClient, { queryKey: ['favorites'] })\n */\nexport function createInvalidate(\n options: CreateInvalidateOptions,\n): EventCallable<void>\nexport function createInvalidate(\n queryClient: QueryClient,\n options: CreateInvalidateOptions,\n): EventCallable<void>\nexport function createInvalidate(\n arg1: QueryClient | CreateInvalidateOptions,\n arg2?: CreateInvalidateOptions,\n): EventCallable<void> {\n const [explicitClient, options] = parseArgs(arg1, arg2)\n const { queryKey, exact, refetchType, type } = options\n\n // Same locking semantics as the other factories: explicit client freezes\n // the store, default flows through global $queryClient (and respects\n // fork({ values: [[$queryClient, qc]] }) for per-scope isolation).\n const $effectiveClient: Store<QueryClient | null> = explicitClient\n ? createStore(explicitClient as QueryClient | null, {\n serialize: 'ignore',\n })\n : $queryClient\n\n const $resolvedKey = resolveKey(queryKey)\n\n const invalidate = createEvent<void>()\n\n const invalidateFx = attach({\n source: { qc: $effectiveClient, key: $resolvedKey },\n effect: ({ qc, key }) => {\n if (!qc) return\n const filters: InvalidateQueryFilters = { queryKey: key }\n if (exact !== undefined) filters.exact = exact\n if (refetchType !== undefined) filters.refetchType = refetchType\n if (type !== undefined) filters.type = type\n return qc.invalidateQueries(filters)\n },\n })\n\n sample({ clock: invalidate, target: invalidateFx })\n\n return invalidate\n}\n\nfunction parseArgs(\n arg1: QueryClient | CreateInvalidateOptions,\n arg2?: CreateInvalidateOptions,\n): [QueryClient | null, CreateInvalidateOptions] {\n if (arg2 !== undefined) return [arg1 as QueryClient, arg2]\n return [null, arg1 as CreateInvalidateOptions]\n}\n"]}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var effector = require('effector');
|
|
4
|
+
var queryCore = require('@tanstack/query-core');
|
|
5
|
+
var queryClient_cjs = require('./queryClient.cjs');
|
|
6
|
+
var createBaseQuery_cjs = require('./createBaseQuery.cjs');
|
|
7
|
+
|
|
8
|
+
// src/createMutation.ts
|
|
9
|
+
function createMutation(arg1, arg2) {
|
|
10
|
+
const [explicitClient, options] = parseMutationArgs(arg1, arg2);
|
|
11
|
+
const { name, ...observerOptions } = options;
|
|
12
|
+
if (!name) createBaseQuery_cjs.warnMissingName("createMutation");
|
|
13
|
+
const $effectiveClient = explicitClient ? effector.createStore(explicitClient, {
|
|
14
|
+
serialize: "ignore"
|
|
15
|
+
}) : queryClient_cjs.$queryClient;
|
|
16
|
+
const dataUpdated = effector.createEvent();
|
|
17
|
+
const errorUpdated = effector.createEvent();
|
|
18
|
+
const statusUpdated = effector.createEvent();
|
|
19
|
+
const variablesUpdated = effector.createEvent();
|
|
20
|
+
const isPausedUpdated = effector.createEvent();
|
|
21
|
+
const finishedSuccess = effector.createEvent();
|
|
22
|
+
const finishedFailure = effector.createEvent();
|
|
23
|
+
const $data = effector.createStore(void 0, {
|
|
24
|
+
skipVoid: false,
|
|
25
|
+
...createBaseQuery_cjs.sidConfig(name, "$data")
|
|
26
|
+
}).on(dataUpdated, (_, v) => v);
|
|
27
|
+
const $error = effector.createStore(null, {
|
|
28
|
+
skipVoid: false,
|
|
29
|
+
...createBaseQuery_cjs.sidConfig(name, "$error")
|
|
30
|
+
}).on(errorUpdated, (_, v) => v);
|
|
31
|
+
const $status = effector.createStore("idle", {
|
|
32
|
+
...createBaseQuery_cjs.sidConfig(name, "$status")
|
|
33
|
+
}).on(statusUpdated, (_, v) => v);
|
|
34
|
+
const $variables = effector.createStore(void 0, {
|
|
35
|
+
skipVoid: false,
|
|
36
|
+
...createBaseQuery_cjs.sidConfig(name, "$variables")
|
|
37
|
+
}).on(variablesUpdated, (_, v) => v);
|
|
38
|
+
const $isPaused = effector.createStore(false, {
|
|
39
|
+
...createBaseQuery_cjs.sidConfig(name, "$isPaused")
|
|
40
|
+
}).on(isPausedUpdated, (_, v) => v);
|
|
41
|
+
const $isPending = $status.map((s) => s === "pending");
|
|
42
|
+
const $isSuccess = $status.map((s) => s === "success");
|
|
43
|
+
const $isError = $status.map((s) => s === "error");
|
|
44
|
+
const $isIdle = $status.map((s) => s === "idle");
|
|
45
|
+
const $observer = effector.createStore(null, {
|
|
46
|
+
serialize: "ignore"
|
|
47
|
+
});
|
|
48
|
+
const observerCreated = effector.createEvent();
|
|
49
|
+
$observer.on(observerCreated, (_, obs) => obs);
|
|
50
|
+
const observerSubscriptions = /* @__PURE__ */ new WeakMap();
|
|
51
|
+
const start = effector.createEvent();
|
|
52
|
+
const unmounted = effector.createEvent();
|
|
53
|
+
const startFx = effector.attach({
|
|
54
|
+
source: { qc: $effectiveClient, observer: $observer },
|
|
55
|
+
effect: ({ qc, observer: existingObserver }) => {
|
|
56
|
+
if (!qc) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
"[@tanstack/query-effector] No QueryClient is set for createMutation. Call setQueryClient(qc) before start, pass it to fork({ values: [[$queryClient, qc]] }), or pass it explicitly to the factory."
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const observer = existingObserver ?? new queryCore.MutationObserver(
|
|
62
|
+
qc,
|
|
63
|
+
observerOptions
|
|
64
|
+
);
|
|
65
|
+
const dispatchData = effector.scopeBind(dataUpdated, { safe: true });
|
|
66
|
+
const dispatchError = effector.scopeBind(errorUpdated, { safe: true });
|
|
67
|
+
const dispatchStatus = effector.scopeBind(statusUpdated, { safe: true });
|
|
68
|
+
const dispatchVariables = effector.scopeBind(variablesUpdated, { safe: true });
|
|
69
|
+
const dispatchIsPaused = effector.scopeBind(isPausedUpdated, { safe: true });
|
|
70
|
+
const dispatchFinishedSuccess = effector.scopeBind(finishedSuccess, { safe: true });
|
|
71
|
+
const dispatchFinishedFailure = effector.scopeBind(finishedFailure, { safe: true });
|
|
72
|
+
observerSubscriptions.get(observer)?.();
|
|
73
|
+
let prevStatus = "idle";
|
|
74
|
+
const unsubscribe = observer.subscribe((result) => {
|
|
75
|
+
dispatchData(result.data);
|
|
76
|
+
dispatchError(result.error);
|
|
77
|
+
dispatchStatus(result.status);
|
|
78
|
+
dispatchVariables(result.variables);
|
|
79
|
+
dispatchIsPaused(result.isPaused);
|
|
80
|
+
if (prevStatus === "pending") {
|
|
81
|
+
if (result.status === "success") {
|
|
82
|
+
dispatchFinishedSuccess({
|
|
83
|
+
params: result.variables,
|
|
84
|
+
result: result.data
|
|
85
|
+
});
|
|
86
|
+
} else if (result.status === "error") {
|
|
87
|
+
dispatchFinishedFailure({
|
|
88
|
+
params: result.variables,
|
|
89
|
+
error: result.error
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
prevStatus = result.status;
|
|
94
|
+
});
|
|
95
|
+
observerSubscriptions.set(observer, unsubscribe);
|
|
96
|
+
return observer;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
effector.sample({ clock: start, target: startFx });
|
|
100
|
+
effector.sample({ clock: startFx.doneData, target: observerCreated });
|
|
101
|
+
const unmountFx = effector.attach({
|
|
102
|
+
source: $observer,
|
|
103
|
+
effect: (observer) => {
|
|
104
|
+
if (!observer) return;
|
|
105
|
+
observerSubscriptions.get(observer)?.();
|
|
106
|
+
observerSubscriptions.delete(observer);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
effector.sample({ clock: unmounted, target: unmountFx });
|
|
110
|
+
const mutate = effector.createEvent();
|
|
111
|
+
const mutateFx = effector.attach({
|
|
112
|
+
source: $observer,
|
|
113
|
+
effect: (observer, variables) => {
|
|
114
|
+
if (!observer) return;
|
|
115
|
+
observer.mutate(variables).catch(() => {
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
effector.sample({ clock: mutate, target: mutateFx });
|
|
120
|
+
const mutateWith = effector.createEvent();
|
|
121
|
+
const mutateWithFx = effector.attach({
|
|
122
|
+
source: $observer,
|
|
123
|
+
effect: (observer, {
|
|
124
|
+
variables,
|
|
125
|
+
...callbacks
|
|
126
|
+
}) => {
|
|
127
|
+
if (!observer) return;
|
|
128
|
+
observer.mutate(variables, callbacks).catch(() => {
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
effector.sample({ clock: mutateWith, target: mutateWithFx });
|
|
133
|
+
const reset = effector.createEvent();
|
|
134
|
+
const resetFx = effector.attach({
|
|
135
|
+
source: $observer,
|
|
136
|
+
effect: (observer) => {
|
|
137
|
+
if (!observer) return;
|
|
138
|
+
observer.reset();
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
effector.sample({ clock: reset, target: resetFx });
|
|
142
|
+
return {
|
|
143
|
+
$data,
|
|
144
|
+
$error,
|
|
145
|
+
$status,
|
|
146
|
+
$variables,
|
|
147
|
+
$isPaused,
|
|
148
|
+
$isPending,
|
|
149
|
+
$isSuccess,
|
|
150
|
+
$isError,
|
|
151
|
+
$isIdle,
|
|
152
|
+
$observer,
|
|
153
|
+
$queryClient: $effectiveClient,
|
|
154
|
+
mutate,
|
|
155
|
+
mutateWith,
|
|
156
|
+
reset,
|
|
157
|
+
start,
|
|
158
|
+
unmounted,
|
|
159
|
+
finished: {
|
|
160
|
+
success: finishedSuccess,
|
|
161
|
+
failure: finishedFailure
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function parseMutationArgs(arg1, arg2) {
|
|
166
|
+
if (arg2 !== void 0) {
|
|
167
|
+
return [arg1, arg2];
|
|
168
|
+
}
|
|
169
|
+
return [
|
|
170
|
+
null,
|
|
171
|
+
arg1
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
exports.createMutation = createMutation;
|
|
176
|
+
//# sourceMappingURL=createMutation.cjs.map
|
|
177
|
+
//# sourceMappingURL=createMutation.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/createMutation.ts"],"names":["warnMissingName","createStore","$queryClient","createEvent","sidConfig","attach","MutationObserver","scopeBind","sample"],"mappings":";;;;;;;;AAgBO,SAAS,cAAA,CAMd,MAGA,IAAA,EAC2C;AAC3C,EAAA,MAAM,CAAC,cAAA,EAAgB,OAAO,CAAA,GAAI,iBAAA,CAKhC,MAAM,IAAI,CAAA;AAEZ,EAAA,MAAM,EAAE,IAAA,EAAM,GAAG,eAAA,EAAgB,GAAI,OAAA;AAErC,EAAA,IAAI,CAAC,IAAA,EAAMA,mCAAA,CAAgB,gBAAgB,CAAA;AAE3C,EAAA,MAAM,gBAAA,GAA8C,cAAA,GAChDC,oBAAA,CAAY,cAAA,EAAsC;AAAA,IAChD,SAAA,EAAW;AAAA,GACZ,CAAA,GACDC,4BAAA;AAEJ,EAAA,MAAM,cAAcC,oBAAA,EAA+B;AACnD,EAAA,MAAM,eAAeA,oBAAA,EAA2B;AAChD,EAAA,MAAM,gBAAgBA,oBAAA,EAA4B;AAClD,EAAA,MAAM,mBAAmBA,oBAAA,EAAoC;AAC7D,EAAA,MAAM,kBAAkBA,oBAAA,EAA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EAAW,SAAS,CAAA,CAAE,MAAM,MAAM;AAAA,MAAC,CAAC,CAAA;AAAA,IACtD;AAAA,GACD,CAAA;AAED,EAAAG,eAAA,CAAO,EAAE,KAAA,EAAO,UAAA,EAAY,MAAA,EAAQ,cAAc,CAAA;AAElD,EAAA,MAAM,QAAQL,oBAAA,EAAkB;AAEhC,EAAA,MAAM,UAAUE,eAAA,CAAO;AAAA,IACrB,MAAA,EAAQ,SAAA;AAAA,IACR,MAAA,EAAQ,CAAC,QAAA,KAAa;AACpB,MAAA,IAAI,CAAC,QAAA,EAAU;AACf,MAAA,QAAA,CAAS,KAAA,EAAM;AAAA,IACjB;AAAA,GACD,CAAA;AAED,EAAAG,eAAA,CAAO,EAAE,KAAA,EAAO,KAAA,EAAO,MAAA,EAAQ,SAAS,CAAA;AAExC,EAAA,OAAO;AAAA,IACL,KAAA;AAAA,IACA,MAAA;AAAA,IACA,OAAA;AAAA,IACA,UAAA;AAAA,IACA,SAAA;AAAA,IACA,UAAA;AAAA,IACA,UAAA;AAAA,IACA,QAAA;AAAA,IACA,OAAA;AAAA,IACA,SAAA;AAAA,IACA,YAAA,EAAc,gBAAA;AAAA,IACd,MAAA;AAAA,IACA,UAAA;AAAA,IACA,KAAA;AAAA,IACA,KAAA;AAAA,IACA,SAAA;AAAA,IACA,QAAA,EAAU;AAAA,MACR,OAAA,EAAS,eAAA;AAAA,MACT,OAAA,EAAS;AAAA;AACX,GACF;AACF;AAEA,SAAS,iBAAA,CACP,MAGA,IAAA,EAIA;AACA,EAAA,IAAI,SAAS,MAAA,EAAW;AACtB,IAAA,OAAO,CAAC,MAAqB,IAAI,CAAA;AAAA,EACnC;AACA,EAAA,OAAO;AAAA,IACL,IAAA;AAAA,IACA;AAAA,GACF;AACF","file":"createMutation.cjs","sourcesContent":["import {\n attach,\n createEvent,\n createStore,\n sample,\n scopeBind,\n} from 'effector'\nimport type { Store } from 'effector'\nimport { MutationObserver } from '@tanstack/query-core'\nimport type { MutateOptions, QueryClient } from '@tanstack/query-core'\nimport { $queryClient } from './queryClient'\nimport { sidConfig, warnMissingName } from './createBaseQuery'\nimport type { CreateMutationOptions, MutationResult } from './types'\n\ntype MutationStatus = 'idle' | 'pending' | 'success' | 'error'\n\nexport function createMutation<\n TData = unknown,\n TError = Error,\n TVariables = void,\n TOnMutateResult = unknown,\n>(\n arg1:\n | QueryClient\n | CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n arg2?: CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n): MutationResult<TData, TError, TVariables> {\n const [explicitClient, options] = parseMutationArgs<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >(arg1, arg2)\n\n const { name, ...observerOptions } = options\n\n if (!name) warnMissingName('createMutation')\n\n const $effectiveClient: Store<QueryClient | null> = explicitClient\n ? createStore(explicitClient as QueryClient | null, {\n serialize: 'ignore',\n })\n : $queryClient\n\n const dataUpdated = createEvent<TData | undefined>()\n const errorUpdated = createEvent<TError | null>()\n const statusUpdated = createEvent<MutationStatus>()\n const variablesUpdated = createEvent<TVariables | undefined>()\n const isPausedUpdated = createEvent<boolean>()\n const finishedSuccess = createEvent<{ params: TVariables; result: TData }>()\n const finishedFailure = createEvent<{ params: TVariables; error: TError }>()\n\n const $data = createStore<TData | undefined>(undefined, {\n skipVoid: false,\n ...sidConfig(name, '$data'),\n }).on(dataUpdated, (_, v) => v)\n const $error = createStore<TError | null>(null, {\n skipVoid: false,\n ...sidConfig(name, '$error'),\n }).on(errorUpdated, (_, v) => v)\n const $status = createStore<MutationStatus>('idle', {\n ...sidConfig(name, '$status'),\n }).on(statusUpdated, (_, v) => v)\n const $variables = createStore<TVariables | undefined>(undefined, {\n skipVoid: false,\n ...sidConfig(name, '$variables'),\n }).on(variablesUpdated, (_, v) => v)\n const $isPaused = createStore(false, {\n ...sidConfig(name, '$isPaused'),\n }).on(isPausedUpdated, (_, v) => v)\n\n // Derived stores via .map don't take sid — they recompute from $status on\n // the client after fork({ values }).\n const $isPending = $status.map((s) => s === 'pending')\n const $isSuccess = $status.map((s) => s === 'success')\n const $isError = $status.map((s) => s === 'error')\n const $isIdle = $status.map((s) => s === 'idle')\n\n // Per-scope observer — same pattern as createBaseQuery. Each fork scope\n // gets its own MutationObserver bound to its scope's QueryClient.\n type Observer = MutationObserver<TData, TError, TVariables, TOnMutateResult>\n const $observer = createStore<Observer | null>(null, {\n serialize: 'ignore',\n })\n const observerCreated = createEvent<Observer>()\n $observer.on(observerCreated, (_, obs) => obs)\n\n const observerSubscriptions = new WeakMap<Observer, () => void>()\n\n const start = createEvent<void>()\n const unmounted = createEvent<void>()\n\n const startFx = attach({\n source: { qc: $effectiveClient, observer: $observer },\n effect: ({ qc, observer: existingObserver }) => {\n if (!qc) {\n throw new Error(\n '[@tanstack/query-effector] No QueryClient is set for createMutation. Call setQueryClient(qc) before start, ' +\n 'pass it to fork({ values: [[$queryClient, qc]] }), or pass it explicitly to the factory.',\n )\n }\n\n const observer =\n existingObserver ??\n new MutationObserver<TData, TError, TVariables, TOnMutateResult>(\n qc,\n observerOptions,\n )\n\n const dispatchData = scopeBind(dataUpdated, { safe: true })\n const dispatchError = scopeBind(errorUpdated, { safe: true })\n const dispatchStatus = scopeBind(statusUpdated, { safe: true })\n const dispatchVariables = scopeBind(variablesUpdated, { safe: true })\n const dispatchIsPaused = scopeBind(isPausedUpdated, { safe: true })\n const dispatchFinishedSuccess = scopeBind(finishedSuccess, { safe: true })\n const dispatchFinishedFailure = scopeBind(finishedFailure, { safe: true })\n\n observerSubscriptions.get(observer)?.()\n\n // Track status transitions to emit `finished.success` / `finished.failure`\n // exactly once per pending → terminal transition. The observer holds at\n // most one inflight mutation at a time, so a single previous-status flag\n // is sufficient.\n let prevStatus: MutationStatus = 'idle'\n\n const unsubscribe = observer.subscribe((result) => {\n dispatchData(result.data)\n dispatchError(result.error)\n dispatchStatus(result.status)\n dispatchVariables(result.variables)\n dispatchIsPaused(result.isPaused)\n\n if (prevStatus === 'pending') {\n if (result.status === 'success') {\n dispatchFinishedSuccess({\n params: result.variables as TVariables,\n result: result.data as TData,\n })\n } else if (result.status === 'error') {\n dispatchFinishedFailure({\n params: result.variables as TVariables,\n error: result.error as TError,\n })\n }\n }\n prevStatus = result.status\n })\n\n observerSubscriptions.set(observer, unsubscribe)\n\n return observer\n },\n })\n\n sample({ clock: start, target: startFx })\n sample({ clock: startFx.doneData, target: observerCreated })\n\n // Mutation observers (unlike query observers) live across mount cycles —\n // their data state survives unmount and can be observed again on re-start.\n // Match that by keeping `$observer` populated; we only drop the\n // subscription so listeners stop receiving updates after unmount.\n const unmountFx = attach({\n source: $observer,\n effect: (observer) => {\n if (!observer) return\n observerSubscriptions.get(observer)?.()\n observerSubscriptions.delete(observer)\n },\n })\n sample({ clock: unmounted, target: unmountFx })\n\n const mutate = createEvent<TVariables>()\n\n // Fire-and-forget: don't return the promise so allSettled doesn't block\n // (would deadlock under fake timers since observer.mutate awaits the user's\n // mutationFn). Errors are tracked via the observer subscription which\n // updates $error/$status and emits `finished.failure`.\n const mutateFx = attach({\n source: $observer,\n effect: (observer, variables: TVariables) => {\n if (!observer) return\n observer.mutate(variables).catch(() => {})\n },\n })\n\n sample({ clock: mutate, target: mutateFx })\n\n // Per-call callbacks variant: forwards onSuccess/onError/onSettled to\n // observer.mutate(vars, opts). Use this for component-local reactions\n // that don't fit module-level `sample({ clock: finished.success })` wiring.\n const mutateWith = createEvent<{\n variables: TVariables\n onSuccess?: MutateOptions<TData, TError, TVariables>['onSuccess']\n onError?: MutateOptions<TData, TError, TVariables>['onError']\n onSettled?: MutateOptions<TData, TError, TVariables>['onSettled']\n }>()\n\n const mutateWithFx = attach({\n source: $observer,\n effect: (\n observer,\n {\n variables,\n ...callbacks\n }: {\n variables: TVariables\n onSuccess?: MutateOptions<TData, TError, TVariables>['onSuccess']\n onError?: MutateOptions<TData, TError, TVariables>['onError']\n onSettled?: MutateOptions<TData, TError, TVariables>['onSettled']\n },\n ) => {\n if (!observer) return\n observer.mutate(variables, callbacks).catch(() => {})\n },\n })\n\n sample({ clock: mutateWith, target: mutateWithFx })\n\n const reset = createEvent<void>()\n\n const resetFx = attach({\n source: $observer,\n effect: (observer) => {\n if (!observer) return\n observer.reset()\n },\n })\n\n sample({ clock: reset, target: resetFx })\n\n return {\n $data,\n $error,\n $status,\n $variables,\n $isPaused,\n $isPending,\n $isSuccess,\n $isError,\n $isIdle,\n $observer,\n $queryClient: $effectiveClient,\n mutate,\n mutateWith,\n reset,\n start,\n unmounted,\n finished: {\n success: finishedSuccess,\n failure: finishedFailure,\n },\n }\n}\n\nfunction parseMutationArgs<TData, TError, TVariables, TOnMutateResult>(\n arg1:\n | QueryClient\n | CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n arg2?: CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n): [\n QueryClient | null,\n CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n] {\n if (arg2 !== undefined) {\n return [arg1 as QueryClient, arg2]\n }\n return [\n null,\n arg1 as CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ]\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
2
|
+
import { CreateMutationOptions, MutationResult } from './types.cjs';
|
|
3
|
+
import 'effector';
|
|
4
|
+
|
|
5
|
+
declare function createMutation<TData = unknown, TError = Error, TVariables = void, TOnMutateResult = unknown>(arg1: QueryClient | CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>, arg2?: CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>): MutationResult<TData, TError, TVariables>;
|
|
6
|
+
|
|
7
|
+
export { createMutation };
|