UNPKG

17.8 kBJavaScriptView Raw
1'use strict';
2
3var _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
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = undefined;
9
10var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
11
12var _desc, _value, _class;
13
14var _debugDecorator = require('./debug-decorator');
15
16Function.prototype.$asyncbind = function $asyncbind(self, catcher) {
17 'use strict';
18
19 if (!Function.prototype.$asyncbind) {
20 Object.defineProperty(Function.prototype, '$asyncbind', {
21 value: $asyncbind,
22 enumerable: false,
23 configurable: true,
24 writable: true
25 });
26 }
27
28 if (!$asyncbind.trampoline) {
29 $asyncbind.trampoline = function trampoline(t, x, s, e) {
30 return function b(q) {
31 while (q) {
32 if (q.then) return q.then(b, e);
33
34 try {
35 if (q.pop) {
36 if (q.length) return q.pop() ? x.call(t) : q;
37 q = s;
38 } else q = q.call(t);
39 } catch (r) {
40 return e(r);
41 }
42 }
43 };
44 };
45 }
46
47 if (!$asyncbind.LazyThenable) {
48 $asyncbind.LazyThenable = function () {
49 function isThenable(obj) {
50 return obj && obj instanceof Object && typeof obj.then === 'function';
51 }
52
53 function resolution(p, r, how) {
54 try {
55 var x = how ? how(r) : r;
56 if (p === x) return p.reject(new TypeError('Promise resolution loop'));
57
58 if (isThenable(x)) {
59 x.then(function (y) {
60 resolution(p, y);
61 }, function (e) {
62 p.reject(e);
63 });
64 } else {
65 p.resolve(x);
66 }
67 } catch (ex) {
68 p.reject(ex);
69 }
70 }
71
72 function Chained() {}
73
74 ;
75 Chained.prototype = {
76 resolve: _unchained,
77 reject: _unchained,
78 then: thenChain
79 };
80
81 function _unchained(v) {}
82
83 function thenChain(res, rej) {
84 this.resolve = res;
85 this.reject = rej;
86 }
87
88 function then(res, rej) {
89 var chain = new Chained();
90
91 try {
92 this._resolver(function (value) {
93 return isThenable(value) ? value.then(res, rej) : resolution(chain, value, res);
94 }, function (ex) {
95 resolution(chain, ex, rej);
96 });
97 } catch (ex) {
98 resolution(chain, ex, rej);
99 }
100
101 return chain;
102 }
103
104 function Thenable(resolver) {
105 this._resolver = resolver;
106 this.then = then;
107 }
108
109 ;
110
111 Thenable.resolve = function (v) {
112 return Thenable.isThenable(v) ? v : {
113 then: function then(resolve) {
114 return resolve(v);
115 }
116 };
117 };
118
119 Thenable.isThenable = isThenable;
120 return Thenable;
121 }();
122
123 $asyncbind.EagerThenable = $asyncbind.Thenable = ($asyncbind.EagerThenableFactory = function (tick) {
124 tick = tick || (typeof process === 'undefined' ? 'undefined' : _typeof(process)) === 'object' && process.nextTick || typeof setImmediate === 'function' && setImmediate || function (f) {
125 setTimeout(f, 0);
126 };
127
128 var soon = function () {
129 var fq = [],
130 fqStart = 0,
131 bufferSize = 1024;
132
133 function callQueue() {
134 while (fq.length - fqStart) {
135 fq[fqStart]();
136 fq[fqStart++] = undefined;
137
138 if (fqStart === bufferSize) {
139 fq.splice(0, bufferSize);
140 fqStart = 0;
141 }
142 }
143 }
144
145 return function (fn) {
146 fq.push(fn);
147 if (fq.length - fqStart === 1) tick(callQueue);
148 };
149 }();
150
151 function Zousan(func) {
152 if (func) {
153 var me = this;
154 func(function (arg) {
155 me.resolve(arg);
156 }, function (arg) {
157 me.reject(arg);
158 });
159 }
160 }
161
162 Zousan.prototype = {
163 resolve: function resolve(value) {
164 if (this.state !== undefined) return;
165 if (value === this) return this.reject(new TypeError('Attempt to resolve promise with self'));
166 var me = this;
167
168 if (value && (typeof value === 'function' || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object')) {
169 try {
170 var first = 0;
171 var then = value.then;
172
173 if (typeof then === 'function') {
174 then.call(value, function (ra) {
175 if (!first++) {
176 me.resolve(ra);
177 }
178 }, function (rr) {
179 if (!first++) {
180 me.reject(rr);
181 }
182 });
183 return;
184 }
185 } catch (e) {
186 if (!first) this.reject(e);
187 return;
188 }
189 }
190
191 this.state = STATE_FULFILLED;
192 this.v = value;
193 if (me.c) soon(function () {
194 for (var n = 0, l = me.c.length; n < l; n++) {
195 STATE_FULFILLED(me.c[n], value);
196 }
197 });
198 },
199 reject: function reject(reason) {
200 if (this.state !== undefined) return;
201 this.state = STATE_REJECTED;
202 this.v = reason;
203 var clients = this.c;
204 if (clients) soon(function () {
205 for (var n = 0, l = clients.length; n < l; n++) {
206 STATE_REJECTED(clients[n], reason);
207 }
208 });
209 },
210 then: function then(onF, onR) {
211 var p = new Zousan();
212 var client = {
213 y: onF,
214 n: onR,
215 p: p
216 };
217
218 if (this.state === undefined) {
219 if (this.c) this.c.push(client);else this.c = [client];
220 } else {
221 var s = this.state,
222 a = this.v;
223 soon(function () {
224 s(client, a);
225 });
226 }
227
228 return p;
229 }
230 };
231
232 function STATE_FULFILLED(c, arg) {
233 if (typeof c.y === 'function') {
234 try {
235 var yret = c.y.call(undefined, arg);
236 c.p.resolve(yret);
237 } catch (err) {
238 c.p.reject(err);
239 }
240 } else c.p.resolve(arg);
241 }
242
243 function STATE_REJECTED(c, reason) {
244 if (typeof c.n === 'function') {
245 try {
246 var yret = c.n.call(undefined, reason);
247 c.p.resolve(yret);
248 } catch (err) {
249 c.p.reject(err);
250 }
251 } else c.p.reject(reason);
252 }
253
254 Zousan.resolve = function (val) {
255 if (val && val instanceof Zousan) return val;
256 var z = new Zousan();
257 z.resolve(val);
258 return z;
259 };
260
261 Zousan.reject = function (err) {
262 if (err && err instanceof Zousan) return err;
263 var z = new Zousan();
264 z.reject(err);
265 return z;
266 };
267
268 Zousan.version = '2.3.2-nodent';
269 return Zousan;
270 })();
271 }
272
273 var resolver = this;
274
275 switch (catcher) {
276 case true:
277 return new $asyncbind.Thenable(boundThen);
278
279 case 0:
280 return new $asyncbind.LazyThenable(boundThen);
281
282 case undefined:
283 boundThen.then = boundThen;
284 return boundThen;
285
286 default:
287 return function () {
288 try {
289 return resolver.apply(self, arguments);
290 } catch (ex) {
291 return catcher(ex);
292 }
293 };
294 }
295
296 function boundThen() {
297 return resolver.apply(self, arguments);
298 }
299};
300
301function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
302
303function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
304 var desc = {};
305 Object['ke' + 'ys'](descriptor).forEach(function (key) {
306 desc[key] = descriptor[key];
307 });
308 desc.enumerable = !!desc.enumerable;
309 desc.configurable = !!desc.configurable;
310
311 if ('value' in desc || desc.initializer) {
312 desc.writable = true;
313 }
314
315 desc = decorators.slice().reverse().reduce(function (desc, decorator) {
316 return decorator(target, property, desc) || desc;
317 }, desc);
318
319 if (context && desc.initializer !== void 0) {
320 desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
321 desc.initializer = undefined;
322 }
323
324 if (desc.initializer === void 0) {
325 Object['define' + 'Property'](target, property, desc);
326 desc = null;
327 }
328
329 return desc;
330}
331
332var FallbackTransport = (_class = function () {
333
334 // note: activeTransport is actually "?Transport", but
335 // everywhere I am using it is in `async`, so error gets returned as Promise.reject
336 function FallbackTransport(transports) {
337 _classCallCheck(this, FallbackTransport);
338
339 this.name = 'FallbackTransport';
340 this.activeName = '';
341 this.debug = false;
342
343 this.transports = transports;
344 }
345
346 // first one that inits successfuly is the final one; others won't even start initing
347
348
349 _createClass(FallbackTransport, [{
350 key: '_tryInitTransports',
351 value: function _tryInitTransports() {
352 return new Promise(function ($return, $error) {
353 var $Try_1_Finally = function ($Try_1_Exit) {
354 return function ($Try_1_Value) {
355 try {
356 if (!_iteratorNormalCompletion && _iterator.return) {
357 _iterator.return();
358 }
359 } catch ($exception_4) {
360 return $Try_3_Finally($error)($exception_4);
361 } finally {
362 if (_didIteratorError) {
363 return $error(_iteratorError);
364 }
365 }
366
367 return $Try_1_Exit && $Try_1_Exit.call(this, $Try_1_Value);
368 }.$asyncbind(this, $error);
369 }.$asyncbind(this);
370 var res, lastError, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, transport;
371
372 res = [];
373 lastError = null;
374 _iteratorNormalCompletion = true;
375 _didIteratorError = false;
376 _iteratorError = undefined;
377
378 function $Try_1_Post() {
379 if (res.length === 0) {
380 return $error(lastError || new Error('No transport could be initialized.'));
381 }
382 return $return(res);
383 }
384
385 var $Try_1_Catch = function (err) {
386 _didIteratorError = true;
387 _iteratorError = err;
388 return $Try_1_Finally($Try_1_Post).call(this);
389 }.$asyncbind(this, $Try_1_Finally($error));
390
391 try {
392 _iterator = this.transports[Symbol.iterator]();
393 return Function.$asyncbind.trampoline(this, $Loop_9_exit, $Loop_9_step, $Try_1_Catch)($Loop_9);
394 function $Loop_9() {
395 if (!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) {
396 transport = _step.value;
397
398 function $Try_2_Post() {
399 return $Loop_9_step;
400 }
401
402 var $Try_2_Catch = function (e) {
403 lastError = e;
404 return $Try_2_Post.call(this);
405 }.$asyncbind(this, $Try_1_Catch);
406 try {
407 return transport.init(this.debug).then(function ($await_13) {
408 res.push(transport);
409 return $Try_2_Post.call(this);
410 }.$asyncbind(this, $Try_2_Catch), $Try_2_Catch);
411 } catch (e) {
412 $Try_2_Catch(e)
413 }
414 } else return [1];
415 }
416
417 function $Loop_9_step() {
418 _iteratorNormalCompletion = true;
419 return $Loop_9;
420 }
421
422 function $Loop_9_exit() {
423 return $Try_1_Finally($Try_1_Post).call(this);
424 }
425 } catch (err) {
426 $Try_1_Catch(err)
427 }
428 }.$asyncbind(this));
429 }
430
431 // first one that inits successfuly is the final one; others won't even start initing
432
433 }, {
434 key: '_tryConfigureTransports',
435 value: function _tryConfigureTransports(data) {
436 return new Promise(function ($return, $error) {
437 var $Try_5_Finally = function ($Try_5_Exit) {
438 return function ($Try_5_Value) {
439 try {
440 if (!_iteratorNormalCompletion2 && _iterator2.return) {
441 _iterator2.return();
442 }
443 } catch ($exception_8) {
444 return $Try_7_Finally($error)($exception_8);
445 } finally {
446 if (_didIteratorError2) {
447 return $error(_iteratorError2);
448 }
449 }
450
451 return $Try_5_Exit && $Try_5_Exit.call(this, $Try_5_Value);
452 }.$asyncbind(this, $error);
453 }.$asyncbind(this);
454 var lastError, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, transport;
455
456 lastError = null;
457 _iteratorNormalCompletion2 = true;
458 _didIteratorError2 = false;
459 _iteratorError2 = undefined;
460
461 function $Try_5_Post() {
462 return $error(lastError || new Error('No transport could be initialized.'));
463 }
464
465 var $Try_5_Catch = function (err) {
466 _didIteratorError2 = true;
467 _iteratorError2 = err;
468 return $Try_5_Finally($Try_5_Post).call(this);
469 }.$asyncbind(this, $Try_5_Finally($error));
470 try {
471 _iterator2 = this._availableTransports[Symbol.iterator]();
472 return Function.$asyncbind.trampoline(this, $Loop_11_exit, $Loop_11_step, $Try_5_Catch)($Loop_11);
473 function $Loop_11() {
474 if (!(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done)) {
475 transport = _step2.value;
476 var $Try_6_Catch = function (e) {
477 lastError = e;
478 return $Loop_11_step;
479 }.$asyncbind(this, $Try_5_Catch);
480 try {
481 return transport.configure(data).then(function ($await_14) {
482 return $Try_5_Finally($return)(transport);
483 }.$asyncbind(this, $Try_6_Catch), $Try_6_Catch);
484 } catch (e) {
485 $Try_6_Catch(e)
486 }
487 } else return [1];
488 }
489
490 function $Loop_11_step() {
491 _iteratorNormalCompletion2 = true;
492 return $Loop_11;
493 }
494
495 function $Loop_11_exit() {
496 return $Try_5_Finally($Try_5_Post).call(this);
497 }
498 } catch (err) {
499 $Try_5_Catch(err)
500 }
501 }.$asyncbind(this));
502 }
503 }, {
504 key: 'init',
505 value: function init(debug) {
506 return new Promise(function ($return, $error) {
507 var transports;
508
509 this.debug = !!debug;
510
511 // init ALL OF THEM
512 return this._tryInitTransports().then(function ($await_15) {
513 transports = $await_15;
514 this._availableTransports = transports;
515
516 // a slight hack - configured is always false, so we force caller to call configure()
517 // to find out the actual working transport (bridge falls on configure, not on info)
518 this.version = transports[0].version;
519 this.configured = false;
520 return $return();
521 }.$asyncbind(this, $error), $error);
522 }.$asyncbind(this));
523 }
524 }, {
525 key: 'configure',
526 value: function configure(signedData) {
527 return new Promise(function ($return, $error) {
528 return this._tryConfigureTransports(signedData).then(function ($await_16) {
529 this.activeTransport = $await_16;
530 this.configured = this.activeTransport.configured;
531 this.version = this.activeTransport.version;
532 this.activeName = this.activeTransport.name;
533 return $return();
534 }.$asyncbind(this, $error), $error);
535 }.$asyncbind(this));
536 }
537
538 // using async so I get Promise.recect on this.activeTransport == null (or other error), not Error
539
540 }, {
541 key: 'enumerate',
542 value: function enumerate() {
543 return new Promise(function ($return, $error) {
544 return $return(this.activeTransport.enumerate());
545 }.$asyncbind(this));
546 }
547 }, {
548 key: 'listen',
549 value: function listen(old) {
550 return new Promise(function ($return, $error) {
551 return $return(this.activeTransport.listen(old));
552 }.$asyncbind(this));
553 }
554 }, {
555 key: 'acquire',
556 value: function acquire(input) {
557 return new Promise(function ($return, $error) {
558 return $return(this.activeTransport.acquire(input));
559 }.$asyncbind(this));
560 }
561 }, {
562 key: 'release',
563 value: function release(session) {
564 return new Promise(function ($return, $error) {
565 return $return(this.activeTransport.release(session));
566 }.$asyncbind(this));
567 }
568 }, {
569 key: 'call',
570 value: function call(session, name, data) {
571 return new Promise(function ($return, $error) {
572 return $return(this.activeTransport.call(session, name, data));
573 }.$asyncbind(this));
574 }
575 }]);
576
577 return FallbackTransport;
578}(), (_applyDecoratedDescriptor(_class.prototype, 'init', [_debugDecorator.debugInOut], Object.getOwnPropertyDescriptor(_class.prototype, 'init'), _class.prototype)), _class);
579exports.default = FallbackTransport;
580module.exports = exports['default'];
\No newline at end of file