f9a85ffce985e87d0bd69b102ab5c78801918eebd207d6bf9e9920768bf49189a462c55359cc19105399f56a788b182856caffcdc3cd4a7435b9c171012600 1.3 KB

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