1 | "use strict";
|
---|
2 | var __extends = (this && this.__extends) || (function () {
|
---|
3 | var extendStatics = function (d, b) {
|
---|
4 | extendStatics = Object.setPrototypeOf ||
|
---|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
---|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
---|
7 | return extendStatics(d, b);
|
---|
8 | }
|
---|
9 | return function (d, b) {
|
---|
10 | extendStatics(d, b);
|
---|
11 | function __() { this.constructor = d; }
|
---|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
---|
13 | };
|
---|
14 | })();
|
---|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
16 | var Action_1 = require("./Action");
|
---|
17 | var AsyncAction = (function (_super) {
|
---|
18 | __extends(AsyncAction, _super);
|
---|
19 | function AsyncAction(scheduler, work) {
|
---|
20 | var _this = _super.call(this, scheduler, work) || this;
|
---|
21 | _this.scheduler = scheduler;
|
---|
22 | _this.work = work;
|
---|
23 | _this.pending = false;
|
---|
24 | return _this;
|
---|
25 | }
|
---|
26 | AsyncAction.prototype.schedule = function (state, delay) {
|
---|
27 | if (delay === void 0) { delay = 0; }
|
---|
28 | if (this.closed) {
|
---|
29 | return this;
|
---|
30 | }
|
---|
31 | this.state = state;
|
---|
32 | var id = this.id;
|
---|
33 | var scheduler = this.scheduler;
|
---|
34 | if (id != null) {
|
---|
35 | this.id = this.recycleAsyncId(scheduler, id, delay);
|
---|
36 | }
|
---|
37 | this.pending = true;
|
---|
38 | this.delay = delay;
|
---|
39 | this.id = this.id || this.requestAsyncId(scheduler, this.id, delay);
|
---|
40 | return this;
|
---|
41 | };
|
---|
42 | AsyncAction.prototype.requestAsyncId = function (scheduler, id, delay) {
|
---|
43 | if (delay === void 0) { delay = 0; }
|
---|
44 | return setInterval(scheduler.flush.bind(scheduler, this), delay);
|
---|
45 | };
|
---|
46 | AsyncAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
|
---|
47 | if (delay === void 0) { delay = 0; }
|
---|
48 | if (delay !== null && this.delay === delay && this.pending === false) {
|
---|
49 | return id;
|
---|
50 | }
|
---|
51 | clearInterval(id);
|
---|
52 | return undefined;
|
---|
53 | };
|
---|
54 | AsyncAction.prototype.execute = function (state, delay) {
|
---|
55 | if (this.closed) {
|
---|
56 | return new Error('executing a cancelled action');
|
---|
57 | }
|
---|
58 | this.pending = false;
|
---|
59 | var error = this._execute(state, delay);
|
---|
60 | if (error) {
|
---|
61 | return error;
|
---|
62 | }
|
---|
63 | else if (this.pending === false && this.id != null) {
|
---|
64 | this.id = this.recycleAsyncId(this.scheduler, this.id, null);
|
---|
65 | }
|
---|
66 | };
|
---|
67 | AsyncAction.prototype._execute = function (state, delay) {
|
---|
68 | var errored = false;
|
---|
69 | var errorValue = undefined;
|
---|
70 | try {
|
---|
71 | this.work(state);
|
---|
72 | }
|
---|
73 | catch (e) {
|
---|
74 | errored = true;
|
---|
75 | errorValue = !!e && e || new Error(e);
|
---|
76 | }
|
---|
77 | if (errored) {
|
---|
78 | this.unsubscribe();
|
---|
79 | return errorValue;
|
---|
80 | }
|
---|
81 | };
|
---|
82 | AsyncAction.prototype._unsubscribe = function () {
|
---|
83 | var id = this.id;
|
---|
84 | var scheduler = this.scheduler;
|
---|
85 | var actions = scheduler.actions;
|
---|
86 | var index = actions.indexOf(this);
|
---|
87 | this.work = null;
|
---|
88 | this.state = null;
|
---|
89 | this.pending = false;
|
---|
90 | this.scheduler = null;
|
---|
91 | if (index !== -1) {
|
---|
92 | actions.splice(index, 1);
|
---|
93 | }
|
---|
94 | if (id != null) {
|
---|
95 | this.id = this.recycleAsyncId(scheduler, id, null);
|
---|
96 | }
|
---|
97 | this.delay = null;
|
---|
98 | };
|
---|
99 | return AsyncAction;
|
---|
100 | }(Action_1.Action));
|
---|
101 | exports.AsyncAction = AsyncAction;
|
---|
102 | //# sourceMappingURL=AsyncAction.js.map |
---|