UNPKG

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