UNPKG

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