UNPKG

6.95 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var queryCore = require('@tanstack/query-core');
6var jotai = require('jotai');
7
8var queryClientAtom = jotai.atom(new queryCore.QueryClient());
9
10function atomWithQuery(createQuery, getQueryClient) {
11 if (getQueryClient === void 0) {
12 getQueryClient = function getQueryClient(get) {
13 return get(queryClientAtom);
14 };
15 }
16
17 var previousDataCache = new WeakMap();
18 var queryDataAtom = jotai.atom(function (get) {
19 var queryClient = getQueryClient(get);
20 var options = typeof createQuery === 'function' ? createQuery(get) : createQuery;
21 var observer = new queryCore.QueryObserver(queryClient, options);
22 var initialResult = observer.getCurrentResult();
23
24 if (initialResult.data === undefined && options.keepPreviousData && previousDataCache.has(queryClient)) {
25 initialResult.data = previousDataCache.get(queryClient);
26 }
27
28 var resolve = null;
29
30 var makePending = function makePending() {
31 return new Promise(function (r) {
32 resolve = r;
33 });
34 };
35
36 var resultAtom = jotai.atom(initialResult.data === undefined && options.enabled !== false ? makePending() : initialResult);
37 var setResult = null;
38
39 var listener = function listener(result) {
40 if (result.isFetching || !result.isError && result.data === undefined) {
41 return;
42 }
43
44 if (!resolve && !setResult) {
45 throw new Error('setting result without mount');
46 }
47
48 if (resolve) {
49 resolve(result);
50 resolve = null;
51 }
52
53 if (setResult) {
54 setResult(result);
55 }
56
57 if (result.data !== undefined) {
58 previousDataCache.set(queryClient, result.data);
59 }
60 };
61
62 var unsubscribe = null;
63 var timer;
64
65 var startQuery = function startQuery() {
66 if (!setResult && unsubscribe) {
67 clearTimeout(timer);
68 unsubscribe();
69 unsubscribe = null;
70 }
71
72 if (options.enabled !== false) {
73 if (setResult) {
74 observer.refetch({
75 cancelRefetch: true
76 }).then(setResult);
77 } else {
78 unsubscribe = observer.subscribe(listener);
79 }
80 }
81
82 if (!setResult) {
83 timer = setTimeout(function () {
84 if (unsubscribe) {
85 unsubscribe();
86 unsubscribe = null;
87 }
88 }, 1000);
89 }
90 };
91
92 startQuery();
93
94 resultAtom.onMount = function (update) {
95 setResult = update;
96
97 if (unsubscribe) {
98 clearTimeout(timer);
99 } else {
100 startQuery();
101 }
102
103 return function () {
104 setResult = null;
105
106 if (unsubscribe) {
107 unsubscribe();
108 }
109 };
110 };
111
112 return {
113 resultAtom: resultAtom,
114 makePending: makePending,
115 startQuery: startQuery
116 };
117 });
118 var queryAtom = jotai.atom(function (get) {
119 var _get = get(queryDataAtom),
120 resultAtom = _get.resultAtom;
121
122 var result = get(resultAtom);
123
124 if (result.isError) {
125 throw result.error;
126 }
127
128 return result.data;
129 }, function (get, set, action) {
130 var _get2 = get(queryDataAtom),
131 resultAtom = _get2.resultAtom,
132 makePending = _get2.makePending,
133 startQuery = _get2.startQuery;
134
135 switch (action.type) {
136 case 'refetch':
137 {
138 set(resultAtom, makePending());
139 startQuery();
140 }
141 }
142 });
143 return queryAtom;
144}
145
146function atomWithInfiniteQuery(createQuery, getQueryClient) {
147 if (getQueryClient === void 0) {
148 getQueryClient = function getQueryClient(get) {
149 return get(queryClientAtom);
150 };
151 }
152
153 var queryDataAtom = jotai.atom(function (get) {
154 var queryClient = getQueryClient(get);
155 var options = typeof createQuery === 'function' ? createQuery(get) : createQuery;
156 var defaultedOptions = queryClient.defaultQueryOptions(options);
157 var observer = new queryCore.InfiniteQueryObserver(queryClient, defaultedOptions);
158 var initialResult = observer.getCurrentResult();
159 var resolve = null;
160 var resultAtom = jotai.atom(initialResult.data === undefined && options.enabled !== false ? new Promise(function (r) {
161 resolve = r;
162 }) : initialResult);
163
164 var setResult = function setResult() {
165 throw new Error('setting result without mount');
166 };
167
168 var state = {
169 isMounted: false,
170 unsubscribe: null
171 };
172
173 var listener = function listener(result) {
174 if (result.isFetching || !result.isError && result.data === undefined || result.isError && queryCore.isCancelledError(result.error)) {
175 return;
176 }
177
178 if (resolve) {
179 setTimeout(function () {
180 if (!state.isMounted) {
181 state.unsubscribe == null ? void 0 : state.unsubscribe();
182 state.unsubscribe = null;
183 }
184 }, 1000);
185 resolve(result);
186 resolve = null;
187 } else {
188 setResult(result);
189 }
190 };
191
192 if (options.enabled !== false) {
193 state.unsubscribe = observer.subscribe(listener);
194 }
195
196 resultAtom.onMount = function (update) {
197 setResult = update;
198 state.isMounted = true;
199
200 if (options.enabled !== false && !state.unsubscribe) {
201 state.unsubscribe = observer.subscribe(listener);
202 listener(observer.getCurrentResult());
203 }
204
205 return function () {
206 return state.unsubscribe == null ? void 0 : state.unsubscribe();
207 };
208 };
209
210 return {
211 options: options,
212 resultAtom: resultAtom,
213 observer: observer,
214 state: state
215 };
216 }, function (get, set, action) {
217 var _get2 = get(queryDataAtom),
218 options = _get2.options,
219 resultAtom = _get2.resultAtom,
220 observer = _get2.observer,
221 state = _get2.state;
222
223 if (options.enabled === false) {
224 return;
225 }
226
227 switch (action.type) {
228 case 'refetch':
229 {
230 set(resultAtom, new Promise(function () {}));
231
232 if (!state.isMounted) {
233 state.unsubscribe == null ? void 0 : state.unsubscribe();
234 state.unsubscribe = null;
235 }
236
237 observer.refetch(action.payload).then(function (result) {
238 set(resultAtom, result);
239 });
240 return;
241 }
242
243 case 'fetchPreviousPage':
244 {
245 observer.fetchPreviousPage();
246 return;
247 }
248
249 case 'fetchNextPage':
250 {
251 observer.fetchNextPage();
252 return;
253 }
254 }
255 });
256 var queryAtom = jotai.atom(function (get) {
257 var _get3 = get(queryDataAtom),
258 resultAtom = _get3.resultAtom;
259
260 var result = get(resultAtom);
261
262 if (result.isError) {
263 throw result.error;
264 }
265
266 return result.data;
267 }, function (_get, set, action) {
268 return set(queryDataAtom, action);
269 });
270 return queryAtom;
271}
272
273exports.atomWithInfiniteQuery = atomWithInfiniteQuery;
274exports.atomWithQuery = atomWithQuery;
275exports.queryClientAtom = queryClientAtom;