UNPKG

2.66 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 var getOptions = function getOptions(get) {
28 return _extends({
29 staleTime: 200
30 }, typeof createQuery === 'function' ? createQuery(get) : createQuery);
31 };
32 var _atomsWithQuery = jotaiTanstackQuery.atomsWithQuery(getOptions, getQueryClient),
33 dataAtom = _atomsWithQuery[0];
34 return jotai.atom(function (get) {
35 var options = getOptions(get);
36 if (options.enabled === false) {
37 var queryClient = getQueryClient(get);
38 return queryClient.getQueryData(options.queryKey);
39 }
40 return get(dataAtom);
41 }, function (_get, set, action) {
42 if (action.type === 'refetch') {
43 return set(dataAtom, {
44 type: 'refetch',
45 force: true
46 });
47 }
48 });
49}
50
51function atomWithInfiniteQuery(createQuery, getQueryClient) {
52 if (getQueryClient === void 0) {
53 getQueryClient = function getQueryClient(get) {
54 return get(jotaiTanstackQuery.queryClientAtom);
55 };
56 }
57 var getOptions = function getOptions(get) {
58 return _extends({
59 staleTime: 200
60 }, typeof createQuery === 'function' ? createQuery(get) : createQuery);
61 };
62 var _atomsWithInfiniteQue = jotaiTanstackQuery.atomsWithInfiniteQuery(getOptions, getQueryClient),
63 dataAtom = _atomsWithInfiniteQue[0];
64 return jotai.atom(function (get) {
65 var options = getOptions(get);
66 if (options.enabled === false) {
67 var queryClient = getQueryClient(get);
68 return queryClient.getQueryData(options.queryKey);
69 }
70 return get(dataAtom);
71 }, function (_get, set, action) {
72 if (action.type === 'refetch') {
73 return set(dataAtom, {
74 type: 'refetch',
75 force: true,
76 options: action.payload
77 });
78 }
79 return set(dataAtom, action);
80 });
81}
82
83Object.defineProperty(exports, 'queryClientAtom', {
84 enumerable: true,
85 get: function () { return jotaiTanstackQuery.queryClientAtom; }
86});
87exports.atomWithInfiniteQuery = atomWithInfiniteQuery;
88exports.atomWithQuery = atomWithQuery;