UNPKG

88 kBJavaScriptView Raw
1/*! Kefir.js v3.8.8
2 * https://github.com/kefirjs/kefir
3 */
4
5function createObj(proto) {
6 var F = function () {};
7 F.prototype = proto;
8 return new F();
9}
10
11function extend(target /*, mixin1, mixin2...*/) {
12 var length = arguments.length,
13 i = void 0,
14 prop = void 0;
15 for (i = 1; i < length; i++) {
16 for (prop in arguments[i]) {
17 target[prop] = arguments[i][prop];
18 }
19 }
20 return target;
21}
22
23function inherit(Child, Parent /*, mixin1, mixin2...*/) {
24 var length = arguments.length,
25 i = void 0;
26 Child.prototype = createObj(Parent.prototype);
27 Child.prototype.constructor = Child;
28 for (i = 2; i < length; i++) {
29 extend(Child.prototype, arguments[i]);
30 }
31 return Child;
32}
33
34var NOTHING = ['<nothing>'];
35var END = 'end';
36var VALUE = 'value';
37var ERROR = 'error';
38var ANY = 'any';
39
40function concat(a, b) {
41 var result = void 0,
42 length = void 0,
43 i = void 0,
44 j = void 0;
45 if (a.length === 0) {
46 return b;
47 }
48 if (b.length === 0) {
49 return a;
50 }
51 j = 0;
52 result = new Array(a.length + b.length);
53 length = a.length;
54 for (i = 0; i < length; i++, j++) {
55 result[j] = a[i];
56 }
57 length = b.length;
58 for (i = 0; i < length; i++, j++) {
59 result[j] = b[i];
60 }
61 return result;
62}
63
64function find(arr, value) {
65 var length = arr.length,
66 i = void 0;
67 for (i = 0; i < length; i++) {
68 if (arr[i] === value) {
69 return i;
70 }
71 }
72 return -1;
73}
74
75function findByPred(arr, pred) {
76 var length = arr.length,
77 i = void 0;
78 for (i = 0; i < length; i++) {
79 if (pred(arr[i])) {
80 return i;
81 }
82 }
83 return -1;
84}
85
86function cloneArray(input) {
87 var length = input.length,
88 result = new Array(length),
89 i = void 0;
90 for (i = 0; i < length; i++) {
91 result[i] = input[i];
92 }
93 return result;
94}
95
96function remove(input, index) {
97 var length = input.length,
98 result = void 0,
99 i = void 0,
100 j = void 0;
101 if (index >= 0 && index < length) {
102 if (length === 1) {
103 return [];
104 } else {
105 result = new Array(length - 1);
106 for (i = 0, j = 0; i < length; i++) {
107 if (i !== index) {
108 result[j] = input[i];
109 j++;
110 }
111 }
112 return result;
113 }
114 } else {
115 return input;
116 }
117}
118
119function map(input, fn) {
120 var length = input.length,
121 result = new Array(length),
122 i = void 0;
123 for (i = 0; i < length; i++) {
124 result[i] = fn(input[i]);
125 }
126 return result;
127}
128
129function forEach(arr, fn) {
130 var length = arr.length,
131 i = void 0;
132 for (i = 0; i < length; i++) {
133 fn(arr[i]);
134 }
135}
136
137function fillArray(arr, value) {
138 var length = arr.length,
139 i = void 0;
140 for (i = 0; i < length; i++) {
141 arr[i] = value;
142 }
143}
144
145function contains(arr, value) {
146 return find(arr, value) !== -1;
147}
148
149function slide(cur, next, max) {
150 var length = Math.min(max, cur.length + 1),
151 offset = cur.length - length + 1,
152 result = new Array(length),
153 i = void 0;
154 for (i = offset; i < length; i++) {
155 result[i - offset] = cur[i];
156 }
157 result[length - 1] = next;
158 return result;
159}
160
161function callSubscriber(type, fn, event) {
162 if (type === ANY) {
163 fn(event);
164 } else if (type === event.type) {
165 if (type === VALUE || type === ERROR) {
166 fn(event.value);
167 } else {
168 fn();
169 }
170 }
171}
172
173function Dispatcher() {
174 this._items = [];
175 this._spies = [];
176 this._inLoop = 0;
177 this._removedItems = null;
178}
179
180extend(Dispatcher.prototype, {
181 add: function (type, fn) {
182 this._items = concat(this._items, [{ type: type, fn: fn }]);
183 return this._items.length;
184 },
185 remove: function (type, fn) {
186 var index = findByPred(this._items, function (x) {
187 return x.type === type && x.fn === fn;
188 });
189
190 // if we're currently in a notification loop,
191 // remember this subscriber was removed
192 if (this._inLoop !== 0 && index !== -1) {
193 if (this._removedItems === null) {
194 this._removedItems = [];
195 }
196 this._removedItems.push(this._items[index]);
197 }
198
199 this._items = remove(this._items, index);
200 return this._items.length;
201 },
202 addSpy: function (fn) {
203 this._spies = concat(this._spies, [fn]);
204 return this._spies.length;
205 },
206
207
208 // Because spies are only ever a function that perform logging as
209 // their only side effect, we don't need the same complicated
210 // removal logic like in remove()
211 removeSpy: function (fn) {
212 this._spies = remove(this._spies, this._spies.indexOf(fn));
213 return this._spies.length;
214 },
215 dispatch: function (event) {
216 this._inLoop++;
217 for (var i = 0, spies = this._spies; this._spies !== null && i < spies.length; i++) {
218 spies[i](event);
219 }
220
221 for (var _i = 0, items = this._items; _i < items.length; _i++) {
222 // cleanup was called
223 if (this._items === null) {
224 break;
225 }
226
227 // this subscriber was removed
228 if (this._removedItems !== null && contains(this._removedItems, items[_i])) {
229 continue;
230 }
231
232 callSubscriber(items[_i].type, items[_i].fn, event);
233 }
234 this._inLoop--;
235 if (this._inLoop === 0) {
236 this._removedItems = null;
237 }
238 },
239 cleanup: function () {
240 this._items = null;
241 this._spies = null;
242 }
243});
244
245function Observable() {
246 this._dispatcher = new Dispatcher();
247 this._active = false;
248 this._alive = true;
249 this._activating = false;
250 this._logHandlers = null;
251 this._spyHandlers = null;
252}
253
254extend(Observable.prototype, {
255 _name: 'observable',
256
257 _onActivation: function () {},
258 _onDeactivation: function () {},
259 _setActive: function (active) {
260 if (this._active !== active) {
261 this._active = active;
262 if (active) {
263 this._activating = true;
264 this._onActivation();
265 this._activating = false;
266 } else {
267 this._onDeactivation();
268 }
269 }
270 },
271 _clear: function () {
272 this._setActive(false);
273 this._dispatcher.cleanup();
274 this._dispatcher = null;
275 this._logHandlers = null;
276 },
277 _emit: function (type, x) {
278 switch (type) {
279 case VALUE:
280 return this._emitValue(x);
281 case ERROR:
282 return this._emitError(x);
283 case END:
284 return this._emitEnd();
285 }
286 },
287 _emitValue: function (value) {
288 if (this._alive) {
289 this._dispatcher.dispatch({ type: VALUE, value: value });
290 }
291 },
292 _emitError: function (value) {
293 if (this._alive) {
294 this._dispatcher.dispatch({ type: ERROR, value: value });
295 }
296 },
297 _emitEnd: function () {
298 if (this._alive) {
299 this._alive = false;
300 this._dispatcher.dispatch({ type: END });
301 this._clear();
302 }
303 },
304 _on: function (type, fn) {
305 if (this._alive) {
306 this._dispatcher.add(type, fn);
307 this._setActive(true);
308 } else {
309 callSubscriber(type, fn, { type: END });
310 }
311 return this;
312 },
313 _off: function (type, fn) {
314 if (this._alive) {
315 var count = this._dispatcher.remove(type, fn);
316 if (count === 0) {
317 this._setActive(false);
318 }
319 }
320 return this;
321 },
322 onValue: function (fn) {
323 return this._on(VALUE, fn);
324 },
325 onError: function (fn) {
326 return this._on(ERROR, fn);
327 },
328 onEnd: function (fn) {
329 return this._on(END, fn);
330 },
331 onAny: function (fn) {
332 return this._on(ANY, fn);
333 },
334 offValue: function (fn) {
335 return this._off(VALUE, fn);
336 },
337 offError: function (fn) {
338 return this._off(ERROR, fn);
339 },
340 offEnd: function (fn) {
341 return this._off(END, fn);
342 },
343 offAny: function (fn) {
344 return this._off(ANY, fn);
345 },
346 observe: function (observerOrOnValue, onError, onEnd) {
347 var _this = this;
348 var closed = false;
349
350 var observer = !observerOrOnValue || typeof observerOrOnValue === 'function' ? { value: observerOrOnValue, error: onError, end: onEnd } : observerOrOnValue;
351
352 var handler = function (event) {
353 if (event.type === END) {
354 closed = true;
355 }
356 if (event.type === VALUE && observer.value) {
357 observer.value(event.value);
358 } else if (event.type === ERROR && observer.error) {
359 observer.error(event.value);
360 } else if (event.type === END && observer.end) {
361 observer.end(event.value);
362 }
363 };
364
365 this.onAny(handler);
366
367 return {
368 unsubscribe: function () {
369 if (!closed) {
370 _this.offAny(handler);
371 closed = true;
372 }
373 },
374
375 get closed() {
376 return closed;
377 }
378 };
379 },
380
381
382 // A and B must be subclasses of Stream and Property (order doesn't matter)
383 _ofSameType: function (A, B) {
384 return A.prototype.getType() === this.getType() ? A : B;
385 },
386 setName: function (sourceObs /* optional */, selfName) {
387 this._name = selfName ? sourceObs._name + '.' + selfName : sourceObs;
388 return this;
389 },
390 log: function () {
391 var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.toString();
392
393 var isCurrent = void 0;
394 var handler = function (event) {
395 var type = '<' + event.type + (isCurrent ? ':current' : '') + '>';
396 if (event.type === END) {
397 console.log(name, type);
398 } else {
399 console.log(name, type, event.value);
400 }
401 };
402
403 if (this._alive) {
404 if (!this._logHandlers) {
405 this._logHandlers = [];
406 }
407 this._logHandlers.push({ name: name, handler: handler });
408 }
409
410 isCurrent = true;
411 this.onAny(handler);
412 isCurrent = false;
413
414 return this;
415 },
416 offLog: function () {
417 var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.toString();
418
419 if (this._logHandlers) {
420 var handlerIndex = findByPred(this._logHandlers, function (obj) {
421 return obj.name === name;
422 });
423 if (handlerIndex !== -1) {
424 this.offAny(this._logHandlers[handlerIndex].handler);
425 this._logHandlers.splice(handlerIndex, 1);
426 }
427 }
428
429 return this;
430 },
431 spy: function () {
432 var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.toString();
433
434 var handler = function (event) {
435 var type = '<' + event.type + '>';
436 if (event.type === END) {
437 console.log(name, type);
438 } else {
439 console.log(name, type, event.value);
440 }
441 };
442 if (this._alive) {
443 if (!this._spyHandlers) {
444 this._spyHandlers = [];
445 }
446 this._spyHandlers.push({ name: name, handler: handler });
447 this._dispatcher.addSpy(handler);
448 }
449 return this;
450 },
451 offSpy: function () {
452 var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.toString();
453
454 if (this._spyHandlers) {
455 var handlerIndex = findByPred(this._spyHandlers, function (obj) {
456 return obj.name === name;
457 });
458 if (handlerIndex !== -1) {
459 this._dispatcher.removeSpy(this._spyHandlers[handlerIndex].handler);
460 this._spyHandlers.splice(handlerIndex, 1);
461 }
462 }
463 return this;
464 }
465});
466
467// extend() can't handle `toString` in IE8
468Observable.prototype.toString = function () {
469 return '[' + this._name + ']';
470};
471
472function Stream() {
473 Observable.call(this);
474}
475
476inherit(Stream, Observable, {
477 _name: 'stream',
478
479 getType: function () {
480 return 'stream';
481 }
482});
483
484function Property() {
485 Observable.call(this);
486 this._currentEvent = null;
487}
488
489inherit(Property, Observable, {
490 _name: 'property',
491
492 _emitValue: function (value) {
493 if (this._alive) {
494 this._currentEvent = { type: VALUE, value: value };
495 if (!this._activating) {
496 this._dispatcher.dispatch({ type: VALUE, value: value });
497 }
498 }
499 },
500 _emitError: function (value) {
501 if (this._alive) {
502 this._currentEvent = { type: ERROR, value: value };
503 if (!this._activating) {
504 this._dispatcher.dispatch({ type: ERROR, value: value });
505 }
506 }
507 },
508 _emitEnd: function () {
509 if (this._alive) {
510 this._alive = false;
511 if (!this._activating) {
512 this._dispatcher.dispatch({ type: END });
513 }
514 this._clear();
515 }
516 },
517 _on: function (type, fn) {
518 if (this._alive) {
519 this._dispatcher.add(type, fn);
520 this._setActive(true);
521 }
522 if (this._currentEvent !== null) {
523 callSubscriber(type, fn, this._currentEvent);
524 }
525 if (!this._alive) {
526 callSubscriber(type, fn, { type: END });
527 }
528 return this;
529 },
530 getType: function () {
531 return 'property';
532 }
533});
534
535var neverS = new Stream();
536neverS._emitEnd();
537neverS._name = 'never';
538
539function never() {
540 return neverS;
541}
542
543function timeBased(mixin) {
544 function AnonymousStream(wait, options) {
545 var _this = this;
546
547 Stream.call(this);
548 this._wait = wait;
549 this._intervalId = null;
550 this._$onTick = function () {
551 return _this._onTick();
552 };
553 this._init(options);
554 }
555
556 inherit(AnonymousStream, Stream, {
557 _init: function () {},
558 _free: function () {},
559 _onTick: function () {},
560 _onActivation: function () {
561 this._intervalId = setInterval(this._$onTick, this._wait);
562 },
563 _onDeactivation: function () {
564 if (this._intervalId !== null) {
565 clearInterval(this._intervalId);
566 this._intervalId = null;
567 }
568 },
569 _clear: function () {
570 Stream.prototype._clear.call(this);
571 this._$onTick = null;
572 this._free();
573 }
574 }, mixin);
575
576 return AnonymousStream;
577}
578
579var S = timeBased({
580 _name: 'later',
581
582 _init: function (_ref) {
583 var x = _ref.x;
584
585 this._x = x;
586 },
587 _free: function () {
588 this._x = null;
589 },
590 _onTick: function () {
591 this._emitValue(this._x);
592 this._emitEnd();
593 }
594});
595
596function later(wait, x) {
597 return new S(wait, { x: x });
598}
599
600var S$1 = timeBased({
601 _name: 'interval',
602
603 _init: function (_ref) {
604 var x = _ref.x;
605
606 this._x = x;
607 },
608 _free: function () {
609 this._x = null;
610 },
611 _onTick: function () {
612 this._emitValue(this._x);
613 }
614});
615
616function interval(wait, x) {
617 return new S$1(wait, { x: x });
618}
619
620var S$2 = timeBased({
621 _name: 'sequentially',
622
623 _init: function (_ref) {
624 var xs = _ref.xs;
625
626 this._xs = cloneArray(xs);
627 },
628 _free: function () {
629 this._xs = null;
630 },
631 _onTick: function () {
632 if (this._xs.length === 1) {
633 this._emitValue(this._xs[0]);
634 this._emitEnd();
635 } else {
636 this._emitValue(this._xs.shift());
637 }
638 }
639});
640
641function sequentially(wait, xs) {
642 return xs.length === 0 ? never() : new S$2(wait, { xs: xs });
643}
644
645var S$3 = timeBased({
646 _name: 'fromPoll',
647
648 _init: function (_ref) {
649 var fn = _ref.fn;
650
651 this._fn = fn;
652 },
653 _free: function () {
654 this._fn = null;
655 },
656 _onTick: function () {
657 var fn = this._fn;
658 this._emitValue(fn());
659 }
660});
661
662function fromPoll(wait, fn) {
663 return new S$3(wait, { fn: fn });
664}
665
666function emitter(obs) {
667 function value(x) {
668 obs._emitValue(x);
669 return obs._active;
670 }
671
672 function error(x) {
673 obs._emitError(x);
674 return obs._active;
675 }
676
677 function end() {
678 obs._emitEnd();
679 return obs._active;
680 }
681
682 function event(e) {
683 obs._emit(e.type, e.value);
684 return obs._active;
685 }
686
687 return {
688 value: value,
689 error: error,
690 end: end,
691 event: event,
692
693 // legacy
694 emit: value,
695 emitEvent: event
696 };
697}
698
699var S$4 = timeBased({
700 _name: 'withInterval',
701
702 _init: function (_ref) {
703 var fn = _ref.fn;
704
705 this._fn = fn;
706 this._emitter = emitter(this);
707 },
708 _free: function () {
709 this._fn = null;
710 this._emitter = null;
711 },
712 _onTick: function () {
713 var fn = this._fn;
714 fn(this._emitter);
715 }
716});
717
718function withInterval(wait, fn) {
719 return new S$4(wait, { fn: fn });
720}
721
722function S$5(fn) {
723 Stream.call(this);
724 this._fn = fn;
725 this._unsubscribe = null;
726}
727
728inherit(S$5, Stream, {
729 _name: 'stream',
730
731 _onActivation: function () {
732 var fn = this._fn;
733 var unsubscribe = fn(emitter(this));
734 this._unsubscribe = typeof unsubscribe === 'function' ? unsubscribe : null;
735
736 // fix https://github.com/kefirjs/kefir/issues/35
737 if (!this._active) {
738 this._callUnsubscribe();
739 }
740 },
741 _callUnsubscribe: function () {
742 if (this._unsubscribe !== null) {
743 this._unsubscribe();
744 this._unsubscribe = null;
745 }
746 },
747 _onDeactivation: function () {
748 this._callUnsubscribe();
749 },
750 _clear: function () {
751 Stream.prototype._clear.call(this);
752 this._fn = null;
753 }
754});
755
756function stream(fn) {
757 return new S$5(fn);
758}
759
760function fromCallback(callbackConsumer) {
761 var called = false;
762
763 return stream(function (emitter) {
764 if (!called) {
765 callbackConsumer(function (x) {
766 emitter.emit(x);
767 emitter.end();
768 });
769 called = true;
770 }
771 }).setName('fromCallback');
772}
773
774function fromNodeCallback(callbackConsumer) {
775 var called = false;
776
777 return stream(function (emitter) {
778 if (!called) {
779 callbackConsumer(function (error, x) {
780 if (error) {
781 emitter.error(error);
782 } else {
783 emitter.emit(x);
784 }
785 emitter.end();
786 });
787 called = true;
788 }
789 }).setName('fromNodeCallback');
790}
791
792function spread(fn, length) {
793 switch (length) {
794 case 0:
795 return function () {
796 return fn();
797 };
798 case 1:
799 return function (a) {
800 return fn(a[0]);
801 };
802 case 2:
803 return function (a) {
804 return fn(a[0], a[1]);
805 };
806 case 3:
807 return function (a) {
808 return fn(a[0], a[1], a[2]);
809 };
810 case 4:
811 return function (a) {
812 return fn(a[0], a[1], a[2], a[3]);
813 };
814 default:
815 return function (a) {
816 return fn.apply(null, a);
817 };
818 }
819}
820
821function apply(fn, c, a) {
822 var aLength = a ? a.length : 0;
823 if (c == null) {
824 switch (aLength) {
825 case 0:
826 return fn();
827 case 1:
828 return fn(a[0]);
829 case 2:
830 return fn(a[0], a[1]);
831 case 3:
832 return fn(a[0], a[1], a[2]);
833 case 4:
834 return fn(a[0], a[1], a[2], a[3]);
835 default:
836 return fn.apply(null, a);
837 }
838 } else {
839 switch (aLength) {
840 case 0:
841 return fn.call(c);
842 default:
843 return fn.apply(c, a);
844 }
845 }
846}
847
848function fromSubUnsub(sub, unsub, transformer /* Function | falsey */) {
849 return stream(function (emitter) {
850 var handler = transformer ? function () {
851 emitter.emit(apply(transformer, this, arguments));
852 } : function (x) {
853 emitter.emit(x);
854 };
855
856 sub(handler);
857 return function () {
858 return unsub(handler);
859 };
860 }).setName('fromSubUnsub');
861}
862
863var pairs = [['addEventListener', 'removeEventListener'], ['addListener', 'removeListener'], ['on', 'off']];
864
865function fromEvents(target, eventName, transformer) {
866 var sub = void 0,
867 unsub = void 0;
868
869 for (var i = 0; i < pairs.length; i++) {
870 if (typeof target[pairs[i][0]] === 'function' && typeof target[pairs[i][1]] === 'function') {
871 sub = pairs[i][0];
872 unsub = pairs[i][1];
873 break;
874 }
875 }
876
877 if (sub === undefined) {
878 throw new Error("target don't support any of " + 'addEventListener/removeEventListener, addListener/removeListener, on/off method pair');
879 }
880
881 return fromSubUnsub(function (handler) {
882 return target[sub](eventName, handler);
883 }, function (handler) {
884 return target[unsub](eventName, handler);
885 }, transformer).setName('fromEvents');
886}
887
888// HACK:
889// We don't call parent Class constructor, but instead putting all necessary
890// properties into prototype to simulate ended Property
891// (see Propperty and Observable classes).
892
893function P(value) {
894 this._currentEvent = { type: 'value', value: value, current: true };
895}
896
897inherit(P, Property, {
898 _name: 'constant',
899 _active: false,
900 _activating: false,
901 _alive: false,
902 _dispatcher: null,
903 _logHandlers: null
904});
905
906function constant(x) {
907 return new P(x);
908}
909
910// HACK:
911// We don't call parent Class constructor, but instead putting all necessary
912// properties into prototype to simulate ended Property
913// (see Propperty and Observable classes).
914
915function P$1(value) {
916 this._currentEvent = { type: 'error', value: value, current: true };
917}
918
919inherit(P$1, Property, {
920 _name: 'constantError',
921 _active: false,
922 _activating: false,
923 _alive: false,
924 _dispatcher: null,
925 _logHandlers: null
926});
927
928function constantError(x) {
929 return new P$1(x);
930}
931
932function createConstructor(BaseClass, name) {
933 return function AnonymousObservable(source, options) {
934 var _this = this;
935
936 BaseClass.call(this);
937 this._source = source;
938 this._name = source._name + '.' + name;
939 this._init(options);
940 this._$handleAny = function (event) {
941 return _this._handleAny(event);
942 };
943 };
944}
945
946function createClassMethods(BaseClass) {
947 return {
948 _init: function () {},
949 _free: function () {},
950 _handleValue: function (x) {
951 this._emitValue(x);
952 },
953 _handleError: function (x) {
954 this._emitError(x);
955 },
956 _handleEnd: function () {
957 this._emitEnd();
958 },
959 _handleAny: function (event) {
960 switch (event.type) {
961 case VALUE:
962 return this._handleValue(event.value);
963 case ERROR:
964 return this._handleError(event.value);
965 case END:
966 return this._handleEnd();
967 }
968 },
969 _onActivation: function () {
970 this._source.onAny(this._$handleAny);
971 },
972 _onDeactivation: function () {
973 this._source.offAny(this._$handleAny);
974 },
975 _clear: function () {
976 BaseClass.prototype._clear.call(this);
977 this._source = null;
978 this._$handleAny = null;
979 this._free();
980 }
981 };
982}
983
984function createStream(name, mixin) {
985 var S = createConstructor(Stream, name);
986 inherit(S, Stream, createClassMethods(Stream), mixin);
987 return S;
988}
989
990function createProperty(name, mixin) {
991 var P = createConstructor(Property, name);
992 inherit(P, Property, createClassMethods(Property), mixin);
993 return P;
994}
995
996var P$2 = createProperty('toProperty', {
997 _init: function (_ref) {
998 var fn = _ref.fn;
999
1000 this._getInitialCurrent = fn;
1001 },
1002 _onActivation: function () {
1003 if (this._getInitialCurrent !== null) {
1004 var getInitial = this._getInitialCurrent;
1005 this._emitValue(getInitial());
1006 }
1007 this._source.onAny(this._$handleAny); // copied from patterns/one-source
1008 }
1009});
1010
1011function toProperty(obs) {
1012 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
1013
1014 if (fn !== null && typeof fn !== 'function') {
1015 throw new Error('You should call toProperty() with a function or no arguments.');
1016 }
1017 return new P$2(obs, { fn: fn });
1018}
1019
1020var S$6 = createStream('changes', {
1021 _handleValue: function (x) {
1022 if (!this._activating) {
1023 this._emitValue(x);
1024 }
1025 },
1026 _handleError: function (x) {
1027 if (!this._activating) {
1028 this._emitError(x);
1029 }
1030 }
1031});
1032
1033function changes(obs) {
1034 return new S$6(obs);
1035}
1036
1037function fromPromise(promise) {
1038 var called = false;
1039
1040 var result = stream(function (emitter) {
1041 if (!called) {
1042 var onValue = function (x) {
1043 emitter.emit(x);
1044 emitter.end();
1045 };
1046 var onError = function (x) {
1047 emitter.error(x);
1048 emitter.end();
1049 };
1050 var _promise = promise.then(onValue, onError);
1051
1052 // prevent libraries like 'Q' or 'when' from swallowing exceptions
1053 if (_promise && typeof _promise.done === 'function') {
1054 _promise.done();
1055 }
1056
1057 called = true;
1058 }
1059 });
1060
1061 return toProperty(result, null).setName('fromPromise');
1062}
1063
1064function getGlodalPromise() {
1065 if (typeof Promise === 'function') {
1066 return Promise;
1067 } else {
1068 throw new Error("There isn't default Promise, use shim or parameter");
1069 }
1070}
1071
1072var toPromise = function (obs) {
1073 var Promise = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : getGlodalPromise();
1074
1075 var last = null;
1076 return new Promise(function (resolve, reject) {
1077 obs.onAny(function (event) {
1078 if (event.type === END && last !== null) {
1079 (last.type === VALUE ? resolve : reject)(last.value);
1080 last = null;
1081 } else {
1082 last = event;
1083 }
1084 });
1085 });
1086};
1087
1088function symbolObservablePonyfill(root) {
1089 var result;
1090 var Symbol = root.Symbol;
1091
1092 if (typeof Symbol === 'function') {
1093 if (Symbol.observable) {
1094 result = Symbol.observable;
1095 } else {
1096 result = Symbol('observable');
1097 Symbol.observable = result;
1098 }
1099 } else {
1100 result = '@@observable';
1101 }
1102
1103 return result;
1104}
1105
1106/* global window */
1107var root;
1108
1109if (typeof self !== 'undefined') {
1110 root = self;
1111} else if (typeof window !== 'undefined') {
1112 root = window;
1113} else if (typeof global !== 'undefined') {
1114 root = global;
1115} else if (typeof module !== 'undefined') {
1116 root = module;
1117} else {
1118 root = Function('return this')();
1119}
1120
1121var result = symbolObservablePonyfill(root);
1122
1123// this file contains some hot JS modules systems stuff
1124
1125var $$observable = result.default ? result.default : result;
1126
1127function fromESObservable(_observable) {
1128 var observable = _observable[$$observable] ? _observable[$$observable]() : _observable;
1129 return stream(function (emitter) {
1130 var unsub = observable.subscribe({
1131 error: function (error) {
1132 emitter.error(error);
1133 emitter.end();
1134 },
1135 next: function (value) {
1136 emitter.emit(value);
1137 },
1138 complete: function () {
1139 emitter.end();
1140 }
1141 });
1142
1143 if (unsub.unsubscribe) {
1144 return function () {
1145 unsub.unsubscribe();
1146 };
1147 } else {
1148 return unsub;
1149 }
1150 }).setName('fromESObservable');
1151}
1152
1153function ESObservable(observable) {
1154 this._observable = observable.takeErrors(1);
1155}
1156
1157extend(ESObservable.prototype, {
1158 subscribe: function (observerOrOnNext, onError, onComplete) {
1159 var _this = this;
1160
1161 var observer = typeof observerOrOnNext === 'function' ? { next: observerOrOnNext, error: onError, complete: onComplete } : observerOrOnNext;
1162
1163 var fn = function (event) {
1164 if (event.type === END) {
1165 closed = true;
1166 }
1167
1168 if (event.type === VALUE && observer.next) {
1169 observer.next(event.value);
1170 } else if (event.type === ERROR && observer.error) {
1171 observer.error(event.value);
1172 } else if (event.type === END && observer.complete) {
1173 observer.complete(event.value);
1174 }
1175 };
1176
1177 this._observable.onAny(fn);
1178 var closed = false;
1179
1180 var subscription = {
1181 unsubscribe: function () {
1182 closed = true;
1183 _this._observable.offAny(fn);
1184 },
1185 get closed() {
1186 return closed;
1187 }
1188 };
1189 return subscription;
1190 }
1191});
1192
1193// Need to assign directly b/c Symbols aren't enumerable.
1194ESObservable.prototype[$$observable] = function () {
1195 return this;
1196};
1197
1198function toESObservable() {
1199 return new ESObservable(this);
1200}
1201
1202function collect(source, keys, values) {
1203 for (var prop in source) {
1204 if (source.hasOwnProperty(prop)) {
1205 keys.push(prop);
1206 values.push(source[prop]);
1207 }
1208 }
1209}
1210
1211function defaultErrorsCombinator(errors) {
1212 var latestError = void 0;
1213 for (var i = 0; i < errors.length; i++) {
1214 if (errors[i] !== undefined) {
1215 if (latestError === undefined || latestError.index < errors[i].index) {
1216 latestError = errors[i];
1217 }
1218 }
1219 }
1220 return latestError.error;
1221}
1222
1223function Combine(active, passive, combinator) {
1224 var _this = this;
1225
1226 Stream.call(this);
1227 this._activeCount = active.length;
1228 this._sources = concat(active, passive);
1229 this._combinator = combinator;
1230 this._aliveCount = 0;
1231 this._latestValues = new Array(this._sources.length);
1232 this._latestErrors = new Array(this._sources.length);
1233 fillArray(this._latestValues, NOTHING);
1234 this._emitAfterActivation = false;
1235 this._endAfterActivation = false;
1236 this._latestErrorIndex = 0;
1237
1238 this._$handlers = [];
1239
1240 var _loop = function (i) {
1241 _this._$handlers.push(function (event) {
1242 return _this._handleAny(i, event);
1243 });
1244 };
1245
1246 for (var i = 0; i < this._sources.length; i++) {
1247 _loop(i);
1248 }
1249}
1250
1251inherit(Combine, Stream, {
1252 _name: 'combine',
1253
1254 _onActivation: function () {
1255 this._aliveCount = this._activeCount;
1256
1257 // we need to suscribe to _passive_ sources before _active_
1258 // (see https://github.com/kefirjs/kefir/issues/98)
1259 for (var i = this._activeCount; i < this._sources.length; i++) {
1260 this._sources[i].onAny(this._$handlers[i]);
1261 }
1262 for (var _i = 0; _i < this._activeCount; _i++) {
1263 this._sources[_i].onAny(this._$handlers[_i]);
1264 }
1265
1266 if (this._emitAfterActivation) {
1267 this._emitAfterActivation = false;
1268 this._emitIfFull();
1269 }
1270 if (this._endAfterActivation) {
1271 this._emitEnd();
1272 }
1273 },
1274 _onDeactivation: function () {
1275 var length = this._sources.length,
1276 i = void 0;
1277 for (i = 0; i < length; i++) {
1278 this._sources[i].offAny(this._$handlers[i]);
1279 }
1280 },
1281 _emitIfFull: function () {
1282 var hasAllValues = true;
1283 var hasErrors = false;
1284 var length = this._latestValues.length;
1285 var valuesCopy = new Array(length);
1286 var errorsCopy = new Array(length);
1287
1288 for (var i = 0; i < length; i++) {
1289 valuesCopy[i] = this._latestValues[i];
1290 errorsCopy[i] = this._latestErrors[i];
1291
1292 if (valuesCopy[i] === NOTHING) {
1293 hasAllValues = false;
1294 }
1295
1296 if (errorsCopy[i] !== undefined) {
1297 hasErrors = true;
1298 }
1299 }
1300
1301 if (hasAllValues) {
1302 var combinator = this._combinator;
1303 this._emitValue(combinator(valuesCopy));
1304 }
1305 if (hasErrors) {
1306 this._emitError(defaultErrorsCombinator(errorsCopy));
1307 }
1308 },
1309 _handleAny: function (i, event) {
1310 if (event.type === VALUE || event.type === ERROR) {
1311 if (event.type === VALUE) {
1312 this._latestValues[i] = event.value;
1313 this._latestErrors[i] = undefined;
1314 }
1315 if (event.type === ERROR) {
1316 this._latestValues[i] = NOTHING;
1317 this._latestErrors[i] = {
1318 index: this._latestErrorIndex++,
1319 error: event.value
1320 };
1321 }
1322
1323 if (i < this._activeCount) {
1324 if (this._activating) {
1325 this._emitAfterActivation = true;
1326 } else {
1327 this._emitIfFull();
1328 }
1329 }
1330 } else {
1331 // END
1332
1333 if (i < this._activeCount) {
1334 this._aliveCount--;
1335 if (this._aliveCount === 0) {
1336 if (this._activating) {
1337 this._endAfterActivation = true;
1338 } else {
1339 this._emitEnd();
1340 }
1341 }
1342 }
1343 }
1344 },
1345 _clear: function () {
1346 Stream.prototype._clear.call(this);
1347 this._sources = null;
1348 this._latestValues = null;
1349 this._latestErrors = null;
1350 this._combinator = null;
1351 this._$handlers = null;
1352 }
1353});
1354
1355function combineAsArray(active) {
1356 var passive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
1357 var combinator = arguments[2];
1358
1359 if (!Array.isArray(passive)) {
1360 throw new Error('Combine can only combine active and passive collections of the same type.');
1361 }
1362
1363 combinator = combinator ? spread(combinator, active.length + passive.length) : function (x) {
1364 return x;
1365 };
1366 return active.length === 0 ? never() : new Combine(active, passive, combinator);
1367}
1368
1369function combineAsObject(active) {
1370 var passive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1371 var combinator = arguments[2];
1372
1373 if (typeof passive !== 'object' || Array.isArray(passive)) {
1374 throw new Error('Combine can only combine active and passive collections of the same type.');
1375 }
1376
1377 var keys = [],
1378 activeObservables = [],
1379 passiveObservables = [];
1380
1381 collect(active, keys, activeObservables);
1382 collect(passive, keys, passiveObservables);
1383
1384 var objectify = function (values) {
1385 var event = {};
1386 for (var i = values.length - 1; 0 <= i; i--) {
1387 event[keys[i]] = values[i];
1388 }
1389 return combinator ? combinator(event) : event;
1390 };
1391
1392 return activeObservables.length === 0 ? never() : new Combine(activeObservables, passiveObservables, objectify);
1393}
1394
1395function combine(active, passive, combinator) {
1396 if (typeof passive === 'function') {
1397 combinator = passive;
1398 passive = undefined;
1399 }
1400
1401 return Array.isArray(active) ? combineAsArray(active, passive, combinator) : combineAsObject(active, passive, combinator);
1402}
1403
1404var Observable$2 = {
1405 empty: function () {
1406 return never();
1407 },
1408
1409
1410 // Monoid based on merge() seems more useful than one based on concat().
1411 concat: function (a, b) {
1412 return a.merge(b);
1413 },
1414 of: function (x) {
1415 return constant(x);
1416 },
1417 map: function (fn, obs) {
1418 return obs.map(fn);
1419 },
1420 bimap: function (fnErr, fnVal, obs) {
1421 return obs.mapErrors(fnErr).map(fnVal);
1422 },
1423
1424
1425 // This ap strictly speaking incompatible with chain. If we derive ap from chain we get
1426 // different (not very useful) behavior. But spec requires that if method can be derived
1427 // it must have the same behavior as hand-written method. We intentionally violate the spec
1428 // in hope that it won't cause many troubles in practice. And in return we have more useful type.
1429 ap: function (obsFn, obsVal) {
1430 return combine([obsFn, obsVal], function (fn, val) {
1431 return fn(val);
1432 });
1433 },
1434 chain: function (fn, obs) {
1435 return obs.flatMap(fn);
1436 }
1437};
1438
1439
1440
1441var staticLand = Object.freeze({
1442 Observable: Observable$2
1443});
1444
1445var mixin = {
1446 _init: function (_ref) {
1447 var fn = _ref.fn;
1448
1449 this._fn = fn;
1450 },
1451 _free: function () {
1452 this._fn = null;
1453 },
1454 _handleValue: function (x) {
1455 var fn = this._fn;
1456 this._emitValue(fn(x));
1457 }
1458};
1459
1460var S$7 = createStream('map', mixin);
1461var P$3 = createProperty('map', mixin);
1462
1463var id = function (x) {
1464 return x;
1465};
1466
1467function map$1(obs) {
1468 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id;
1469
1470 return new (obs._ofSameType(S$7, P$3))(obs, { fn: fn });
1471}
1472
1473var mixin$1 = {
1474 _init: function (_ref) {
1475 var fn = _ref.fn;
1476
1477 this._fn = fn;
1478 },
1479 _free: function () {
1480 this._fn = null;
1481 },
1482 _handleValue: function (x) {
1483 var fn = this._fn;
1484 if (fn(x)) {
1485 this._emitValue(x);
1486 }
1487 }
1488};
1489
1490var S$8 = createStream('filter', mixin$1);
1491var P$4 = createProperty('filter', mixin$1);
1492
1493var id$1 = function (x) {
1494 return x;
1495};
1496
1497function filter(obs) {
1498 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$1;
1499
1500 return new (obs._ofSameType(S$8, P$4))(obs, { fn: fn });
1501}
1502
1503var mixin$2 = {
1504 _init: function (_ref) {
1505 var n = _ref.n;
1506
1507 this._n = n;
1508 if (n <= 0) {
1509 this._emitEnd();
1510 }
1511 },
1512 _handleValue: function (x) {
1513 if (this._n === 0) {
1514 return;
1515 }
1516 this._n--;
1517 this._emitValue(x);
1518 if (this._n === 0) {
1519 this._emitEnd();
1520 }
1521 }
1522};
1523
1524var S$9 = createStream('take', mixin$2);
1525var P$5 = createProperty('take', mixin$2);
1526
1527function take(obs, n) {
1528 return new (obs._ofSameType(S$9, P$5))(obs, { n: n });
1529}
1530
1531var mixin$3 = {
1532 _init: function (_ref) {
1533 var n = _ref.n;
1534
1535 this._n = n;
1536 if (n <= 0) {
1537 this._emitEnd();
1538 }
1539 },
1540 _handleError: function (x) {
1541 if (this._n === 0) {
1542 return;
1543 }
1544 this._n--;
1545 this._emitError(x);
1546 if (this._n === 0) {
1547 this._emitEnd();
1548 }
1549 }
1550};
1551
1552var S$10 = createStream('takeErrors', mixin$3);
1553var P$6 = createProperty('takeErrors', mixin$3);
1554
1555function takeErrors(obs, n) {
1556 return new (obs._ofSameType(S$10, P$6))(obs, { n: n });
1557}
1558
1559var mixin$4 = {
1560 _init: function (_ref) {
1561 var fn = _ref.fn;
1562
1563 this._fn = fn;
1564 },
1565 _free: function () {
1566 this._fn = null;
1567 },
1568 _handleValue: function (x) {
1569 var fn = this._fn;
1570 if (fn(x)) {
1571 this._emitValue(x);
1572 } else {
1573 this._emitEnd();
1574 }
1575 }
1576};
1577
1578var S$11 = createStream('takeWhile', mixin$4);
1579var P$7 = createProperty('takeWhile', mixin$4);
1580
1581var id$2 = function (x) {
1582 return x;
1583};
1584
1585function takeWhile(obs) {
1586 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$2;
1587
1588 return new (obs._ofSameType(S$11, P$7))(obs, { fn: fn });
1589}
1590
1591var mixin$5 = {
1592 _init: function () {
1593 this._lastValue = NOTHING;
1594 },
1595 _free: function () {
1596 this._lastValue = null;
1597 },
1598 _handleValue: function (x) {
1599 this._lastValue = x;
1600 },
1601 _handleEnd: function () {
1602 if (this._lastValue !== NOTHING) {
1603 this._emitValue(this._lastValue);
1604 }
1605 this._emitEnd();
1606 }
1607};
1608
1609var S$12 = createStream('last', mixin$5);
1610var P$8 = createProperty('last', mixin$5);
1611
1612function last(obs) {
1613 return new (obs._ofSameType(S$12, P$8))(obs);
1614}
1615
1616var mixin$6 = {
1617 _init: function (_ref) {
1618 var n = _ref.n;
1619
1620 this._n = Math.max(0, n);
1621 },
1622 _handleValue: function (x) {
1623 if (this._n === 0) {
1624 this._emitValue(x);
1625 } else {
1626 this._n--;
1627 }
1628 }
1629};
1630
1631var S$13 = createStream('skip', mixin$6);
1632var P$9 = createProperty('skip', mixin$6);
1633
1634function skip(obs, n) {
1635 return new (obs._ofSameType(S$13, P$9))(obs, { n: n });
1636}
1637
1638var mixin$7 = {
1639 _init: function (_ref) {
1640 var fn = _ref.fn;
1641
1642 this._fn = fn;
1643 },
1644 _free: function () {
1645 this._fn = null;
1646 },
1647 _handleValue: function (x) {
1648 var fn = this._fn;
1649 if (this._fn !== null && !fn(x)) {
1650 this._fn = null;
1651 }
1652 if (this._fn === null) {
1653 this._emitValue(x);
1654 }
1655 }
1656};
1657
1658var S$14 = createStream('skipWhile', mixin$7);
1659var P$10 = createProperty('skipWhile', mixin$7);
1660
1661var id$3 = function (x) {
1662 return x;
1663};
1664
1665function skipWhile(obs) {
1666 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$3;
1667
1668 return new (obs._ofSameType(S$14, P$10))(obs, { fn: fn });
1669}
1670
1671var mixin$8 = {
1672 _init: function (_ref) {
1673 var fn = _ref.fn;
1674
1675 this._fn = fn;
1676 this._prev = NOTHING;
1677 },
1678 _free: function () {
1679 this._fn = null;
1680 this._prev = null;
1681 },
1682 _handleValue: function (x) {
1683 var fn = this._fn;
1684 if (this._prev === NOTHING || !fn(this._prev, x)) {
1685 this._prev = x;
1686 this._emitValue(x);
1687 }
1688 }
1689};
1690
1691var S$15 = createStream('skipDuplicates', mixin$8);
1692var P$11 = createProperty('skipDuplicates', mixin$8);
1693
1694var eq = function (a, b) {
1695 return a === b;
1696};
1697
1698function skipDuplicates(obs) {
1699 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : eq;
1700
1701 return new (obs._ofSameType(S$15, P$11))(obs, { fn: fn });
1702}
1703
1704var mixin$9 = {
1705 _init: function (_ref) {
1706 var fn = _ref.fn,
1707 seed = _ref.seed;
1708
1709 this._fn = fn;
1710 this._prev = seed;
1711 },
1712 _free: function () {
1713 this._prev = null;
1714 this._fn = null;
1715 },
1716 _handleValue: function (x) {
1717 if (this._prev !== NOTHING) {
1718 var fn = this._fn;
1719 this._emitValue(fn(this._prev, x));
1720 }
1721 this._prev = x;
1722 }
1723};
1724
1725var S$16 = createStream('diff', mixin$9);
1726var P$12 = createProperty('diff', mixin$9);
1727
1728function defaultFn(a, b) {
1729 return [a, b];
1730}
1731
1732function diff(obs, fn) {
1733 var seed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NOTHING;
1734
1735 return new (obs._ofSameType(S$16, P$12))(obs, { fn: fn || defaultFn, seed: seed });
1736}
1737
1738var P$13 = createProperty('scan', {
1739 _init: function (_ref) {
1740 var fn = _ref.fn,
1741 seed = _ref.seed;
1742
1743 this._fn = fn;
1744 this._seed = seed;
1745 if (seed !== NOTHING) {
1746 this._emitValue(seed);
1747 }
1748 },
1749 _free: function () {
1750 this._fn = null;
1751 this._seed = null;
1752 },
1753 _handleValue: function (x) {
1754 var fn = this._fn;
1755 if (this._currentEvent === null || this._currentEvent.type === ERROR) {
1756 this._emitValue(this._seed === NOTHING ? x : fn(this._seed, x));
1757 } else {
1758 this._emitValue(fn(this._currentEvent.value, x));
1759 }
1760 }
1761});
1762
1763function scan(obs, fn) {
1764 var seed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NOTHING;
1765
1766 return new P$13(obs, { fn: fn, seed: seed });
1767}
1768
1769var mixin$10 = {
1770 _init: function (_ref) {
1771 var fn = _ref.fn;
1772
1773 this._fn = fn;
1774 },
1775 _free: function () {
1776 this._fn = null;
1777 },
1778 _handleValue: function (x) {
1779 var fn = this._fn;
1780 var xs = fn(x);
1781 for (var i = 0; i < xs.length; i++) {
1782 this._emitValue(xs[i]);
1783 }
1784 }
1785};
1786
1787var S$17 = createStream('flatten', mixin$10);
1788
1789var id$4 = function (x) {
1790 return x;
1791};
1792
1793function flatten(obs) {
1794 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$4;
1795
1796 return new S$17(obs, { fn: fn });
1797}
1798
1799var END_MARKER = {};
1800
1801var mixin$11 = {
1802 _init: function (_ref) {
1803 var _this = this;
1804
1805 var wait = _ref.wait;
1806
1807 this._wait = Math.max(0, wait);
1808 this._buff = [];
1809 this._$shiftBuff = function () {
1810 var value = _this._buff.shift();
1811 if (value === END_MARKER) {
1812 _this._emitEnd();
1813 } else {
1814 _this._emitValue(value);
1815 }
1816 };
1817 },
1818 _free: function () {
1819 this._buff = null;
1820 this._$shiftBuff = null;
1821 },
1822 _handleValue: function (x) {
1823 if (this._activating) {
1824 this._emitValue(x);
1825 } else {
1826 this._buff.push(x);
1827 setTimeout(this._$shiftBuff, this._wait);
1828 }
1829 },
1830 _handleEnd: function () {
1831 if (this._activating) {
1832 this._emitEnd();
1833 } else {
1834 this._buff.push(END_MARKER);
1835 setTimeout(this._$shiftBuff, this._wait);
1836 }
1837 }
1838};
1839
1840var S$18 = createStream('delay', mixin$11);
1841var P$14 = createProperty('delay', mixin$11);
1842
1843function delay(obs, wait) {
1844 return new (obs._ofSameType(S$18, P$14))(obs, { wait: wait });
1845}
1846
1847var now = Date.now ? function () {
1848 return Date.now();
1849} : function () {
1850 return new Date().getTime();
1851};
1852
1853var mixin$12 = {
1854 _init: function (_ref) {
1855 var _this = this;
1856
1857 var wait = _ref.wait,
1858 leading = _ref.leading,
1859 trailing = _ref.trailing;
1860
1861 this._wait = Math.max(0, wait);
1862 this._leading = leading;
1863 this._trailing = trailing;
1864 this._trailingValue = null;
1865 this._timeoutId = null;
1866 this._endLater = false;
1867 this._lastCallTime = 0;
1868 this._$trailingCall = function () {
1869 return _this._trailingCall();
1870 };
1871 },
1872 _free: function () {
1873 this._trailingValue = null;
1874 this._$trailingCall = null;
1875 },
1876 _handleValue: function (x) {
1877 if (this._activating) {
1878 this._emitValue(x);
1879 } else {
1880 var curTime = now();
1881 if (this._lastCallTime === 0 && !this._leading) {
1882 this._lastCallTime = curTime;
1883 }
1884 var remaining = this._wait - (curTime - this._lastCallTime);
1885 if (remaining <= 0) {
1886 this._cancelTrailing();
1887 this._lastCallTime = curTime;
1888 this._emitValue(x);
1889 } else if (this._trailing) {
1890 this._cancelTrailing();
1891 this._trailingValue = x;
1892 this._timeoutId = setTimeout(this._$trailingCall, remaining);
1893 }
1894 }
1895 },
1896 _handleEnd: function () {
1897 if (this._activating) {
1898 this._emitEnd();
1899 } else {
1900 if (this._timeoutId) {
1901 this._endLater = true;
1902 } else {
1903 this._emitEnd();
1904 }
1905 }
1906 },
1907 _cancelTrailing: function () {
1908 if (this._timeoutId !== null) {
1909 clearTimeout(this._timeoutId);
1910 this._timeoutId = null;
1911 }
1912 },
1913 _trailingCall: function () {
1914 this._emitValue(this._trailingValue);
1915 this._timeoutId = null;
1916 this._trailingValue = null;
1917 this._lastCallTime = !this._leading ? 0 : now();
1918 if (this._endLater) {
1919 this._emitEnd();
1920 }
1921 }
1922};
1923
1924var S$19 = createStream('throttle', mixin$12);
1925var P$15 = createProperty('throttle', mixin$12);
1926
1927function throttle(obs, wait) {
1928 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
1929 _ref2$leading = _ref2.leading,
1930 leading = _ref2$leading === undefined ? true : _ref2$leading,
1931 _ref2$trailing = _ref2.trailing,
1932 trailing = _ref2$trailing === undefined ? true : _ref2$trailing;
1933
1934 return new (obs._ofSameType(S$19, P$15))(obs, { wait: wait, leading: leading, trailing: trailing });
1935}
1936
1937var mixin$13 = {
1938 _init: function (_ref) {
1939 var _this = this;
1940
1941 var wait = _ref.wait,
1942 immediate = _ref.immediate;
1943
1944 this._wait = Math.max(0, wait);
1945 this._immediate = immediate;
1946 this._lastAttempt = 0;
1947 this._timeoutId = null;
1948 this._laterValue = null;
1949 this._endLater = false;
1950 this._$later = function () {
1951 return _this._later();
1952 };
1953 },
1954 _free: function () {
1955 this._laterValue = null;
1956 this._$later = null;
1957 },
1958 _handleValue: function (x) {
1959 if (this._activating) {
1960 this._emitValue(x);
1961 } else {
1962 this._lastAttempt = now();
1963 if (this._immediate && !this._timeoutId) {
1964 this._emitValue(x);
1965 }
1966 if (!this._timeoutId) {
1967 this._timeoutId = setTimeout(this._$later, this._wait);
1968 }
1969 if (!this._immediate) {
1970 this._laterValue = x;
1971 }
1972 }
1973 },
1974 _handleEnd: function () {
1975 if (this._activating) {
1976 this._emitEnd();
1977 } else {
1978 if (this._timeoutId && !this._immediate) {
1979 this._endLater = true;
1980 } else {
1981 this._emitEnd();
1982 }
1983 }
1984 },
1985 _later: function () {
1986 var last = now() - this._lastAttempt;
1987 if (last < this._wait && last >= 0) {
1988 this._timeoutId = setTimeout(this._$later, this._wait - last);
1989 } else {
1990 this._timeoutId = null;
1991 if (!this._immediate) {
1992 var _laterValue = this._laterValue;
1993 this._laterValue = null;
1994 this._emitValue(_laterValue);
1995 }
1996 if (this._endLater) {
1997 this._emitEnd();
1998 }
1999 }
2000 }
2001};
2002
2003var S$20 = createStream('debounce', mixin$13);
2004var P$16 = createProperty('debounce', mixin$13);
2005
2006function debounce(obs, wait) {
2007 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
2008 _ref2$immediate = _ref2.immediate,
2009 immediate = _ref2$immediate === undefined ? false : _ref2$immediate;
2010
2011 return new (obs._ofSameType(S$20, P$16))(obs, { wait: wait, immediate: immediate });
2012}
2013
2014var mixin$14 = {
2015 _init: function (_ref) {
2016 var fn = _ref.fn;
2017
2018 this._fn = fn;
2019 },
2020 _free: function () {
2021 this._fn = null;
2022 },
2023 _handleError: function (x) {
2024 var fn = this._fn;
2025 this._emitError(fn(x));
2026 }
2027};
2028
2029var S$21 = createStream('mapErrors', mixin$14);
2030var P$17 = createProperty('mapErrors', mixin$14);
2031
2032var id$5 = function (x) {
2033 return x;
2034};
2035
2036function mapErrors(obs) {
2037 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$5;
2038
2039 return new (obs._ofSameType(S$21, P$17))(obs, { fn: fn });
2040}
2041
2042var mixin$15 = {
2043 _init: function (_ref) {
2044 var fn = _ref.fn;
2045
2046 this._fn = fn;
2047 },
2048 _free: function () {
2049 this._fn = null;
2050 },
2051 _handleError: function (x) {
2052 var fn = this._fn;
2053 if (fn(x)) {
2054 this._emitError(x);
2055 }
2056 }
2057};
2058
2059var S$22 = createStream('filterErrors', mixin$15);
2060var P$18 = createProperty('filterErrors', mixin$15);
2061
2062var id$6 = function (x) {
2063 return x;
2064};
2065
2066function filterErrors(obs) {
2067 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$6;
2068
2069 return new (obs._ofSameType(S$22, P$18))(obs, { fn: fn });
2070}
2071
2072var mixin$16 = {
2073 _handleValue: function () {}
2074};
2075
2076var S$23 = createStream('ignoreValues', mixin$16);
2077var P$19 = createProperty('ignoreValues', mixin$16);
2078
2079function ignoreValues(obs) {
2080 return new (obs._ofSameType(S$23, P$19))(obs);
2081}
2082
2083var mixin$17 = {
2084 _handleError: function () {}
2085};
2086
2087var S$24 = createStream('ignoreErrors', mixin$17);
2088var P$20 = createProperty('ignoreErrors', mixin$17);
2089
2090function ignoreErrors(obs) {
2091 return new (obs._ofSameType(S$24, P$20))(obs);
2092}
2093
2094var mixin$18 = {
2095 _handleEnd: function () {}
2096};
2097
2098var S$25 = createStream('ignoreEnd', mixin$18);
2099var P$21 = createProperty('ignoreEnd', mixin$18);
2100
2101function ignoreEnd(obs) {
2102 return new (obs._ofSameType(S$25, P$21))(obs);
2103}
2104
2105var mixin$19 = {
2106 _init: function (_ref) {
2107 var fn = _ref.fn;
2108
2109 this._fn = fn;
2110 },
2111 _free: function () {
2112 this._fn = null;
2113 },
2114 _handleEnd: function () {
2115 var fn = this._fn;
2116 this._emitValue(fn());
2117 this._emitEnd();
2118 }
2119};
2120
2121var S$26 = createStream('beforeEnd', mixin$19);
2122var P$22 = createProperty('beforeEnd', mixin$19);
2123
2124function beforeEnd(obs, fn) {
2125 return new (obs._ofSameType(S$26, P$22))(obs, { fn: fn });
2126}
2127
2128var mixin$20 = {
2129 _init: function (_ref) {
2130 var min = _ref.min,
2131 max = _ref.max;
2132
2133 this._max = max;
2134 this._min = min;
2135 this._buff = [];
2136 },
2137 _free: function () {
2138 this._buff = null;
2139 },
2140 _handleValue: function (x) {
2141 this._buff = slide(this._buff, x, this._max);
2142 if (this._buff.length >= this._min) {
2143 this._emitValue(this._buff);
2144 }
2145 }
2146};
2147
2148var S$27 = createStream('slidingWindow', mixin$20);
2149var P$23 = createProperty('slidingWindow', mixin$20);
2150
2151function slidingWindow(obs, max) {
2152 var min = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
2153
2154 return new (obs._ofSameType(S$27, P$23))(obs, { min: min, max: max });
2155}
2156
2157var mixin$21 = {
2158 _init: function (_ref) {
2159 var fn = _ref.fn,
2160 flushOnEnd = _ref.flushOnEnd;
2161
2162 this._fn = fn;
2163 this._flushOnEnd = flushOnEnd;
2164 this._buff = [];
2165 },
2166 _free: function () {
2167 this._buff = null;
2168 },
2169 _flush: function () {
2170 if (this._buff !== null && this._buff.length !== 0) {
2171 this._emitValue(this._buff);
2172 this._buff = [];
2173 }
2174 },
2175 _handleValue: function (x) {
2176 this._buff.push(x);
2177 var fn = this._fn;
2178 if (!fn(x)) {
2179 this._flush();
2180 }
2181 },
2182 _handleEnd: function () {
2183 if (this._flushOnEnd) {
2184 this._flush();
2185 }
2186 this._emitEnd();
2187 }
2188};
2189
2190var S$28 = createStream('bufferWhile', mixin$21);
2191var P$24 = createProperty('bufferWhile', mixin$21);
2192
2193var id$7 = function (x) {
2194 return x;
2195};
2196
2197function bufferWhile(obs, fn) {
2198 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
2199 _ref2$flushOnEnd = _ref2.flushOnEnd,
2200 flushOnEnd = _ref2$flushOnEnd === undefined ? true : _ref2$flushOnEnd;
2201
2202 return new (obs._ofSameType(S$28, P$24))(obs, { fn: fn || id$7, flushOnEnd: flushOnEnd });
2203}
2204
2205var mixin$22 = {
2206 _init: function (_ref) {
2207 var count = _ref.count,
2208 flushOnEnd = _ref.flushOnEnd;
2209
2210 this._count = count;
2211 this._flushOnEnd = flushOnEnd;
2212 this._buff = [];
2213 },
2214 _free: function () {
2215 this._buff = null;
2216 },
2217 _flush: function () {
2218 if (this._buff !== null && this._buff.length !== 0) {
2219 this._emitValue(this._buff);
2220 this._buff = [];
2221 }
2222 },
2223 _handleValue: function (x) {
2224 this._buff.push(x);
2225 if (this._buff.length >= this._count) {
2226 this._flush();
2227 }
2228 },
2229 _handleEnd: function () {
2230 if (this._flushOnEnd) {
2231 this._flush();
2232 }
2233 this._emitEnd();
2234 }
2235};
2236
2237var S$29 = createStream('bufferWithCount', mixin$22);
2238var P$25 = createProperty('bufferWithCount', mixin$22);
2239
2240function bufferWhile$1(obs, count) {
2241 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
2242 _ref2$flushOnEnd = _ref2.flushOnEnd,
2243 flushOnEnd = _ref2$flushOnEnd === undefined ? true : _ref2$flushOnEnd;
2244
2245 return new (obs._ofSameType(S$29, P$25))(obs, { count: count, flushOnEnd: flushOnEnd });
2246}
2247
2248var mixin$23 = {
2249 _init: function (_ref) {
2250 var _this = this;
2251
2252 var wait = _ref.wait,
2253 count = _ref.count,
2254 flushOnEnd = _ref.flushOnEnd;
2255
2256 this._wait = wait;
2257 this._count = count;
2258 this._flushOnEnd = flushOnEnd;
2259 this._intervalId = null;
2260 this._$onTick = function () {
2261 return _this._flush();
2262 };
2263 this._buff = [];
2264 },
2265 _free: function () {
2266 this._$onTick = null;
2267 this._buff = null;
2268 },
2269 _flush: function () {
2270 if (this._buff !== null) {
2271 this._emitValue(this._buff);
2272 this._buff = [];
2273 }
2274 },
2275 _handleValue: function (x) {
2276 this._buff.push(x);
2277 if (this._buff.length >= this._count) {
2278 clearInterval(this._intervalId);
2279 this._flush();
2280 this._intervalId = setInterval(this._$onTick, this._wait);
2281 }
2282 },
2283 _handleEnd: function () {
2284 if (this._flushOnEnd && this._buff.length !== 0) {
2285 this._flush();
2286 }
2287 this._emitEnd();
2288 },
2289 _onActivation: function () {
2290 this._intervalId = setInterval(this._$onTick, this._wait);
2291 this._source.onAny(this._$handleAny); // copied from patterns/one-source
2292 },
2293 _onDeactivation: function () {
2294 if (this._intervalId !== null) {
2295 clearInterval(this._intervalId);
2296 this._intervalId = null;
2297 }
2298 this._source.offAny(this._$handleAny); // copied from patterns/one-source
2299 }
2300};
2301
2302var S$30 = createStream('bufferWithTimeOrCount', mixin$23);
2303var P$26 = createProperty('bufferWithTimeOrCount', mixin$23);
2304
2305function bufferWithTimeOrCount(obs, wait, count) {
2306 var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
2307 _ref2$flushOnEnd = _ref2.flushOnEnd,
2308 flushOnEnd = _ref2$flushOnEnd === undefined ? true : _ref2$flushOnEnd;
2309
2310 return new (obs._ofSameType(S$30, P$26))(obs, { wait: wait, count: count, flushOnEnd: flushOnEnd });
2311}
2312
2313function xformForObs(obs) {
2314 return {
2315 '@@transducer/step': function (res, input) {
2316 obs._emitValue(input);
2317 return null;
2318 },
2319 '@@transducer/result': function () {
2320 obs._emitEnd();
2321 return null;
2322 }
2323 };
2324}
2325
2326var mixin$24 = {
2327 _init: function (_ref) {
2328 var transducer = _ref.transducer;
2329
2330 this._xform = transducer(xformForObs(this));
2331 },
2332 _free: function () {
2333 this._xform = null;
2334 },
2335 _handleValue: function (x) {
2336 if (this._xform['@@transducer/step'](null, x) !== null) {
2337 this._xform['@@transducer/result'](null);
2338 }
2339 },
2340 _handleEnd: function () {
2341 this._xform['@@transducer/result'](null);
2342 }
2343};
2344
2345var S$31 = createStream('transduce', mixin$24);
2346var P$27 = createProperty('transduce', mixin$24);
2347
2348function transduce(obs, transducer) {
2349 return new (obs._ofSameType(S$31, P$27))(obs, { transducer: transducer });
2350}
2351
2352var mixin$25 = {
2353 _init: function (_ref) {
2354 var fn = _ref.fn;
2355
2356 this._handler = fn;
2357 this._emitter = emitter(this);
2358 },
2359 _free: function () {
2360 this._handler = null;
2361 this._emitter = null;
2362 },
2363 _handleAny: function (event) {
2364 this._handler(this._emitter, event);
2365 }
2366};
2367
2368var S$32 = createStream('withHandler', mixin$25);
2369var P$28 = createProperty('withHandler', mixin$25);
2370
2371function withHandler(obs, fn) {
2372 return new (obs._ofSameType(S$32, P$28))(obs, { fn: fn });
2373}
2374
2375var isArray = Array.isArray || function (xs) {
2376 return Object.prototype.toString.call(xs) === '[object Array]';
2377};
2378
2379function Zip(sources, combinator) {
2380 var _this = this;
2381
2382 Stream.call(this);
2383
2384 this._buffers = map(sources, function (source) {
2385 return isArray(source) ? cloneArray(source) : [];
2386 });
2387 this._sources = map(sources, function (source) {
2388 return isArray(source) ? never() : source;
2389 });
2390
2391 this._combinator = combinator ? spread(combinator, this._sources.length) : function (x) {
2392 return x;
2393 };
2394 this._aliveCount = 0;
2395
2396 this._$handlers = [];
2397
2398 var _loop = function (i) {
2399 _this._$handlers.push(function (event) {
2400 return _this._handleAny(i, event);
2401 });
2402 };
2403
2404 for (var i = 0; i < this._sources.length; i++) {
2405 _loop(i);
2406 }
2407}
2408
2409inherit(Zip, Stream, {
2410 _name: 'zip',
2411
2412 _onActivation: function () {
2413 // if all sources are arrays
2414 while (this._isFull()) {
2415 this._emit();
2416 }
2417
2418 var length = this._sources.length;
2419 this._aliveCount = length;
2420 for (var i = 0; i < length && this._active; i++) {
2421 this._sources[i].onAny(this._$handlers[i]);
2422 }
2423 },
2424 _onDeactivation: function () {
2425 for (var i = 0; i < this._sources.length; i++) {
2426 this._sources[i].offAny(this._$handlers[i]);
2427 }
2428 },
2429 _emit: function () {
2430 var values = new Array(this._buffers.length);
2431 for (var i = 0; i < this._buffers.length; i++) {
2432 values[i] = this._buffers[i].shift();
2433 }
2434 var combinator = this._combinator;
2435 this._emitValue(combinator(values));
2436 },
2437 _isFull: function () {
2438 for (var i = 0; i < this._buffers.length; i++) {
2439 if (this._buffers[i].length === 0) {
2440 return false;
2441 }
2442 }
2443 return true;
2444 },
2445 _handleAny: function (i, event) {
2446 if (event.type === VALUE) {
2447 this._buffers[i].push(event.value);
2448 if (this._isFull()) {
2449 this._emit();
2450 }
2451 }
2452 if (event.type === ERROR) {
2453 this._emitError(event.value);
2454 }
2455 if (event.type === END) {
2456 this._aliveCount--;
2457 if (this._aliveCount === 0) {
2458 this._emitEnd();
2459 }
2460 }
2461 },
2462 _clear: function () {
2463 Stream.prototype._clear.call(this);
2464 this._sources = null;
2465 this._buffers = null;
2466 this._combinator = null;
2467 this._$handlers = null;
2468 }
2469});
2470
2471function zip(observables, combinator /* Function | falsey */) {
2472 return observables.length === 0 ? never() : new Zip(observables, combinator);
2473}
2474
2475var id$8 = function (x) {
2476 return x;
2477};
2478
2479function AbstractPool() {
2480 var _this = this;
2481
2482 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
2483 _ref$queueLim = _ref.queueLim,
2484 queueLim = _ref$queueLim === undefined ? 0 : _ref$queueLim,
2485 _ref$concurLim = _ref.concurLim,
2486 concurLim = _ref$concurLim === undefined ? -1 : _ref$concurLim,
2487 _ref$drop = _ref.drop,
2488 drop = _ref$drop === undefined ? 'new' : _ref$drop;
2489
2490 Stream.call(this);
2491
2492 this._queueLim = queueLim < 0 ? -1 : queueLim;
2493 this._concurLim = concurLim < 0 ? -1 : concurLim;
2494 this._drop = drop;
2495 this._queue = [];
2496 this._curSources = [];
2497 this._$handleSubAny = function (event) {
2498 return _this._handleSubAny(event);
2499 };
2500 this._$endHandlers = [];
2501 this._currentlyAdding = null;
2502
2503 if (this._concurLim === 0) {
2504 this._emitEnd();
2505 }
2506}
2507
2508inherit(AbstractPool, Stream, {
2509 _name: 'abstractPool',
2510
2511 _add: function (obj, toObs /* Function | falsey */) {
2512 toObs = toObs || id$8;
2513 if (this._concurLim === -1 || this._curSources.length < this._concurLim) {
2514 this._addToCur(toObs(obj));
2515 } else {
2516 if (this._queueLim === -1 || this._queue.length < this._queueLim) {
2517 this._addToQueue(toObs(obj));
2518 } else if (this._drop === 'old') {
2519 this._removeOldest();
2520 this._add(obj, toObs);
2521 }
2522 }
2523 },
2524 _addAll: function (obss) {
2525 var _this2 = this;
2526
2527 forEach(obss, function (obs) {
2528 return _this2._add(obs);
2529 });
2530 },
2531 _remove: function (obs) {
2532 if (this._removeCur(obs) === -1) {
2533 this._removeQueue(obs);
2534 }
2535 },
2536 _addToQueue: function (obs) {
2537 this._queue = concat(this._queue, [obs]);
2538 },
2539 _addToCur: function (obs) {
2540 if (this._active) {
2541 // HACK:
2542 //
2543 // We have two optimizations for cases when `obs` is ended. We don't want
2544 // to add such observable to the list, but only want to emit events
2545 // from it (if it has some).
2546 //
2547 // Instead of this hacks, we could just did following,
2548 // but it would be 5-8 times slower:
2549 //
2550 // this._curSources = concat(this._curSources, [obs]);
2551 // this._subscribe(obs);
2552 //
2553
2554 // #1
2555 // This one for cases when `obs` already ended
2556 // e.g., Kefir.constant() or Kefir.never()
2557 if (!obs._alive) {
2558 if (obs._currentEvent) {
2559 this._emit(obs._currentEvent.type, obs._currentEvent.value);
2560 }
2561 // The _emit above could have caused this stream to end.
2562 if (this._active) {
2563 if (this._queue.length !== 0) {
2564 this._pullQueue();
2565 } else if (this._curSources.length === 0) {
2566 this._onEmpty();
2567 }
2568 }
2569 return;
2570 }
2571
2572 // #2
2573 // This one is for cases when `obs` going to end synchronously on
2574 // first subscriber e.g., Kefir.stream(em => {em.emit(1); em.end()})
2575 this._currentlyAdding = obs;
2576 obs.onAny(this._$handleSubAny);
2577 this._currentlyAdding = null;
2578 if (obs._alive) {
2579 this._curSources = concat(this._curSources, [obs]);
2580 if (this._active) {
2581 this._subToEnd(obs);
2582 }
2583 } else {
2584 if (this._queue.length !== 0) {
2585 this._pullQueue();
2586 } else if (this._curSources.length === 0) {
2587 this._onEmpty();
2588 }
2589 }
2590 } else {
2591 this._curSources = concat(this._curSources, [obs]);
2592 }
2593 },
2594 _subToEnd: function (obs) {
2595 var _this3 = this;
2596
2597 var onEnd = function () {
2598 return _this3._removeCur(obs);
2599 };
2600 this._$endHandlers.push({ obs: obs, handler: onEnd });
2601 obs.onEnd(onEnd);
2602 },
2603 _subscribe: function (obs) {
2604 obs.onAny(this._$handleSubAny);
2605
2606 // it can become inactive in responce of subscribing to `obs.onAny` above
2607 if (this._active) {
2608 this._subToEnd(obs);
2609 }
2610 },
2611 _unsubscribe: function (obs) {
2612 obs.offAny(this._$handleSubAny);
2613
2614 var onEndI = findByPred(this._$endHandlers, function (obj) {
2615 return obj.obs === obs;
2616 });
2617 if (onEndI !== -1) {
2618 obs.offEnd(this._$endHandlers[onEndI].handler);
2619 this._$endHandlers.splice(onEndI, 1);
2620 }
2621 },
2622 _handleSubAny: function (event) {
2623 if (event.type === VALUE) {
2624 this._emitValue(event.value);
2625 } else if (event.type === ERROR) {
2626 this._emitError(event.value);
2627 }
2628 },
2629 _removeQueue: function (obs) {
2630 var index = find(this._queue, obs);
2631 this._queue = remove(this._queue, index);
2632 return index;
2633 },
2634 _removeCur: function (obs) {
2635 if (this._active) {
2636 this._unsubscribe(obs);
2637 }
2638 var index = find(this._curSources, obs);
2639 this._curSources = remove(this._curSources, index);
2640 if (index !== -1) {
2641 if (this._queue.length !== 0) {
2642 this._pullQueue();
2643 } else if (this._curSources.length === 0) {
2644 this._onEmpty();
2645 }
2646 }
2647 return index;
2648 },
2649 _removeOldest: function () {
2650 this._removeCur(this._curSources[0]);
2651 },
2652 _pullQueue: function () {
2653 if (this._queue.length !== 0) {
2654 this._queue = cloneArray(this._queue);
2655 this._addToCur(this._queue.shift());
2656 }
2657 },
2658 _onActivation: function () {
2659 for (var i = 0, sources = this._curSources; i < sources.length && this._active; i++) {
2660 this._subscribe(sources[i]);
2661 }
2662 },
2663 _onDeactivation: function () {
2664 for (var i = 0, sources = this._curSources; i < sources.length; i++) {
2665 this._unsubscribe(sources[i]);
2666 }
2667 if (this._currentlyAdding !== null) {
2668 this._unsubscribe(this._currentlyAdding);
2669 }
2670 },
2671 _isEmpty: function () {
2672 return this._curSources.length === 0;
2673 },
2674 _onEmpty: function () {},
2675 _clear: function () {
2676 Stream.prototype._clear.call(this);
2677 this._queue = null;
2678 this._curSources = null;
2679 this._$handleSubAny = null;
2680 this._$endHandlers = null;
2681 }
2682});
2683
2684function Merge(sources) {
2685 AbstractPool.call(this);
2686 this._addAll(sources);
2687 this._initialised = true;
2688}
2689
2690inherit(Merge, AbstractPool, {
2691 _name: 'merge',
2692
2693 _onEmpty: function () {
2694 if (this._initialised) {
2695 this._emitEnd();
2696 }
2697 }
2698});
2699
2700function merge(observables) {
2701 return observables.length === 0 ? never() : new Merge(observables);
2702}
2703
2704function S$33(generator) {
2705 var _this = this;
2706
2707 Stream.call(this);
2708 this._generator = generator;
2709 this._source = null;
2710 this._inLoop = false;
2711 this._iteration = 0;
2712 this._$handleAny = function (event) {
2713 return _this._handleAny(event);
2714 };
2715}
2716
2717inherit(S$33, Stream, {
2718 _name: 'repeat',
2719
2720 _handleAny: function (event) {
2721 if (event.type === END) {
2722 this._source = null;
2723 this._getSource();
2724 } else {
2725 this._emit(event.type, event.value);
2726 }
2727 },
2728 _getSource: function () {
2729 if (!this._inLoop) {
2730 this._inLoop = true;
2731 var generator = this._generator;
2732 while (this._source === null && this._alive && this._active) {
2733 this._source = generator(this._iteration++);
2734 if (this._source) {
2735 this._source.onAny(this._$handleAny);
2736 } else {
2737 this._emitEnd();
2738 }
2739 }
2740 this._inLoop = false;
2741 }
2742 },
2743 _onActivation: function () {
2744 if (this._source) {
2745 this._source.onAny(this._$handleAny);
2746 } else {
2747 this._getSource();
2748 }
2749 },
2750 _onDeactivation: function () {
2751 if (this._source) {
2752 this._source.offAny(this._$handleAny);
2753 }
2754 },
2755 _clear: function () {
2756 Stream.prototype._clear.call(this);
2757 this._generator = null;
2758 this._source = null;
2759 this._$handleAny = null;
2760 }
2761});
2762
2763var repeat = function (generator) {
2764 return new S$33(generator);
2765};
2766
2767function concat$1(observables) {
2768 return repeat(function (index) {
2769 return observables.length > index ? observables[index] : false;
2770 }).setName('concat');
2771}
2772
2773function Pool() {
2774 AbstractPool.call(this);
2775}
2776
2777inherit(Pool, AbstractPool, {
2778 _name: 'pool',
2779
2780 plug: function (obs) {
2781 this._add(obs);
2782 return this;
2783 },
2784 unplug: function (obs) {
2785 this._remove(obs);
2786 return this;
2787 }
2788});
2789
2790function FlatMap(source, fn, options) {
2791 var _this = this;
2792
2793 AbstractPool.call(this, options);
2794 this._source = source;
2795 this._fn = fn;
2796 this._mainEnded = false;
2797 this._lastCurrent = null;
2798 this._$handleMain = function (event) {
2799 return _this._handleMain(event);
2800 };
2801}
2802
2803inherit(FlatMap, AbstractPool, {
2804 _onActivation: function () {
2805 AbstractPool.prototype._onActivation.call(this);
2806 if (this._active) {
2807 this._source.onAny(this._$handleMain);
2808 }
2809 },
2810 _onDeactivation: function () {
2811 AbstractPool.prototype._onDeactivation.call(this);
2812 this._source.offAny(this._$handleMain);
2813 this._hadNoEvSinceDeact = true;
2814 },
2815 _handleMain: function (event) {
2816 if (event.type === VALUE) {
2817 // Is latest value before deactivation survived, and now is 'current' on this activation?
2818 // We don't want to handle such values, to prevent to constantly add
2819 // same observale on each activation/deactivation when our main source
2820 // is a `Kefir.conatant()` for example.
2821 var sameCurr = this._activating && this._hadNoEvSinceDeact && this._lastCurrent === event.value;
2822 if (!sameCurr) {
2823 this._add(event.value, this._fn);
2824 }
2825 this._lastCurrent = event.value;
2826 this._hadNoEvSinceDeact = false;
2827 }
2828
2829 if (event.type === ERROR) {
2830 this._emitError(event.value);
2831 }
2832
2833 if (event.type === END) {
2834 if (this._isEmpty()) {
2835 this._emitEnd();
2836 } else {
2837 this._mainEnded = true;
2838 }
2839 }
2840 },
2841 _onEmpty: function () {
2842 if (this._mainEnded) {
2843 this._emitEnd();
2844 }
2845 },
2846 _clear: function () {
2847 AbstractPool.prototype._clear.call(this);
2848 this._source = null;
2849 this._lastCurrent = null;
2850 this._$handleMain = null;
2851 }
2852});
2853
2854function FlatMapErrors(source, fn) {
2855 FlatMap.call(this, source, fn);
2856}
2857
2858inherit(FlatMapErrors, FlatMap, {
2859 // Same as in FlatMap, only VALUE/ERROR flipped
2860 _handleMain: function (event) {
2861 if (event.type === ERROR) {
2862 var sameCurr = this._activating && this._hadNoEvSinceDeact && this._lastCurrent === event.value;
2863 if (!sameCurr) {
2864 this._add(event.value, this._fn);
2865 }
2866 this._lastCurrent = event.value;
2867 this._hadNoEvSinceDeact = false;
2868 }
2869
2870 if (event.type === VALUE) {
2871 this._emitValue(event.value);
2872 }
2873
2874 if (event.type === END) {
2875 if (this._isEmpty()) {
2876 this._emitEnd();
2877 } else {
2878 this._mainEnded = true;
2879 }
2880 }
2881 }
2882});
2883
2884function createConstructor$1(BaseClass, name) {
2885 return function AnonymousObservable(primary, secondary, options) {
2886 var _this = this;
2887
2888 BaseClass.call(this);
2889 this._primary = primary;
2890 this._secondary = secondary;
2891 this._name = primary._name + '.' + name;
2892 this._lastSecondary = NOTHING;
2893 this._$handleSecondaryAny = function (event) {
2894 return _this._handleSecondaryAny(event);
2895 };
2896 this._$handlePrimaryAny = function (event) {
2897 return _this._handlePrimaryAny(event);
2898 };
2899 this._init(options);
2900 };
2901}
2902
2903function createClassMethods$1(BaseClass) {
2904 return {
2905 _init: function () {},
2906 _free: function () {},
2907 _handlePrimaryValue: function (x) {
2908 this._emitValue(x);
2909 },
2910 _handlePrimaryError: function (x) {
2911 this._emitError(x);
2912 },
2913 _handlePrimaryEnd: function () {
2914 this._emitEnd();
2915 },
2916 _handleSecondaryValue: function (x) {
2917 this._lastSecondary = x;
2918 },
2919 _handleSecondaryError: function (x) {
2920 this._emitError(x);
2921 },
2922 _handleSecondaryEnd: function () {},
2923 _handlePrimaryAny: function (event) {
2924 switch (event.type) {
2925 case VALUE:
2926 return this._handlePrimaryValue(event.value);
2927 case ERROR:
2928 return this._handlePrimaryError(event.value);
2929 case END:
2930 return this._handlePrimaryEnd(event.value);
2931 }
2932 },
2933 _handleSecondaryAny: function (event) {
2934 switch (event.type) {
2935 case VALUE:
2936 return this._handleSecondaryValue(event.value);
2937 case ERROR:
2938 return this._handleSecondaryError(event.value);
2939 case END:
2940 this._handleSecondaryEnd(event.value);
2941 this._removeSecondary();
2942 }
2943 },
2944 _removeSecondary: function () {
2945 if (this._secondary !== null) {
2946 this._secondary.offAny(this._$handleSecondaryAny);
2947 this._$handleSecondaryAny = null;
2948 this._secondary = null;
2949 }
2950 },
2951 _onActivation: function () {
2952 if (this._secondary !== null) {
2953 this._secondary.onAny(this._$handleSecondaryAny);
2954 }
2955 if (this._active) {
2956 this._primary.onAny(this._$handlePrimaryAny);
2957 }
2958 },
2959 _onDeactivation: function () {
2960 if (this._secondary !== null) {
2961 this._secondary.offAny(this._$handleSecondaryAny);
2962 }
2963 this._primary.offAny(this._$handlePrimaryAny);
2964 },
2965 _clear: function () {
2966 BaseClass.prototype._clear.call(this);
2967 this._primary = null;
2968 this._secondary = null;
2969 this._lastSecondary = null;
2970 this._$handleSecondaryAny = null;
2971 this._$handlePrimaryAny = null;
2972 this._free();
2973 }
2974 };
2975}
2976
2977function createStream$1(name, mixin) {
2978 var S = createConstructor$1(Stream, name);
2979 inherit(S, Stream, createClassMethods$1(Stream), mixin);
2980 return S;
2981}
2982
2983function createProperty$1(name, mixin) {
2984 var P = createConstructor$1(Property, name);
2985 inherit(P, Property, createClassMethods$1(Property), mixin);
2986 return P;
2987}
2988
2989var mixin$26 = {
2990 _handlePrimaryValue: function (x) {
2991 if (this._lastSecondary !== NOTHING && this._lastSecondary) {
2992 this._emitValue(x);
2993 }
2994 },
2995 _handleSecondaryEnd: function () {
2996 if (this._lastSecondary === NOTHING || !this._lastSecondary) {
2997 this._emitEnd();
2998 }
2999 }
3000};
3001
3002var S$34 = createStream$1('filterBy', mixin$26);
3003var P$29 = createProperty$1('filterBy', mixin$26);
3004
3005function filterBy(primary, secondary) {
3006 return new (primary._ofSameType(S$34, P$29))(primary, secondary);
3007}
3008
3009var id2 = function (_, x) {
3010 return x;
3011};
3012
3013function sampledBy(passive, active, combinator) {
3014 var _combinator = combinator ? function (a, b) {
3015 return combinator(b, a);
3016 } : id2;
3017 return combine([active], [passive], _combinator).setName(passive, 'sampledBy');
3018}
3019
3020var mixin$27 = {
3021 _handlePrimaryValue: function (x) {
3022 if (this._lastSecondary !== NOTHING) {
3023 this._emitValue(x);
3024 }
3025 },
3026 _handleSecondaryEnd: function () {
3027 if (this._lastSecondary === NOTHING) {
3028 this._emitEnd();
3029 }
3030 }
3031};
3032
3033var S$35 = createStream$1('skipUntilBy', mixin$27);
3034var P$30 = createProperty$1('skipUntilBy', mixin$27);
3035
3036function skipUntilBy(primary, secondary) {
3037 return new (primary._ofSameType(S$35, P$30))(primary, secondary);
3038}
3039
3040var mixin$28 = {
3041 _handleSecondaryValue: function () {
3042 this._emitEnd();
3043 }
3044};
3045
3046var S$36 = createStream$1('takeUntilBy', mixin$28);
3047var P$31 = createProperty$1('takeUntilBy', mixin$28);
3048
3049function takeUntilBy(primary, secondary) {
3050 return new (primary._ofSameType(S$36, P$31))(primary, secondary);
3051}
3052
3053var mixin$29 = {
3054 _init: function () {
3055 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
3056 _ref$flushOnEnd = _ref.flushOnEnd,
3057 flushOnEnd = _ref$flushOnEnd === undefined ? true : _ref$flushOnEnd;
3058
3059 this._buff = [];
3060 this._flushOnEnd = flushOnEnd;
3061 },
3062 _free: function () {
3063 this._buff = null;
3064 },
3065 _flush: function () {
3066 if (this._buff !== null) {
3067 this._emitValue(this._buff);
3068 this._buff = [];
3069 }
3070 },
3071 _handlePrimaryEnd: function () {
3072 if (this._flushOnEnd) {
3073 this._flush();
3074 }
3075 this._emitEnd();
3076 },
3077 _onActivation: function () {
3078 this._primary.onAny(this._$handlePrimaryAny);
3079 if (this._alive && this._secondary !== null) {
3080 this._secondary.onAny(this._$handleSecondaryAny);
3081 }
3082 },
3083 _handlePrimaryValue: function (x) {
3084 this._buff.push(x);
3085 },
3086 _handleSecondaryValue: function () {
3087 this._flush();
3088 },
3089 _handleSecondaryEnd: function () {
3090 if (!this._flushOnEnd) {
3091 this._emitEnd();
3092 }
3093 }
3094};
3095
3096var S$37 = createStream$1('bufferBy', mixin$29);
3097var P$32 = createProperty$1('bufferBy', mixin$29);
3098
3099function bufferBy(primary, secondary, options /* optional */) {
3100 return new (primary._ofSameType(S$37, P$32))(primary, secondary, options);
3101}
3102
3103var mixin$30 = {
3104 _init: function () {
3105 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
3106 _ref$flushOnEnd = _ref.flushOnEnd,
3107 flushOnEnd = _ref$flushOnEnd === undefined ? true : _ref$flushOnEnd,
3108 _ref$flushOnChange = _ref.flushOnChange,
3109 flushOnChange = _ref$flushOnChange === undefined ? false : _ref$flushOnChange;
3110
3111 this._buff = [];
3112 this._flushOnEnd = flushOnEnd;
3113 this._flushOnChange = flushOnChange;
3114 },
3115 _free: function () {
3116 this._buff = null;
3117 },
3118 _flush: function () {
3119 if (this._buff !== null) {
3120 this._emitValue(this._buff);
3121 this._buff = [];
3122 }
3123 },
3124 _handlePrimaryEnd: function () {
3125 if (this._flushOnEnd) {
3126 this._flush();
3127 }
3128 this._emitEnd();
3129 },
3130 _handlePrimaryValue: function (x) {
3131 this._buff.push(x);
3132 if (this._lastSecondary !== NOTHING && !this._lastSecondary) {
3133 this._flush();
3134 }
3135 },
3136 _handleSecondaryEnd: function () {
3137 if (!this._flushOnEnd && (this._lastSecondary === NOTHING || this._lastSecondary)) {
3138 this._emitEnd();
3139 }
3140 },
3141 _handleSecondaryValue: function (x) {
3142 if (this._flushOnChange && !x) {
3143 this._flush();
3144 }
3145
3146 // from default _handleSecondaryValue
3147 this._lastSecondary = x;
3148 }
3149};
3150
3151var S$38 = createStream$1('bufferWhileBy', mixin$30);
3152var P$33 = createProperty$1('bufferWhileBy', mixin$30);
3153
3154function bufferWhileBy(primary, secondary, options /* optional */) {
3155 return new (primary._ofSameType(S$38, P$33))(primary, secondary, options);
3156}
3157
3158var f = function () {
3159 return false;
3160};
3161var t = function () {
3162 return true;
3163};
3164
3165function awaiting(a, b) {
3166 var result = merge([map$1(a, t), map$1(b, f)]);
3167 result = skipDuplicates(result);
3168 result = toProperty(result, f);
3169 return result.setName(a, 'awaiting');
3170}
3171
3172var mixin$31 = {
3173 _init: function (_ref) {
3174 var fn = _ref.fn;
3175
3176 this._fn = fn;
3177 },
3178 _free: function () {
3179 this._fn = null;
3180 },
3181 _handleValue: function (x) {
3182 var fn = this._fn;
3183 var result = fn(x);
3184 if (result.convert) {
3185 this._emitError(result.error);
3186 } else {
3187 this._emitValue(x);
3188 }
3189 }
3190};
3191
3192var S$39 = createStream('valuesToErrors', mixin$31);
3193var P$34 = createProperty('valuesToErrors', mixin$31);
3194
3195var defFn = function (x) {
3196 return { convert: true, error: x };
3197};
3198
3199function valuesToErrors(obs) {
3200 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defFn;
3201
3202 return new (obs._ofSameType(S$39, P$34))(obs, { fn: fn });
3203}
3204
3205var mixin$32 = {
3206 _init: function (_ref) {
3207 var fn = _ref.fn;
3208
3209 this._fn = fn;
3210 },
3211 _free: function () {
3212 this._fn = null;
3213 },
3214 _handleError: function (x) {
3215 var fn = this._fn;
3216 var result = fn(x);
3217 if (result.convert) {
3218 this._emitValue(result.value);
3219 } else {
3220 this._emitError(x);
3221 }
3222 }
3223};
3224
3225var S$40 = createStream('errorsToValues', mixin$32);
3226var P$35 = createProperty('errorsToValues', mixin$32);
3227
3228var defFn$1 = function (x) {
3229 return { convert: true, value: x };
3230};
3231
3232function errorsToValues(obs) {
3233 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defFn$1;
3234
3235 return new (obs._ofSameType(S$40, P$35))(obs, { fn: fn });
3236}
3237
3238var mixin$33 = {
3239 _handleError: function (x) {
3240 this._emitError(x);
3241 this._emitEnd();
3242 }
3243};
3244
3245var S$41 = createStream('endOnError', mixin$33);
3246var P$36 = createProperty('endOnError', mixin$33);
3247
3248function endOnError(obs) {
3249 return new (obs._ofSameType(S$41, P$36))(obs);
3250}
3251
3252// Create a stream
3253// -----------------------------------------------------------------------------
3254
3255// () -> Stream
3256// (number, any) -> Stream
3257// (number, any) -> Stream
3258// (number, Array<any>) -> Stream
3259// (number, Function) -> Stream
3260// (number, Function) -> Stream
3261// (Function) -> Stream
3262// (Function) -> Stream
3263// Target = {addEventListener, removeEventListener}|{addListener, removeListener}|{on, off}
3264// (Target, string, Function|undefined) -> Stream
3265// (Function) -> Stream
3266// Create a property
3267// -----------------------------------------------------------------------------
3268
3269// (any) -> Property
3270// (any) -> Property
3271// Convert observables
3272// -----------------------------------------------------------------------------
3273
3274// (Stream|Property, Function|undefined) -> Property
3275Observable.prototype.toProperty = function (fn) {
3276 return toProperty(this, fn);
3277};
3278
3279// (Stream|Property) -> Stream
3280Observable.prototype.changes = function () {
3281 return changes(this);
3282};
3283
3284// Interoperation with other implimentations
3285// -----------------------------------------------------------------------------
3286
3287// (Promise) -> Property
3288// (Stream|Property, Function|undefined) -> Promise
3289Observable.prototype.toPromise = function (Promise) {
3290 return toPromise(this, Promise);
3291};
3292
3293// (ESObservable) -> Stream
3294// (Stream|Property) -> ES7 Observable
3295Observable.prototype.toESObservable = toESObservable;
3296Observable.prototype[$$observable] = toESObservable;
3297
3298// Modify an observable
3299// -----------------------------------------------------------------------------
3300
3301// (Stream, Function|undefined) -> Stream
3302// (Property, Function|undefined) -> Property
3303Observable.prototype.map = function (fn) {
3304 return map$1(this, fn);
3305};
3306
3307// (Stream, Function|undefined) -> Stream
3308// (Property, Function|undefined) -> Property
3309Observable.prototype.filter = function (fn) {
3310 return filter(this, fn);
3311};
3312
3313// (Stream, number) -> Stream
3314// (Property, number) -> Property
3315Observable.prototype.take = function (n) {
3316 return take(this, n);
3317};
3318
3319// (Stream, number) -> Stream
3320// (Property, number) -> Property
3321Observable.prototype.takeErrors = function (n) {
3322 return takeErrors(this, n);
3323};
3324
3325// (Stream, Function|undefined) -> Stream
3326// (Property, Function|undefined) -> Property
3327Observable.prototype.takeWhile = function (fn) {
3328 return takeWhile(this, fn);
3329};
3330
3331// (Stream) -> Stream
3332// (Property) -> Property
3333Observable.prototype.last = function () {
3334 return last(this);
3335};
3336
3337// (Stream, number) -> Stream
3338// (Property, number) -> Property
3339Observable.prototype.skip = function (n) {
3340 return skip(this, n);
3341};
3342
3343// (Stream, Function|undefined) -> Stream
3344// (Property, Function|undefined) -> Property
3345Observable.prototype.skipWhile = function (fn) {
3346 return skipWhile(this, fn);
3347};
3348
3349// (Stream, Function|undefined) -> Stream
3350// (Property, Function|undefined) -> Property
3351Observable.prototype.skipDuplicates = function (fn) {
3352 return skipDuplicates(this, fn);
3353};
3354
3355// (Stream, Function|falsey, any|undefined) -> Stream
3356// (Property, Function|falsey, any|undefined) -> Property
3357Observable.prototype.diff = function (fn, seed) {
3358 return diff(this, fn, seed);
3359};
3360
3361// (Stream|Property, Function, any|undefined) -> Property
3362Observable.prototype.scan = function (fn, seed) {
3363 return scan(this, fn, seed);
3364};
3365
3366// (Stream, Function|undefined) -> Stream
3367// (Property, Function|undefined) -> Property
3368Observable.prototype.flatten = function (fn) {
3369 return flatten(this, fn);
3370};
3371
3372// (Stream, number) -> Stream
3373// (Property, number) -> Property
3374Observable.prototype.delay = function (wait) {
3375 return delay(this, wait);
3376};
3377
3378// Options = {leading: boolean|undefined, trailing: boolean|undefined}
3379// (Stream, number, Options|undefined) -> Stream
3380// (Property, number, Options|undefined) -> Property
3381Observable.prototype.throttle = function (wait, options) {
3382 return throttle(this, wait, options);
3383};
3384
3385// Options = {immediate: boolean|undefined}
3386// (Stream, number, Options|undefined) -> Stream
3387// (Property, number, Options|undefined) -> Property
3388Observable.prototype.debounce = function (wait, options) {
3389 return debounce(this, wait, options);
3390};
3391
3392// (Stream, Function|undefined) -> Stream
3393// (Property, Function|undefined) -> Property
3394Observable.prototype.mapErrors = function (fn) {
3395 return mapErrors(this, fn);
3396};
3397
3398// (Stream, Function|undefined) -> Stream
3399// (Property, Function|undefined) -> Property
3400Observable.prototype.filterErrors = function (fn) {
3401 return filterErrors(this, fn);
3402};
3403
3404// (Stream) -> Stream
3405// (Property) -> Property
3406Observable.prototype.ignoreValues = function () {
3407 return ignoreValues(this);
3408};
3409
3410// (Stream) -> Stream
3411// (Property) -> Property
3412Observable.prototype.ignoreErrors = function () {
3413 return ignoreErrors(this);
3414};
3415
3416// (Stream) -> Stream
3417// (Property) -> Property
3418Observable.prototype.ignoreEnd = function () {
3419 return ignoreEnd(this);
3420};
3421
3422// (Stream, Function) -> Stream
3423// (Property, Function) -> Property
3424Observable.prototype.beforeEnd = function (fn) {
3425 return beforeEnd(this, fn);
3426};
3427
3428// (Stream, number, number|undefined) -> Stream
3429// (Property, number, number|undefined) -> Property
3430Observable.prototype.slidingWindow = function (max, min) {
3431 return slidingWindow(this, max, min);
3432};
3433
3434// Options = {flushOnEnd: boolean|undefined}
3435// (Stream, Function|falsey, Options|undefined) -> Stream
3436// (Property, Function|falsey, Options|undefined) -> Property
3437Observable.prototype.bufferWhile = function (fn, options) {
3438 return bufferWhile(this, fn, options);
3439};
3440
3441// (Stream, number) -> Stream
3442// (Property, number) -> Property
3443Observable.prototype.bufferWithCount = function (count, options) {
3444 return bufferWhile$1(this, count, options);
3445};
3446
3447// Options = {flushOnEnd: boolean|undefined}
3448// (Stream, number, number, Options|undefined) -> Stream
3449// (Property, number, number, Options|undefined) -> Property
3450Observable.prototype.bufferWithTimeOrCount = function (wait, count, options) {
3451 return bufferWithTimeOrCount(this, wait, count, options);
3452};
3453
3454// (Stream, Function) -> Stream
3455// (Property, Function) -> Property
3456Observable.prototype.transduce = function (transducer) {
3457 return transduce(this, transducer);
3458};
3459
3460// (Stream, Function) -> Stream
3461// (Property, Function) -> Property
3462Observable.prototype.withHandler = function (fn) {
3463 return withHandler(this, fn);
3464};
3465
3466// (Stream, Stream -> a) -> a
3467// (Property, Property -> a) -> a
3468Observable.prototype.thru = function (fn) {
3469 return fn(this);
3470};
3471
3472// Combine observables
3473// -----------------------------------------------------------------------------
3474
3475// (Array<Stream|Property>, Function|undefiend) -> Stream
3476// (Array<Stream|Property>, Array<Stream|Property>, Function|undefiend) -> Stream
3477Observable.prototype.combine = function (other, combinator) {
3478 return combine([this, other], combinator);
3479};
3480
3481// (Array<Stream|Property>, Function|undefiend) -> Stream
3482Observable.prototype.zip = function (other, combinator) {
3483 return zip([this, other], combinator);
3484};
3485
3486// (Array<Stream|Property>) -> Stream
3487Observable.prototype.merge = function (other) {
3488 return merge([this, other]);
3489};
3490
3491// (Array<Stream|Property>) -> Stream
3492Observable.prototype.concat = function (other) {
3493 return concat$1([this, other]);
3494};
3495
3496// () -> Pool
3497var pool = function () {
3498 return new Pool();
3499};
3500
3501// (Function) -> Stream
3502// Options = {concurLim: number|undefined, queueLim: number|undefined, drop: 'old'|'new'|undefiend}
3503// (Stream|Property, Function|falsey, Options|undefined) -> Stream
3504Observable.prototype.flatMap = function (fn) {
3505 return new FlatMap(this, fn).setName(this, 'flatMap');
3506};
3507Observable.prototype.flatMapLatest = function (fn) {
3508 return new FlatMap(this, fn, { concurLim: 1, drop: 'old' }).setName(this, 'flatMapLatest');
3509};
3510Observable.prototype.flatMapFirst = function (fn) {
3511 return new FlatMap(this, fn, { concurLim: 1 }).setName(this, 'flatMapFirst');
3512};
3513Observable.prototype.flatMapConcat = function (fn) {
3514 return new FlatMap(this, fn, { queueLim: -1, concurLim: 1 }).setName(this, 'flatMapConcat');
3515};
3516Observable.prototype.flatMapConcurLimit = function (fn, limit) {
3517 return new FlatMap(this, fn, { queueLim: -1, concurLim: limit }).setName(this, 'flatMapConcurLimit');
3518};
3519
3520// (Stream|Property, Function|falsey) -> Stream
3521Observable.prototype.flatMapErrors = function (fn) {
3522 return new FlatMapErrors(this, fn).setName(this, 'flatMapErrors');
3523};
3524
3525// Combine two observables
3526// -----------------------------------------------------------------------------
3527
3528// (Stream, Stream|Property) -> Stream
3529// (Property, Stream|Property) -> Property
3530Observable.prototype.filterBy = function (other) {
3531 return filterBy(this, other);
3532};
3533
3534// (Stream, Stream|Property, Function|undefiend) -> Stream
3535// (Property, Stream|Property, Function|undefiend) -> Property
3536Observable.prototype.sampledBy = function (other, combinator) {
3537 return sampledBy(this, other, combinator);
3538};
3539
3540// (Stream, Stream|Property) -> Stream
3541// (Property, Stream|Property) -> Property
3542Observable.prototype.skipUntilBy = function (other) {
3543 return skipUntilBy(this, other);
3544};
3545
3546// (Stream, Stream|Property) -> Stream
3547// (Property, Stream|Property) -> Property
3548Observable.prototype.takeUntilBy = function (other) {
3549 return takeUntilBy(this, other);
3550};
3551
3552// Options = {flushOnEnd: boolean|undefined}
3553// (Stream, Stream|Property, Options|undefined) -> Stream
3554// (Property, Stream|Property, Options|undefined) -> Property
3555Observable.prototype.bufferBy = function (other, options) {
3556 return bufferBy(this, other, options);
3557};
3558
3559// Options = {flushOnEnd: boolean|undefined}
3560// (Stream, Stream|Property, Options|undefined) -> Stream
3561// (Property, Stream|Property, Options|undefined) -> Property
3562Observable.prototype.bufferWhileBy = function (other, options) {
3563 return bufferWhileBy(this, other, options);
3564};
3565
3566// Deprecated
3567// -----------------------------------------------------------------------------
3568
3569var DEPRECATION_WARNINGS = true;
3570function dissableDeprecationWarnings() {
3571 DEPRECATION_WARNINGS = false;
3572}
3573
3574function warn(msg) {
3575 if (DEPRECATION_WARNINGS && console && typeof console.warn === 'function') {
3576 var msg2 = '\nHere is an Error object for you containing the call stack:';
3577 console.warn(msg, msg2, new Error());
3578 }
3579}
3580
3581// (Stream|Property, Stream|Property) -> Property
3582Observable.prototype.awaiting = function (other) {
3583 warn('You are using deprecated .awaiting() method, see https://github.com/kefirjs/kefir/issues/145');
3584 return awaiting(this, other);
3585};
3586
3587// (Stream, Function|undefined) -> Stream
3588// (Property, Function|undefined) -> Property
3589Observable.prototype.valuesToErrors = function (fn) {
3590 warn('You are using deprecated .valuesToErrors() method, see https://github.com/kefirjs/kefir/issues/149');
3591 return valuesToErrors(this, fn);
3592};
3593
3594// (Stream, Function|undefined) -> Stream
3595// (Property, Function|undefined) -> Property
3596Observable.prototype.errorsToValues = function (fn) {
3597 warn('You are using deprecated .errorsToValues() method, see https://github.com/kefirjs/kefir/issues/149');
3598 return errorsToValues(this, fn);
3599};
3600
3601// (Stream) -> Stream
3602// (Property) -> Property
3603Observable.prototype.endOnError = function () {
3604 warn('You are using deprecated .endOnError() method, see https://github.com/kefirjs/kefir/issues/150');
3605 return endOnError(this);
3606};
3607
3608// Exports
3609// --------------------------------------------------------------------------
3610
3611var Kefir = {
3612 Observable: Observable,
3613 Stream: Stream,
3614 Property: Property,
3615 never: never,
3616 later: later,
3617 interval: interval,
3618 sequentially: sequentially,
3619 fromPoll: fromPoll,
3620 withInterval: withInterval,
3621 fromCallback: fromCallback,
3622 fromNodeCallback: fromNodeCallback,
3623 fromEvents: fromEvents,
3624 stream: stream,
3625 constant: constant,
3626 constantError: constantError,
3627 fromPromise: fromPromise,
3628 fromESObservable: fromESObservable,
3629 combine: combine,
3630 zip: zip,
3631 merge: merge,
3632 concat: concat$1,
3633 Pool: Pool,
3634 pool: pool,
3635 repeat: repeat,
3636 staticLand: staticLand
3637};
3638
3639Kefir.Kefir = Kefir;
3640
3641export { dissableDeprecationWarnings, Kefir, Observable, Stream, Property, never, later, interval, sequentially, fromPoll, withInterval, fromCallback, fromNodeCallback, fromEvents, stream, constant, constantError, fromPromise, fromESObservable, combine, zip, merge, concat$1 as concat, Pool, pool, repeat, staticLand };export default Kefir;