UNPKG

4.25 kBJavaScriptView Raw
1System.register(['jotai-urql', 'jotai'], (function (exports) {
2 'use strict';
3 var atomsWithQuery, clientAtom, atomsWithMutation, atomsWithSubscription, atom;
4 return {
5 setters: [function (module) {
6 atomsWithQuery = module.atomsWithQuery;
7 clientAtom = module.clientAtom;
8 atomsWithMutation = module.atomsWithMutation;
9 atomsWithSubscription = module.atomsWithSubscription;
10 exports('clientAtom', module.clientAtom);
11 }, function (module) {
12 atom = module.atom;
13 }],
14 execute: (function () {
15
16 exports({
17 atomWithMutation: atomWithMutation,
18 atomWithQuery: atomWithQuery,
19 atomWithSubscription: atomWithSubscription
20 });
21
22 function atomWithQuery(createQueryArgs, getClient = (get) => get(clientAtom)) {
23 const getArgs = (get) => {
24 const queryArgs = createQueryArgs(get);
25 return [
26 queryArgs.query,
27 queryArgs.variables,
28 {
29 ...queryArgs.requestPolicy && {
30 requestPolicy: queryArgs.requestPolicy
31 },
32 ...queryArgs.context
33 }
34 ];
35 };
36 const [dataAtom, statusAtom] = atomsWithQuery(getArgs, getClient);
37 return atom(
38 (get) => {
39 const queryArgs = createQueryArgs(get);
40 if (queryArgs.pause) {
41 return null;
42 }
43 const status = get(statusAtom);
44 if (status.error) {
45 throw status.error;
46 }
47 if ("data" in status) {
48 return status;
49 }
50 get(dataAtom);
51 return status;
52 },
53 (_get, set, action) => {
54 if (action.type === "reexecute") {
55 console.warn(
56 "DEPRECATED [atomWithQuery] use refetch instead of reexecute"
57 );
58 action.type = "refetch";
59 }
60 if ("opts" in action) {
61 console.warn("DEPRECATED [atomWithQuery] action.opts is no longer used");
62 }
63 switch (action.type) {
64 case "refetch": {
65 return set(statusAtom, action);
66 }
67 }
68 }
69 );
70 }
71
72 function atomWithMutation(createQuery, getClient = (get) => get(clientAtom)) {
73 const [, statusAtom] = atomsWithMutation(getClient);
74 return atom(
75 (get) => {
76 const status = get(statusAtom);
77 return status;
78 },
79 async (get, set, action) => {
80 const args = [
81 createQuery(get),
82 action.variables,
83 action.context || {}
84 ];
85 await set(statusAtom, args);
86 return Promise.resolve(get(statusAtom, { unstable_promise: true })).then(
87 (status) => {
88 var _a;
89 (_a = action.callback) == null ? void 0 : _a.call(action, status);
90 if (status.error) {
91 throw status.error;
92 }
93 }
94 );
95 }
96 );
97 }
98
99 function atomWithSubscription(createSubscriptionArgs, getClient = (get) => get(clientAtom)) {
100 const getArgs = (get) => {
101 const subscriptionArgs = createSubscriptionArgs(get);
102 return [
103 subscriptionArgs.query,
104 subscriptionArgs.variables,
105 subscriptionArgs.context || {}
106 ];
107 };
108 const [dataAtom, statusAtom] = atomsWithSubscription(getArgs, getClient);
109 return atom(
110 (get) => {
111 const subscriptionArgs = createSubscriptionArgs(get);
112 if (subscriptionArgs.pause) {
113 return null;
114 }
115 const status = get(statusAtom);
116 if (status.error) {
117 throw status.error;
118 }
119 if ("data" in status) {
120 return status;
121 }
122 get(dataAtom);
123 return status;
124 },
125 (_get, set, action) => {
126 switch (action.type) {
127 case "refetch": {
128 return set(statusAtom, action);
129 }
130 }
131 }
132 );
133 }
134
135 })
136 };
137}));