Scheduler.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * An execution context and a data structure to order tasks and schedule their
  3. * execution. Provides a notion of (potentially virtual) time, through the
  4. * `now()` getter method.
  5. *
  6. * Each unit of work in a Scheduler is called an {@link Action}.
  7. *
  8. * ```ts
  9. * class Scheduler {
  10. * now(): number;
  11. * schedule(work, delay?, state?): Subscription;
  12. * }
  13. * ```
  14. *
  15. * @class Scheduler
  16. */
  17. export class Scheduler {
  18. constructor(SchedulerAction, now = Scheduler.now) {
  19. this.SchedulerAction = SchedulerAction;
  20. this.now = now;
  21. }
  22. /**
  23. * Schedules a function, `work`, for execution. May happen at some point in
  24. * the future, according to the `delay` parameter, if specified. May be passed
  25. * some context object, `state`, which will be passed to the `work` function.
  26. *
  27. * The given arguments will be processed an stored as an Action object in a
  28. * queue of actions.
  29. *
  30. * @param {function(state: ?T): ?Subscription} work A function representing a
  31. * task, or some unit of work to be executed by the Scheduler.
  32. * @param {number} [delay] Time to wait before executing the work, where the
  33. * time unit is implicit and defined by the Scheduler itself.
  34. * @param {T} [state] Some contextual data that the `work` function uses when
  35. * called by the Scheduler.
  36. * @return {Subscription} A subscription in order to be able to unsubscribe
  37. * the scheduled work.
  38. */
  39. schedule(work, delay = 0, state) {
  40. return new this.SchedulerAction(this, work).schedule(state, delay);
  41. }
  42. }
  43. Scheduler.now = Date.now ? Date.now : () => +new Date();
  44. //# sourceMappingURL=Scheduler.js.map