timer.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { Observable } from '../Observable';
  2. import { async as asyncScheduler } from '../scheduler/async';
  3. import { isScheduler } from '../util/isScheduler';
  4. import { isValidDate } from '../util/isDate';
  5. export function timer(dueTime = 0, intervalOrScheduler, scheduler = asyncScheduler) {
  6. let intervalDuration = -1;
  7. if (intervalOrScheduler != null) {
  8. if (isScheduler(intervalOrScheduler)) {
  9. scheduler = intervalOrScheduler;
  10. }
  11. else {
  12. intervalDuration = intervalOrScheduler;
  13. }
  14. }
  15. return new Observable((subscriber) => {
  16. let due = isValidDate(dueTime) ? +dueTime - scheduler.now() : dueTime;
  17. if (due < 0) {
  18. due = 0;
  19. }
  20. let n = 0;
  21. return scheduler.schedule(function () {
  22. if (!subscriber.closed) {
  23. subscriber.next(n++);
  24. if (0 <= intervalDuration) {
  25. this.schedule(undefined, intervalDuration);
  26. }
  27. else {
  28. subscriber.complete();
  29. }
  30. }
  31. }, due);
  32. });
  33. }
  34. //# sourceMappingURL=timer.js.map