1 | import { createClient } from '@urql/core';
|
2 | import { atom } from 'jotai';
|
3 | import { pipe, skip, subscribe } from 'wonka';
|
4 |
|
5 | const DEFAULT_URL = (() => {
|
6 | try {
|
7 | return process.env.JOTAI_URQL_DEFAULT_URL;
|
8 | } catch {
|
9 | return void 0;
|
10 | }
|
11 | })() || "/graphql";
|
12 | const clientAtom = atom(createClient({ url: DEFAULT_URL }));
|
13 |
|
14 | var __defProp = Object.defineProperty;
|
15 | var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
16 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
17 | var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
18 | var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
19 | var __spreadValues = (a, b) => {
|
20 | for (var prop in b || (b = {}))
|
21 | if (__hasOwnProp.call(b, prop))
|
22 | __defNormalProp(a, prop, b[prop]);
|
23 | if (__getOwnPropSymbols)
|
24 | for (var prop of __getOwnPropSymbols(b)) {
|
25 | if (__propIsEnum.call(b, prop))
|
26 | __defNormalProp(a, prop, b[prop]);
|
27 | }
|
28 | return a;
|
29 | };
|
30 | const isOperationResultWithData$1 = (result) => "data" in result;
|
31 | function atomWithQuery(createQueryArgs, getClient = (get) => get(clientAtom)) {
|
32 | const refAtom = atom(() => ({}));
|
33 | const createResultAtom = (ref, client, args, opts) => {
|
34 | let resolve = null;
|
35 | const resultAtom = atom(new Promise((r) => {
|
36 | resolve = r;
|
37 | }));
|
38 | ref.resultAtom = resultAtom;
|
39 | let setResult = () => {
|
40 | throw new Error("setting result without mount");
|
41 | };
|
42 | const listener = (result) => {
|
43 | if (resultAtom !== ref.resultAtom) {
|
44 | return;
|
45 | }
|
46 | if (!isOperationResultWithData$1(result)) {
|
47 | throw new Error("result does not have data");
|
48 | }
|
49 | if (resolve) {
|
50 | resolve(result);
|
51 | resolve = null;
|
52 | } else {
|
53 | setResult(result);
|
54 | }
|
55 | };
|
56 | client.query(args.query, args.variables, __spreadValues(__spreadValues(__spreadValues({}, args.requestPolicy && { requestPolicy: args.requestPolicy }), args.context), opts)).toPromise().then(listener).catch(() => {
|
57 | });
|
58 | resultAtom.onMount = (update) => {
|
59 | setResult = update;
|
60 | const subscription = pipe(client.query(args.query, args.variables, __spreadValues(__spreadValues(__spreadValues({}, args.requestPolicy && { requestPolicy: args.requestPolicy }), args.context), opts)), skip(1), subscribe(listener));
|
61 | return () => subscription.unsubscribe();
|
62 | };
|
63 | return resultAtom;
|
64 | };
|
65 | const queryResultAtom = atom((get) => {
|
66 | const args = createQueryArgs(get);
|
67 | if (args.pause) {
|
68 | return null;
|
69 | }
|
70 | const client = getClient(get);
|
71 | const resultAtom = createResultAtom(get(refAtom), client, args);
|
72 | return { resultAtom, client, args };
|
73 | });
|
74 | const overwrittenResultAtom = atom(null);
|
75 | const queryAtom = atom((get) => {
|
76 | const queryResult = get(queryResultAtom);
|
77 | if (!queryResult) {
|
78 | return null;
|
79 | }
|
80 | let { resultAtom } = queryResult;
|
81 | const overwrittenResult = get(overwrittenResultAtom);
|
82 | if (overwrittenResult && overwrittenResult.oldResultAtom === resultAtom) {
|
83 | resultAtom = overwrittenResult.newResultAtom;
|
84 | }
|
85 | return get(resultAtom);
|
86 | }, (get, set, action) => {
|
87 | switch (action.type) {
|
88 | case "reexecute": {
|
89 | const queryResult = get(queryResultAtom);
|
90 | if (!queryResult) {
|
91 | throw new Error("query is paused");
|
92 | }
|
93 | const { resultAtom, client, args } = queryResult;
|
94 | set(overwrittenResultAtom, {
|
95 | oldResultAtom: resultAtom,
|
96 | newResultAtom: createResultAtom(get(refAtom), client, args, action.opts)
|
97 | });
|
98 | }
|
99 | }
|
100 | });
|
101 | return queryAtom;
|
102 | }
|
103 |
|
104 | function atomWithMutation(createQuery, getClient = (get) => get(clientAtom)) {
|
105 | const operationResultAtom = atom(new Promise(() => {
|
106 | }));
|
107 | const queryResultAtom = atom((get) => get(operationResultAtom), (get, set, action) => {
|
108 | set(operationResultAtom, new Promise(() => {
|
109 | }));
|
110 | const client = getClient(get);
|
111 | const query = createQuery(get);
|
112 | client.mutation(query, action.variables, action.context).toPromise().then((result) => {
|
113 | var _a;
|
114 | set(operationResultAtom, result);
|
115 | (_a = action.callback) == null ? void 0 : _a.call(action, result);
|
116 | }).catch(() => {
|
117 | });
|
118 | });
|
119 | return queryResultAtom;
|
120 | }
|
121 |
|
122 | const isOperationResultWithData = (result) => "data" in result;
|
123 | function atomWithSubscription(createSubscriptionArgs, getClient = (get) => get(clientAtom)) {
|
124 | const queryResultAtom = atom((get) => {
|
125 | const args = createSubscriptionArgs(get);
|
126 | if (args.pause) {
|
127 | return { args };
|
128 | }
|
129 | const client = getClient(get);
|
130 | let resolve = null;
|
131 | const resultAtom = atom(new Promise((r) => {
|
132 | resolve = r;
|
133 | }));
|
134 | let setResult = () => {
|
135 | throw new Error("setting result without mount");
|
136 | };
|
137 | const listener = (result) => {
|
138 | if (!isOperationResultWithData(result)) {
|
139 | throw new Error("result does not have data");
|
140 | }
|
141 | if (resolve) {
|
142 | resolve(result);
|
143 | resolve = null;
|
144 | } else {
|
145 | setResult(result);
|
146 | }
|
147 | };
|
148 | const subscriptionInRender = pipe(client.subscription(args.query, args.variables, args.context), subscribe(listener));
|
149 | let timer = setTimeout(() => {
|
150 | timer = null;
|
151 | subscriptionInRender.unsubscribe();
|
152 | }, 1e3);
|
153 | resultAtom.onMount = (update) => {
|
154 | setResult = update;
|
155 | let subscription;
|
156 | if (timer) {
|
157 | clearTimeout(timer);
|
158 | subscription = subscriptionInRender;
|
159 | } else {
|
160 | subscription = pipe(client.subscription(args.query, args.variables, args.context), subscribe(listener));
|
161 | }
|
162 | return () => subscription.unsubscribe();
|
163 | };
|
164 | return { resultAtom, args };
|
165 | });
|
166 | const queryAtom = atom((get) => {
|
167 | const { resultAtom } = get(queryResultAtom);
|
168 | return resultAtom ? get(resultAtom) : null;
|
169 | });
|
170 | return queryAtom;
|
171 | }
|
172 |
|
173 | export { atomWithMutation, atomWithQuery, atomWithSubscription, clientAtom };
|