1 | 'use strict';
|
2 |
|
3 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.setFetch = setFetch;
|
9 | exports.request = request;
|
10 |
|
11 | Function.prototype.$asyncbind = function $asyncbind(self, catcher) {
|
12 | 'use strict';
|
13 |
|
14 | if (!Function.prototype.$asyncbind) {
|
15 | Object.defineProperty(Function.prototype, '$asyncbind', {
|
16 | value: $asyncbind,
|
17 | enumerable: false,
|
18 | configurable: true,
|
19 | writable: true
|
20 | });
|
21 | }
|
22 |
|
23 | if (!$asyncbind.trampoline) {
|
24 | $asyncbind.trampoline = function trampoline(t, x, s, e) {
|
25 | return function b(q) {
|
26 | while (q) {
|
27 | if (q.then) return q.then(b, e);
|
28 |
|
29 | try {
|
30 | if (q.pop) {
|
31 | if (q.length) return q.pop() ? x.call(t) : q;
|
32 | q = s;
|
33 | } else q = q.call(t);
|
34 | } catch (r) {
|
35 | return e(r);
|
36 | }
|
37 | }
|
38 | };
|
39 | };
|
40 | }
|
41 |
|
42 | if (!$asyncbind.LazyThenable) {
|
43 | $asyncbind.LazyThenable = function () {
|
44 | function isThenable(obj) {
|
45 | return obj && obj instanceof Object && typeof obj.then === 'function';
|
46 | }
|
47 |
|
48 | function resolution(p, r, how) {
|
49 | try {
|
50 | var x = how ? how(r) : r;
|
51 | if (p === x) return p.reject(new TypeError('Promise resolution loop'));
|
52 |
|
53 | if (isThenable(x)) {
|
54 | x.then(function (y) {
|
55 | resolution(p, y);
|
56 | }, function (e) {
|
57 | p.reject(e);
|
58 | });
|
59 | } else {
|
60 | p.resolve(x);
|
61 | }
|
62 | } catch (ex) {
|
63 | p.reject(ex);
|
64 | }
|
65 | }
|
66 |
|
67 | function Chained() {}
|
68 |
|
69 | ;
|
70 | Chained.prototype = {
|
71 | resolve: _unchained,
|
72 | reject: _unchained,
|
73 | then: thenChain
|
74 | };
|
75 |
|
76 | function _unchained(v) {}
|
77 |
|
78 | function thenChain(res, rej) {
|
79 | this.resolve = res;
|
80 | this.reject = rej;
|
81 | }
|
82 |
|
83 | function then(res, rej) {
|
84 | var chain = new Chained();
|
85 |
|
86 | try {
|
87 | this._resolver(function (value) {
|
88 | return isThenable(value) ? value.then(res, rej) : resolution(chain, value, res);
|
89 | }, function (ex) {
|
90 | resolution(chain, ex, rej);
|
91 | });
|
92 | } catch (ex) {
|
93 | resolution(chain, ex, rej);
|
94 | }
|
95 |
|
96 | return chain;
|
97 | }
|
98 |
|
99 | function Thenable(resolver) {
|
100 | this._resolver = resolver;
|
101 | this.then = then;
|
102 | }
|
103 |
|
104 | ;
|
105 |
|
106 | Thenable.resolve = function (v) {
|
107 | return Thenable.isThenable(v) ? v : {
|
108 | then: function then(resolve) {
|
109 | return resolve(v);
|
110 | }
|
111 | };
|
112 | };
|
113 |
|
114 | Thenable.isThenable = isThenable;
|
115 | return Thenable;
|
116 | }();
|
117 |
|
118 | $asyncbind.EagerThenable = $asyncbind.Thenable = ($asyncbind.EagerThenableFactory = function (tick) {
|
119 | tick = tick || (typeof process === 'undefined' ? 'undefined' : _typeof(process)) === 'object' && process.nextTick || typeof setImmediate === 'function' && setImmediate || function (f) {
|
120 | setTimeout(f, 0);
|
121 | };
|
122 |
|
123 | var soon = function () {
|
124 | var fq = [],
|
125 | fqStart = 0,
|
126 | bufferSize = 1024;
|
127 |
|
128 | function callQueue() {
|
129 | while (fq.length - fqStart) {
|
130 | fq[fqStart]();
|
131 | fq[fqStart++] = undefined;
|
132 |
|
133 | if (fqStart === bufferSize) {
|
134 | fq.splice(0, bufferSize);
|
135 | fqStart = 0;
|
136 | }
|
137 | }
|
138 | }
|
139 |
|
140 | return function (fn) {
|
141 | fq.push(fn);
|
142 | if (fq.length - fqStart === 1) tick(callQueue);
|
143 | };
|
144 | }();
|
145 |
|
146 | function Zousan(func) {
|
147 | if (func) {
|
148 | var me = this;
|
149 | func(function (arg) {
|
150 | me.resolve(arg);
|
151 | }, function (arg) {
|
152 | me.reject(arg);
|
153 | });
|
154 | }
|
155 | }
|
156 |
|
157 | Zousan.prototype = {
|
158 | resolve: function resolve(value) {
|
159 | if (this.state !== undefined) return;
|
160 | if (value === this) return this.reject(new TypeError('Attempt to resolve promise with self'));
|
161 | var me = this;
|
162 |
|
163 | if (value && (typeof value === 'function' || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object')) {
|
164 | try {
|
165 | var first = 0;
|
166 | var then = value.then;
|
167 |
|
168 | if (typeof then === 'function') {
|
169 | then.call(value, function (ra) {
|
170 | if (!first++) {
|
171 | me.resolve(ra);
|
172 | }
|
173 | }, function (rr) {
|
174 | if (!first++) {
|
175 | me.reject(rr);
|
176 | }
|
177 | });
|
178 | return;
|
179 | }
|
180 | } catch (e) {
|
181 | if (!first) this.reject(e);
|
182 | return;
|
183 | }
|
184 | }
|
185 |
|
186 | this.state = STATE_FULFILLED;
|
187 | this.v = value;
|
188 | if (me.c) soon(function () {
|
189 | for (var n = 0, l = me.c.length; n < l; n++) {
|
190 | STATE_FULFILLED(me.c[n], value);
|
191 | }
|
192 | });
|
193 | },
|
194 | reject: function reject(reason) {
|
195 | if (this.state !== undefined) return;
|
196 | this.state = STATE_REJECTED;
|
197 | this.v = reason;
|
198 | var clients = this.c;
|
199 | if (clients) soon(function () {
|
200 | for (var n = 0, l = clients.length; n < l; n++) {
|
201 | STATE_REJECTED(clients[n], reason);
|
202 | }
|
203 | });
|
204 | },
|
205 | then: function then(onF, onR) {
|
206 | var p = new Zousan();
|
207 | var client = {
|
208 | y: onF,
|
209 | n: onR,
|
210 | p: p
|
211 | };
|
212 |
|
213 | if (this.state === undefined) {
|
214 | if (this.c) this.c.push(client);else this.c = [client];
|
215 | } else {
|
216 | var s = this.state,
|
217 | a = this.v;
|
218 | soon(function () {
|
219 | s(client, a);
|
220 | });
|
221 | }
|
222 |
|
223 | return p;
|
224 | }
|
225 | };
|
226 |
|
227 | function STATE_FULFILLED(c, arg) {
|
228 | if (typeof c.y === 'function') {
|
229 | try {
|
230 | var yret = c.y.call(undefined, arg);
|
231 | c.p.resolve(yret);
|
232 | } catch (err) {
|
233 | c.p.reject(err);
|
234 | }
|
235 | } else c.p.resolve(arg);
|
236 | }
|
237 |
|
238 | function STATE_REJECTED(c, reason) {
|
239 | if (typeof c.n === 'function') {
|
240 | try {
|
241 | var yret = c.n.call(undefined, reason);
|
242 | c.p.resolve(yret);
|
243 | } catch (err) {
|
244 | c.p.reject(err);
|
245 | }
|
246 | } else c.p.reject(reason);
|
247 | }
|
248 |
|
249 | Zousan.resolve = function (val) {
|
250 | if (val && val instanceof Zousan) return val;
|
251 | var z = new Zousan();
|
252 | z.resolve(val);
|
253 | return z;
|
254 | };
|
255 |
|
256 | Zousan.reject = function (err) {
|
257 | if (err && err instanceof Zousan) return err;
|
258 | var z = new Zousan();
|
259 | z.reject(err);
|
260 | return z;
|
261 | };
|
262 |
|
263 | Zousan.version = '2.3.2-nodent';
|
264 | return Zousan;
|
265 | })();
|
266 | }
|
267 |
|
268 | var resolver = this;
|
269 |
|
270 | switch (catcher) {
|
271 | case true:
|
272 | return new $asyncbind.Thenable(boundThen);
|
273 |
|
274 | case 0:
|
275 | return new $asyncbind.LazyThenable(boundThen);
|
276 |
|
277 | case undefined:
|
278 | boundThen.then = boundThen;
|
279 | return boundThen;
|
280 |
|
281 | default:
|
282 | return function () {
|
283 | try {
|
284 | return resolver.apply(self, arguments);
|
285 | } catch (ex) {
|
286 | return catcher(ex);
|
287 | }
|
288 | };
|
289 | }
|
290 |
|
291 | function boundThen() {
|
292 | return resolver.apply(self, arguments);
|
293 | }
|
294 | };
|
295 |
|
296 |
|
297 |
|
298 | var _fetch = typeof window === 'undefined' ? function () {
|
299 | return Promise.reject();
|
300 | } : window.fetch;
|
301 |
|
302 | function setFetch(fetch) {
|
303 | _fetch = fetch;
|
304 | }
|
305 |
|
306 | function contentType(body) {
|
307 | if (typeof body === 'string') {
|
308 | if (body === '') {
|
309 | return 'text/plain';
|
310 | }
|
311 | return 'application/octet-stream';
|
312 | } else {
|
313 | return 'application/json';
|
314 | }
|
315 | }
|
316 |
|
317 | function wrapBody(body) {
|
318 | if (typeof body === 'string') {
|
319 | return body;
|
320 | } else {
|
321 | return JSON.stringify(body);
|
322 | }
|
323 | }
|
324 |
|
325 | function parseResult(text) {
|
326 | try {
|
327 | return JSON.parse(text);
|
328 | } catch (e) {
|
329 | return text;
|
330 | }
|
331 | }
|
332 |
|
333 | function request(options) {
|
334 | return new Promise(function ($return, $error) {
|
335 | var res, resText, resJson;
|
336 | return _fetch(options.url, {
|
337 | method: options.method,
|
338 | headers: {
|
339 | 'Content-Type': contentType(options.body || '')
|
340 | },
|
341 | body: wrapBody(options.body)
|
342 | }).then(function ($await_2) {
|
343 | res = $await_2;
|
344 | return res.text().then(function ($await_3) {
|
345 | resText = $await_3;
|
346 | if (res.ok) {
|
347 | return $return(parseResult(resText));
|
348 | } else {
|
349 | resJson = parseResult(resText);
|
350 | if (resJson.error) {
|
351 | return $error(new Error(resJson.error));
|
352 | } else {
|
353 | return $error(new Error(resText));
|
354 | }
|
355 | }
|
356 | return $return();
|
357 | }.$asyncbind(this, $error), $error);
|
358 | }.$asyncbind(this, $error), $error);
|
359 | }.$asyncbind(this));
|
360 | } |
\ | No newline at end of file |