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