1 | System.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 baseResultAtom = atom(
|
40 | new Promise((r) => {
|
41 | resolve = r;
|
42 | })
|
43 | );
|
44 | let setResult = () => {
|
45 | throw new Error("setting result without mount");
|
46 | };
|
47 | const listener = (result) => {
|
48 | if (result instanceof Promise) {
|
49 | setResult(result);
|
50 | return;
|
51 | }
|
52 | if (!isOperationResultWithData$1(result)) {
|
53 | throw new Error("result does not have data");
|
54 | }
|
55 | if (resolve) {
|
56 | resolve(result);
|
57 | resolve = null;
|
58 | } else {
|
59 | setResult(result);
|
60 | }
|
61 | };
|
62 | client.query(args.query, args.variables, {
|
63 | ...args.requestPolicy && { requestPolicy: args.requestPolicy },
|
64 | ...args.context
|
65 | }).toPromise().then(listener).catch(() => {
|
66 | });
|
67 | baseResultAtom.onMount = (update) => {
|
68 | setResult = update;
|
69 | };
|
70 | const subscriptionAtom = atom(null);
|
71 | const resultAtom = atom(
|
72 | (get2) => get2(baseResultAtom),
|
73 | (get2, set, callback) => {
|
74 | const subscription = pipe(
|
75 | client.query(args.query, args.variables, {
|
76 | ...args.requestPolicy && { requestPolicy: args.requestPolicy },
|
77 | ...args.context
|
78 | }),
|
79 | subscribe(listener)
|
80 | );
|
81 | set(subscriptionAtom, subscription);
|
82 | callback(() => {
|
83 | var _a;
|
84 | return (_a = get2(subscriptionAtom)) == null ? void 0 : _a.unsubscribe();
|
85 | });
|
86 | }
|
87 | );
|
88 | resultAtom.onMount = (init) => {
|
89 | let cleanup;
|
90 | init((c) => {
|
91 | cleanup = c;
|
92 | });
|
93 | return cleanup;
|
94 | };
|
95 | return { args, client, resultAtom, subscriptionAtom, listener };
|
96 | });
|
97 | const queryAtom = atom(
|
98 | (get) => {
|
99 | const queryResult = get(queryResultAtom);
|
100 | if (!queryResult) {
|
101 | return null;
|
102 | }
|
103 | const { resultAtom } = queryResult;
|
104 | return get(resultAtom);
|
105 | },
|
106 | (get, set, action) => {
|
107 | switch (action.type) {
|
108 | case "reexecute": {
|
109 | const queryResult = get(queryResultAtom);
|
110 | if (!queryResult) {
|
111 | throw new Error("query is paused");
|
112 | }
|
113 | const { args, client, subscriptionAtom, listener } = queryResult;
|
114 | listener(new Promise(() => {
|
115 | }));
|
116 | const newSubscription = pipe(
|
117 | client.query(args.query, args.variables, {
|
118 | ...args.requestPolicy && { requestPolicy: args.requestPolicy },
|
119 | ...args.context,
|
120 | ...action.opts
|
121 | }),
|
122 | subscribe(listener)
|
123 | );
|
124 | const oldSubscription = get(subscriptionAtom);
|
125 | oldSubscription == null ? void 0 : oldSubscription.unsubscribe();
|
126 | set(subscriptionAtom, newSubscription);
|
127 | }
|
128 | }
|
129 | }
|
130 | );
|
131 | return queryAtom;
|
132 | }
|
133 |
|
134 | function atomWithMutation(createQuery, getClient = (get) => get(clientAtom)) {
|
135 | const operationResultAtom = atom(
|
136 | new Promise(() => {
|
137 | })
|
138 | );
|
139 | const queryResultAtom = atom(
|
140 | (get) => get(operationResultAtom),
|
141 | (get, set, action) => {
|
142 | set(
|
143 | operationResultAtom,
|
144 | new Promise(() => {
|
145 | })
|
146 | );
|
147 | const client = getClient(get);
|
148 | const query = createQuery(get);
|
149 | client.mutation(query, action.variables, action.context).toPromise().then((result) => {
|
150 | var _a;
|
151 | set(operationResultAtom, result);
|
152 | (_a = action.callback) == null ? void 0 : _a.call(action, result);
|
153 | }).catch(() => {
|
154 | });
|
155 | }
|
156 | );
|
157 | return queryResultAtom;
|
158 | }
|
159 |
|
160 | const isOperationResultWithData = (result) => "data" in result;
|
161 | function atomWithSubscription(createSubscriptionArgs, getClient = (get) => get(clientAtom)) {
|
162 | const queryResultAtom = atom((get) => {
|
163 | const args = createSubscriptionArgs(get);
|
164 | if (args.pause) {
|
165 | return { args };
|
166 | }
|
167 | const client = getClient(get);
|
168 | let resolve = null;
|
169 | const resultAtom = atom(
|
170 | new Promise((r) => {
|
171 | resolve = r;
|
172 | })
|
173 | );
|
174 | let setResult = () => {
|
175 | throw new Error("setting result without mount");
|
176 | };
|
177 | let isMounted = false;
|
178 | const listener = (result) => {
|
179 | if (!isOperationResultWithData(result)) {
|
180 | throw new Error("result does not have data");
|
181 | }
|
182 | if (resolve) {
|
183 | if (!isMounted) {
|
184 | subscription == null ? void 0 : subscription.unsubscribe();
|
185 | subscription = null;
|
186 | }
|
187 | resolve(result);
|
188 | resolve = null;
|
189 | } else {
|
190 | setResult(result);
|
191 | }
|
192 | };
|
193 | let subscription = pipe(
|
194 | client.subscription(args.query, args.variables, args.context),
|
195 | subscribe(listener)
|
196 | );
|
197 | resultAtom.onMount = (update) => {
|
198 | setResult = update;
|
199 | isMounted = true;
|
200 | if (!subscription) {
|
201 | subscription = pipe(
|
202 | client.subscription(args.query, args.variables, args.context),
|
203 | subscribe(listener)
|
204 | );
|
205 | }
|
206 | return () => subscription == null ? void 0 : subscription.unsubscribe();
|
207 | };
|
208 | return { resultAtom, args };
|
209 | });
|
210 | const queryAtom = atom((get) => {
|
211 | const { resultAtom } = get(queryResultAtom);
|
212 | return resultAtom ? get(resultAtom) : null;
|
213 | });
|
214 | return queryAtom;
|
215 | }
|
216 |
|
217 | })
|
218 | };
|
219 | }));
|