UNPKG

24 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 _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
11
12var _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; }; }();
13
14var _desc, _value, _class;
15
16var _debugDecorator = require('./debug-decorator');
17
18Function.prototype.$asyncbind = function $asyncbind(self, catcher) {
19 'use strict';
20
21 if (!Function.prototype.$asyncbind) {
22 Object.defineProperty(Function.prototype, '$asyncbind', {
23 value: $asyncbind,
24 enumerable: false,
25 configurable: true,
26 writable: true
27 });
28 }
29
30 if (!$asyncbind.trampoline) {
31 $asyncbind.trampoline = function trampoline(t, x, s, e) {
32 return function b(q) {
33 while (q) {
34 if (q.then) return q.then(b, e);
35
36 try {
37 if (q.pop) {
38 if (q.length) return q.pop() ? x.call(t) : q;
39 q = s;
40 } else q = q.call(t);
41 } catch (r) {
42 return e(r);
43 }
44 }
45 };
46 };
47 }
48
49 if (!$asyncbind.LazyThenable) {
50 $asyncbind.LazyThenable = function () {
51 function isThenable(obj) {
52 return obj && obj instanceof Object && typeof obj.then === 'function';
53 }
54
55 function resolution(p, r, how) {
56 try {
57 var x = how ? how(r) : r;
58 if (p === x) return p.reject(new TypeError('Promise resolution loop'));
59
60 if (isThenable(x)) {
61 x.then(function (y) {
62 resolution(p, y);
63 }, function (e) {
64 p.reject(e);
65 });
66 } else {
67 p.resolve(x);
68 }
69 } catch (ex) {
70 p.reject(ex);
71 }
72 }
73
74 function Chained() {}
75
76 ;
77 Chained.prototype = {
78 resolve: _unchained,
79 reject: _unchained,
80 then: thenChain
81 };
82
83 function _unchained(v) {}
84
85 function thenChain(res, rej) {
86 this.resolve = res;
87 this.reject = rej;
88 }
89
90 function then(res, rej) {
91 var chain = new Chained();
92
93 try {
94 this._resolver(function (value) {
95 return isThenable(value) ? value.then(res, rej) : resolution(chain, value, res);
96 }, function (ex) {
97 resolution(chain, ex, rej);
98 });
99 } catch (ex) {
100 resolution(chain, ex, rej);
101 }
102
103 return chain;
104 }
105
106 function Thenable(resolver) {
107 this._resolver = resolver;
108 this.then = then;
109 }
110
111 ;
112
113 Thenable.resolve = function (v) {
114 return Thenable.isThenable(v) ? v : {
115 then: function then(resolve) {
116 return resolve(v);
117 }
118 };
119 };
120
121 Thenable.isThenable = isThenable;
122 return Thenable;
123 }();
124
125 $asyncbind.EagerThenable = $asyncbind.Thenable = ($asyncbind.EagerThenableFactory = function (tick) {
126 tick = tick || (typeof process === 'undefined' ? 'undefined' : _typeof(process)) === 'object' && process.nextTick || typeof setImmediate === 'function' && setImmediate || function (f) {
127 setTimeout(f, 0);
128 };
129
130 var soon = function () {
131 var fq = [],
132 fqStart = 0,
133 bufferSize = 1024;
134
135 function callQueue() {
136 while (fq.length - fqStart) {
137 fq[fqStart]();
138 fq[fqStart++] = undefined;
139
140 if (fqStart === bufferSize) {
141 fq.splice(0, bufferSize);
142 fqStart = 0;
143 }
144 }
145 }
146
147 return function (fn) {
148 fq.push(fn);
149 if (fq.length - fqStart === 1) tick(callQueue);
150 };
151 }();
152
153 function Zousan(func) {
154 if (func) {
155 var me = this;
156 func(function (arg) {
157 me.resolve(arg);
158 }, function (arg) {
159 me.reject(arg);
160 });
161 }
162 }
163
164 Zousan.prototype = {
165 resolve: function resolve(value) {
166 if (this.state !== undefined) return;
167 if (value === this) return this.reject(new TypeError('Attempt to resolve promise with self'));
168 var me = this;
169
170 if (value && (typeof value === 'function' || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object')) {
171 try {
172 var first = 0;
173 var then = value.then;
174
175 if (typeof then === 'function') {
176 then.call(value, function (ra) {
177 if (!first++) {
178 me.resolve(ra);
179 }
180 }, function (rr) {
181 if (!first++) {
182 me.reject(rr);
183 }
184 });
185 return;
186 }
187 } catch (e) {
188 if (!first) this.reject(e);
189 return;
190 }
191 }
192
193 this.state = STATE_FULFILLED;
194 this.v = value;
195 if (me.c) soon(function () {
196 for (var n = 0, l = me.c.length; n < l; n++) {
197 STATE_FULFILLED(me.c[n], value);
198 }
199 });
200 },
201 reject: function reject(reason) {
202 if (this.state !== undefined) return;
203 this.state = STATE_REJECTED;
204 this.v = reason;
205 var clients = this.c;
206 if (clients) soon(function () {
207 for (var n = 0, l = clients.length; n < l; n++) {
208 STATE_REJECTED(clients[n], reason);
209 }
210 });
211 },
212 then: function then(onF, onR) {
213 var p = new Zousan();
214 var client = {
215 y: onF,
216 n: onR,
217 p: p
218 };
219
220 if (this.state === undefined) {
221 if (this.c) this.c.push(client);else this.c = [client];
222 } else {
223 var s = this.state,
224 a = this.v;
225 soon(function () {
226 s(client, a);
227 });
228 }
229
230 return p;
231 }
232 };
233
234 function STATE_FULFILLED(c, arg) {
235 if (typeof c.y === 'function') {
236 try {
237 var yret = c.y.call(undefined, arg);
238 c.p.resolve(yret);
239 } catch (err) {
240 c.p.reject(err);
241 }
242 } else c.p.resolve(arg);
243 }
244
245 function STATE_REJECTED(c, reason) {
246 if (typeof c.n === 'function') {
247 try {
248 var yret = c.n.call(undefined, reason);
249 c.p.resolve(yret);
250 } catch (err) {
251 c.p.reject(err);
252 }
253 } else c.p.reject(reason);
254 }
255
256 Zousan.resolve = function (val) {
257 if (val && val instanceof Zousan) return val;
258 var z = new Zousan();
259 z.resolve(val);
260 return z;
261 };
262
263 Zousan.reject = function (err) {
264 if (err && err instanceof Zousan) return err;
265 var z = new Zousan();
266 z.reject(err);
267 return z;
268 };
269
270 Zousan.version = '2.3.2-nodent';
271 return Zousan;
272 })();
273 }
274
275 var resolver = this;
276
277 switch (catcher) {
278 case true:
279 return new $asyncbind.Thenable(boundThen);
280
281 case 0:
282 return new $asyncbind.LazyThenable(boundThen);
283
284 case undefined:
285 boundThen.then = boundThen;
286 return boundThen;
287
288 default:
289 return function () {
290 try {
291 return resolver.apply(self, arguments);
292 } catch (ex) {
293 return catcher(ex);
294 }
295 };
296 }
297
298 function boundThen() {
299 return resolver.apply(self, arguments);
300 }
301};
302
303function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }
304
305function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
306
307function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
308
309function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
310 var desc = {};
311 Object['ke' + 'ys'](descriptor).forEach(function (key) {
312 desc[key] = descriptor[key];
313 });
314 desc.enumerable = !!desc.enumerable;
315 desc.configurable = !!desc.configurable;
316
317 if ('value' in desc || desc.initializer) {
318 desc.writable = true;
319 }
320
321 desc = decorators.slice().reverse().reduce(function (desc, decorator) {
322 return decorator(target, property, desc) || desc;
323 }, desc);
324
325 if (context && desc.initializer !== void 0) {
326 desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
327 desc.initializer = undefined;
328 }
329
330 if (desc.initializer === void 0) {
331 Object['define' + 'Property'](target, property, desc);
332 desc = null;
333 }
334
335 return desc;
336}
337
338function compare(a, b) {
339 if (!isNaN(parseInt(a.path))) {
340 return parseInt(a.path) - parseInt(b.path);
341 } else {
342 return a.path < b.path ? -1 : a.path > b.path ? 1 : 0;
343 }
344}
345
346var ParallelTransport = (_class = function () {
347 function ParallelTransport(transports) {
348 _classCallCheck(this, ParallelTransport);
349
350 this.name = 'ParallelTransport';
351 this.debug = false;
352
353 this.transports = transports;
354 }
355
356 _createClass(ParallelTransport, [{
357 key: '_prepend',
358 value: function _prepend(name, devices) {
359 return devices.map(function (device) {
360 return {
361 path: name + '-' + device.path,
362 session: device.session == null ? null : name + '-' + device.session
363 };
364 });
365 }
366 }, {
367 key: '_filter',
368 value: function _filter(name, devices) {
369 var _this = this;
370
371 return devices.filter(function (device) {
372 return _this._parseName(device.path).name === name;
373 }).map(function (device) {
374 return _extends({}, device, {
375 path: _this._parseName(device.path).rest,
376 session: device.session == null ? device.session : _this._parseName(device.session).rest
377 });
378 });
379 }
380 }, {
381 key: '_antiFilter',
382 value: function _antiFilter(name, devices) {
383 var _this2 = this;
384
385 return devices.filter(function (device) {
386 return _this2._parseName(device.path).name !== name;
387 }).map(function (device) {
388 return _extends({}, device, {
389 path: _this2._parseName(device.path).rest,
390 session: device.session == null ? device.session : _this2._parseName(device.session).rest
391 });
392 });
393 }
394 }, {
395 key: 'enumerate',
396 value: function enumerate() {
397 return new Promise(function ($return, $error) {
398 var $Try_1_Finally = function ($Try_1_Exit) {
399 return function ($Try_1_Value) {
400 try {
401 if (!_iteratorNormalCompletion && _iterator.return) {
402 _iterator.return();
403 }
404 } catch ($exception_3) {
405 return $Try_2_Finally($error)($exception_3);
406 } finally {
407 if (_didIteratorError) {
408 return $error(_iteratorError);
409 }
410 }
411
412 return $Try_1_Exit && $Try_1_Exit.call(this, $Try_1_Value);
413 }.$asyncbind(this, $error);
414 }.$asyncbind(this);
415 var res, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, name, devices;
416
417 res = [];
418 _iteratorNormalCompletion = true;
419 _didIteratorError = false;
420 _iteratorError = undefined;
421
422 function $Try_1_Post() {
423 return $return(res.sort(compare));
424 }
425
426 var $Try_1_Catch = function (err) {
427 _didIteratorError = true;
428 _iteratorError = err;
429 return $Try_1_Finally($Try_1_Post).call(this);
430 }.$asyncbind(this, $Try_1_Finally($error));
431
432 try {
433 _iterator = Object.keys(this.transports)[Symbol.iterator]();
434 return Function.$asyncbind.trampoline(this, $Loop_12_exit, $Loop_12_step, $Try_1_Catch)($Loop_12);
435 function $Loop_12() {
436 if (!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) {
437 name = _step.value;
438 return this.transports[name].enumerate().then(function ($await_18) {
439 devices = $await_18;
440 res.push.apply(res, _toConsumableArray(this._prepend(name, devices)));
441 return $Loop_12_step;
442 }.$asyncbind(this, $Try_1_Catch), $Try_1_Catch);
443 } else return [1];
444 }
445
446 function $Loop_12_step() {
447 _iteratorNormalCompletion = true;
448 return $Loop_12;
449 }
450
451 function $Loop_12_exit() {
452 return $Try_1_Finally($Try_1_Post).call(this);
453 }
454 } catch (err) {
455 $Try_1_Catch(err)
456 }
457 }.$asyncbind(this));
458 }
459 }, {
460 key: 'listen',
461 value: function listen(old) {
462 return new Promise(function ($return, $error) {
463 var _this3, promises, _ref, name, devices, antiFiltered, prepended;
464
465 _this3 = this;
466
467 promises = Object.keys(this.transports).map(function (name) {
468 return new Promise(function ($return, $error) {
469 var oldFiltered, devices;
470
471 oldFiltered = old == null ? null : _this3._filter(name, old);
472 return _this3.transports[name].listen(oldFiltered).then(function ($await_19) {
473 devices = $await_19;
474 return $return({ name: name, devices: devices });
475 }.$asyncbind(this, $error), $error);
476 }.$asyncbind(this));
477 });
478
479 return Promise.race(promises).then(function ($await_20) {
480 _ref = $await_20;
481 name = _ref.name;
482 devices = _ref.devices;
483
484
485 antiFiltered = old == null ? [] : this._antiFilter(name, old);
486 prepended = this._prepend(name, devices);
487
488 return $return(antiFiltered.concat(prepended).sort(compare));
489 }.$asyncbind(this, $error), $error);
490 }.$asyncbind(this));
491 }
492 }, {
493 key: '_parseName',
494 value: function _parseName(input) {
495 if (input == null) {
496 throw new Error('Wrong input');
497 }
498
499 var _input$split = input.split('-');
500
501 var _input$split2 = _toArray(_input$split);
502
503 var name = _input$split2[0];
504
505 var restArray = _input$split2.slice(1);
506
507 if (restArray.length === 0) {
508 throw new Error('Input has to contain transport name.');
509 }
510 var transport = this.transports[name];
511 if (transport == null) {
512 throw new Error('Input has to contain valid transport name.');
513 }
514 var rest = restArray.join('-');
515
516 return {
517 transport: transport,
518 name: name,
519 rest: rest
520 };
521 }
522 }, {
523 key: 'acquire',
524 value: function acquire(input) {
525 return new Promise(function ($return, $error) {
526 var path, previous, newInput, res;
527
528 path = this._parseName(input.path);
529 previous = input.previous == null ? null : this._parseName(input.previous);
530 if (previous != null && path.name !== previous.name) {
531 return $error(new Error('Session transport has to equal path transport.'));
532 }
533 newInput = {
534 path: path.rest,
535 previous: previous == null ? null : previous.rest,
536 checkPrevious: input.checkPrevious
537 };
538 return path.transport.acquire(newInput).then(function ($await_21) {
539 res = $await_21;
540 return $return(path.name + '-' + res);
541 }.$asyncbind(this, $error), $error);
542 }.$asyncbind(this));
543 }
544 }, {
545 key: 'release',
546 value: function release(session) {
547 return new Promise(function ($return, $error) {
548 var sessionP = this._parseName(session);
549 return $return(sessionP.transport.release(sessionP.rest));
550 }.$asyncbind(this));
551 }
552 }, {
553 key: '_checkConfigured',
554 value: function _checkConfigured() {
555 // configured is true if all of the transports are configured
556 var _iteratorNormalCompletion2 = true;
557 var _didIteratorError2 = false;
558 var _iteratorError2 = undefined;
559
560 try {
561 for (var _iterator2 = Object.keys(this.transports)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
562 var name = _step2.value;
563
564 var transport = this.transports[name];
565 if (!transport.configured) {
566 return false;
567 }
568 }
569 } catch (err) {
570 _didIteratorError2 = true;
571 _iteratorError2 = err;
572 } finally {
573 try {
574 if (!_iteratorNormalCompletion2 && _iterator2.return) {
575 _iterator2.return();
576 }
577 } finally {
578 if (_didIteratorError2) {
579 throw _iteratorError2;
580 }
581 }
582 }
583
584 return true;
585 }
586 }, {
587 key: 'configure',
588 value: function configure(signedData) {
589 return new Promise(function ($return, $error) {
590 var $Try_6_Finally = function ($Try_6_Exit) {
591 return function ($Try_6_Value) {
592 try {
593 if (!_iteratorNormalCompletion3 && _iterator3.return) {
594 _iterator3.return();
595 }
596 } catch ($exception_8) {
597 return $Try_7_Finally($error)($exception_8);
598 } finally {
599 if (_didIteratorError3) {
600 return $error(_iteratorError3);
601 }
602 }
603
604 return $Try_6_Exit && $Try_6_Exit.call(this, $Try_6_Value);
605 }.$asyncbind(this, $error);
606 }.$asyncbind(this);
607 var _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _iterator3, _step3, name, transport;
608
609 _iteratorNormalCompletion3 = true;
610 _didIteratorError3 = false;
611 _iteratorError3 = undefined;
612
613 function $Try_6_Post() {
614 this.configured = this._checkConfigured();
615 return $return();
616 }
617
618 var $Try_6_Catch = function (err) {
619 _didIteratorError3 = true;
620 _iteratorError3 = err;
621 return $Try_6_Finally($Try_6_Post).call(this);
622 }.$asyncbind(this, $Try_6_Finally($error));
623 try {
624 _iterator3 = Object.keys(this.transports)[Symbol.iterator]();
625 return Function.$asyncbind.trampoline(this, $Loop_14_exit, $Loop_14_step, $Try_6_Catch)($Loop_14);
626 function $Loop_14() {
627 if (!(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done)) {
628 name = _step3.value;
629
630 transport = this.transports[name];
631 return transport.configure(signedData).then(function ($await_22) {
632 return $Loop_14_step;
633 }.$asyncbind(this, $Try_6_Catch), $Try_6_Catch);
634 } else return [1];
635 }
636
637 function $Loop_14_step() {
638 _iteratorNormalCompletion3 = true;
639 return $Loop_14;
640 }
641
642 function $Loop_14_exit() {
643 return $Try_6_Finally($Try_6_Post).call(this);
644 }
645 } catch (err) {
646 $Try_6_Catch(err)
647 }
648 }.$asyncbind(this));
649 }
650 }, {
651 key: 'call',
652 value: function call(session, name, data) {
653 return new Promise(function ($return, $error) {
654 var sessionP = this._parseName(session);
655 return $return(sessionP.transport.call(sessionP.rest, name, data));
656 }.$asyncbind(this));
657 }
658
659 // resolves when the transport can be used; rejects when it cannot
660
661 }, {
662 key: 'init',
663 value: function init(debug) {
664 return new Promise(function ($return, $error) {
665 var $Try_9_Finally = function ($Try_9_Exit) {
666 return function ($Try_9_Value) {
667 try {
668 if (!_iteratorNormalCompletion4 && _iterator4.return) {
669 _iterator4.return();
670 }
671 } catch ($exception_11) {
672 return $Try_10_Finally($error)($exception_11);
673 } finally {
674 if (_didIteratorError4) {
675 return $error(_iteratorError4);
676 }
677 }
678
679 return $Try_9_Exit && $Try_9_Exit.call(this, $Try_9_Value);
680 }.$asyncbind(this, $error);
681 }.$asyncbind(this);
682 var version, _iteratorNormalCompletion4, _didIteratorError4, _iteratorError4, _iterator4, _step4, name, transport;
683
684 this.debug = !!debug;
685 version = '';
686 _iteratorNormalCompletion4 = true;
687 _didIteratorError4 = false;
688 _iteratorError4 = undefined;
689
690 function $Try_9_Post() {
691 this.version = version;
692 this.configured = this._checkConfigured();
693 return $return();
694 }
695
696 var $Try_9_Catch = function (err) {
697 _didIteratorError4 = true;
698 _iteratorError4 = err;
699 return $Try_9_Finally($Try_9_Post).call(this);
700 }.$asyncbind(this, $Try_9_Finally($error));
701 try {
702 _iterator4 = Object.keys(this.transports)[Symbol.iterator]();
703 return Function.$asyncbind.trampoline(this, $Loop_16_exit, $Loop_16_step, $Try_9_Catch)($Loop_16);
704 function $Loop_16() {
705 if (!(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done)) {
706 name = _step4.value;
707
708 transport = this.transports[name];
709 return transport.init(debug).then(function ($await_23) {
710 version = version + (name + ':' + transport.version + ';');
711 return $Loop_16_step;
712 }.$asyncbind(this, $Try_9_Catch), $Try_9_Catch);
713 } else return [1];
714 }
715
716 function $Loop_16_step() {
717 _iteratorNormalCompletion4 = true;
718 return $Loop_16;
719 }
720
721 function $Loop_16_exit() {
722 return $Try_9_Finally($Try_9_Post).call(this);
723 }
724 } catch (err) {
725 $Try_9_Catch(err)
726 }
727 }.$asyncbind(this));
728 }
729 }]);
730
731 return ParallelTransport;
732}(), (_applyDecoratedDescriptor(_class.prototype, 'enumerate', [_debugDecorator.debugInOut], Object.getOwnPropertyDescriptor(_class.prototype, 'enumerate'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'listen', [_debugDecorator.debugInOut], Object.getOwnPropertyDescriptor(_class.prototype, 'listen'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'acquire', [_debugDecorator.debugInOut], Object.getOwnPropertyDescriptor(_class.prototype, 'acquire'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'release', [_debugDecorator.debugInOut], Object.getOwnPropertyDescriptor(_class.prototype, 'release'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'configure', [_debugDecorator.debugInOut], Object.getOwnPropertyDescriptor(_class.prototype, 'configure'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'call', [_debugDecorator.debugInOut], Object.getOwnPropertyDescriptor(_class.prototype, 'call'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'init', [_debugDecorator.debugInOut], Object.getOwnPropertyDescriptor(_class.prototype, 'init'), _class.prototype)), _class);
733exports.default = ParallelTransport;
734module.exports = exports['default'];
\No newline at end of file