1 | /** PURE_IMPORTS_START tslib,_Action PURE_IMPORTS_END */
|
---|
2 | import * as tslib_1 from "tslib";
|
---|
3 | import { Action } from './Action';
|
---|
4 | var AsyncAction = /*@__PURE__*/ (function (_super) {
|
---|
5 | tslib_1.__extends(AsyncAction, _super);
|
---|
6 | function AsyncAction(scheduler, work) {
|
---|
7 | var _this = _super.call(this, scheduler, work) || this;
|
---|
8 | _this.scheduler = scheduler;
|
---|
9 | _this.work = work;
|
---|
10 | _this.pending = false;
|
---|
11 | return _this;
|
---|
12 | }
|
---|
13 | AsyncAction.prototype.schedule = function (state, delay) {
|
---|
14 | if (delay === void 0) {
|
---|
15 | delay = 0;
|
---|
16 | }
|
---|
17 | if (this.closed) {
|
---|
18 | return this;
|
---|
19 | }
|
---|
20 | this.state = state;
|
---|
21 | var id = this.id;
|
---|
22 | var scheduler = this.scheduler;
|
---|
23 | if (id != null) {
|
---|
24 | this.id = this.recycleAsyncId(scheduler, id, delay);
|
---|
25 | }
|
---|
26 | this.pending = true;
|
---|
27 | this.delay = delay;
|
---|
28 | this.id = this.id || this.requestAsyncId(scheduler, this.id, delay);
|
---|
29 | return this;
|
---|
30 | };
|
---|
31 | AsyncAction.prototype.requestAsyncId = function (scheduler, id, delay) {
|
---|
32 | if (delay === void 0) {
|
---|
33 | delay = 0;
|
---|
34 | }
|
---|
35 | return setInterval(scheduler.flush.bind(scheduler, this), delay);
|
---|
36 | };
|
---|
37 | AsyncAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
|
---|
38 | if (delay === void 0) {
|
---|
39 | delay = 0;
|
---|
40 | }
|
---|
41 | if (delay !== null && this.delay === delay && this.pending === false) {
|
---|
42 | return id;
|
---|
43 | }
|
---|
44 | clearInterval(id);
|
---|
45 | return undefined;
|
---|
46 | };
|
---|
47 | AsyncAction.prototype.execute = function (state, delay) {
|
---|
48 | if (this.closed) {
|
---|
49 | return new Error('executing a cancelled action');
|
---|
50 | }
|
---|
51 | this.pending = false;
|
---|
52 | var error = this._execute(state, delay);
|
---|
53 | if (error) {
|
---|
54 | return error;
|
---|
55 | }
|
---|
56 | else if (this.pending === false && this.id != null) {
|
---|
57 | this.id = this.recycleAsyncId(this.scheduler, this.id, null);
|
---|
58 | }
|
---|
59 | };
|
---|
60 | AsyncAction.prototype._execute = function (state, delay) {
|
---|
61 | var errored = false;
|
---|
62 | var errorValue = undefined;
|
---|
63 | try {
|
---|
64 | this.work(state);
|
---|
65 | }
|
---|
66 | catch (e) {
|
---|
67 | errored = true;
|
---|
68 | errorValue = !!e && e || new Error(e);
|
---|
69 | }
|
---|
70 | if (errored) {
|
---|
71 | this.unsubscribe();
|
---|
72 | return errorValue;
|
---|
73 | }
|
---|
74 | };
|
---|
75 | AsyncAction.prototype._unsubscribe = function () {
|
---|
76 | var id = this.id;
|
---|
77 | var scheduler = this.scheduler;
|
---|
78 | var actions = scheduler.actions;
|
---|
79 | var index = actions.indexOf(this);
|
---|
80 | this.work = null;
|
---|
81 | this.state = null;
|
---|
82 | this.pending = false;
|
---|
83 | this.scheduler = null;
|
---|
84 | if (index !== -1) {
|
---|
85 | actions.splice(index, 1);
|
---|
86 | }
|
---|
87 | if (id != null) {
|
---|
88 | this.id = this.recycleAsyncId(scheduler, id, null);
|
---|
89 | }
|
---|
90 | this.delay = null;
|
---|
91 | };
|
---|
92 | return AsyncAction;
|
---|
93 | }(Action));
|
---|
94 | export { AsyncAction };
|
---|
95 | //# sourceMappingURL=AsyncAction.js.map
|
---|