ae659a3bd5a3c2b3f2dff24d3bed45a883fcfa83aeb37972d85103860fd86de0c835cd14b37fc68eceb74dbd1f30be2df3400f243596bdec37e3d204b294fc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Action } from './scheduler/Action';
  2. import { Subscription } from './Subscription';
  3. import { SchedulerLike, SchedulerAction } from './types';
  4. import { dateTimestampProvider } from './scheduler/dateTimestampProvider';
  5. /**
  6. * An execution context and a data structure to order tasks and schedule their
  7. * execution. Provides a notion of (potentially virtual) time, through the
  8. * `now()` getter method.
  9. *
  10. * Each unit of work in a Scheduler is called an `Action`.
  11. *
  12. * ```ts
  13. * class Scheduler {
  14. * now(): number;
  15. * schedule(work, delay?, state?): Subscription;
  16. * }
  17. * ```
  18. *
  19. * @deprecated Scheduler is an internal implementation detail of RxJS, and
  20. * should not be used directly. Rather, create your own class and implement
  21. * {@link SchedulerLike}. Will be made internal in v8.
  22. */
  23. export class Scheduler implements SchedulerLike {
  24. public static now: () => number = dateTimestampProvider.now;
  25. constructor(private schedulerActionCtor: typeof Action, now: () => number = Scheduler.now) {
  26. this.now = now;
  27. }
  28. /**
  29. * A getter method that returns a number representing the current time
  30. * (at the time this function was called) according to the scheduler's own
  31. * internal clock.
  32. * @return A number that represents the current time. May or may not
  33. * have a relation to wall-clock time. May or may not refer to a time unit
  34. * (e.g. milliseconds).
  35. */
  36. public now: () => number;
  37. /**
  38. * Schedules a function, `work`, for execution. May happen at some point in
  39. * the future, according to the `delay` parameter, if specified. May be passed
  40. * some context object, `state`, which will be passed to the `work` function.
  41. *
  42. * The given arguments will be processed an stored as an Action object in a
  43. * queue of actions.
  44. *
  45. * @param work A function representing a task, or some unit of work to be
  46. * executed by the Scheduler.
  47. * @param delay Time to wait before executing the work, where the time unit is
  48. * implicit and defined by the Scheduler itself.
  49. * @param state Some contextual data that the `work` function uses when called
  50. * by the Scheduler.
  51. * @return A subscription in order to be able to unsubscribe the scheduled work.
  52. */
  53. public schedule<T>(work: (this: SchedulerAction<T>, state?: T) => void, delay: number = 0, state?: T): Subscription {
  54. return new this.schedulerActionCtor<T>(this, work).schedule(state, delay);
  55. }
  56. }