UNPKG

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