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 || 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 |
|
34 | return _extends.apply(this, arguments);
|
35 | }
|
36 |
|
37 | var isOperationResultWithData$1 = function isOperationResultWithData(result) {
|
38 | return 'data' in result;
|
39 | };
|
40 |
|
41 | function atomWithQuery(createQueryArgs, getClient) {
|
42 | if (getClient === void 0) {
|
43 | getClient = function getClient(get) {
|
44 | return get(clientAtom);
|
45 | };
|
46 | }
|
47 |
|
48 | var refAtom = jotai.atom(function () {
|
49 | return {};
|
50 | });
|
51 |
|
52 | var createResultAtom = function createResultAtom(ref, client, args, opts) {
|
53 | var resolve = null;
|
54 | var resultAtom = jotai.atom(new Promise(function (r) {
|
55 | resolve = r;
|
56 | }));
|
57 | ref.resultAtom = resultAtom;
|
58 |
|
59 | var setResult = function setResult() {
|
60 | throw new Error('setting result without mount');
|
61 | };
|
62 |
|
63 | var listener = function listener(result) {
|
64 | if (resultAtom !== ref.resultAtom) {
|
65 | return;
|
66 | }
|
67 |
|
68 | if (!isOperationResultWithData$1(result)) {
|
69 | throw new Error('result does not have data');
|
70 | }
|
71 |
|
72 | if (resolve) {
|
73 | resolve(result);
|
74 | resolve = null;
|
75 | } else {
|
76 | setResult(result);
|
77 | }
|
78 | };
|
79 |
|
80 | client.query(args.query, args.variables, _extends({}, args.requestPolicy && {
|
81 | requestPolicy: args.requestPolicy
|
82 | }, args.context, opts)).toPromise().then(listener).catch(function () {});
|
83 |
|
84 | resultAtom.onMount = function (update) {
|
85 | setResult = update;
|
86 | var subscription = wonka.pipe(client.query(args.query, args.variables, _extends({}, args.requestPolicy && {
|
87 | requestPolicy: args.requestPolicy
|
88 | }, args.context, opts)), wonka.skip(1), wonka.subscribe(listener));
|
89 | return function () {
|
90 | return subscription.unsubscribe();
|
91 | };
|
92 | };
|
93 |
|
94 | return resultAtom;
|
95 | };
|
96 |
|
97 | var queryResultAtom = jotai.atom(function (get) {
|
98 | var args = createQueryArgs(get);
|
99 |
|
100 | if (args.pause) {
|
101 | return null;
|
102 | }
|
103 |
|
104 | var client = getClient(get);
|
105 | var resultAtom = createResultAtom(get(refAtom), client, args);
|
106 | return {
|
107 | resultAtom: resultAtom,
|
108 | client: client,
|
109 | args: args
|
110 | };
|
111 | });
|
112 | var overwrittenResultAtom = jotai.atom(null);
|
113 | var queryAtom = jotai.atom(function (get) {
|
114 | var queryResult = get(queryResultAtom);
|
115 |
|
116 | if (!queryResult) {
|
117 | return null;
|
118 | }
|
119 |
|
120 | var resultAtom = queryResult.resultAtom;
|
121 | var overwrittenResult = get(overwrittenResultAtom);
|
122 |
|
123 | if (overwrittenResult && overwrittenResult.oldResultAtom === resultAtom) {
|
124 | resultAtom = overwrittenResult.newResultAtom;
|
125 | }
|
126 |
|
127 | return get(resultAtom);
|
128 | }, function (get, set, action) {
|
129 | switch (action.type) {
|
130 | case 'reexecute':
|
131 | {
|
132 | var queryResult = get(queryResultAtom);
|
133 |
|
134 | if (!queryResult) {
|
135 | throw new Error('query is paused');
|
136 | }
|
137 |
|
138 | var resultAtom = queryResult.resultAtom,
|
139 | client = queryResult.client,
|
140 | args = queryResult.args;
|
141 | set(overwrittenResultAtom, {
|
142 | oldResultAtom: resultAtom,
|
143 | newResultAtom: createResultAtom(get(refAtom), client, args, action.opts)
|
144 | });
|
145 | }
|
146 | }
|
147 | });
|
148 | return queryAtom;
|
149 | }
|
150 |
|
151 | function atomWithMutation(createQuery, getClient) {
|
152 | if (getClient === void 0) {
|
153 | getClient = function getClient(get) {
|
154 | return get(clientAtom);
|
155 | };
|
156 | }
|
157 |
|
158 | var operationResultAtom = jotai.atom(new Promise(function () {}));
|
159 | var queryResultAtom = jotai.atom(function (get) {
|
160 | return get(operationResultAtom);
|
161 | }, function (get, set, action) {
|
162 | set(operationResultAtom, new Promise(function () {}));
|
163 | var client = getClient(get);
|
164 | var query = createQuery(get);
|
165 | client.mutation(query, action.variables, action.context).toPromise().then(function (result) {
|
166 | set(operationResultAtom, result);
|
167 | action.callback == null ? void 0 : action.callback(result);
|
168 | }).catch(function () {});
|
169 | });
|
170 | return queryResultAtom;
|
171 | }
|
172 |
|
173 | var isOperationResultWithData = function isOperationResultWithData(result) {
|
174 | return 'data' in result;
|
175 | };
|
176 |
|
177 | function atomWithSubscription(createSubscriptionArgs, getClient) {
|
178 | if (getClient === void 0) {
|
179 | getClient = function getClient(get) {
|
180 | return get(clientAtom);
|
181 | };
|
182 | }
|
183 |
|
184 | var queryResultAtom = jotai.atom(function (get) {
|
185 | var args = createSubscriptionArgs(get);
|
186 |
|
187 | if (args.pause) {
|
188 | return {
|
189 | args: args
|
190 | };
|
191 | }
|
192 |
|
193 | var client = getClient(get);
|
194 | var resolve = null;
|
195 | var resultAtom = jotai.atom(new Promise(function (r) {
|
196 | resolve = r;
|
197 | }));
|
198 |
|
199 | var setResult = function setResult() {
|
200 | throw new Error('setting result without mount');
|
201 | };
|
202 |
|
203 | var listener = function listener(result) {
|
204 | if (!isOperationResultWithData(result)) {
|
205 | throw new Error('result does not have data');
|
206 | }
|
207 |
|
208 | if (resolve) {
|
209 | resolve(result);
|
210 | resolve = null;
|
211 | } else {
|
212 | setResult(result);
|
213 | }
|
214 | };
|
215 |
|
216 | var subscriptionInRender = wonka.pipe(client.subscription(args.query, args.variables, args.context), wonka.subscribe(listener));
|
217 | var timer = setTimeout(function () {
|
218 | timer = null;
|
219 | subscriptionInRender.unsubscribe();
|
220 | }, 1000);
|
221 |
|
222 | resultAtom.onMount = function (update) {
|
223 | setResult = update;
|
224 | var subscription;
|
225 |
|
226 | if (timer) {
|
227 | clearTimeout(timer);
|
228 | subscription = subscriptionInRender;
|
229 | } else {
|
230 | subscription = wonka.pipe(client.subscription(args.query, args.variables, args.context), wonka.subscribe(listener));
|
231 | }
|
232 |
|
233 | return function () {
|
234 | return subscription.unsubscribe();
|
235 | };
|
236 | };
|
237 |
|
238 | return {
|
239 | resultAtom: resultAtom,
|
240 | args: args
|
241 | };
|
242 | });
|
243 | var queryAtom = jotai.atom(function (get) {
|
244 | var _get = get(queryResultAtom),
|
245 | resultAtom = _get.resultAtom;
|
246 |
|
247 | return resultAtom ? get(resultAtom) : null;
|
248 | });
|
249 | return queryAtom;
|
250 | }
|
251 |
|
252 | exports.atomWithMutation = atomWithMutation;
|
253 | exports.atomWithQuery = atomWithQuery;
|
254 | exports.atomWithSubscription = atomWithSubscription;
|
255 | exports.clientAtom = clientAtom;
|
256 |
|
257 | Object.defineProperty(exports, '__esModule', { value: true });
|
258 |
|
259 | }));
|