timer.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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, intervalOrScheduler, scheduler) {
  6. if (dueTime === void 0) { dueTime = 0; }
  7. if (scheduler === void 0) { scheduler = asyncScheduler; }
  8. var intervalDuration = -1;
  9. if (intervalOrScheduler != null) {
  10. if (isScheduler(intervalOrScheduler)) {
  11. scheduler = intervalOrScheduler;
  12. }
  13. else {
  14. intervalDuration = intervalOrScheduler;
  15. }
  16. }
  17. return new Observable(function (subscriber) {
  18. var due = isValidDate(dueTime) ? +dueTime - scheduler.now() : dueTime;
  19. if (due < 0) {
  20. due = 0;
  21. }
  22. var n = 0;
  23. return scheduler.schedule(function () {
  24. if (!subscriber.closed) {
  25. subscriber.next(n++);
  26. if (0 <= intervalDuration) {
  27. this.schedule(undefined, intervalDuration);
  28. }
  29. else {
  30. subscriber.complete();
  31. }
  32. }
  33. }, due);
  34. });
  35. }
  36. //# sourceMappingURL=timer.js.map