3ef9a907a065601c7a7bab565398291e1a677b3f060dc41ecee040208c399c9e7fc35c502d1f4aa811e8021955b246f9965f3e98ff0d50e4dfc5290849b45f 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Subscription } from '../Subscription';
  2. import { SchedulerAction, SchedulerLike } from '../types';
  3. export function executeSchedule(
  4. parentSubscription: Subscription,
  5. scheduler: SchedulerLike,
  6. work: () => void,
  7. delay: number,
  8. repeat: true
  9. ): void;
  10. export function executeSchedule(
  11. parentSubscription: Subscription,
  12. scheduler: SchedulerLike,
  13. work: () => void,
  14. delay?: number,
  15. repeat?: false
  16. ): Subscription;
  17. export function executeSchedule(
  18. parentSubscription: Subscription,
  19. scheduler: SchedulerLike,
  20. work: () => void,
  21. delay = 0,
  22. repeat = false
  23. ): Subscription | void {
  24. const scheduleSubscription = scheduler.schedule(function (this: SchedulerAction<any>) {
  25. work();
  26. if (repeat) {
  27. parentSubscription.add(this.schedule(null, delay));
  28. } else {
  29. this.unsubscribe();
  30. }
  31. }, delay);
  32. parentSubscription.add(scheduleSubscription);
  33. if (!repeat) {
  34. // Because user-land scheduler implementations are unlikely to properly reuse
  35. // Actions for repeat scheduling, we can't trust that the returned subscription
  36. // will control repeat subscription scenarios. So we're trying to avoid using them
  37. // incorrectly within this library.
  38. return scheduleSubscription;
  39. }
  40. }