31c70d876eb4e401f0186e7fc82f6aa3734e1c88ebdcc09e81378bc396f13df279f0dc23150028e59594829aed8679b353a220ec9aae47ceeedba34777079b 1.7 KB

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