[6a3a178] | 1 | import { Action } from './Action';
|
---|
| 2 | export class AsyncAction extends Action {
|
---|
| 3 | constructor(scheduler, work) {
|
---|
| 4 | super(scheduler, work);
|
---|
| 5 | this.scheduler = scheduler;
|
---|
| 6 | this.work = work;
|
---|
| 7 | this.pending = false;
|
---|
| 8 | }
|
---|
| 9 | schedule(state, delay = 0) {
|
---|
| 10 | if (this.closed) {
|
---|
| 11 | return this;
|
---|
| 12 | }
|
---|
| 13 | this.state = state;
|
---|
| 14 | const id = this.id;
|
---|
| 15 | const scheduler = this.scheduler;
|
---|
| 16 | if (id != null) {
|
---|
| 17 | this.id = this.recycleAsyncId(scheduler, id, delay);
|
---|
| 18 | }
|
---|
| 19 | this.pending = true;
|
---|
| 20 | this.delay = delay;
|
---|
| 21 | this.id = this.id || this.requestAsyncId(scheduler, this.id, delay);
|
---|
| 22 | return this;
|
---|
| 23 | }
|
---|
| 24 | requestAsyncId(scheduler, id, delay = 0) {
|
---|
| 25 | return setInterval(scheduler.flush.bind(scheduler, this), delay);
|
---|
| 26 | }
|
---|
| 27 | recycleAsyncId(scheduler, id, delay = 0) {
|
---|
| 28 | if (delay !== null && this.delay === delay && this.pending === false) {
|
---|
| 29 | return id;
|
---|
| 30 | }
|
---|
| 31 | clearInterval(id);
|
---|
| 32 | return undefined;
|
---|
| 33 | }
|
---|
| 34 | execute(state, delay) {
|
---|
| 35 | if (this.closed) {
|
---|
| 36 | return new Error('executing a cancelled action');
|
---|
| 37 | }
|
---|
| 38 | this.pending = false;
|
---|
| 39 | const error = this._execute(state, delay);
|
---|
| 40 | if (error) {
|
---|
| 41 | return error;
|
---|
| 42 | }
|
---|
| 43 | else if (this.pending === false && this.id != null) {
|
---|
| 44 | this.id = this.recycleAsyncId(this.scheduler, this.id, null);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | _execute(state, delay) {
|
---|
| 48 | let errored = false;
|
---|
| 49 | let errorValue = undefined;
|
---|
| 50 | try {
|
---|
| 51 | this.work(state);
|
---|
| 52 | }
|
---|
| 53 | catch (e) {
|
---|
| 54 | errored = true;
|
---|
| 55 | errorValue = !!e && e || new Error(e);
|
---|
| 56 | }
|
---|
| 57 | if (errored) {
|
---|
| 58 | this.unsubscribe();
|
---|
| 59 | return errorValue;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | _unsubscribe() {
|
---|
| 63 | const id = this.id;
|
---|
| 64 | const scheduler = this.scheduler;
|
---|
| 65 | const actions = scheduler.actions;
|
---|
| 66 | const index = actions.indexOf(this);
|
---|
| 67 | this.work = null;
|
---|
| 68 | this.state = null;
|
---|
| 69 | this.pending = false;
|
---|
| 70 | this.scheduler = null;
|
---|
| 71 | if (index !== -1) {
|
---|
| 72 | actions.splice(index, 1);
|
---|
| 73 | }
|
---|
| 74 | if (id != null) {
|
---|
| 75 | this.id = this.recycleAsyncId(scheduler, id, null);
|
---|
| 76 | }
|
---|
| 77 | this.delay = null;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | //# sourceMappingURL=AsyncAction.js.map |
---|