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