1 | System.register(['react-query', 'jotai'], (function (exports) {
|
2 | 'use strict';
|
3 | var QueryClient, QueryObserver, InfiniteQueryObserver, isCancelledError, atom;
|
4 | return {
|
5 | setters: [function (module) {
|
6 | QueryClient = module.QueryClient;
|
7 | QueryObserver = module.QueryObserver;
|
8 | InfiniteQueryObserver = module.InfiniteQueryObserver;
|
9 | isCancelledError = module.isCancelledError;
|
10 | }, function (module) {
|
11 | atom = module.atom;
|
12 | }],
|
13 | execute: (function () {
|
14 |
|
15 | exports({
|
16 | atomWithInfiniteQuery: atomWithInfiniteQuery,
|
17 | atomWithQuery: atomWithQuery
|
18 | });
|
19 |
|
20 | const queryClientAtom = exports('queryClientAtom', atom(new QueryClient()));
|
21 |
|
22 | function atomWithQuery(createQuery, getQueryClient = (get) => get(queryClientAtom)) {
|
23 | const queryDataAtom = atom((get) => {
|
24 | const queryClient = getQueryClient(get);
|
25 | const options = typeof createQuery === "function" ? createQuery(get) : createQuery;
|
26 | let settlePromise = null;
|
27 | const getInitialData = () => {
|
28 | let data = queryClient.getQueryData(options.queryKey);
|
29 | if (data === void 0 && options.initialData) {
|
30 | data = typeof options.initialData === "function" ? options.initialData() : options.initialData;
|
31 | }
|
32 | return data;
|
33 | };
|
34 | const initialData = getInitialData();
|
35 | const dataAtom = atom(initialData === void 0 && options.enabled !== false ? new Promise((resolve, reject) => {
|
36 | settlePromise = (data, err) => {
|
37 | if (err) {
|
38 | reject(err);
|
39 | } else {
|
40 | resolve(data);
|
41 | }
|
42 | };
|
43 | }) : initialData);
|
44 | let setData = () => {
|
45 | throw new Error("atomWithQuery: setting data without mount");
|
46 | };
|
47 | const listener = (result) => {
|
48 | if (result.error) {
|
49 | if (settlePromise) {
|
50 | settlePromise(void 0, result.error);
|
51 | settlePromise = null;
|
52 | } else {
|
53 | setData(Promise.reject(result.error));
|
54 | }
|
55 | return;
|
56 | }
|
57 | if (result.data === void 0) {
|
58 | return;
|
59 | }
|
60 | if (settlePromise) {
|
61 | settlePromise(result.data);
|
62 | settlePromise = null;
|
63 | } else {
|
64 | setData(result.data);
|
65 | }
|
66 | };
|
67 | const defaultedOptions = queryClient.defaultQueryObserverOptions(options);
|
68 | if (initialData === void 0 && options.enabled !== false) {
|
69 | if (typeof defaultedOptions.staleTime !== "number") {
|
70 | defaultedOptions.staleTime = 1e3;
|
71 | }
|
72 | }
|
73 | const observer = new QueryObserver(queryClient, defaultedOptions);
|
74 | if (initialData === void 0 && options.enabled !== false) {
|
75 | observer.fetchOptimistic(defaultedOptions).then(listener).catch((error) => listener({ error }));
|
76 | }
|
77 | dataAtom.onMount = (update) => {
|
78 | setData = update;
|
79 | if (options.enabled !== false) {
|
80 | return observer.subscribe(listener);
|
81 | }
|
82 | };
|
83 | return { dataAtom, observer };
|
84 | }, (get, set, action) => {
|
85 | switch (action.type) {
|
86 | case "refetch": {
|
87 | const { dataAtom, observer } = get(queryDataAtom);
|
88 | set(dataAtom, new Promise(() => {
|
89 | }));
|
90 | const p = Promise.resolve().then(() => observer.refetch({ cancelRefetch: true })).then(() => {
|
91 | });
|
92 | return p;
|
93 | }
|
94 | default:
|
95 | throw new Error("no action");
|
96 | }
|
97 | });
|
98 | const queryAtom = atom((get) => {
|
99 | const { dataAtom } = get(queryDataAtom);
|
100 | return get(dataAtom);
|
101 | }, (_get, set, action) => set(queryDataAtom, action));
|
102 | return queryAtom;
|
103 | }
|
104 |
|
105 | var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
106 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
107 | var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
108 | var __objRest = (source, exclude) => {
|
109 | var target = {};
|
110 | for (var prop in source)
|
111 | if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
112 | target[prop] = source[prop];
|
113 | if (source != null && __getOwnPropSymbols)
|
114 | for (var prop of __getOwnPropSymbols(source)) {
|
115 | if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
116 | target[prop] = source[prop];
|
117 | }
|
118 | return target;
|
119 | };
|
120 | function atomWithInfiniteQuery(createQuery, getQueryClient = (get) => get(queryClientAtom)) {
|
121 | const queryDataAtom = atom((get) => {
|
122 | const queryClient = getQueryClient(get);
|
123 | const options = typeof createQuery === "function" ? createQuery(get) : createQuery;
|
124 | let settlePromise = null;
|
125 | const getInitialData = () => {
|
126 | let data = queryClient.getQueryData(options.queryKey);
|
127 | if (data === void 0 && options.initialData) {
|
128 | data = typeof options.initialData === "function" ? options.initialData() : options.initialData;
|
129 | }
|
130 | return data;
|
131 | };
|
132 | const initialData = getInitialData();
|
133 | const dataAtom = atom(initialData === void 0 && options.enabled !== false ? new Promise((resolve, reject) => {
|
134 | settlePromise = (data, err) => {
|
135 | if (err) {
|
136 | reject(err);
|
137 | } else {
|
138 | resolve(data);
|
139 | }
|
140 | };
|
141 | }) : initialData);
|
142 | let setData = () => {
|
143 | throw new Error("atomWithInfiniteQuery: setting data without mount");
|
144 | };
|
145 | const listener = (result) => {
|
146 | if (result.error && !isCancelledError(result.error)) {
|
147 | if (settlePromise) {
|
148 | settlePromise(void 0, result.error);
|
149 | settlePromise = null;
|
150 | } else {
|
151 | setData(Promise.reject(result.error));
|
152 | }
|
153 | return;
|
154 | }
|
155 | if (result.data === void 0) {
|
156 | return;
|
157 | }
|
158 | if (settlePromise) {
|
159 | settlePromise(result.data);
|
160 | settlePromise = null;
|
161 | } else {
|
162 | setData(result.data);
|
163 | }
|
164 | };
|
165 | const defaultedOptions = queryClient.defaultQueryObserverOptions(options);
|
166 | if (initialData === void 0 && options.enabled !== false) {
|
167 | if (typeof defaultedOptions.staleTime !== "number") {
|
168 | defaultedOptions.staleTime = 1e3;
|
169 | }
|
170 | }
|
171 | const observer = new InfiniteQueryObserver(queryClient, defaultedOptions);
|
172 | if (initialData === void 0 && options.enabled !== false) {
|
173 | observer.fetchOptimistic(defaultedOptions).then(listener).catch((error) => listener({ error }));
|
174 | }
|
175 | dataAtom.onMount = (update) => {
|
176 | setData = update;
|
177 | if (options.enabled !== false) {
|
178 | return observer.subscribe(listener);
|
179 | }
|
180 | };
|
181 | return { dataAtom, observer, options };
|
182 | }, (get, _set, action) => {
|
183 | const { observer } = get(queryDataAtom);
|
184 | switch (action.type) {
|
185 | case "refetch": {
|
186 | const _a = action, options = __objRest(_a, ["type"]);
|
187 | void observer.refetch(options);
|
188 | break;
|
189 | }
|
190 | case "fetchPreviousPage": {
|
191 | void observer.fetchPreviousPage();
|
192 | break;
|
193 | }
|
194 | case "fetchNextPage": {
|
195 | void observer.fetchNextPage();
|
196 | break;
|
197 | }
|
198 | }
|
199 | });
|
200 | const queryAtom = atom((get) => {
|
201 | const { dataAtom } = get(queryDataAtom);
|
202 | return get(dataAtom);
|
203 | }, (_get, set, action) => set(queryDataAtom, action));
|
204 | return queryAtom;
|
205 | }
|
206 |
|
207 | })
|
208 | };
|
209 | }));
|