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

windowTime.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Subject } from '../Subject';
  2. import { async } from '../scheduler/async';
  3. import { Subscriber } from '../Subscriber';
  4. import { isNumeric } from '../util/isNumeric';
  5. import { isScheduler } from '../util/isScheduler';
  6. export function windowTime(windowTimeSpan) {
  7. let scheduler = async;
  8. let windowCreationInterval = null;
  9. let maxWindowSize = Number.POSITIVE_INFINITY;
  10. if (isScheduler(arguments[3])) {
  11. scheduler = arguments[3];
  12. }
  13. if (isScheduler(arguments[2])) {
  14. scheduler = arguments[2];
  15. }
  16. else if (isNumeric(arguments[2])) {
  17. maxWindowSize = Number(arguments[2]);
  18. }
  19. if (isScheduler(arguments[1])) {
  20. scheduler = arguments[1];
  21. }
  22. else if (isNumeric(arguments[1])) {
  23. windowCreationInterval = Number(arguments[1]);
  24. }
  25. return function windowTimeOperatorFunction(source) {
  26. return source.lift(new WindowTimeOperator(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler));
  27. };
  28. }
  29. class WindowTimeOperator {
  30. constructor(windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler) {
  31. this.windowTimeSpan = windowTimeSpan;
  32. this.windowCreationInterval = windowCreationInterval;
  33. this.maxWindowSize = maxWindowSize;
  34. this.scheduler = scheduler;
  35. }
  36. call(subscriber, source) {
  37. return source.subscribe(new WindowTimeSubscriber(subscriber, this.windowTimeSpan, this.windowCreationInterval, this.maxWindowSize, this.scheduler));
  38. }
  39. }
  40. class CountedSubject extends Subject {
  41. constructor() {
  42. super(...arguments);
  43. this._numberOfNextedValues = 0;
  44. }
  45. next(value) {
  46. this._numberOfNextedValues++;
  47. super.next(value);
  48. }
  49. get numberOfNextedValues() {
  50. return this._numberOfNextedValues;
  51. }
  52. }
  53. class WindowTimeSubscriber extends Subscriber {
  54. constructor(destination, windowTimeSpan, windowCreationInterval, maxWindowSize, scheduler) {
  55. super(destination);
  56. this.destination = destination;
  57. this.windowTimeSpan = windowTimeSpan;
  58. this.windowCreationInterval = windowCreationInterval;
  59. this.maxWindowSize = maxWindowSize;
  60. this.scheduler = scheduler;
  61. this.windows = [];
  62. const window = this.openWindow();
  63. if (windowCreationInterval !== null && windowCreationInterval >= 0) {
  64. const closeState = { subscriber: this, window, context: null };
  65. const creationState = { windowTimeSpan, windowCreationInterval, subscriber: this, scheduler };
  66. this.add(scheduler.schedule(dispatchWindowClose, windowTimeSpan, closeState));
  67. this.add(scheduler.schedule(dispatchWindowCreation, windowCreationInterval, creationState));
  68. }
  69. else {
  70. const timeSpanOnlyState = { subscriber: this, window, windowTimeSpan };
  71. this.add(scheduler.schedule(dispatchWindowTimeSpanOnly, windowTimeSpan, timeSpanOnlyState));
  72. }
  73. }
  74. _next(value) {
  75. const windows = this.windows;
  76. const len = windows.length;
  77. for (let i = 0; i < len; i++) {
  78. const window = windows[i];
  79. if (!window.closed) {
  80. window.next(value);
  81. if (window.numberOfNextedValues >= this.maxWindowSize) {
  82. this.closeWindow(window);
  83. }
  84. }
  85. }
  86. }
  87. _error(err) {
  88. const windows = this.windows;
  89. while (windows.length > 0) {
  90. windows.shift().error(err);
  91. }
  92. this.destination.error(err);
  93. }
  94. _complete() {
  95. const windows = this.windows;
  96. while (windows.length > 0) {
  97. const window = windows.shift();
  98. if (!window.closed) {
  99. window.complete();
  100. }
  101. }
  102. this.destination.complete();
  103. }
  104. openWindow() {
  105. const window = new CountedSubject();
  106. this.windows.push(window);
  107. const destination = this.destination;
  108. destination.next(window);
  109. return window;
  110. }
  111. closeWindow(window) {
  112. window.complete();
  113. const windows = this.windows;
  114. windows.splice(windows.indexOf(window), 1);
  115. }
  116. }
  117. function dispatchWindowTimeSpanOnly(state) {
  118. const { subscriber, windowTimeSpan, window } = state;
  119. if (window) {
  120. subscriber.closeWindow(window);
  121. }
  122. state.window = subscriber.openWindow();
  123. this.schedule(state, windowTimeSpan);
  124. }
  125. function dispatchWindowCreation(state) {
  126. const { windowTimeSpan, subscriber, scheduler, windowCreationInterval } = state;
  127. const window = subscriber.openWindow();
  128. const action = this;
  129. let context = { action, subscription: null };
  130. const timeSpanState = { subscriber, window, context };
  131. context.subscription = scheduler.schedule(dispatchWindowClose, windowTimeSpan, timeSpanState);
  132. action.add(context.subscription);
  133. action.schedule(state, windowCreationInterval);
  134. }
  135. function dispatchWindowClose(state) {
  136. const { subscriber, window, context } = state;
  137. if (context && context.action && context.subscription) {
  138. context.action.remove(context.subscription);
  139. }
  140. subscriber.closeWindow(window);
  141. }
  142. //# sourceMappingURL=windowTime.js.map