| 1 |
import { AsyncAction } from './AsyncAction'; |
| 2 |
import { Subscription } from '../Subscription'; |
| 3 |
import { QueueScheduler } from './QueueScheduler'; |
| 4 |
import { SchedulerAction } from '../types'; |
| 5 |
import { TimerHandle } from './timerHandle'; |
| 6 |
|
| 7 |
export class QueueAction<T> extends AsyncAction<T> { |
| 8 |
constructor(protected scheduler: QueueScheduler, protected work: (this: SchedulerAction<T>, state?: T) => void) { |
| 9 |
super(scheduler, work); |
| 10 |
} |
| 11 |
|
| 12 |
public schedule(state?: T, delay: number = 0): Subscription { |
| 13 |
if (delay > 0) { |
| 14 |
return super.schedule(state, delay); |
| 15 |
} |
| 16 |
this.delay = delay; |
| 17 |
this.state = state; |
| 18 |
this.scheduler.flush(this); |
| 19 |
return this; |
| 20 |
} |
| 21 |
|
| 22 |
public execute(state: T, delay: number): any { |
| 23 |
return delay > 0 || this.closed ? super.execute(state, delay) : this._execute(state, delay); |
| 24 |
} |
| 25 |
|
| 26 |
protected requestAsyncId(scheduler: QueueScheduler, id?: TimerHandle, delay: number = 0): TimerHandle { |
| 27 |
// If delay exists and is greater than 0, or if the delay is null (the |
| 28 |
// action wasn't rescheduled) but was originally scheduled as an async |
| 29 |
// action, then recycle as an async action. |
| 30 |
|
| 31 |
if ((delay != null && delay > 0) || (delay == null && this.delay > 0)) { |
| 32 |
return super.requestAsyncId(scheduler, id, delay); |
| 33 |
} |
| 34 |
|
| 35 |
// Otherwise flush the scheduler starting with this action. |
| 36 |
scheduler.flush(this); |
| 37 |
|
| 38 |
// HACK: In the past, this was returning `void`. However, `void` isn't a valid |
| 39 |
// `TimerHandle`, and generally the return value here isn't really used. So the |
| 40 |
// compromise is to return `0` which is both "falsy" and a valid `TimerHandle`, |
| 41 |
// as opposed to refactoring every other instanceo of `requestAsyncId`. |
| 42 |
return 0; |
| 43 |
} |
| 44 |
} |