UNPKG

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