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

zip.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { fromArray } from './fromArray';
  2. import { isArray } from '../util/isArray';
  3. import { Subscriber } from '../Subscriber';
  4. import { iterator as Symbol_iterator } from '../../internal/symbol/iterator';
  5. import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
  6. export function zip(...observables) {
  7. const resultSelector = observables[observables.length - 1];
  8. if (typeof resultSelector === 'function') {
  9. observables.pop();
  10. }
  11. return fromArray(observables, undefined).lift(new ZipOperator(resultSelector));
  12. }
  13. export class ZipOperator {
  14. constructor(resultSelector) {
  15. this.resultSelector = resultSelector;
  16. }
  17. call(subscriber, source) {
  18. return source.subscribe(new ZipSubscriber(subscriber, this.resultSelector));
  19. }
  20. }
  21. export class ZipSubscriber extends Subscriber {
  22. constructor(destination, resultSelector, values = Object.create(null)) {
  23. super(destination);
  24. this.resultSelector = resultSelector;
  25. this.iterators = [];
  26. this.active = 0;
  27. this.resultSelector = (typeof resultSelector === 'function') ? resultSelector : undefined;
  28. }
  29. _next(value) {
  30. const iterators = this.iterators;
  31. if (isArray(value)) {
  32. iterators.push(new StaticArrayIterator(value));
  33. }
  34. else if (typeof value[Symbol_iterator] === 'function') {
  35. iterators.push(new StaticIterator(value[Symbol_iterator]()));
  36. }
  37. else {
  38. iterators.push(new ZipBufferIterator(this.destination, this, value));
  39. }
  40. }
  41. _complete() {
  42. const iterators = this.iterators;
  43. const len = iterators.length;
  44. this.unsubscribe();
  45. if (len === 0) {
  46. this.destination.complete();
  47. return;
  48. }
  49. this.active = len;
  50. for (let i = 0; i < len; i++) {
  51. let iterator = iterators[i];
  52. if (iterator.stillUnsubscribed) {
  53. const destination = this.destination;
  54. destination.add(iterator.subscribe());
  55. }
  56. else {
  57. this.active--;
  58. }
  59. }
  60. }
  61. notifyInactive() {
  62. this.active--;
  63. if (this.active === 0) {
  64. this.destination.complete();
  65. }
  66. }
  67. checkIterators() {
  68. const iterators = this.iterators;
  69. const len = iterators.length;
  70. const destination = this.destination;
  71. for (let i = 0; i < len; i++) {
  72. let iterator = iterators[i];
  73. if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
  74. return;
  75. }
  76. }
  77. let shouldComplete = false;
  78. const args = [];
  79. for (let i = 0; i < len; i++) {
  80. let iterator = iterators[i];
  81. let result = iterator.next();
  82. if (iterator.hasCompleted()) {
  83. shouldComplete = true;
  84. }
  85. if (result.done) {
  86. destination.complete();
  87. return;
  88. }
  89. args.push(result.value);
  90. }
  91. if (this.resultSelector) {
  92. this._tryresultSelector(args);
  93. }
  94. else {
  95. destination.next(args);
  96. }
  97. if (shouldComplete) {
  98. destination.complete();
  99. }
  100. }
  101. _tryresultSelector(args) {
  102. let result;
  103. try {
  104. result = this.resultSelector.apply(this, args);
  105. }
  106. catch (err) {
  107. this.destination.error(err);
  108. return;
  109. }
  110. this.destination.next(result);
  111. }
  112. }
  113. class StaticIterator {
  114. constructor(iterator) {
  115. this.iterator = iterator;
  116. this.nextResult = iterator.next();
  117. }
  118. hasValue() {
  119. return true;
  120. }
  121. next() {
  122. const result = this.nextResult;
  123. this.nextResult = this.iterator.next();
  124. return result;
  125. }
  126. hasCompleted() {
  127. const nextResult = this.nextResult;
  128. return Boolean(nextResult && nextResult.done);
  129. }
  130. }
  131. class StaticArrayIterator {
  132. constructor(array) {
  133. this.array = array;
  134. this.index = 0;
  135. this.length = 0;
  136. this.length = array.length;
  137. }
  138. [Symbol_iterator]() {
  139. return this;
  140. }
  141. next(value) {
  142. const i = this.index++;
  143. const array = this.array;
  144. return i < this.length ? { value: array[i], done: false } : { value: null, done: true };
  145. }
  146. hasValue() {
  147. return this.array.length > this.index;
  148. }
  149. hasCompleted() {
  150. return this.array.length === this.index;
  151. }
  152. }
  153. class ZipBufferIterator extends SimpleOuterSubscriber {
  154. constructor(destination, parent, observable) {
  155. super(destination);
  156. this.parent = parent;
  157. this.observable = observable;
  158. this.stillUnsubscribed = true;
  159. this.buffer = [];
  160. this.isComplete = false;
  161. }
  162. [Symbol_iterator]() {
  163. return this;
  164. }
  165. next() {
  166. const buffer = this.buffer;
  167. if (buffer.length === 0 && this.isComplete) {
  168. return { value: null, done: true };
  169. }
  170. else {
  171. return { value: buffer.shift(), done: false };
  172. }
  173. }
  174. hasValue() {
  175. return this.buffer.length > 0;
  176. }
  177. hasCompleted() {
  178. return this.buffer.length === 0 && this.isComplete;
  179. }
  180. notifyComplete() {
  181. if (this.buffer.length > 0) {
  182. this.isComplete = true;
  183. this.parent.notifyInactive();
  184. }
  185. else {
  186. this.destination.complete();
  187. }
  188. }
  189. notifyNext(innerValue) {
  190. this.buffer.push(innerValue);
  191. this.parent.checkIterators();
  192. }
  193. subscribe() {
  194. return innerSubscribe(this.observable, new SimpleInnerSubscriber(this));
  195. }
  196. }
  197. //# sourceMappingURL=zip.js.map