UNPKG

19 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 methodName = context.method,
142 method = delegate.iterator[methodName];
143 if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel;
144 var record = tryCatch(method, delegate.iterator, context.arg);
145 if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
146 var info = record.arg;
147 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);
148 }
149 function pushTryEntry(locs) {
150 var entry = {
151 tryLoc: locs[0]
152 };
153 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
154 }
155 function resetTryEntry(entry) {
156 var record = entry.completion || {};
157 record.type = "normal", delete record.arg, entry.completion = record;
158 }
159 function Context(tryLocsList) {
160 this.tryEntries = [{
161 tryLoc: "root"
162 }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
163 }
164 function values(iterable) {
165 if (iterable) {
166 var iteratorMethod = iterable[iteratorSymbol];
167 if (iteratorMethod) return iteratorMethod.call(iterable);
168 if ("function" == typeof iterable.next) return iterable;
169 if (!isNaN(iterable.length)) {
170 var i = -1,
171 next = function next() {
172 for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
173 return next.value = undefined, next.done = !0, next;
174 };
175 return next.next = next;
176 }
177 }
178 return {
179 next: doneResult
180 };
181 }
182 function doneResult() {
183 return {
184 value: undefined,
185 done: !0
186 };
187 }
188 return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", {
189 value: GeneratorFunctionPrototype,
190 configurable: !0
191 }), defineProperty(GeneratorFunctionPrototype, "constructor", {
192 value: GeneratorFunction,
193 configurable: !0
194 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
195 var ctor = "function" == typeof genFun && genFun.constructor;
196 return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
197 }, exports.mark = function (genFun) {
198 return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
199 }, exports.awrap = function (arg) {
200 return {
201 __await: arg
202 };
203 }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
204 return this;
205 }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
206 void 0 === PromiseImpl && (PromiseImpl = Promise);
207 var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
208 return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
209 return result.done ? result.value : iter.next();
210 });
211 }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
212 return this;
213 }), define(Gp, "toString", function () {
214 return "[object Generator]";
215 }), exports.keys = function (val) {
216 var object = Object(val),
217 keys = [];
218 for (var key in object) keys.push(key);
219 return keys.reverse(), function next() {
220 for (; keys.length;) {
221 var key = keys.pop();
222 if (key in object) return next.value = key, next.done = !1, next;
223 }
224 return next.done = !0, next;
225 };
226 }, exports.values = values, Context.prototype = {
227 constructor: Context,
228 reset: function (skipTempReset) {
229 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);
230 },
231 stop: function () {
232 this.done = !0;
233 var rootRecord = this.tryEntries[0].completion;
234 if ("throw" === rootRecord.type) throw rootRecord.arg;
235 return this.rval;
236 },
237 dispatchException: function (exception) {
238 if (this.done) throw exception;
239 var context = this;
240 function handle(loc, caught) {
241 return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
242 }
243 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
244 var entry = this.tryEntries[i],
245 record = entry.completion;
246 if ("root" === entry.tryLoc) return handle("end");
247 if (entry.tryLoc <= this.prev) {
248 var hasCatch = hasOwn.call(entry, "catchLoc"),
249 hasFinally = hasOwn.call(entry, "finallyLoc");
250 if (hasCatch && hasFinally) {
251 if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
252 if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
253 } else if (hasCatch) {
254 if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
255 } else {
256 if (!hasFinally) throw new Error("try statement without catch or finally");
257 if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
258 }
259 }
260 }
261 },
262 abrupt: function (type, arg) {
263 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
264 var entry = this.tryEntries[i];
265 if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
266 var finallyEntry = entry;
267 break;
268 }
269 }
270 finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
271 var record = finallyEntry ? finallyEntry.completion : {};
272 return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
273 },
274 complete: function (record, afterLoc) {
275 if ("throw" === record.type) throw record.arg;
276 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;
277 },
278 finish: function (finallyLoc) {
279 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
280 var entry = this.tryEntries[i];
281 if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
282 }
283 },
284 catch: function (tryLoc) {
285 for (var i = this.tryEntries.length - 1; i >= 0; --i) {
286 var entry = this.tryEntries[i];
287 if (entry.tryLoc === tryLoc) {
288 var record = entry.completion;
289 if ("throw" === record.type) {
290 var thrown = record.arg;
291 resetTryEntry(entry);
292 }
293 return thrown;
294 }
295 }
296 throw new Error("illegal catch attempt");
297 },
298 delegateYield: function (iterable, resultName, nextLoc) {
299 return this.delegate = {
300 iterator: values(iterable),
301 resultName: resultName,
302 nextLoc: nextLoc
303 }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
304 }
305 }, exports;
306}
307function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
308 try {
309 var info = gen[key](arg);
310 var value = info.value;
311 } catch (error) {
312 reject(error);
313 return;
314 }
315 if (info.done) {
316 resolve(value);
317 } else {
318 Promise.resolve(value).then(_next, _throw);
319 }
320}
321function _asyncToGenerator(fn) {
322 return function () {
323 var self = this,
324 args = arguments;
325 return new Promise(function (resolve, reject) {
326 var gen = fn.apply(self, args);
327 function _next(value) {
328 asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
329 }
330 function _throw(err) {
331 asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
332 }
333 _next(undefined);
334 });
335 };
336}
337function _extends() {
338 _extends = Object.assign ? Object.assign.bind() : function (target) {
339 for (var i = 1; i < arguments.length; i++) {
340 var source = arguments[i];
341 for (var key in source) {
342 if (Object.prototype.hasOwnProperty.call(source, key)) {
343 target[key] = source[key];
344 }
345 }
346 }
347 return target;
348 };
349 return _extends.apply(this, arguments);
350}
351
352function atomWithQuery(createQueryArgs, getClient) {
353 if (getClient === void 0) {
354 getClient = function getClient(get) {
355 return get(jotaiUrql.clientAtom);
356 };
357 }
358 console.warn('[DEPRECATED] use `jotai-urql` instead.');
359 var getArgs = function getArgs(get) {
360 var queryArgs = createQueryArgs(get);
361 return [queryArgs.query, queryArgs.variables, _extends({}, queryArgs.requestPolicy && {
362 requestPolicy: queryArgs.requestPolicy
363 }, queryArgs.context)];
364 };
365 var _atomsWithQuery = jotaiUrql.atomsWithQuery(getArgs, getClient),
366 dataAtom = _atomsWithQuery[0],
367 statusAtom = _atomsWithQuery[1];
368 return jotai.atom(function (get) {
369 var queryArgs = createQueryArgs(get);
370 if (queryArgs.pause) {
371 return null;
372 }
373 var status = get(statusAtom);
374 if (status.error) {
375 throw status.error;
376 }
377 if ('data' in status) {
378 return status;
379 }
380 get(dataAtom);
381 return status;
382 }, function (_get, set, action) {
383 if (action.type === 'reexecute') {
384 console.warn('DEPRECATED [atomWithQuery] use refetch instead of reexecute');
385 action.type = 'refetch';
386 }
387 if ('opts' in action) {
388 console.warn('DEPRECATED [atomWithQuery] action.opts is no longer used');
389 }
390 switch (action.type) {
391 case 'refetch':
392 {
393 return set(statusAtom, action);
394 }
395 }
396 });
397}
398
399function atomWithMutation(createQuery, getClient) {
400 if (getClient === void 0) {
401 getClient = function getClient(get) {
402 return get(jotaiUrql.clientAtom);
403 };
404 }
405 console.warn('[DEPRECATED] use `jotai-urql` instead.');
406 var _atomsWithMutation = jotaiUrql.atomsWithMutation(getClient),
407 statusAtom = _atomsWithMutation[1];
408 return jotai.atom(function (get) {
409 var status = get(statusAtom);
410 return status;
411 }, function () {
412 var _ref = _asyncToGenerator(_regeneratorRuntime().mark(function _callee(get, set, action) {
413 var args;
414 return _regeneratorRuntime().wrap(function _callee$(_context) {
415 while (1) switch (_context.prev = _context.next) {
416 case 0:
417 args = [createQuery(get), action.variables, action.context || {}];
418 _context.next = 3;
419 return set(statusAtom, args);
420 case 3:
421 return _context.abrupt("return", Promise.resolve(get(statusAtom, {
422 unstable_promise: true
423 })).then(function (status) {
424 action.callback == null ? void 0 : action.callback(status);
425 if (status.error) {
426 throw status.error;
427 }
428 }));
429 case 4:
430 case "end":
431 return _context.stop();
432 }
433 }, _callee);
434 }));
435 return function (_x, _x2, _x3) {
436 return _ref.apply(this, arguments);
437 };
438 }());
439}
440
441function atomWithSubscription(createSubscriptionArgs, getClient) {
442 if (getClient === void 0) {
443 getClient = function getClient(get) {
444 return get(jotaiUrql.clientAtom);
445 };
446 }
447 console.warn('[DEPRECATED] use `jotai-urql` instead.');
448 var getArgs = function getArgs(get) {
449 var subscriptionArgs = createSubscriptionArgs(get);
450 return [subscriptionArgs.query, subscriptionArgs.variables, subscriptionArgs.context || {}];
451 };
452 var _atomsWithSubscriptio = jotaiUrql.atomsWithSubscription(getArgs, getClient),
453 dataAtom = _atomsWithSubscriptio[0],
454 statusAtom = _atomsWithSubscriptio[1];
455 return jotai.atom(function (get) {
456 var subscriptionArgs = createSubscriptionArgs(get);
457 if (subscriptionArgs.pause) {
458 return null;
459 }
460 var status = get(statusAtom);
461 if (status.error) {
462 throw status.error;
463 }
464 if ('data' in status) {
465 return status;
466 }
467 get(dataAtom);
468 return status;
469 }, function (_get, set, action) {
470 switch (action.type) {
471 case 'refetch':
472 {
473 return set(statusAtom, action);
474 }
475 }
476 });
477}
478
479Object.defineProperty(exports, 'clientAtom', {
480 enumerable: true,
481 get: function () { return jotaiUrql.clientAtom; }
482});
483exports.atomWithMutation = atomWithMutation;
484exports.atomWithQuery = atomWithQuery;
485exports.atomWithSubscription = atomWithSubscription;