UNPKG

8.11 kBJavaScriptView Raw
1System.register(['@urql/core', 'jotai', 'wonka'], (function (exports) {
2 'use strict';
3 var createClient, atom, pipe, subscribe;
4 return {
5 setters: [function (module) {
6 createClient = module.createClient;
7 }, function (module) {
8 atom = module.atom;
9 }, function (module) {
10 pipe = module.pipe;
11 subscribe = module.subscribe;
12 }],
13 execute: (function () {
14
15 exports({
16 atomWithMutation: atomWithMutation,
17 atomWithQuery: atomWithQuery,
18 atomWithSubscription: atomWithSubscription
19 });
20
21 const DEFAULT_URL = (() => {
22 try {
23 return process.env.JOTAI_URQL_DEFAULT_URL;
24 } catch {
25 return void 0;
26 }
27 })() || "/graphql";
28 const clientAtom = exports('clientAtom', atom(createClient({ url: DEFAULT_URL })));
29
30 const isOperationResultWithData$1 = (result) => "data" in result && !result.error;
31 function atomWithQuery(createQueryArgs, getClient = (get) => get(clientAtom)) {
32 const queryResultAtom = atom((get) => {
33 const args = createQueryArgs(get);
34 if (args.pause) {
35 return null;
36 }
37 const client = getClient(get);
38 let resolve = null;
39 const makePending = () => new Promise((r) => {
40 resolve = r;
41 });
42 const resultAtom = atom(makePending());
43 let setResult = null;
44 const listener = (result) => {
45 if (!resolve && !setResult) {
46 throw new Error("setting result without mount");
47 }
48 if (resolve) {
49 resolve(result);
50 resolve = null;
51 }
52 if (setResult) {
53 setResult(result);
54 }
55 };
56 let subscription = null;
57 let timer;
58 const startQuery = (opts) => {
59 if (subscription) {
60 clearTimeout(timer);
61 subscription.unsubscribe();
62 }
63 subscription = pipe(
64 client.query(args.query, args.variables, {
65 ...args.requestPolicy && { requestPolicy: args.requestPolicy },
66 ...args.context,
67 ...opts
68 }),
69 subscribe(listener)
70 );
71 if (!setResult) {
72 timer = setTimeout(() => {
73 if (subscription) {
74 subscription.unsubscribe();
75 subscription = null;
76 }
77 }, 1e3);
78 }
79 };
80 startQuery();
81 resultAtom.onMount = (update) => {
82 setResult = update;
83 if (subscription) {
84 clearTimeout(timer);
85 } else {
86 startQuery();
87 }
88 return () => {
89 setResult = null;
90 if (subscription) {
91 subscription.unsubscribe();
92 subscription = null;
93 }
94 };
95 };
96 return { resultAtom, makePending, startQuery };
97 });
98 const queryAtom = atom(
99 (get) => {
100 const queryResult = get(queryResultAtom);
101 if (!queryResult) {
102 return null;
103 }
104 const { resultAtom } = queryResult;
105 const result = get(resultAtom);
106 if (!isOperationResultWithData$1(result)) {
107 throw result.error;
108 }
109 return result;
110 },
111 (get, set, action) => {
112 if (action.type === "reexecute") {
113 console.warn(
114 "DEPRECATED [atomWithQuery] use refetch instead of reexecute"
115 );
116 action.type = "refetch";
117 }
118 switch (action.type) {
119 case "refetch": {
120 const queryResult = get(queryResultAtom);
121 if (!queryResult) {
122 throw new Error("query is paused");
123 }
124 const { resultAtom, makePending, startQuery } = queryResult;
125 set(resultAtom, makePending());
126 startQuery(action.opts);
127 return;
128 }
129 }
130 }
131 );
132 return queryAtom;
133 }
134
135 function atomWithMutation(createQuery, getClient = (get) => get(clientAtom)) {
136 const operationResultAtom = atom(
137 new Promise(() => {
138 })
139 );
140 const queryResultAtom = atom(
141 (get) => get(operationResultAtom),
142 async (get, set, action) => {
143 set(
144 operationResultAtom,
145 new Promise(() => {
146 })
147 );
148 const client = getClient(get);
149 const query = createQuery(get);
150 return client.mutation(query, action.variables, action.context).toPromise().then((result) => {
151 var _a;
152 (_a = action.callback) == null ? void 0 : _a.call(action, result);
153 if (result.error) {
154 throw result.error;
155 }
156 set(operationResultAtom, result);
157 });
158 }
159 );
160 return queryResultAtom;
161 }
162
163 const isOperationResultWithData = (result) => "data" in result && !result.error;
164 function atomWithSubscription(createSubscriptionArgs, getClient = (get) => get(clientAtom)) {
165 const queryResultAtom = atom((get) => {
166 const args = createSubscriptionArgs(get);
167 if (args.pause) {
168 return null;
169 }
170 const client = getClient(get);
171 let resolve = null;
172 const makePending = () => new Promise((r) => {
173 resolve = r;
174 });
175 const resultAtom = atom(makePending());
176 let setResult = null;
177 const listener = (result) => {
178 if (resolve) {
179 resolve(result);
180 resolve = null;
181 }
182 if (setResult) {
183 setResult(result);
184 }
185 };
186 let subscription = null;
187 let timer;
188 const startSub = () => {
189 if (subscription) {
190 clearTimeout(timer);
191 subscription.unsubscribe();
192 }
193 subscription = pipe(
194 client.subscription(args.query, args.variables, args.context),
195 subscribe(listener)
196 );
197 if (!setResult) {
198 timer = setTimeout(() => {
199 if (subscription) {
200 subscription.unsubscribe();
201 subscription = null;
202 }
203 }, 1e3);
204 }
205 };
206 startSub();
207 resultAtom.onMount = (update) => {
208 setResult = update;
209 if (subscription) {
210 clearTimeout(timer);
211 } else {
212 startSub();
213 }
214 return () => {
215 setResult = null;
216 if (subscription) {
217 subscription.unsubscribe();
218 subscription = null;
219 }
220 };
221 };
222 return { resultAtom, makePending, startSub };
223 });
224 const queryAtom = atom(
225 (get) => {
226 const queryResult = get(queryResultAtom);
227 if (!queryResult) {
228 return null;
229 }
230 const { resultAtom } = queryResult;
231 const result = get(resultAtom);
232 if (!isOperationResultWithData(result)) {
233 throw result.error;
234 }
235 return result;
236 },
237 (get, set, action) => {
238 switch (action.type) {
239 case "refetch": {
240 const queryResult = get(queryResultAtom);
241 if (!queryResult) {
242 throw new Error("query is paused");
243 }
244 const { resultAtom, makePending, startSub } = queryResult;
245 set(resultAtom, makePending());
246 startSub();
247 return;
248 }
249 }
250 }
251 );
252 return queryAtom;
253 }
254
255 })
256 };
257}));