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 7.4KB

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