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