1 | "use strict";
|
2 |
|
3 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
4 |
|
5 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
6 |
|
7 | var DLList, Sync;
|
8 | DLList = require("./DLList");
|
9 | Sync = class Sync {
|
10 | constructor(name, Promise) {
|
11 | this.schedule = this.schedule.bind(this);
|
12 | this.name = name;
|
13 | this.Promise = Promise;
|
14 | this._running = 0;
|
15 | this._queue = new DLList();
|
16 | }
|
17 |
|
18 | isEmpty() {
|
19 | return this._queue.length === 0;
|
20 | }
|
21 |
|
22 | _tryToRun() {
|
23 | var _this = this;
|
24 |
|
25 | return _asyncToGenerator(function* () {
|
26 | var args, cb, error, reject, resolve, returned, task;
|
27 |
|
28 | if (_this._running < 1 && _this._queue.length > 0) {
|
29 | _this._running++;
|
30 |
|
31 | var _this$_queue$shift = _this._queue.shift();
|
32 |
|
33 | task = _this$_queue$shift.task;
|
34 | args = _this$_queue$shift.args;
|
35 | resolve = _this$_queue$shift.resolve;
|
36 | reject = _this$_queue$shift.reject;
|
37 | cb = yield _asyncToGenerator(function* () {
|
38 | try {
|
39 | returned = yield task(...args);
|
40 | return function () {
|
41 | return resolve(returned);
|
42 | };
|
43 | } catch (error1) {
|
44 | error = error1;
|
45 | return function () {
|
46 | return reject(error);
|
47 | };
|
48 | }
|
49 | })();
|
50 | _this._running--;
|
51 |
|
52 | _this._tryToRun();
|
53 |
|
54 | return cb();
|
55 | }
|
56 | })();
|
57 | }
|
58 |
|
59 | schedule(task, ...args) {
|
60 | var promise, reject, resolve;
|
61 | resolve = reject = null;
|
62 | promise = new this.Promise(function (_resolve, _reject) {
|
63 | resolve = _resolve;
|
64 | return reject = _reject;
|
65 | });
|
66 |
|
67 | this._queue.push({
|
68 | task,
|
69 | args,
|
70 | resolve,
|
71 | reject
|
72 | });
|
73 |
|
74 | this._tryToRun();
|
75 |
|
76 | return promise;
|
77 | }
|
78 |
|
79 | };
|
80 | module.exports = Sync; |
\ | No newline at end of file |