UNPKG

2.8 kBJavaScriptView Raw
1'use strict';
2
3var jotaiTanstackQuery = require('jotai-tanstack-query');
4var jotai = require('jotai');
5
6function _extends() {
7 _extends = Object.assign ? Object.assign.bind() : function (target) {
8 for (var i = 1; i < arguments.length; i++) {
9 var source = arguments[i];
10 for (var key in source) {
11 if (Object.prototype.hasOwnProperty.call(source, key)) {
12 target[key] = source[key];
13 }
14 }
15 }
16 return target;
17 };
18 return _extends.apply(this, arguments);
19}
20
21function atomWithQuery(createQuery, getQueryClient) {
22 if (getQueryClient === void 0) {
23 getQueryClient = function getQueryClient(get) {
24 return get(jotaiTanstackQuery.queryClientAtom);
25 };
26 }
27 console.warn('[DEPRECATED] use `jotai-tanstack-query` instead.');
28 var getOptions = function getOptions(get) {
29 return _extends({
30 staleTime: 200
31 }, typeof createQuery === 'function' ? createQuery(get) : createQuery);
32 };
33 var _atomsWithQuery = jotaiTanstackQuery.atomsWithQuery(getOptions, getQueryClient),
34 dataAtom = _atomsWithQuery[0];
35 return jotai.atom(function (get) {
36 var options = getOptions(get);
37 if (options.enabled === false) {
38 var queryClient = getQueryClient(get);
39 return queryClient.getQueryData(options.queryKey);
40 }
41 return get(dataAtom);
42 }, function (_get, set, action) {
43 if (action.type === 'refetch') {
44 return set(dataAtom, {
45 type: 'refetch',
46 force: true
47 });
48 }
49 });
50}
51
52function atomWithInfiniteQuery(createQuery, getQueryClient) {
53 if (getQueryClient === void 0) {
54 getQueryClient = function getQueryClient(get) {
55 return get(jotaiTanstackQuery.queryClientAtom);
56 };
57 }
58 console.warn('[DEPRECATED] use `jotai-tanstack-query` instead.');
59 var getOptions = function getOptions(get) {
60 return _extends({
61 staleTime: 200
62 }, typeof createQuery === 'function' ? createQuery(get) : createQuery);
63 };
64 var _atomsWithInfiniteQue = jotaiTanstackQuery.atomsWithInfiniteQuery(getOptions, getQueryClient),
65 dataAtom = _atomsWithInfiniteQue[0];
66 return jotai.atom(function (get) {
67 var options = getOptions(get);
68 if (options.enabled === false) {
69 var queryClient = getQueryClient(get);
70 return queryClient.getQueryData(options.queryKey);
71 }
72 return get(dataAtom);
73 }, function (_get, set, action) {
74 if (action.type === 'refetch') {
75 return set(dataAtom, {
76 type: 'refetch',
77 force: true,
78 options: action.payload
79 });
80 }
81 return set(dataAtom, action);
82 });
83}
84
85Object.defineProperty(exports, 'queryClientAtom', {
86 enumerable: true,
87 get: function () { return jotaiTanstackQuery.queryClientAtom; }
88});
89exports.atomWithInfiniteQuery = atomWithInfiniteQuery;
90exports.atomWithQuery = atomWithQuery;