| 1 |
import { AsyncAction } from './AsyncAction'; |
| 2 |
import { AsyncScheduler } from './AsyncScheduler'; |
| 3 |
|
| 4 |
export class AsapScheduler extends AsyncScheduler { |
| 5 |
public flush(action?: AsyncAction<any>): void { |
| 6 |
this._active = true; |
| 7 |
// The async id that effects a call to flush is stored in _scheduled. |
| 8 |
// Before executing an action, it's necessary to check the action's async |
| 9 |
// id to determine whether it's supposed to be executed in the current |
| 10 |
// flush. |
| 11 |
// Previous implementations of this method used a count to determine this, |
| 12 |
// but that was unsound, as actions that are unsubscribed - i.e. cancelled - |
| 13 |
// are removed from the actions array and that can shift actions that are |
| 14 |
// scheduled to be executed in a subsequent flush into positions at which |
| 15 |
// they are executed within the current flush. |
| 16 |
const flushId = this._scheduled; |
| 17 |
this._scheduled = undefined; |
| 18 |
|
| 19 |
const { actions } = this; |
| 20 |
let error: any; |
| 21 |
action = action || actions.shift()!; |
| 22 |
|
| 23 |
do { |
| 24 |
if ((error = action.execute(action.state, action.delay))) { |
| 25 |
break; |
| 26 |
} |
| 27 |
} while ((action = actions[0]) && action.id === flushId && actions.shift()); |
| 28 |
|
| 29 |
this._active = false; |
| 30 |
|
| 31 |
if (error) { |
| 32 |
while ((action = actions[0]) && action.id === flushId && actions.shift()) { |
| 33 |
action.unsubscribe(); |
| 34 |
} |
| 35 |
throw error; |
| 36 |
} |
| 37 |
} |
| 38 |
} |