UNPKG

18.8 kBJavaScriptView Raw
1'use strict';
2
3var jotaiUrql = require('jotai-urql');
4var jotai = require('jotai');
5
6function _regeneratorRuntime() {
7 _regeneratorRuntime = function () {
8 return exports;
9 };
10 var exports = {},
11 Op = Object.prototype,
12 hasOwn = Op.hasOwnProperty,
13 defineProperty = Object.defineProperty || function (obj, key, desc) {
14 obj[key] = desc.value;
15 },
16 $Symbol = "function" == typeof Symbol ? Symbol : {},
17 iteratorSymbol = $Symbol.iterator || "@@iterator",
18 asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
19 toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
20 function define(obj, key, value) {
21 return Object.defineProperty(obj, key, {
22 value: value,
23 enumerable: !0,
24 configurable: !0,
25 writable: !0
26 }), obj[key];
27 }
28 try {
29 define({}, "");
30 } catch (err) {
31 define = function (obj, key, value) {
32 return obj[key] = value;
33 };
34 }
35 function wrap(innerFn, outerFn, self, tryLocsList) {
36 var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
37 generator = Object.create(protoGenerator.prototype),
38 context = new Context(tryLocsList || []);
39 return defineProperty(generator, "_invoke", {
40 value: makeInvokeMethod(innerFn, self, context)
41 }), generator;
42 }
43 function tryCatch(fn, obj, arg) {
44 try {
45 return {
46 type: "normal",
47 arg: fn.call(obj, arg)
48 };
49 } catch (err) {
50 return {
51 type: "throw",
52 arg: err
53 };
54 }
55 }
56 exports.wrap = wrap;
57 var ContinueSentinel = {};
58 function Generator() {}
59 function GeneratorFunction() {}
60 function GeneratorFunctionPrototype() {}
61 var IteratorPrototype = {};
62 define(IteratorPrototype, iteratorSymbol, function () {
63 return this;
64 });
65 var getProto = Object.getPrototypeOf,
66 NativeIteratorPrototype = getProto && getProto(getProto(values([])));
67 NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
68 var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
69 function defineIteratorMethods(prototype) {
70 ["next", "throw", "return"].forEach(function (method) {
71 define(prototype, method, function (arg) {
72 return this._invoke(method, arg);
73 });
74 });
75 }
76 function AsyncIterator(generator, PromiseImpl) {
77 function invoke(method, arg, resolve, reject) {
78 var record = tryCatch(generator[method], generator, arg);
79 if ("throw" !== record.type) {
80 var result = record.arg,
81 value = result.value;
82 return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
83 invoke("next", value, resolve, reject);
84 }, function (err) {
85 invoke("throw", err, resolve, reject);
86 }) : PromiseImpl.resolve(value).then(function (unwrapped) {
87 result.value = unwrapped, resolve(result);
88 }, function (error) {
89 return invoke("throw", error, resolve, reject);
90 });
91 }
92 reject(record.arg);
93 }
94 var previousPromise;
95 defineProperty(this, "_invoke", {
96 value: function (method, arg) {
97 function callInvokeWithMethodAndArg() {
98 return new PromiseImpl(function (resolve, reject) {
99 invoke(method, arg, resolve, reject);
100 });
101 }
102 return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
103 }
104 });
105 }
106 function makeInvokeMethod(innerFn, self, context) {
107 var state = "suspendedStart";
108 return function (method, arg) {
109 if ("executing" === state) throw new Error("Generator is already running");
110 if ("completed" === state) {
111 if ("throw" === method) throw arg;
112 return doneResult();
113 }
114 for (context.method = method, context.arg = arg;;) {
115 var delegate = context.delegate;
116 if (delegate) {
117 var delegateResult = maybeInvokeDelegate(delegate, context);
118 if (delegateResult) {
119 if (delegateResult === ContinueSentinel) continue;
120 return delegateResult;
121 }
122 }
123 if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
124 if ("suspendedStart" === state) throw state = "completed", context.arg;
125 context.dispatchException(context.arg);
126 } else "return" === context.method && context.abrupt("return", context.arg);
127 state = "executing";
128 var record = tryCatch(innerFn, self, context);
129 if ("normal" === record.type) {
130 if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
131 return {
132 value: record.arg,
133 done: context.done
134 };
135 }
136 "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
137 }
138 };
139 }
140 function maybeInvokeDelegate(delegate, context) {
141 var method = delegate.iterator[context.method];
142 if (undefined === method) {
143 if (context.delegate = null, "throw" === context.method) {
144 if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
145 context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
146 }
147 return ContinueSentinel;
148 }
149 var record = tryCatch(method, delegate.iterator, context.arg);
150 if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
151 var info = record.arg;
152 return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
153 }
154 function pushTryEntry(locs) {
155 var entry = {
156 tryLoc: locs[0]
157 };
158 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
159 }
160 function resetTryEntry(entry) {
161 var record = entry.completion || {};
162 record.type = "normal", delete record.arg, entry.completion = record;
163 }
164 function Context(tryLocsList) {
165 this.tryEntries = [{
166 tryLoc: "root"
167 }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
168 }
169 function values(iterable) {
170 if (iterable) {
171 var iteratorMethod = iterable[iteratorSymbol];
172 if (iteratorMethod) return iteratorMethod.call(iterable);
173 if ("function" == typeof iterable.next) return iterable;
174 if (!isNaN(iterable.length)) {
175 var i = -1,
176 next = function next() {
177 for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
178 return next.value = undefined, next.done = !0, next;
179 };
180 return next.next = next;
181 }
182 }
183 return {
184 next: doneResult
185 };
186 }
187 function doneResult() {
188 return {
189 value: undefined,
190 done: !0
191 };
192 }
193 return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", {
194 value: GeneratorFunctionPrototype,
195 configurable: !0
196 }), defineProperty(GeneratorFunctionPrototype, "constructor", {
197 value: GeneratorFunction,
198 configurable: !0
199 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
200 var ctor = "function" == typeof genFun && genFun.constructor;
201 return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
202 }, exports.mark = function (genFun) {
203 return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
204 }, exports.awrap = function (arg) {
205 return {
206 __await: arg
207 };
208 }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
209 return this;
210 }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
211 void 0 === PromiseImpl && (PromiseImpl = Promise);
212 var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
213 return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
214 return result.done ? result.value : iter.next();
215 });
216 }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
217 return this;
218 }), define(Gp, "toString", function () {
219 return "[object Generator]";
220 }), exports.keys = function (val) {
221 var object = Object(val),
222 keys = [];
223 for (var key in object) keys.push(key);
224 return keys.reverse(), function next() {
225 for (; keys.length;) {
226 var key = keys.pop();
227 if (key in object) return next.value = key, next.done = !1, next;
228 }
229 return next.done = !0, next;
230 };
231 }, exports.values = values, Context.prototype = {
232 constructor: Context,
233 reset: function (skipTempReset) {
234 if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
235 },
236 stop: function () {
237 this.done = !0;
238 var rootRecord = this.tryEntries[0].completion;
239 if ("throw" === rootRecord.type) throw rootRecord.arg;
240 return this.rval;
241 },
242 dispatchException: function (exception) {
243 if (this.done) throw exception;
244 var context = this;
245 function handle(loc, caught) {
246 return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
247 }
248 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
249 var entry = this.tryEntries[i],
250 record = entry.completion;
251 if ("root" === entry.tryLoc) return handle("end");
252 if (entry.tryLoc <= this.prev) {
253 var hasCatch = hasOwn.call(entry, "catchLoc"),
254 hasFinally = hasOwn.call(entry, "finallyLoc");
255 if (hasCatch && hasFinally) {
256 if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
257 if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
258 } else if (hasCatch) {
259 if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
260 } else {
261 if (!hasFinally) throw new Error("try statement without catch or finally");
262 if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
263 }
264 }
265 }
266 },
267 abrupt: function (type, arg) {
268 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
269 var entry = this.tryEntries[i];
270 if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
271 var finallyEntry = entry;
272 break;
273 }
274 }
275 finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
276 var record = finallyEntry ? finallyEntry.completion : {};
277 return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
278 },
279 complete: function (record, afterLoc) {
280 if ("throw" === record.type) throw record.arg;
281 return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
282 },
283 finish: function (finallyLoc) {
284 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
285 var entry = this.tryEntries[i];
286 if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
287 }
288 },
289 catch: function (tryLoc) {
290 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
291 var entry = this.tryEntries[i];
292 if (entry.tryLoc === tryLoc) {
293 var record = entry.completion;
294 if ("throw" === record.type) {
295 var thrown = record.arg;
296 resetTryEntry(entry);
297 }
298 return thrown;
299 }
300 }
301 throw new Error("illegal catch attempt");
302 },
303 delegateYield: function (iterable, resultName, nextLoc) {
304 return this.delegate = {
305 iterator: values(iterable),
306 resultName: resultName,
307 nextLoc: nextLoc
308 }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
309 }
310 }, exports;
311}
312function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
313 try {
314 var info = gen[key](arg);
315 var value = info.value;
316 } catch (error) {
317 reject(error);
318 return;
319 }
320 if (info.done) {
321 resolve(value);
322 } else {
323 Promise.resolve(value).then(_next, _throw);
324 }
325}
326function _asyncToGenerator(fn) {
327 return function () {
328 var self = this,
329 args = arguments;
330 return new Promise(function (resolve, reject) {
331 var gen = fn.apply(self, args);
332 function _next(value) {
333 asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
334 }
335 function _throw(err) {
336 asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
337 }
338 _next(undefined);
339 });
340 };
341}
342function _extends() {
343 _extends = Object.assign ? Object.assign.bind() : function (target) {
344 for (var i = 1; i < arguments.length; i++) {
345 var source = arguments[i];
346 for (var key in source) {
347 if (Object.prototype.hasOwnProperty.call(source, key)) {
348 target[key] = source[key];
349 }
350 }
351 }
352 return target;
353 };
354 return _extends.apply(this, arguments);
355}
356
357function atomWithQuery(createQueryArgs, getClient) {
358 if (getClient === void 0) {
359 getClient = function getClient(get) {
360 return get(jotaiUrql.clientAtom);
361 };
362 }
363 var getArgs = function getArgs(get) {
364 var queryArgs = createQueryArgs(get);
365 return [queryArgs.query, queryArgs.variables, _extends({}, queryArgs.requestPolicy && {
366 requestPolicy: queryArgs.requestPolicy
367 }, queryArgs.context)];
368 };
369 var _atomsWithQuery = jotaiUrql.atomsWithQuery(getArgs, getClient),
370 dataAtom = _atomsWithQuery[0],
371 statusAtom = _atomsWithQuery[1];
372 return jotai.atom(function (get) {
373 var queryArgs = createQueryArgs(get);
374 if (queryArgs.pause) {
375 return null;
376 }
377 var status = get(statusAtom);
378 if (status.error) {
379 throw status.error;
380 }
381 if ('data' in status) {
382 return status;
383 }
384 get(dataAtom);
385 return status;
386 }, function (_get, set, action) {
387 if (action.type === 'reexecute') {
388 console.warn('DEPRECATED [atomWithQuery] use refetch instead of reexecute');
389 action.type = 'refetch';
390 }
391 if ('opts' in action) {
392 console.warn('DEPRECATED [atomWithQuery] action.opts is no longer used');
393 }
394 switch (action.type) {
395 case 'refetch':
396 {
397 return set(statusAtom, action);
398 }
399 }
400 });
401}
402
403function atomWithMutation(createQuery, getClient) {
404 if (getClient === void 0) {
405 getClient = function getClient(get) {
406 return get(jotaiUrql.clientAtom);
407 };
408 }
409 var _atomsWithMutation = jotaiUrql.atomsWithMutation(getClient),
410 statusAtom = _atomsWithMutation[1];
411 return jotai.atom(function (get) {
412 var status = get(statusAtom);
413 return status;
414 }, function () {
415 var _ref = _asyncToGenerator(_regeneratorRuntime().mark(function _callee(get, set, action) {
416 var args;
417 return _regeneratorRuntime().wrap(function _callee$(_context) {
418 while (1) {
419 switch (_context.prev = _context.next) {
420 case 0:
421 args = [createQuery(get), action.variables, action.context || {}];
422 _context.next = 3;
423 return set(statusAtom, args);
424 case 3:
425 return _context.abrupt("return", Promise.resolve(get(statusAtom, {
426 unstable_promise: true
427 })).then(function (status) {
428 action.callback == null ? void 0 : action.callback(status);
429 if (status.error) {
430 throw status.error;
431 }
432 }));
433 case 4:
434 case "end":
435 return _context.stop();
436 }
437 }
438 }, _callee);
439 }));
440 return function (_x, _x2, _x3) {
441 return _ref.apply(this, arguments);
442 };
443 }());
444}
445
446function atomWithSubscription(createSubscriptionArgs, getClient) {
447 if (getClient === void 0) {
448 getClient = function getClient(get) {
449 return get(jotaiUrql.clientAtom);
450 };
451 }
452 var getArgs = function getArgs(get) {
453 var subscriptionArgs = createSubscriptionArgs(get);
454 return [subscriptionArgs.query, subscriptionArgs.variables, subscriptionArgs.context || {}];
455 };
456 var _atomsWithSubscriptio = jotaiUrql.atomsWithSubscription(getArgs, getClient),
457 dataAtom = _atomsWithSubscriptio[0],
458 statusAtom = _atomsWithSubscriptio[1];
459 return jotai.atom(function (get) {
460 var subscriptionArgs = createSubscriptionArgs(get);
461 if (subscriptionArgs.pause) {
462 return null;
463 }
464 var status = get(statusAtom);
465 if (status.error) {
466 throw status.error;
467 }
468 if ('data' in status) {
469 return status;
470 }
471 get(dataAtom);
472 return status;
473 }, function (_get, set, action) {
474 switch (action.type) {
475 case 'refetch':
476 {
477 return set(statusAtom, action);
478 }
479 }
480 });
481}
482
483Object.defineProperty(exports, 'clientAtom', {
484 enumerable: true,
485 get: function () { return jotaiUrql.clientAtom; }
486});
487exports.atomWithMutation = atomWithMutation;
488exports.atomWithQuery = atomWithQuery;
489exports.atomWithSubscription = atomWithSubscription;