timer.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.timer = void 0;
  4. var Observable_1 = require("../Observable");
  5. var async_1 = require("../scheduler/async");
  6. var isScheduler_1 = require("../util/isScheduler");
  7. var isDate_1 = require("../util/isDate");
  8. function timer(dueTime, intervalOrScheduler, scheduler) {
  9. if (dueTime === void 0) { dueTime = 0; }
  10. if (scheduler === void 0) { scheduler = async_1.async; }
  11. var intervalDuration = -1;
  12. if (intervalOrScheduler != null) {
  13. if (isScheduler_1.isScheduler(intervalOrScheduler)) {
  14. scheduler = intervalOrScheduler;
  15. }
  16. else {
  17. intervalDuration = intervalOrScheduler;
  18. }
  19. }
  20. return new Observable_1.Observable(function (subscriber) {
  21. var due = isDate_1.isValidDate(dueTime) ? +dueTime - scheduler.now() : dueTime;
  22. if (due < 0) {
  23. due = 0;
  24. }
  25. var n = 0;
  26. return scheduler.schedule(function () {
  27. if (!subscriber.closed) {
  28. subscriber.next(n++);
  29. if (0 <= intervalDuration) {
  30. this.schedule(undefined, intervalDuration);
  31. }
  32. else {
  33. subscriber.complete();
  34. }
  35. }
  36. }, due);
  37. });
  38. }
  39. exports.timer = timer;
  40. //# sourceMappingURL=timer.js.map