1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@urql/core'), require('jotai'), require('wonka')) :
|
3 | typeof define === 'function' && define.amd ? define(['exports', '@urql/core', 'jotai', 'wonka'], factory) :
|
4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.jotaiUrql = {}, global.core, global.jotai, global.wonka));
|
5 | })(this, (function (exports, core, jotai, wonka) { 'use strict';
|
6 |
|
7 | var DEFAULT_URL = function () {
|
8 | try {
|
9 | return process.env.JOTAI_URQL_DEFAULT_URL;
|
10 | } catch (_unused) {
|
11 | return undefined;
|
12 | }
|
13 | }() || '/graphql';
|
14 |
|
15 | var clientAtom = jotai.atom(core.createClient({
|
16 | url: DEFAULT_URL
|
17 | }));
|
18 |
|
19 | function _extends() {
|
20 | _extends = Object.assign ? Object.assign.bind() : function (target) {
|
21 | for (var i = 1; i < arguments.length; i++) {
|
22 | var source = arguments[i];
|
23 |
|
24 | for (var key in source) {
|
25 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
26 | target[key] = source[key];
|
27 | }
|
28 | }
|
29 | }
|
30 |
|
31 | return target;
|
32 | };
|
33 | return _extends.apply(this, arguments);
|
34 | }
|
35 |
|
36 | var isOperationResultWithData$1 = function isOperationResultWithData(result) {
|
37 | return 'data' in result && !result.error;
|
38 | };
|
39 |
|
40 | function atomWithQuery(createQueryArgs, getClient) {
|
41 | if (getClient === void 0) {
|
42 | getClient = function getClient(get) {
|
43 | return get(clientAtom);
|
44 | };
|
45 | }
|
46 |
|
47 | var queryResultAtom = jotai.atom(function (get) {
|
48 | var args = createQueryArgs(get);
|
49 |
|
50 | if (args.pause) {
|
51 | return null;
|
52 | }
|
53 |
|
54 | var client = getClient(get);
|
55 | var resolve = null;
|
56 | var baseResultAtom = jotai.atom(new Promise(function (r) {
|
57 | resolve = r;
|
58 | }));
|
59 |
|
60 | var setResult = function setResult() {
|
61 | throw new Error('setting result without mount');
|
62 | };
|
63 |
|
64 | var listener = function listener(result) {
|
65 | if (result instanceof Promise) {
|
66 | setResult(result);
|
67 | return;
|
68 | }
|
69 |
|
70 | if (!isOperationResultWithData$1(result)) {
|
71 | throw new Error('result does not have data');
|
72 | }
|
73 |
|
74 | if (resolve) {
|
75 | resolve(result);
|
76 | resolve = null;
|
77 | } else {
|
78 | setResult(result);
|
79 | }
|
80 | };
|
81 |
|
82 | client.query(args.query, args.variables, _extends({}, args.requestPolicy && {
|
83 | requestPolicy: args.requestPolicy
|
84 | }, args.context)).toPromise().then(listener).catch(function () {});
|
85 |
|
86 | baseResultAtom.onMount = function (update) {
|
87 | setResult = update;
|
88 | };
|
89 |
|
90 | var subscriptionAtom = jotai.atom(null);
|
91 | var resultAtom = jotai.atom(function (get) {
|
92 | return get(baseResultAtom);
|
93 | }, function (get, set, callback) {
|
94 | var subscription = wonka.pipe(client.query(args.query, args.variables, _extends({}, args.requestPolicy && {
|
95 | requestPolicy: args.requestPolicy
|
96 | }, args.context)), wonka.subscribe(listener));
|
97 | set(subscriptionAtom, subscription);
|
98 | callback(function () {
|
99 | var _get;
|
100 |
|
101 | return (_get = get(subscriptionAtom)) == null ? void 0 : _get.unsubscribe();
|
102 | });
|
103 | });
|
104 |
|
105 | resultAtom.onMount = function (init) {
|
106 | var cleanup;
|
107 | init(function (c) {
|
108 | cleanup = c;
|
109 | });
|
110 | return cleanup;
|
111 | };
|
112 |
|
113 | return {
|
114 | args: args,
|
115 | client: client,
|
116 | resultAtom: resultAtom,
|
117 | subscriptionAtom: subscriptionAtom,
|
118 | listener: listener
|
119 | };
|
120 | });
|
121 | var queryAtom = jotai.atom(function (get) {
|
122 | var queryResult = get(queryResultAtom);
|
123 |
|
124 | if (!queryResult) {
|
125 | return null;
|
126 | }
|
127 |
|
128 | var resultAtom = queryResult.resultAtom;
|
129 | return get(resultAtom);
|
130 | }, function (get, set, action) {
|
131 | switch (action.type) {
|
132 | case 'reexecute':
|
133 | {
|
134 | var queryResult = get(queryResultAtom);
|
135 |
|
136 | if (!queryResult) {
|
137 | throw new Error('query is paused');
|
138 | }
|
139 |
|
140 | var args = queryResult.args,
|
141 | client = queryResult.client,
|
142 | subscriptionAtom = queryResult.subscriptionAtom,
|
143 | listener = queryResult.listener;
|
144 | listener(new Promise(function () {}));
|
145 | var newSubscription = wonka.pipe(client.query(args.query, args.variables, _extends({}, args.requestPolicy && {
|
146 | requestPolicy: args.requestPolicy
|
147 | }, args.context, action.opts)), wonka.subscribe(listener));
|
148 | var oldSubscription = get(subscriptionAtom);
|
149 | oldSubscription == null ? void 0 : oldSubscription.unsubscribe();
|
150 | set(subscriptionAtom, newSubscription);
|
151 | }
|
152 | }
|
153 | });
|
154 | return queryAtom;
|
155 | }
|
156 |
|
157 | function atomWithMutation(createQuery, getClient) {
|
158 | if (getClient === void 0) {
|
159 | getClient = function getClient(get) {
|
160 | return get(clientAtom);
|
161 | };
|
162 | }
|
163 |
|
164 | var operationResultAtom = jotai.atom(new Promise(function () {}));
|
165 | var queryResultAtom = jotai.atom(function (get) {
|
166 | return get(operationResultAtom);
|
167 | }, function (get, set, action) {
|
168 | set(operationResultAtom, new Promise(function () {}));
|
169 | var client = getClient(get);
|
170 | var query = createQuery(get);
|
171 | client.mutation(query, action.variables, action.context).toPromise().then(function (result) {
|
172 | set(operationResultAtom, result);
|
173 | action.callback == null ? void 0 : action.callback(result);
|
174 | }).catch(function () {});
|
175 | });
|
176 | return queryResultAtom;
|
177 | }
|
178 |
|
179 | var isOperationResultWithData = function isOperationResultWithData(result) {
|
180 | return 'data' in result;
|
181 | };
|
182 |
|
183 | function atomWithSubscription(createSubscriptionArgs, getClient) {
|
184 | if (getClient === void 0) {
|
185 | getClient = function getClient(get) {
|
186 | return get(clientAtom);
|
187 | };
|
188 | }
|
189 |
|
190 | var queryResultAtom = jotai.atom(function (get) {
|
191 | var args = createSubscriptionArgs(get);
|
192 |
|
193 | if (args.pause) {
|
194 | return {
|
195 | args: args
|
196 | };
|
197 | }
|
198 |
|
199 | var client = getClient(get);
|
200 | var resolve = null;
|
201 | var resultAtom = jotai.atom(new Promise(function (r) {
|
202 | resolve = r;
|
203 | }));
|
204 |
|
205 | var setResult = function setResult() {
|
206 | throw new Error('setting result without mount');
|
207 | };
|
208 |
|
209 | var isMounted = false;
|
210 |
|
211 | var listener = function listener(result) {
|
212 | if (!isOperationResultWithData(result)) {
|
213 | throw new Error('result does not have data');
|
214 | }
|
215 |
|
216 | if (resolve) {
|
217 | if (!isMounted) {
|
218 | var _subscription;
|
219 |
|
220 | (_subscription = subscription) == null ? void 0 : _subscription.unsubscribe();
|
221 | subscription = null;
|
222 | }
|
223 |
|
224 | resolve(result);
|
225 | resolve = null;
|
226 | } else {
|
227 | setResult(result);
|
228 | }
|
229 | };
|
230 |
|
231 | var subscription = wonka.pipe(client.subscription(args.query, args.variables, args.context), wonka.subscribe(listener));
|
232 |
|
233 | resultAtom.onMount = function (update) {
|
234 | setResult = update;
|
235 | isMounted = true;
|
236 |
|
237 | if (!subscription) {
|
238 | subscription = wonka.pipe(client.subscription(args.query, args.variables, args.context), wonka.subscribe(listener));
|
239 | }
|
240 |
|
241 | return function () {
|
242 | var _subscription2;
|
243 |
|
244 | return (_subscription2 = subscription) == null ? void 0 : _subscription2.unsubscribe();
|
245 | };
|
246 | };
|
247 |
|
248 | return {
|
249 | resultAtom: resultAtom,
|
250 | args: args
|
251 | };
|
252 | });
|
253 | var queryAtom = jotai.atom(function (get) {
|
254 | var _get = get(queryResultAtom),
|
255 | resultAtom = _get.resultAtom;
|
256 |
|
257 | return resultAtom ? get(resultAtom) : null;
|
258 | });
|
259 | return queryAtom;
|
260 | }
|
261 |
|
262 | exports.atomWithMutation = atomWithMutation;
|
263 | exports.atomWithQuery = atomWithQuery;
|
264 | exports.atomWithSubscription = atomWithSubscription;
|
265 | exports.clientAtom = clientAtom;
|
266 |
|
267 | Object.defineProperty(exports, '__esModule', { value: true });
|
268 |
|
269 | }));
|