Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

delay.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var async_1 = require("../scheduler/async");
  17. var isDate_1 = require("../util/isDate");
  18. var Subscriber_1 = require("../Subscriber");
  19. var Notification_1 = require("../Notification");
  20. function delay(delay, scheduler) {
  21. if (scheduler === void 0) { scheduler = async_1.async; }
  22. var absoluteDelay = isDate_1.isDate(delay);
  23. var delayFor = absoluteDelay ? (+delay - scheduler.now()) : Math.abs(delay);
  24. return function (source) { return source.lift(new DelayOperator(delayFor, scheduler)); };
  25. }
  26. exports.delay = delay;
  27. var DelayOperator = (function () {
  28. function DelayOperator(delay, scheduler) {
  29. this.delay = delay;
  30. this.scheduler = scheduler;
  31. }
  32. DelayOperator.prototype.call = function (subscriber, source) {
  33. return source.subscribe(new DelaySubscriber(subscriber, this.delay, this.scheduler));
  34. };
  35. return DelayOperator;
  36. }());
  37. var DelaySubscriber = (function (_super) {
  38. __extends(DelaySubscriber, _super);
  39. function DelaySubscriber(destination, delay, scheduler) {
  40. var _this = _super.call(this, destination) || this;
  41. _this.delay = delay;
  42. _this.scheduler = scheduler;
  43. _this.queue = [];
  44. _this.active = false;
  45. _this.errored = false;
  46. return _this;
  47. }
  48. DelaySubscriber.dispatch = function (state) {
  49. var source = state.source;
  50. var queue = source.queue;
  51. var scheduler = state.scheduler;
  52. var destination = state.destination;
  53. while (queue.length > 0 && (queue[0].time - scheduler.now()) <= 0) {
  54. queue.shift().notification.observe(destination);
  55. }
  56. if (queue.length > 0) {
  57. var delay_1 = Math.max(0, queue[0].time - scheduler.now());
  58. this.schedule(state, delay_1);
  59. }
  60. else {
  61. this.unsubscribe();
  62. source.active = false;
  63. }
  64. };
  65. DelaySubscriber.prototype._schedule = function (scheduler) {
  66. this.active = true;
  67. var destination = this.destination;
  68. destination.add(scheduler.schedule(DelaySubscriber.dispatch, this.delay, {
  69. source: this, destination: this.destination, scheduler: scheduler
  70. }));
  71. };
  72. DelaySubscriber.prototype.scheduleNotification = function (notification) {
  73. if (this.errored === true) {
  74. return;
  75. }
  76. var scheduler = this.scheduler;
  77. var message = new DelayMessage(scheduler.now() + this.delay, notification);
  78. this.queue.push(message);
  79. if (this.active === false) {
  80. this._schedule(scheduler);
  81. }
  82. };
  83. DelaySubscriber.prototype._next = function (value) {
  84. this.scheduleNotification(Notification_1.Notification.createNext(value));
  85. };
  86. DelaySubscriber.prototype._error = function (err) {
  87. this.errored = true;
  88. this.queue = [];
  89. this.destination.error(err);
  90. this.unsubscribe();
  91. };
  92. DelaySubscriber.prototype._complete = function () {
  93. this.scheduleNotification(Notification_1.Notification.createComplete());
  94. this.unsubscribe();
  95. };
  96. return DelaySubscriber;
  97. }(Subscriber_1.Subscriber));
  98. var DelayMessage = (function () {
  99. function DelayMessage(time, notification) {
  100. this.time = time;
  101. this.notification = notification;
  102. }
  103. return DelayMessage;
  104. }());
  105. //# sourceMappingURL=delay.js.map