Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zip.js 5.7KB

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