UNPKG

89.4 kBJavaScriptView Raw
1/*! Kefir.js v3.8.5
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
1094var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
1095
1096
1097
1098
1099
1100function createCommonjsModule(fn, module) {
1101 return module = { exports: {} }, fn(module, module.exports), module.exports;
1102}
1103
1104var ponyfill = createCommonjsModule(function (module, exports) {
1105'use strict';
1106
1107Object.defineProperty(exports, "__esModule", {
1108 value: true
1109});
1110exports['default'] = symbolObservablePonyfill;
1111function symbolObservablePonyfill(root) {
1112 var result;
1113 var _Symbol = root.Symbol;
1114
1115 if (typeof _Symbol === 'function') {
1116 if (_Symbol.observable) {
1117 result = _Symbol.observable;
1118 } else {
1119 result = _Symbol('observable');
1120 _Symbol.observable = result;
1121 }
1122 } else {
1123 result = '@@observable';
1124 }
1125
1126 return result;
1127}
1128});
1129
1130var index$1 = createCommonjsModule(function (module, exports) {
1131'use strict';
1132
1133Object.defineProperty(exports, "__esModule", {
1134 value: true
1135});
1136
1137
1138
1139var _ponyfill2 = _interopRequireDefault(ponyfill);
1140
1141function _interopRequireDefault(obj) {
1142 return obj && obj.__esModule ? obj : { 'default': obj };
1143}
1144
1145var root; /* global window */
1146
1147if (typeof self !== 'undefined') {
1148 root = self;
1149} else if (typeof window !== 'undefined') {
1150 root = window;
1151} else if (typeof commonjsGlobal !== 'undefined') {
1152 root = commonjsGlobal;
1153} else {
1154 root = module;
1155}
1156
1157var result = (0, _ponyfill2['default'])(root);
1158exports['default'] = result;
1159});
1160
1161var index = index$1;
1162
1163// this file contains some hot JS modules systems stuff
1164
1165var $$observable = index.default ? index.default : index;
1166
1167function fromESObservable(_observable) {
1168 var observable = _observable[$$observable] ? _observable[$$observable]() : _observable;
1169 return stream(function (emitter) {
1170 var unsub = observable.subscribe({
1171 error: function (error) {
1172 emitter.error(error);
1173 emitter.end();
1174 },
1175 next: function (value) {
1176 emitter.emit(value);
1177 },
1178 complete: function () {
1179 emitter.end();
1180 }
1181 });
1182
1183 if (unsub.unsubscribe) {
1184 return function () {
1185 unsub.unsubscribe();
1186 };
1187 } else {
1188 return unsub;
1189 }
1190 }).setName('fromESObservable');
1191}
1192
1193function ESObservable(observable) {
1194 this._observable = observable.takeErrors(1);
1195}
1196
1197extend(ESObservable.prototype, {
1198 subscribe: function (observerOrOnNext, onError, onComplete) {
1199 var _this = this;
1200
1201 var observer = typeof observerOrOnNext === 'function' ? { next: observerOrOnNext, error: onError, complete: onComplete } : observerOrOnNext;
1202
1203 var fn = function (event) {
1204 if (event.type === END) {
1205 closed = true;
1206 }
1207
1208 if (event.type === VALUE && observer.next) {
1209 observer.next(event.value);
1210 } else if (event.type === ERROR && observer.error) {
1211 observer.error(event.value);
1212 } else if (event.type === END && observer.complete) {
1213 observer.complete(event.value);
1214 }
1215 };
1216
1217 this._observable.onAny(fn);
1218 var closed = false;
1219
1220 var subscription = {
1221 unsubscribe: function () {
1222 closed = true;
1223 _this._observable.offAny(fn);
1224 },
1225 get closed() {
1226 return closed;
1227 }
1228 };
1229 return subscription;
1230 }
1231});
1232
1233// Need to assign directly b/c Symbols aren't enumerable.
1234ESObservable.prototype[$$observable] = function () {
1235 return this;
1236};
1237
1238function toESObservable() {
1239 return new ESObservable(this);
1240}
1241
1242function collect(source, keys, values) {
1243 for (var prop in source) {
1244 if (source.hasOwnProperty(prop)) {
1245 keys.push(prop);
1246 values.push(source[prop]);
1247 }
1248 }
1249}
1250
1251function defaultErrorsCombinator(errors) {
1252 var latestError = void 0;
1253 for (var i = 0; i < errors.length; i++) {
1254 if (errors[i] !== undefined) {
1255 if (latestError === undefined || latestError.index < errors[i].index) {
1256 latestError = errors[i];
1257 }
1258 }
1259 }
1260 return latestError.error;
1261}
1262
1263function Combine(active, passive, combinator) {
1264 var _this = this;
1265
1266 Stream.call(this);
1267 this._activeCount = active.length;
1268 this._sources = concat(active, passive);
1269 this._combinator = combinator;
1270 this._aliveCount = 0;
1271 this._latestValues = new Array(this._sources.length);
1272 this._latestErrors = new Array(this._sources.length);
1273 fillArray(this._latestValues, NOTHING);
1274 this._emitAfterActivation = false;
1275 this._endAfterActivation = false;
1276 this._latestErrorIndex = 0;
1277
1278 this._$handlers = [];
1279
1280 var _loop = function (i) {
1281 _this._$handlers.push(function (event) {
1282 return _this._handleAny(i, event);
1283 });
1284 };
1285
1286 for (var i = 0; i < this._sources.length; i++) {
1287 _loop(i);
1288 }
1289}
1290
1291inherit(Combine, Stream, {
1292 _name: 'combine',
1293
1294 _onActivation: function () {
1295 this._aliveCount = this._activeCount;
1296
1297 // we need to suscribe to _passive_ sources before _active_
1298 // (see https://github.com/kefirjs/kefir/issues/98)
1299 for (var i = this._activeCount; i < this._sources.length; i++) {
1300 this._sources[i].onAny(this._$handlers[i]);
1301 }
1302 for (var _i = 0; _i < this._activeCount; _i++) {
1303 this._sources[_i].onAny(this._$handlers[_i]);
1304 }
1305
1306 if (this._emitAfterActivation) {
1307 this._emitAfterActivation = false;
1308 this._emitIfFull();
1309 }
1310 if (this._endAfterActivation) {
1311 this._emitEnd();
1312 }
1313 },
1314 _onDeactivation: function () {
1315 var length = this._sources.length,
1316 i = void 0;
1317 for (i = 0; i < length; i++) {
1318 this._sources[i].offAny(this._$handlers[i]);
1319 }
1320 },
1321 _emitIfFull: function () {
1322 var hasAllValues = true;
1323 var hasErrors = false;
1324 var length = this._latestValues.length;
1325 var valuesCopy = new Array(length);
1326 var errorsCopy = new Array(length);
1327
1328 for (var i = 0; i < length; i++) {
1329 valuesCopy[i] = this._latestValues[i];
1330 errorsCopy[i] = this._latestErrors[i];
1331
1332 if (valuesCopy[i] === NOTHING) {
1333 hasAllValues = false;
1334 }
1335
1336 if (errorsCopy[i] !== undefined) {
1337 hasErrors = true;
1338 }
1339 }
1340
1341 if (hasAllValues) {
1342 var combinator = this._combinator;
1343 this._emitValue(combinator(valuesCopy));
1344 }
1345 if (hasErrors) {
1346 this._emitError(defaultErrorsCombinator(errorsCopy));
1347 }
1348 },
1349 _handleAny: function (i, event) {
1350 if (event.type === VALUE || event.type === ERROR) {
1351 if (event.type === VALUE) {
1352 this._latestValues[i] = event.value;
1353 this._latestErrors[i] = undefined;
1354 }
1355 if (event.type === ERROR) {
1356 this._latestValues[i] = NOTHING;
1357 this._latestErrors[i] = {
1358 index: this._latestErrorIndex++,
1359 error: event.value
1360 };
1361 }
1362
1363 if (i < this._activeCount) {
1364 if (this._activating) {
1365 this._emitAfterActivation = true;
1366 } else {
1367 this._emitIfFull();
1368 }
1369 }
1370 } else {
1371 // END
1372
1373 if (i < this._activeCount) {
1374 this._aliveCount--;
1375 if (this._aliveCount === 0) {
1376 if (this._activating) {
1377 this._endAfterActivation = true;
1378 } else {
1379 this._emitEnd();
1380 }
1381 }
1382 }
1383 }
1384 },
1385 _clear: function () {
1386 Stream.prototype._clear.call(this);
1387 this._sources = null;
1388 this._latestValues = null;
1389 this._latestErrors = null;
1390 this._combinator = null;
1391 this._$handlers = null;
1392 }
1393});
1394
1395function combineAsArray(active) {
1396 var passive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
1397 var combinator = arguments[2];
1398
1399 if (!Array.isArray(passive)) {
1400 throw new Error('Combine can only combine active and passive collections of the same type.');
1401 }
1402
1403 combinator = combinator ? spread(combinator, active.length + passive.length) : function (x) {
1404 return x;
1405 };
1406 return active.length === 0 ? never() : new Combine(active, passive, combinator);
1407}
1408
1409function combineAsObject(active) {
1410 var passive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1411 var combinator = arguments[2];
1412
1413 if (typeof passive !== 'object' || Array.isArray(passive)) {
1414 throw new Error('Combine can only combine active and passive collections of the same type.');
1415 }
1416
1417 var keys = [],
1418 activeObservables = [],
1419 passiveObservables = [];
1420
1421 collect(active, keys, activeObservables);
1422 collect(passive, keys, passiveObservables);
1423
1424 var objectify = function (values) {
1425 var event = {};
1426 for (var i = values.length - 1; 0 <= i; i--) {
1427 event[keys[i]] = values[i];
1428 }
1429 return combinator ? combinator(event) : event;
1430 };
1431
1432 return activeObservables.length === 0 ? never() : new Combine(activeObservables, passiveObservables, objectify);
1433}
1434
1435function combine(active, passive, combinator) {
1436 if (typeof passive === 'function') {
1437 combinator = passive;
1438 passive = undefined;
1439 }
1440
1441 return Array.isArray(active) ? combineAsArray(active, passive, combinator) : combineAsObject(active, passive, combinator);
1442}
1443
1444var Observable$2 = {
1445 empty: function () {
1446 return never();
1447 },
1448
1449
1450 // Monoid based on merge() seems more useful than one based on concat().
1451 concat: function (a, b) {
1452 return a.merge(b);
1453 },
1454 of: function (x) {
1455 return constant(x);
1456 },
1457 map: function (fn, obs) {
1458 return obs.map(fn);
1459 },
1460 bimap: function (fnErr, fnVal, obs) {
1461 return obs.mapErrors(fnErr).map(fnVal);
1462 },
1463
1464
1465 // This ap strictly speaking incompatible with chain. If we derive ap from chain we get
1466 // different (not very useful) behavior. But spec requires that if method can be derived
1467 // it must have the same behavior as hand-written method. We intentionally violate the spec
1468 // in hope that it won't cause many troubles in practice. And in return we have more useful type.
1469 ap: function (obsFn, obsVal) {
1470 return combine([obsFn, obsVal], function (fn, val) {
1471 return fn(val);
1472 });
1473 },
1474 chain: function (fn, obs) {
1475 return obs.flatMap(fn);
1476 }
1477};
1478
1479
1480
1481var staticLand = Object.freeze({
1482 Observable: Observable$2
1483});
1484
1485var mixin = {
1486 _init: function (_ref) {
1487 var fn = _ref.fn;
1488
1489 this._fn = fn;
1490 },
1491 _free: function () {
1492 this._fn = null;
1493 },
1494 _handleValue: function (x) {
1495 var fn = this._fn;
1496 this._emitValue(fn(x));
1497 }
1498};
1499
1500var S$7 = createStream('map', mixin);
1501var P$3 = createProperty('map', mixin);
1502
1503var id = function (x) {
1504 return x;
1505};
1506
1507function map$1(obs) {
1508 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id;
1509
1510 return new (obs._ofSameType(S$7, P$3))(obs, { fn: fn });
1511}
1512
1513var mixin$1 = {
1514 _init: function (_ref) {
1515 var fn = _ref.fn;
1516
1517 this._fn = fn;
1518 },
1519 _free: function () {
1520 this._fn = null;
1521 },
1522 _handleValue: function (x) {
1523 var fn = this._fn;
1524 if (fn(x)) {
1525 this._emitValue(x);
1526 }
1527 }
1528};
1529
1530var S$8 = createStream('filter', mixin$1);
1531var P$4 = createProperty('filter', mixin$1);
1532
1533var id$1 = function (x) {
1534 return x;
1535};
1536
1537function filter(obs) {
1538 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$1;
1539
1540 return new (obs._ofSameType(S$8, P$4))(obs, { fn: fn });
1541}
1542
1543var mixin$2 = {
1544 _init: function (_ref) {
1545 var n = _ref.n;
1546
1547 this._n = n;
1548 if (n <= 0) {
1549 this._emitEnd();
1550 }
1551 },
1552 _handleValue: function (x) {
1553 if (this._n === 0) {
1554 return;
1555 }
1556 this._n--;
1557 this._emitValue(x);
1558 if (this._n === 0) {
1559 this._emitEnd();
1560 }
1561 }
1562};
1563
1564var S$9 = createStream('take', mixin$2);
1565var P$5 = createProperty('take', mixin$2);
1566
1567function take(obs, n) {
1568 return new (obs._ofSameType(S$9, P$5))(obs, { n: n });
1569}
1570
1571var mixin$3 = {
1572 _init: function (_ref) {
1573 var n = _ref.n;
1574
1575 this._n = n;
1576 if (n <= 0) {
1577 this._emitEnd();
1578 }
1579 },
1580 _handleError: function (x) {
1581 if (this._n === 0) {
1582 return;
1583 }
1584 this._n--;
1585 this._emitError(x);
1586 if (this._n === 0) {
1587 this._emitEnd();
1588 }
1589 }
1590};
1591
1592var S$10 = createStream('takeErrors', mixin$3);
1593var P$6 = createProperty('takeErrors', mixin$3);
1594
1595function takeErrors(obs, n) {
1596 return new (obs._ofSameType(S$10, P$6))(obs, { n: n });
1597}
1598
1599var mixin$4 = {
1600 _init: function (_ref) {
1601 var fn = _ref.fn;
1602
1603 this._fn = fn;
1604 },
1605 _free: function () {
1606 this._fn = null;
1607 },
1608 _handleValue: function (x) {
1609 var fn = this._fn;
1610 if (fn(x)) {
1611 this._emitValue(x);
1612 } else {
1613 this._emitEnd();
1614 }
1615 }
1616};
1617
1618var S$11 = createStream('takeWhile', mixin$4);
1619var P$7 = createProperty('takeWhile', mixin$4);
1620
1621var id$2 = function (x) {
1622 return x;
1623};
1624
1625function takeWhile(obs) {
1626 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$2;
1627
1628 return new (obs._ofSameType(S$11, P$7))(obs, { fn: fn });
1629}
1630
1631var mixin$5 = {
1632 _init: function () {
1633 this._lastValue = NOTHING;
1634 },
1635 _free: function () {
1636 this._lastValue = null;
1637 },
1638 _handleValue: function (x) {
1639 this._lastValue = x;
1640 },
1641 _handleEnd: function () {
1642 if (this._lastValue !== NOTHING) {
1643 this._emitValue(this._lastValue);
1644 }
1645 this._emitEnd();
1646 }
1647};
1648
1649var S$12 = createStream('last', mixin$5);
1650var P$8 = createProperty('last', mixin$5);
1651
1652function last(obs) {
1653 return new (obs._ofSameType(S$12, P$8))(obs);
1654}
1655
1656var mixin$6 = {
1657 _init: function (_ref) {
1658 var n = _ref.n;
1659
1660 this._n = Math.max(0, n);
1661 },
1662 _handleValue: function (x) {
1663 if (this._n === 0) {
1664 this._emitValue(x);
1665 } else {
1666 this._n--;
1667 }
1668 }
1669};
1670
1671var S$13 = createStream('skip', mixin$6);
1672var P$9 = createProperty('skip', mixin$6);
1673
1674function skip(obs, n) {
1675 return new (obs._ofSameType(S$13, P$9))(obs, { n: n });
1676}
1677
1678var mixin$7 = {
1679 _init: function (_ref) {
1680 var fn = _ref.fn;
1681
1682 this._fn = fn;
1683 },
1684 _free: function () {
1685 this._fn = null;
1686 },
1687 _handleValue: function (x) {
1688 var fn = this._fn;
1689 if (this._fn !== null && !fn(x)) {
1690 this._fn = null;
1691 }
1692 if (this._fn === null) {
1693 this._emitValue(x);
1694 }
1695 }
1696};
1697
1698var S$14 = createStream('skipWhile', mixin$7);
1699var P$10 = createProperty('skipWhile', mixin$7);
1700
1701var id$3 = function (x) {
1702 return x;
1703};
1704
1705function skipWhile(obs) {
1706 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$3;
1707
1708 return new (obs._ofSameType(S$14, P$10))(obs, { fn: fn });
1709}
1710
1711var mixin$8 = {
1712 _init: function (_ref) {
1713 var fn = _ref.fn;
1714
1715 this._fn = fn;
1716 this._prev = NOTHING;
1717 },
1718 _free: function () {
1719 this._fn = null;
1720 this._prev = null;
1721 },
1722 _handleValue: function (x) {
1723 var fn = this._fn;
1724 if (this._prev === NOTHING || !fn(this._prev, x)) {
1725 this._prev = x;
1726 this._emitValue(x);
1727 }
1728 }
1729};
1730
1731var S$15 = createStream('skipDuplicates', mixin$8);
1732var P$11 = createProperty('skipDuplicates', mixin$8);
1733
1734var eq = function (a, b) {
1735 return a === b;
1736};
1737
1738function skipDuplicates(obs) {
1739 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : eq;
1740
1741 return new (obs._ofSameType(S$15, P$11))(obs, { fn: fn });
1742}
1743
1744var mixin$9 = {
1745 _init: function (_ref) {
1746 var fn = _ref.fn,
1747 seed = _ref.seed;
1748
1749 this._fn = fn;
1750 this._prev = seed;
1751 },
1752 _free: function () {
1753 this._prev = null;
1754 this._fn = null;
1755 },
1756 _handleValue: function (x) {
1757 if (this._prev !== NOTHING) {
1758 var fn = this._fn;
1759 this._emitValue(fn(this._prev, x));
1760 }
1761 this._prev = x;
1762 }
1763};
1764
1765var S$16 = createStream('diff', mixin$9);
1766var P$12 = createProperty('diff', mixin$9);
1767
1768function defaultFn(a, b) {
1769 return [a, b];
1770}
1771
1772function diff(obs, fn) {
1773 var seed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NOTHING;
1774
1775 return new (obs._ofSameType(S$16, P$12))(obs, { fn: fn || defaultFn, seed: seed });
1776}
1777
1778var P$13 = createProperty('scan', {
1779 _init: function (_ref) {
1780 var fn = _ref.fn,
1781 seed = _ref.seed;
1782
1783 this._fn = fn;
1784 this._seed = seed;
1785 if (seed !== NOTHING) {
1786 this._emitValue(seed);
1787 }
1788 },
1789 _free: function () {
1790 this._fn = null;
1791 this._seed = null;
1792 },
1793 _handleValue: function (x) {
1794 var fn = this._fn;
1795 if (this._currentEvent === null || this._currentEvent.type === ERROR) {
1796 this._emitValue(this._seed === NOTHING ? x : fn(this._seed, x));
1797 } else {
1798 this._emitValue(fn(this._currentEvent.value, x));
1799 }
1800 }
1801});
1802
1803function scan(obs, fn) {
1804 var seed = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NOTHING;
1805
1806 return new P$13(obs, { fn: fn, seed: seed });
1807}
1808
1809var mixin$10 = {
1810 _init: function (_ref) {
1811 var fn = _ref.fn;
1812
1813 this._fn = fn;
1814 },
1815 _free: function () {
1816 this._fn = null;
1817 },
1818 _handleValue: function (x) {
1819 var fn = this._fn;
1820 var xs = fn(x);
1821 for (var i = 0; i < xs.length; i++) {
1822 this._emitValue(xs[i]);
1823 }
1824 }
1825};
1826
1827var S$17 = createStream('flatten', mixin$10);
1828
1829var id$4 = function (x) {
1830 return x;
1831};
1832
1833function flatten(obs) {
1834 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$4;
1835
1836 return new S$17(obs, { fn: fn });
1837}
1838
1839var END_MARKER = {};
1840
1841var mixin$11 = {
1842 _init: function (_ref) {
1843 var _this = this;
1844
1845 var wait = _ref.wait;
1846
1847 this._wait = Math.max(0, wait);
1848 this._buff = [];
1849 this._$shiftBuff = function () {
1850 var value = _this._buff.shift();
1851 if (value === END_MARKER) {
1852 _this._emitEnd();
1853 } else {
1854 _this._emitValue(value);
1855 }
1856 };
1857 },
1858 _free: function () {
1859 this._buff = null;
1860 this._$shiftBuff = null;
1861 },
1862 _handleValue: function (x) {
1863 if (this._activating) {
1864 this._emitValue(x);
1865 } else {
1866 this._buff.push(x);
1867 setTimeout(this._$shiftBuff, this._wait);
1868 }
1869 },
1870 _handleEnd: function () {
1871 if (this._activating) {
1872 this._emitEnd();
1873 } else {
1874 this._buff.push(END_MARKER);
1875 setTimeout(this._$shiftBuff, this._wait);
1876 }
1877 }
1878};
1879
1880var S$18 = createStream('delay', mixin$11);
1881var P$14 = createProperty('delay', mixin$11);
1882
1883function delay(obs, wait) {
1884 return new (obs._ofSameType(S$18, P$14))(obs, { wait: wait });
1885}
1886
1887var now = Date.now ? function () {
1888 return Date.now();
1889} : function () {
1890 return new Date().getTime();
1891};
1892
1893var mixin$12 = {
1894 _init: function (_ref) {
1895 var _this = this;
1896
1897 var wait = _ref.wait,
1898 leading = _ref.leading,
1899 trailing = _ref.trailing;
1900
1901 this._wait = Math.max(0, wait);
1902 this._leading = leading;
1903 this._trailing = trailing;
1904 this._trailingValue = null;
1905 this._timeoutId = null;
1906 this._endLater = false;
1907 this._lastCallTime = 0;
1908 this._$trailingCall = function () {
1909 return _this._trailingCall();
1910 };
1911 },
1912 _free: function () {
1913 this._trailingValue = null;
1914 this._$trailingCall = null;
1915 },
1916 _handleValue: function (x) {
1917 if (this._activating) {
1918 this._emitValue(x);
1919 } else {
1920 var curTime = now();
1921 if (this._lastCallTime === 0 && !this._leading) {
1922 this._lastCallTime = curTime;
1923 }
1924 var remaining = this._wait - (curTime - this._lastCallTime);
1925 if (remaining <= 0) {
1926 this._cancelTrailing();
1927 this._lastCallTime = curTime;
1928 this._emitValue(x);
1929 } else if (this._trailing) {
1930 this._cancelTrailing();
1931 this._trailingValue = x;
1932 this._timeoutId = setTimeout(this._$trailingCall, remaining);
1933 }
1934 }
1935 },
1936 _handleEnd: function () {
1937 if (this._activating) {
1938 this._emitEnd();
1939 } else {
1940 if (this._timeoutId) {
1941 this._endLater = true;
1942 } else {
1943 this._emitEnd();
1944 }
1945 }
1946 },
1947 _cancelTrailing: function () {
1948 if (this._timeoutId !== null) {
1949 clearTimeout(this._timeoutId);
1950 this._timeoutId = null;
1951 }
1952 },
1953 _trailingCall: function () {
1954 this._emitValue(this._trailingValue);
1955 this._timeoutId = null;
1956 this._trailingValue = null;
1957 this._lastCallTime = !this._leading ? 0 : now();
1958 if (this._endLater) {
1959 this._emitEnd();
1960 }
1961 }
1962};
1963
1964var S$19 = createStream('throttle', mixin$12);
1965var P$15 = createProperty('throttle', mixin$12);
1966
1967function throttle(obs, wait) {
1968 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
1969 _ref2$leading = _ref2.leading,
1970 leading = _ref2$leading === undefined ? true : _ref2$leading,
1971 _ref2$trailing = _ref2.trailing,
1972 trailing = _ref2$trailing === undefined ? true : _ref2$trailing;
1973
1974 return new (obs._ofSameType(S$19, P$15))(obs, { wait: wait, leading: leading, trailing: trailing });
1975}
1976
1977var mixin$13 = {
1978 _init: function (_ref) {
1979 var _this = this;
1980
1981 var wait = _ref.wait,
1982 immediate = _ref.immediate;
1983
1984 this._wait = Math.max(0, wait);
1985 this._immediate = immediate;
1986 this._lastAttempt = 0;
1987 this._timeoutId = null;
1988 this._laterValue = null;
1989 this._endLater = false;
1990 this._$later = function () {
1991 return _this._later();
1992 };
1993 },
1994 _free: function () {
1995 this._laterValue = null;
1996 this._$later = null;
1997 },
1998 _handleValue: function (x) {
1999 if (this._activating) {
2000 this._emitValue(x);
2001 } else {
2002 this._lastAttempt = now();
2003 if (this._immediate && !this._timeoutId) {
2004 this._emitValue(x);
2005 }
2006 if (!this._timeoutId) {
2007 this._timeoutId = setTimeout(this._$later, this._wait);
2008 }
2009 if (!this._immediate) {
2010 this._laterValue = x;
2011 }
2012 }
2013 },
2014 _handleEnd: function () {
2015 if (this._activating) {
2016 this._emitEnd();
2017 } else {
2018 if (this._timeoutId && !this._immediate) {
2019 this._endLater = true;
2020 } else {
2021 this._emitEnd();
2022 }
2023 }
2024 },
2025 _later: function () {
2026 var last = now() - this._lastAttempt;
2027 if (last < this._wait && last >= 0) {
2028 this._timeoutId = setTimeout(this._$later, this._wait - last);
2029 } else {
2030 this._timeoutId = null;
2031 if (!this._immediate) {
2032 this._emitValue(this._laterValue);
2033 this._laterValue = null;
2034 }
2035 if (this._endLater) {
2036 this._emitEnd();
2037 }
2038 }
2039 }
2040};
2041
2042var S$20 = createStream('debounce', mixin$13);
2043var P$16 = createProperty('debounce', mixin$13);
2044
2045function debounce(obs, wait) {
2046 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
2047 _ref2$immediate = _ref2.immediate,
2048 immediate = _ref2$immediate === undefined ? false : _ref2$immediate;
2049
2050 return new (obs._ofSameType(S$20, P$16))(obs, { wait: wait, immediate: immediate });
2051}
2052
2053var mixin$14 = {
2054 _init: function (_ref) {
2055 var fn = _ref.fn;
2056
2057 this._fn = fn;
2058 },
2059 _free: function () {
2060 this._fn = null;
2061 },
2062 _handleError: function (x) {
2063 var fn = this._fn;
2064 this._emitError(fn(x));
2065 }
2066};
2067
2068var S$21 = createStream('mapErrors', mixin$14);
2069var P$17 = createProperty('mapErrors', mixin$14);
2070
2071var id$5 = function (x) {
2072 return x;
2073};
2074
2075function mapErrors(obs) {
2076 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$5;
2077
2078 return new (obs._ofSameType(S$21, P$17))(obs, { fn: fn });
2079}
2080
2081var mixin$15 = {
2082 _init: function (_ref) {
2083 var fn = _ref.fn;
2084
2085 this._fn = fn;
2086 },
2087 _free: function () {
2088 this._fn = null;
2089 },
2090 _handleError: function (x) {
2091 var fn = this._fn;
2092 if (fn(x)) {
2093 this._emitError(x);
2094 }
2095 }
2096};
2097
2098var S$22 = createStream('filterErrors', mixin$15);
2099var P$18 = createProperty('filterErrors', mixin$15);
2100
2101var id$6 = function (x) {
2102 return x;
2103};
2104
2105function filterErrors(obs) {
2106 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : id$6;
2107
2108 return new (obs._ofSameType(S$22, P$18))(obs, { fn: fn });
2109}
2110
2111var mixin$16 = {
2112 _handleValue: function () {}
2113};
2114
2115var S$23 = createStream('ignoreValues', mixin$16);
2116var P$19 = createProperty('ignoreValues', mixin$16);
2117
2118function ignoreValues(obs) {
2119 return new (obs._ofSameType(S$23, P$19))(obs);
2120}
2121
2122var mixin$17 = {
2123 _handleError: function () {}
2124};
2125
2126var S$24 = createStream('ignoreErrors', mixin$17);
2127var P$20 = createProperty('ignoreErrors', mixin$17);
2128
2129function ignoreErrors(obs) {
2130 return new (obs._ofSameType(S$24, P$20))(obs);
2131}
2132
2133var mixin$18 = {
2134 _handleEnd: function () {}
2135};
2136
2137var S$25 = createStream('ignoreEnd', mixin$18);
2138var P$21 = createProperty('ignoreEnd', mixin$18);
2139
2140function ignoreEnd(obs) {
2141 return new (obs._ofSameType(S$25, P$21))(obs);
2142}
2143
2144var mixin$19 = {
2145 _init: function (_ref) {
2146 var fn = _ref.fn;
2147
2148 this._fn = fn;
2149 },
2150 _free: function () {
2151 this._fn = null;
2152 },
2153 _handleEnd: function () {
2154 var fn = this._fn;
2155 this._emitValue(fn());
2156 this._emitEnd();
2157 }
2158};
2159
2160var S$26 = createStream('beforeEnd', mixin$19);
2161var P$22 = createProperty('beforeEnd', mixin$19);
2162
2163function beforeEnd(obs, fn) {
2164 return new (obs._ofSameType(S$26, P$22))(obs, { fn: fn });
2165}
2166
2167var mixin$20 = {
2168 _init: function (_ref) {
2169 var min = _ref.min,
2170 max = _ref.max;
2171
2172 this._max = max;
2173 this._min = min;
2174 this._buff = [];
2175 },
2176 _free: function () {
2177 this._buff = null;
2178 },
2179 _handleValue: function (x) {
2180 this._buff = slide(this._buff, x, this._max);
2181 if (this._buff.length >= this._min) {
2182 this._emitValue(this._buff);
2183 }
2184 }
2185};
2186
2187var S$27 = createStream('slidingWindow', mixin$20);
2188var P$23 = createProperty('slidingWindow', mixin$20);
2189
2190function slidingWindow(obs, max) {
2191 var min = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
2192
2193 return new (obs._ofSameType(S$27, P$23))(obs, { min: min, max: max });
2194}
2195
2196var mixin$21 = {
2197 _init: function (_ref) {
2198 var fn = _ref.fn,
2199 flushOnEnd = _ref.flushOnEnd;
2200
2201 this._fn = fn;
2202 this._flushOnEnd = flushOnEnd;
2203 this._buff = [];
2204 },
2205 _free: function () {
2206 this._buff = null;
2207 },
2208 _flush: function () {
2209 if (this._buff !== null && this._buff.length !== 0) {
2210 this._emitValue(this._buff);
2211 this._buff = [];
2212 }
2213 },
2214 _handleValue: function (x) {
2215 this._buff.push(x);
2216 var fn = this._fn;
2217 if (!fn(x)) {
2218 this._flush();
2219 }
2220 },
2221 _handleEnd: function () {
2222 if (this._flushOnEnd) {
2223 this._flush();
2224 }
2225 this._emitEnd();
2226 }
2227};
2228
2229var S$28 = createStream('bufferWhile', mixin$21);
2230var P$24 = createProperty('bufferWhile', mixin$21);
2231
2232var id$7 = function (x) {
2233 return x;
2234};
2235
2236function bufferWhile(obs, fn) {
2237 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
2238 _ref2$flushOnEnd = _ref2.flushOnEnd,
2239 flushOnEnd = _ref2$flushOnEnd === undefined ? true : _ref2$flushOnEnd;
2240
2241 return new (obs._ofSameType(S$28, P$24))(obs, { fn: fn || id$7, flushOnEnd: flushOnEnd });
2242}
2243
2244var mixin$22 = {
2245 _init: function (_ref) {
2246 var count = _ref.count,
2247 flushOnEnd = _ref.flushOnEnd;
2248
2249 this._count = count;
2250 this._flushOnEnd = flushOnEnd;
2251 this._buff = [];
2252 },
2253 _free: function () {
2254 this._buff = null;
2255 },
2256 _flush: function () {
2257 if (this._buff !== null && this._buff.length !== 0) {
2258 this._emitValue(this._buff);
2259 this._buff = [];
2260 }
2261 },
2262 _handleValue: function (x) {
2263 this._buff.push(x);
2264 if (this._buff.length >= this._count) {
2265 this._flush();
2266 }
2267 },
2268 _handleEnd: function () {
2269 if (this._flushOnEnd) {
2270 this._flush();
2271 }
2272 this._emitEnd();
2273 }
2274};
2275
2276var S$29 = createStream('bufferWithCount', mixin$22);
2277var P$25 = createProperty('bufferWithCount', mixin$22);
2278
2279function bufferWhile$1(obs, count) {
2280 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
2281 _ref2$flushOnEnd = _ref2.flushOnEnd,
2282 flushOnEnd = _ref2$flushOnEnd === undefined ? true : _ref2$flushOnEnd;
2283
2284 return new (obs._ofSameType(S$29, P$25))(obs, { count: count, flushOnEnd: flushOnEnd });
2285}
2286
2287var mixin$23 = {
2288 _init: function (_ref) {
2289 var _this = this;
2290
2291 var wait = _ref.wait,
2292 count = _ref.count,
2293 flushOnEnd = _ref.flushOnEnd;
2294
2295 this._wait = wait;
2296 this._count = count;
2297 this._flushOnEnd = flushOnEnd;
2298 this._intervalId = null;
2299 this._$onTick = function () {
2300 return _this._flush();
2301 };
2302 this._buff = [];
2303 },
2304 _free: function () {
2305 this._$onTick = null;
2306 this._buff = null;
2307 },
2308 _flush: function () {
2309 if (this._buff !== null) {
2310 this._emitValue(this._buff);
2311 this._buff = [];
2312 }
2313 },
2314 _handleValue: function (x) {
2315 this._buff.push(x);
2316 if (this._buff.length >= this._count) {
2317 clearInterval(this._intervalId);
2318 this._flush();
2319 this._intervalId = setInterval(this._$onTick, this._wait);
2320 }
2321 },
2322 _handleEnd: function () {
2323 if (this._flushOnEnd && this._buff.length !== 0) {
2324 this._flush();
2325 }
2326 this._emitEnd();
2327 },
2328 _onActivation: function () {
2329 this._intervalId = setInterval(this._$onTick, this._wait);
2330 this._source.onAny(this._$handleAny); // copied from patterns/one-source
2331 },
2332 _onDeactivation: function () {
2333 if (this._intervalId !== null) {
2334 clearInterval(this._intervalId);
2335 this._intervalId = null;
2336 }
2337 this._source.offAny(this._$handleAny); // copied from patterns/one-source
2338 }
2339};
2340
2341var S$30 = createStream('bufferWithTimeOrCount', mixin$23);
2342var P$26 = createProperty('bufferWithTimeOrCount', mixin$23);
2343
2344function bufferWithTimeOrCount(obs, wait, count) {
2345 var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
2346 _ref2$flushOnEnd = _ref2.flushOnEnd,
2347 flushOnEnd = _ref2$flushOnEnd === undefined ? true : _ref2$flushOnEnd;
2348
2349 return new (obs._ofSameType(S$30, P$26))(obs, { wait: wait, count: count, flushOnEnd: flushOnEnd });
2350}
2351
2352function xformForObs(obs) {
2353 return {
2354 '@@transducer/step': function (res, input) {
2355 obs._emitValue(input);
2356 return null;
2357 },
2358 '@@transducer/result': function () {
2359 obs._emitEnd();
2360 return null;
2361 }
2362 };
2363}
2364
2365var mixin$24 = {
2366 _init: function (_ref) {
2367 var transducer = _ref.transducer;
2368
2369 this._xform = transducer(xformForObs(this));
2370 },
2371 _free: function () {
2372 this._xform = null;
2373 },
2374 _handleValue: function (x) {
2375 if (this._xform['@@transducer/step'](null, x) !== null) {
2376 this._xform['@@transducer/result'](null);
2377 }
2378 },
2379 _handleEnd: function () {
2380 this._xform['@@transducer/result'](null);
2381 }
2382};
2383
2384var S$31 = createStream('transduce', mixin$24);
2385var P$27 = createProperty('transduce', mixin$24);
2386
2387function transduce(obs, transducer) {
2388 return new (obs._ofSameType(S$31, P$27))(obs, { transducer: transducer });
2389}
2390
2391var mixin$25 = {
2392 _init: function (_ref) {
2393 var fn = _ref.fn;
2394
2395 this._handler = fn;
2396 this._emitter = emitter(this);
2397 },
2398 _free: function () {
2399 this._handler = null;
2400 this._emitter = null;
2401 },
2402 _handleAny: function (event) {
2403 this._handler(this._emitter, event);
2404 }
2405};
2406
2407var S$32 = createStream('withHandler', mixin$25);
2408var P$28 = createProperty('withHandler', mixin$25);
2409
2410function withHandler(obs, fn) {
2411 return new (obs._ofSameType(S$32, P$28))(obs, { fn: fn });
2412}
2413
2414var isArray = Array.isArray || function (xs) {
2415 return Object.prototype.toString.call(xs) === '[object Array]';
2416};
2417
2418function Zip(sources, combinator) {
2419 var _this = this;
2420
2421 Stream.call(this);
2422
2423 this._buffers = map(sources, function (source) {
2424 return isArray(source) ? cloneArray(source) : [];
2425 });
2426 this._sources = map(sources, function (source) {
2427 return isArray(source) ? never() : source;
2428 });
2429
2430 this._combinator = combinator ? spread(combinator, this._sources.length) : function (x) {
2431 return x;
2432 };
2433 this._aliveCount = 0;
2434
2435 this._$handlers = [];
2436
2437 var _loop = function (i) {
2438 _this._$handlers.push(function (event) {
2439 return _this._handleAny(i, event);
2440 });
2441 };
2442
2443 for (var i = 0; i < this._sources.length; i++) {
2444 _loop(i);
2445 }
2446}
2447
2448inherit(Zip, Stream, {
2449 _name: 'zip',
2450
2451 _onActivation: function () {
2452 // if all sources are arrays
2453 while (this._isFull()) {
2454 this._emit();
2455 }
2456
2457 var length = this._sources.length;
2458 this._aliveCount = length;
2459 for (var i = 0; i < length && this._active; i++) {
2460 this._sources[i].onAny(this._$handlers[i]);
2461 }
2462 },
2463 _onDeactivation: function () {
2464 for (var i = 0; i < this._sources.length; i++) {
2465 this._sources[i].offAny(this._$handlers[i]);
2466 }
2467 },
2468 _emit: function () {
2469 var values = new Array(this._buffers.length);
2470 for (var i = 0; i < this._buffers.length; i++) {
2471 values[i] = this._buffers[i].shift();
2472 }
2473 var combinator = this._combinator;
2474 this._emitValue(combinator(values));
2475 },
2476 _isFull: function () {
2477 for (var i = 0; i < this._buffers.length; i++) {
2478 if (this._buffers[i].length === 0) {
2479 return false;
2480 }
2481 }
2482 return true;
2483 },
2484 _handleAny: function (i, event) {
2485 if (event.type === VALUE) {
2486 this._buffers[i].push(event.value);
2487 if (this._isFull()) {
2488 this._emit();
2489 }
2490 }
2491 if (event.type === ERROR) {
2492 this._emitError(event.value);
2493 }
2494 if (event.type === END) {
2495 this._aliveCount--;
2496 if (this._aliveCount === 0) {
2497 this._emitEnd();
2498 }
2499 }
2500 },
2501 _clear: function () {
2502 Stream.prototype._clear.call(this);
2503 this._sources = null;
2504 this._buffers = null;
2505 this._combinator = null;
2506 this._$handlers = null;
2507 }
2508});
2509
2510function zip(observables, combinator /* Function | falsey */) {
2511 return observables.length === 0 ? never() : new Zip(observables, combinator);
2512}
2513
2514var id$8 = function (x) {
2515 return x;
2516};
2517
2518function AbstractPool() {
2519 var _this = this;
2520
2521 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
2522 _ref$queueLim = _ref.queueLim,
2523 queueLim = _ref$queueLim === undefined ? 0 : _ref$queueLim,
2524 _ref$concurLim = _ref.concurLim,
2525 concurLim = _ref$concurLim === undefined ? -1 : _ref$concurLim,
2526 _ref$drop = _ref.drop,
2527 drop = _ref$drop === undefined ? 'new' : _ref$drop;
2528
2529 Stream.call(this);
2530
2531 this._queueLim = queueLim < 0 ? -1 : queueLim;
2532 this._concurLim = concurLim < 0 ? -1 : concurLim;
2533 this._drop = drop;
2534 this._queue = [];
2535 this._curSources = [];
2536 this._$handleSubAny = function (event) {
2537 return _this._handleSubAny(event);
2538 };
2539 this._$endHandlers = [];
2540 this._currentlyAdding = null;
2541
2542 if (this._concurLim === 0) {
2543 this._emitEnd();
2544 }
2545}
2546
2547inherit(AbstractPool, Stream, {
2548 _name: 'abstractPool',
2549
2550 _add: function (obj, toObs /* Function | falsey */) {
2551 toObs = toObs || id$8;
2552 if (this._concurLim === -1 || this._curSources.length < this._concurLim) {
2553 this._addToCur(toObs(obj));
2554 } else {
2555 if (this._queueLim === -1 || this._queue.length < this._queueLim) {
2556 this._addToQueue(toObs(obj));
2557 } else if (this._drop === 'old') {
2558 this._removeOldest();
2559 this._add(obj, toObs);
2560 }
2561 }
2562 },
2563 _addAll: function (obss) {
2564 var _this2 = this;
2565
2566 forEach(obss, function (obs) {
2567 return _this2._add(obs);
2568 });
2569 },
2570 _remove: function (obs) {
2571 if (this._removeCur(obs) === -1) {
2572 this._removeQueue(obs);
2573 }
2574 },
2575 _addToQueue: function (obs) {
2576 this._queue = concat(this._queue, [obs]);
2577 },
2578 _addToCur: function (obs) {
2579 if (this._active) {
2580 // HACK:
2581 //
2582 // We have two optimizations for cases when `obs` is ended. We don't want
2583 // to add such observable to the list, but only want to emit events
2584 // from it (if it has some).
2585 //
2586 // Instead of this hacks, we could just did following,
2587 // but it would be 5-8 times slower:
2588 //
2589 // this._curSources = concat(this._curSources, [obs]);
2590 // this._subscribe(obs);
2591 //
2592
2593 // #1
2594 // This one for cases when `obs` already ended
2595 // e.g., Kefir.constant() or Kefir.never()
2596 if (!obs._alive) {
2597 if (obs._currentEvent) {
2598 this._emit(obs._currentEvent.type, obs._currentEvent.value);
2599 }
2600 // The _emit above could have caused this stream to end.
2601 if (this._active) {
2602 if (this._queue.length !== 0) {
2603 this._pullQueue();
2604 } else if (this._curSources.length === 0) {
2605 this._onEmpty();
2606 }
2607 }
2608 return;
2609 }
2610
2611 // #2
2612 // This one is for cases when `obs` going to end synchronously on
2613 // first subscriber e.g., Kefir.stream(em => {em.emit(1); em.end()})
2614 this._currentlyAdding = obs;
2615 obs.onAny(this._$handleSubAny);
2616 this._currentlyAdding = null;
2617 if (obs._alive) {
2618 this._curSources = concat(this._curSources, [obs]);
2619 if (this._active) {
2620 this._subToEnd(obs);
2621 }
2622 }
2623 } else {
2624 this._curSources = concat(this._curSources, [obs]);
2625 }
2626 },
2627 _subToEnd: function (obs) {
2628 var _this3 = this;
2629
2630 var onEnd = function () {
2631 return _this3._removeCur(obs);
2632 };
2633 this._$endHandlers.push({ obs: obs, handler: onEnd });
2634 obs.onEnd(onEnd);
2635 },
2636 _subscribe: function (obs) {
2637 obs.onAny(this._$handleSubAny);
2638
2639 // it can become inactive in responce of subscribing to `obs.onAny` above
2640 if (this._active) {
2641 this._subToEnd(obs);
2642 }
2643 },
2644 _unsubscribe: function (obs) {
2645 obs.offAny(this._$handleSubAny);
2646
2647 var onEndI = findByPred(this._$endHandlers, function (obj) {
2648 return obj.obs === obs;
2649 });
2650 if (onEndI !== -1) {
2651 obs.offEnd(this._$endHandlers[onEndI].handler);
2652 this._$endHandlers.splice(onEndI, 1);
2653 }
2654 },
2655 _handleSubAny: function (event) {
2656 if (event.type === VALUE) {
2657 this._emitValue(event.value);
2658 } else if (event.type === ERROR) {
2659 this._emitError(event.value);
2660 }
2661 },
2662 _removeQueue: function (obs) {
2663 var index = find(this._queue, obs);
2664 this._queue = remove(this._queue, index);
2665 return index;
2666 },
2667 _removeCur: function (obs) {
2668 if (this._active) {
2669 this._unsubscribe(obs);
2670 }
2671 var index = find(this._curSources, obs);
2672 this._curSources = remove(this._curSources, index);
2673 if (index !== -1) {
2674 if (this._queue.length !== 0) {
2675 this._pullQueue();
2676 } else if (this._curSources.length === 0) {
2677 this._onEmpty();
2678 }
2679 }
2680 return index;
2681 },
2682 _removeOldest: function () {
2683 this._removeCur(this._curSources[0]);
2684 },
2685 _pullQueue: function () {
2686 if (this._queue.length !== 0) {
2687 this._queue = cloneArray(this._queue);
2688 this._addToCur(this._queue.shift());
2689 }
2690 },
2691 _onActivation: function () {
2692 for (var i = 0, sources = this._curSources; i < sources.length && this._active; i++) {
2693 this._subscribe(sources[i]);
2694 }
2695 },
2696 _onDeactivation: function () {
2697 for (var i = 0, sources = this._curSources; i < sources.length; i++) {
2698 this._unsubscribe(sources[i]);
2699 }
2700 if (this._currentlyAdding !== null) {
2701 this._unsubscribe(this._currentlyAdding);
2702 }
2703 },
2704 _isEmpty: function () {
2705 return this._curSources.length === 0;
2706 },
2707 _onEmpty: function () {},
2708 _clear: function () {
2709 Stream.prototype._clear.call(this);
2710 this._queue = null;
2711 this._curSources = null;
2712 this._$handleSubAny = null;
2713 this._$endHandlers = null;
2714 }
2715});
2716
2717function Merge(sources) {
2718 AbstractPool.call(this);
2719 this._addAll(sources);
2720 this._initialised = true;
2721}
2722
2723inherit(Merge, AbstractPool, {
2724 _name: 'merge',
2725
2726 _onEmpty: function () {
2727 if (this._initialised) {
2728 this._emitEnd();
2729 }
2730 }
2731});
2732
2733function merge(observables) {
2734 return observables.length === 0 ? never() : new Merge(observables);
2735}
2736
2737function S$33(generator) {
2738 var _this = this;
2739
2740 Stream.call(this);
2741 this._generator = generator;
2742 this._source = null;
2743 this._inLoop = false;
2744 this._iteration = 0;
2745 this._$handleAny = function (event) {
2746 return _this._handleAny(event);
2747 };
2748}
2749
2750inherit(S$33, Stream, {
2751 _name: 'repeat',
2752
2753 _handleAny: function (event) {
2754 if (event.type === END) {
2755 this._source = null;
2756 this._getSource();
2757 } else {
2758 this._emit(event.type, event.value);
2759 }
2760 },
2761 _getSource: function () {
2762 if (!this._inLoop) {
2763 this._inLoop = true;
2764 var generator = this._generator;
2765 while (this._source === null && this._alive && this._active) {
2766 this._source = generator(this._iteration++);
2767 if (this._source) {
2768 this._source.onAny(this._$handleAny);
2769 } else {
2770 this._emitEnd();
2771 }
2772 }
2773 this._inLoop = false;
2774 }
2775 },
2776 _onActivation: function () {
2777 if (this._source) {
2778 this._source.onAny(this._$handleAny);
2779 } else {
2780 this._getSource();
2781 }
2782 },
2783 _onDeactivation: function () {
2784 if (this._source) {
2785 this._source.offAny(this._$handleAny);
2786 }
2787 },
2788 _clear: function () {
2789 Stream.prototype._clear.call(this);
2790 this._generator = null;
2791 this._source = null;
2792 this._$handleAny = null;
2793 }
2794});
2795
2796var repeat = function (generator) {
2797 return new S$33(generator);
2798};
2799
2800function concat$1(observables) {
2801 return repeat(function (index) {
2802 return observables.length > index ? observables[index] : false;
2803 }).setName('concat');
2804}
2805
2806function Pool() {
2807 AbstractPool.call(this);
2808}
2809
2810inherit(Pool, AbstractPool, {
2811 _name: 'pool',
2812
2813 plug: function (obs) {
2814 this._add(obs);
2815 return this;
2816 },
2817 unplug: function (obs) {
2818 this._remove(obs);
2819 return this;
2820 }
2821});
2822
2823function FlatMap(source, fn, options) {
2824 var _this = this;
2825
2826 AbstractPool.call(this, options);
2827 this._source = source;
2828 this._fn = fn;
2829 this._mainEnded = false;
2830 this._lastCurrent = null;
2831 this._$handleMain = function (event) {
2832 return _this._handleMain(event);
2833 };
2834}
2835
2836inherit(FlatMap, AbstractPool, {
2837 _onActivation: function () {
2838 AbstractPool.prototype._onActivation.call(this);
2839 if (this._active) {
2840 this._source.onAny(this._$handleMain);
2841 }
2842 },
2843 _onDeactivation: function () {
2844 AbstractPool.prototype._onDeactivation.call(this);
2845 this._source.offAny(this._$handleMain);
2846 this._hadNoEvSinceDeact = true;
2847 },
2848 _handleMain: function (event) {
2849 if (event.type === VALUE) {
2850 // Is latest value before deactivation survived, and now is 'current' on this activation?
2851 // We don't want to handle such values, to prevent to constantly add
2852 // same observale on each activation/deactivation when our main source
2853 // is a `Kefir.conatant()` for example.
2854 var sameCurr = this._activating && this._hadNoEvSinceDeact && this._lastCurrent === event.value;
2855 if (!sameCurr) {
2856 this._add(event.value, this._fn);
2857 }
2858 this._lastCurrent = event.value;
2859 this._hadNoEvSinceDeact = false;
2860 }
2861
2862 if (event.type === ERROR) {
2863 this._emitError(event.value);
2864 }
2865
2866 if (event.type === END) {
2867 if (this._isEmpty()) {
2868 this._emitEnd();
2869 } else {
2870 this._mainEnded = true;
2871 }
2872 }
2873 },
2874 _onEmpty: function () {
2875 if (this._mainEnded) {
2876 this._emitEnd();
2877 }
2878 },
2879 _clear: function () {
2880 AbstractPool.prototype._clear.call(this);
2881 this._source = null;
2882 this._lastCurrent = null;
2883 this._$handleMain = null;
2884 }
2885});
2886
2887function FlatMapErrors(source, fn) {
2888 FlatMap.call(this, source, fn);
2889}
2890
2891inherit(FlatMapErrors, FlatMap, {
2892 // Same as in FlatMap, only VALUE/ERROR flipped
2893 _handleMain: function (event) {
2894 if (event.type === ERROR) {
2895 var sameCurr = this._activating && this._hadNoEvSinceDeact && this._lastCurrent === event.value;
2896 if (!sameCurr) {
2897 this._add(event.value, this._fn);
2898 }
2899 this._lastCurrent = event.value;
2900 this._hadNoEvSinceDeact = false;
2901 }
2902
2903 if (event.type === VALUE) {
2904 this._emitValue(event.value);
2905 }
2906
2907 if (event.type === END) {
2908 if (this._isEmpty()) {
2909 this._emitEnd();
2910 } else {
2911 this._mainEnded = true;
2912 }
2913 }
2914 }
2915});
2916
2917function createConstructor$1(BaseClass, name) {
2918 return function AnonymousObservable(primary, secondary, options) {
2919 var _this = this;
2920
2921 BaseClass.call(this);
2922 this._primary = primary;
2923 this._secondary = secondary;
2924 this._name = primary._name + '.' + name;
2925 this._lastSecondary = NOTHING;
2926 this._$handleSecondaryAny = function (event) {
2927 return _this._handleSecondaryAny(event);
2928 };
2929 this._$handlePrimaryAny = function (event) {
2930 return _this._handlePrimaryAny(event);
2931 };
2932 this._init(options);
2933 };
2934}
2935
2936function createClassMethods$1(BaseClass) {
2937 return {
2938 _init: function () {},
2939 _free: function () {},
2940 _handlePrimaryValue: function (x) {
2941 this._emitValue(x);
2942 },
2943 _handlePrimaryError: function (x) {
2944 this._emitError(x);
2945 },
2946 _handlePrimaryEnd: function () {
2947 this._emitEnd();
2948 },
2949 _handleSecondaryValue: function (x) {
2950 this._lastSecondary = x;
2951 },
2952 _handleSecondaryError: function (x) {
2953 this._emitError(x);
2954 },
2955 _handleSecondaryEnd: function () {},
2956 _handlePrimaryAny: function (event) {
2957 switch (event.type) {
2958 case VALUE:
2959 return this._handlePrimaryValue(event.value);
2960 case ERROR:
2961 return this._handlePrimaryError(event.value);
2962 case END:
2963 return this._handlePrimaryEnd(event.value);
2964 }
2965 },
2966 _handleSecondaryAny: function (event) {
2967 switch (event.type) {
2968 case VALUE:
2969 return this._handleSecondaryValue(event.value);
2970 case ERROR:
2971 return this._handleSecondaryError(event.value);
2972 case END:
2973 this._handleSecondaryEnd(event.value);
2974 this._removeSecondary();
2975 }
2976 },
2977 _removeSecondary: function () {
2978 if (this._secondary !== null) {
2979 this._secondary.offAny(this._$handleSecondaryAny);
2980 this._$handleSecondaryAny = null;
2981 this._secondary = null;
2982 }
2983 },
2984 _onActivation: function () {
2985 if (this._secondary !== null) {
2986 this._secondary.onAny(this._$handleSecondaryAny);
2987 }
2988 if (this._active) {
2989 this._primary.onAny(this._$handlePrimaryAny);
2990 }
2991 },
2992 _onDeactivation: function () {
2993 if (this._secondary !== null) {
2994 this._secondary.offAny(this._$handleSecondaryAny);
2995 }
2996 this._primary.offAny(this._$handlePrimaryAny);
2997 },
2998 _clear: function () {
2999 BaseClass.prototype._clear.call(this);
3000 this._primary = null;
3001 this._secondary = null;
3002 this._lastSecondary = null;
3003 this._$handleSecondaryAny = null;
3004 this._$handlePrimaryAny = null;
3005 this._free();
3006 }
3007 };
3008}
3009
3010function createStream$1(name, mixin) {
3011 var S = createConstructor$1(Stream, name);
3012 inherit(S, Stream, createClassMethods$1(Stream), mixin);
3013 return S;
3014}
3015
3016function createProperty$1(name, mixin) {
3017 var P = createConstructor$1(Property, name);
3018 inherit(P, Property, createClassMethods$1(Property), mixin);
3019 return P;
3020}
3021
3022var mixin$26 = {
3023 _handlePrimaryValue: function (x) {
3024 if (this._lastSecondary !== NOTHING && this._lastSecondary) {
3025 this._emitValue(x);
3026 }
3027 },
3028 _handleSecondaryEnd: function () {
3029 if (this._lastSecondary === NOTHING || !this._lastSecondary) {
3030 this._emitEnd();
3031 }
3032 }
3033};
3034
3035var S$34 = createStream$1('filterBy', mixin$26);
3036var P$29 = createProperty$1('filterBy', mixin$26);
3037
3038function filterBy(primary, secondary) {
3039 return new (primary._ofSameType(S$34, P$29))(primary, secondary);
3040}
3041
3042var id2 = function (_, x) {
3043 return x;
3044};
3045
3046function sampledBy(passive, active, combinator) {
3047 var _combinator = combinator ? function (a, b) {
3048 return combinator(b, a);
3049 } : id2;
3050 return combine([active], [passive], _combinator).setName(passive, 'sampledBy');
3051}
3052
3053var mixin$27 = {
3054 _handlePrimaryValue: function (x) {
3055 if (this._lastSecondary !== NOTHING) {
3056 this._emitValue(x);
3057 }
3058 },
3059 _handleSecondaryEnd: function () {
3060 if (this._lastSecondary === NOTHING) {
3061 this._emitEnd();
3062 }
3063 }
3064};
3065
3066var S$35 = createStream$1('skipUntilBy', mixin$27);
3067var P$30 = createProperty$1('skipUntilBy', mixin$27);
3068
3069function skipUntilBy(primary, secondary) {
3070 return new (primary._ofSameType(S$35, P$30))(primary, secondary);
3071}
3072
3073var mixin$28 = {
3074 _handleSecondaryValue: function () {
3075 this._emitEnd();
3076 }
3077};
3078
3079var S$36 = createStream$1('takeUntilBy', mixin$28);
3080var P$31 = createProperty$1('takeUntilBy', mixin$28);
3081
3082function takeUntilBy(primary, secondary) {
3083 return new (primary._ofSameType(S$36, P$31))(primary, secondary);
3084}
3085
3086var mixin$29 = {
3087 _init: function () {
3088 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
3089 _ref$flushOnEnd = _ref.flushOnEnd,
3090 flushOnEnd = _ref$flushOnEnd === undefined ? true : _ref$flushOnEnd;
3091
3092 this._buff = [];
3093 this._flushOnEnd = flushOnEnd;
3094 },
3095 _free: function () {
3096 this._buff = null;
3097 },
3098 _flush: function () {
3099 if (this._buff !== null) {
3100 this._emitValue(this._buff);
3101 this._buff = [];
3102 }
3103 },
3104 _handlePrimaryEnd: function () {
3105 if (this._flushOnEnd) {
3106 this._flush();
3107 }
3108 this._emitEnd();
3109 },
3110 _onActivation: function () {
3111 this._primary.onAny(this._$handlePrimaryAny);
3112 if (this._alive && this._secondary !== null) {
3113 this._secondary.onAny(this._$handleSecondaryAny);
3114 }
3115 },
3116 _handlePrimaryValue: function (x) {
3117 this._buff.push(x);
3118 },
3119 _handleSecondaryValue: function () {
3120 this._flush();
3121 },
3122 _handleSecondaryEnd: function () {
3123 if (!this._flushOnEnd) {
3124 this._emitEnd();
3125 }
3126 }
3127};
3128
3129var S$37 = createStream$1('bufferBy', mixin$29);
3130var P$32 = createProperty$1('bufferBy', mixin$29);
3131
3132function bufferBy(primary, secondary, options /* optional */) {
3133 return new (primary._ofSameType(S$37, P$32))(primary, secondary, options);
3134}
3135
3136var mixin$30 = {
3137 _init: function () {
3138 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
3139 _ref$flushOnEnd = _ref.flushOnEnd,
3140 flushOnEnd = _ref$flushOnEnd === undefined ? true : _ref$flushOnEnd,
3141 _ref$flushOnChange = _ref.flushOnChange,
3142 flushOnChange = _ref$flushOnChange === undefined ? false : _ref$flushOnChange;
3143
3144 this._buff = [];
3145 this._flushOnEnd = flushOnEnd;
3146 this._flushOnChange = flushOnChange;
3147 },
3148 _free: function () {
3149 this._buff = null;
3150 },
3151 _flush: function () {
3152 if (this._buff !== null) {
3153 this._emitValue(this._buff);
3154 this._buff = [];
3155 }
3156 },
3157 _handlePrimaryEnd: function () {
3158 if (this._flushOnEnd) {
3159 this._flush();
3160 }
3161 this._emitEnd();
3162 },
3163 _handlePrimaryValue: function (x) {
3164 this._buff.push(x);
3165 if (this._lastSecondary !== NOTHING && !this._lastSecondary) {
3166 this._flush();
3167 }
3168 },
3169 _handleSecondaryEnd: function () {
3170 if (!this._flushOnEnd && (this._lastSecondary === NOTHING || this._lastSecondary)) {
3171 this._emitEnd();
3172 }
3173 },
3174 _handleSecondaryValue: function (x) {
3175 if (this._flushOnChange && !x) {
3176 this._flush();
3177 }
3178
3179 // from default _handleSecondaryValue
3180 this._lastSecondary = x;
3181 }
3182};
3183
3184var S$38 = createStream$1('bufferWhileBy', mixin$30);
3185var P$33 = createProperty$1('bufferWhileBy', mixin$30);
3186
3187function bufferWhileBy(primary, secondary, options /* optional */) {
3188 return new (primary._ofSameType(S$38, P$33))(primary, secondary, options);
3189}
3190
3191var f = function () {
3192 return false;
3193};
3194var t = function () {
3195 return true;
3196};
3197
3198function awaiting(a, b) {
3199 var result = merge([map$1(a, t), map$1(b, f)]);
3200 result = skipDuplicates(result);
3201 result = toProperty(result, f);
3202 return result.setName(a, 'awaiting');
3203}
3204
3205var mixin$31 = {
3206 _init: function (_ref) {
3207 var fn = _ref.fn;
3208
3209 this._fn = fn;
3210 },
3211 _free: function () {
3212 this._fn = null;
3213 },
3214 _handleValue: function (x) {
3215 var fn = this._fn;
3216 var result = fn(x);
3217 if (result.convert) {
3218 this._emitError(result.error);
3219 } else {
3220 this._emitValue(x);
3221 }
3222 }
3223};
3224
3225var S$39 = createStream('valuesToErrors', mixin$31);
3226var P$34 = createProperty('valuesToErrors', mixin$31);
3227
3228var defFn = function (x) {
3229 return { convert: true, error: x };
3230};
3231
3232function valuesToErrors(obs) {
3233 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defFn;
3234
3235 return new (obs._ofSameType(S$39, P$34))(obs, { fn: fn });
3236}
3237
3238var mixin$32 = {
3239 _init: function (_ref) {
3240 var fn = _ref.fn;
3241
3242 this._fn = fn;
3243 },
3244 _free: function () {
3245 this._fn = null;
3246 },
3247 _handleError: function (x) {
3248 var fn = this._fn;
3249 var result = fn(x);
3250 if (result.convert) {
3251 this._emitValue(result.value);
3252 } else {
3253 this._emitError(x);
3254 }
3255 }
3256};
3257
3258var S$40 = createStream('errorsToValues', mixin$32);
3259var P$35 = createProperty('errorsToValues', mixin$32);
3260
3261var defFn$1 = function (x) {
3262 return { convert: true, value: x };
3263};
3264
3265function errorsToValues(obs) {
3266 var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defFn$1;
3267
3268 return new (obs._ofSameType(S$40, P$35))(obs, { fn: fn });
3269}
3270
3271var mixin$33 = {
3272 _handleError: function (x) {
3273 this._emitError(x);
3274 this._emitEnd();
3275 }
3276};
3277
3278var S$41 = createStream('endOnError', mixin$33);
3279var P$36 = createProperty('endOnError', mixin$33);
3280
3281function endOnError(obs) {
3282 return new (obs._ofSameType(S$41, P$36))(obs);
3283}
3284
3285// Create a stream
3286// -----------------------------------------------------------------------------
3287
3288// () -> Stream
3289// (number, any) -> Stream
3290// (number, any) -> Stream
3291// (number, Array<any>) -> Stream
3292// (number, Function) -> Stream
3293// (number, Function) -> Stream
3294// (Function) -> Stream
3295// (Function) -> Stream
3296// Target = {addEventListener, removeEventListener}|{addListener, removeListener}|{on, off}
3297// (Target, string, Function|undefined) -> Stream
3298// (Function) -> Stream
3299// Create a property
3300// -----------------------------------------------------------------------------
3301
3302// (any) -> Property
3303// (any) -> Property
3304// Convert observables
3305// -----------------------------------------------------------------------------
3306
3307// (Stream|Property, Function|undefined) -> Property
3308Observable.prototype.toProperty = function (fn) {
3309 return toProperty(this, fn);
3310};
3311
3312// (Stream|Property) -> Stream
3313Observable.prototype.changes = function () {
3314 return changes(this);
3315};
3316
3317// Interoperation with other implimentations
3318// -----------------------------------------------------------------------------
3319
3320// (Promise) -> Property
3321// (Stream|Property, Function|undefined) -> Promise
3322Observable.prototype.toPromise = function (Promise) {
3323 return toPromise(this, Promise);
3324};
3325
3326// (ESObservable) -> Stream
3327// (Stream|Property) -> ES7 Observable
3328Observable.prototype.toESObservable = toESObservable;
3329Observable.prototype[$$observable] = toESObservable;
3330
3331// Modify an observable
3332// -----------------------------------------------------------------------------
3333
3334// (Stream, Function|undefined) -> Stream
3335// (Property, Function|undefined) -> Property
3336Observable.prototype.map = function (fn) {
3337 return map$1(this, fn);
3338};
3339
3340// (Stream, Function|undefined) -> Stream
3341// (Property, Function|undefined) -> Property
3342Observable.prototype.filter = function (fn) {
3343 return filter(this, fn);
3344};
3345
3346// (Stream, number) -> Stream
3347// (Property, number) -> Property
3348Observable.prototype.take = function (n) {
3349 return take(this, n);
3350};
3351
3352// (Stream, number) -> Stream
3353// (Property, number) -> Property
3354Observable.prototype.takeErrors = function (n) {
3355 return takeErrors(this, n);
3356};
3357
3358// (Stream, Function|undefined) -> Stream
3359// (Property, Function|undefined) -> Property
3360Observable.prototype.takeWhile = function (fn) {
3361 return takeWhile(this, fn);
3362};
3363
3364// (Stream) -> Stream
3365// (Property) -> Property
3366Observable.prototype.last = function () {
3367 return last(this);
3368};
3369
3370// (Stream, number) -> Stream
3371// (Property, number) -> Property
3372Observable.prototype.skip = function (n) {
3373 return skip(this, n);
3374};
3375
3376// (Stream, Function|undefined) -> Stream
3377// (Property, Function|undefined) -> Property
3378Observable.prototype.skipWhile = function (fn) {
3379 return skipWhile(this, fn);
3380};
3381
3382// (Stream, Function|undefined) -> Stream
3383// (Property, Function|undefined) -> Property
3384Observable.prototype.skipDuplicates = function (fn) {
3385 return skipDuplicates(this, fn);
3386};
3387
3388// (Stream, Function|falsey, any|undefined) -> Stream
3389// (Property, Function|falsey, any|undefined) -> Property
3390Observable.prototype.diff = function (fn, seed) {
3391 return diff(this, fn, seed);
3392};
3393
3394// (Stream|Property, Function, any|undefined) -> Property
3395Observable.prototype.scan = function (fn, seed) {
3396 return scan(this, fn, seed);
3397};
3398
3399// (Stream, Function|undefined) -> Stream
3400// (Property, Function|undefined) -> Property
3401Observable.prototype.flatten = function (fn) {
3402 return flatten(this, fn);
3403};
3404
3405// (Stream, number) -> Stream
3406// (Property, number) -> Property
3407Observable.prototype.delay = function (wait) {
3408 return delay(this, wait);
3409};
3410
3411// Options = {leading: boolean|undefined, trailing: boolean|undefined}
3412// (Stream, number, Options|undefined) -> Stream
3413// (Property, number, Options|undefined) -> Property
3414Observable.prototype.throttle = function (wait, options) {
3415 return throttle(this, wait, options);
3416};
3417
3418// Options = {immediate: boolean|undefined}
3419// (Stream, number, Options|undefined) -> Stream
3420// (Property, number, Options|undefined) -> Property
3421Observable.prototype.debounce = function (wait, options) {
3422 return debounce(this, wait, options);
3423};
3424
3425// (Stream, Function|undefined) -> Stream
3426// (Property, Function|undefined) -> Property
3427Observable.prototype.mapErrors = function (fn) {
3428 return mapErrors(this, fn);
3429};
3430
3431// (Stream, Function|undefined) -> Stream
3432// (Property, Function|undefined) -> Property
3433Observable.prototype.filterErrors = function (fn) {
3434 return filterErrors(this, fn);
3435};
3436
3437// (Stream) -> Stream
3438// (Property) -> Property
3439Observable.prototype.ignoreValues = function () {
3440 return ignoreValues(this);
3441};
3442
3443// (Stream) -> Stream
3444// (Property) -> Property
3445Observable.prototype.ignoreErrors = function () {
3446 return ignoreErrors(this);
3447};
3448
3449// (Stream) -> Stream
3450// (Property) -> Property
3451Observable.prototype.ignoreEnd = function () {
3452 return ignoreEnd(this);
3453};
3454
3455// (Stream, Function) -> Stream
3456// (Property, Function) -> Property
3457Observable.prototype.beforeEnd = function (fn) {
3458 return beforeEnd(this, fn);
3459};
3460
3461// (Stream, number, number|undefined) -> Stream
3462// (Property, number, number|undefined) -> Property
3463Observable.prototype.slidingWindow = function (max, min) {
3464 return slidingWindow(this, max, min);
3465};
3466
3467// Options = {flushOnEnd: boolean|undefined}
3468// (Stream, Function|falsey, Options|undefined) -> Stream
3469// (Property, Function|falsey, Options|undefined) -> Property
3470Observable.prototype.bufferWhile = function (fn, options) {
3471 return bufferWhile(this, fn, options);
3472};
3473
3474// (Stream, number) -> Stream
3475// (Property, number) -> Property
3476Observable.prototype.bufferWithCount = function (count, options) {
3477 return bufferWhile$1(this, count, options);
3478};
3479
3480// Options = {flushOnEnd: boolean|undefined}
3481// (Stream, number, number, Options|undefined) -> Stream
3482// (Property, number, number, Options|undefined) -> Property
3483Observable.prototype.bufferWithTimeOrCount = function (wait, count, options) {
3484 return bufferWithTimeOrCount(this, wait, count, options);
3485};
3486
3487// (Stream, Function) -> Stream
3488// (Property, Function) -> Property
3489Observable.prototype.transduce = function (transducer) {
3490 return transduce(this, transducer);
3491};
3492
3493// (Stream, Function) -> Stream
3494// (Property, Function) -> Property
3495Observable.prototype.withHandler = function (fn) {
3496 return withHandler(this, fn);
3497};
3498
3499// (Stream, Stream -> a) -> a
3500// (Property, Property -> a) -> a
3501Observable.prototype.thru = function (fn) {
3502 return fn(this);
3503};
3504
3505// Combine observables
3506// -----------------------------------------------------------------------------
3507
3508// (Array<Stream|Property>, Function|undefiend) -> Stream
3509// (Array<Stream|Property>, Array<Stream|Property>, Function|undefiend) -> Stream
3510Observable.prototype.combine = function (other, combinator) {
3511 return combine([this, other], combinator);
3512};
3513
3514// (Array<Stream|Property>, Function|undefiend) -> Stream
3515Observable.prototype.zip = function (other, combinator) {
3516 return zip([this, other], combinator);
3517};
3518
3519// (Array<Stream|Property>) -> Stream
3520Observable.prototype.merge = function (other) {
3521 return merge([this, other]);
3522};
3523
3524// (Array<Stream|Property>) -> Stream
3525Observable.prototype.concat = function (other) {
3526 return concat$1([this, other]);
3527};
3528
3529// () -> Pool
3530var pool = function () {
3531 return new Pool();
3532};
3533
3534// (Function) -> Stream
3535// Options = {concurLim: number|undefined, queueLim: number|undefined, drop: 'old'|'new'|undefiend}
3536// (Stream|Property, Function|falsey, Options|undefined) -> Stream
3537Observable.prototype.flatMap = function (fn) {
3538 return new FlatMap(this, fn).setName(this, 'flatMap');
3539};
3540Observable.prototype.flatMapLatest = function (fn) {
3541 return new FlatMap(this, fn, { concurLim: 1, drop: 'old' }).setName(this, 'flatMapLatest');
3542};
3543Observable.prototype.flatMapFirst = function (fn) {
3544 return new FlatMap(this, fn, { concurLim: 1 }).setName(this, 'flatMapFirst');
3545};
3546Observable.prototype.flatMapConcat = function (fn) {
3547 return new FlatMap(this, fn, { queueLim: -1, concurLim: 1 }).setName(this, 'flatMapConcat');
3548};
3549Observable.prototype.flatMapConcurLimit = function (fn, limit) {
3550 return new FlatMap(this, fn, { queueLim: -1, concurLim: limit }).setName(this, 'flatMapConcurLimit');
3551};
3552
3553// (Stream|Property, Function|falsey) -> Stream
3554Observable.prototype.flatMapErrors = function (fn) {
3555 return new FlatMapErrors(this, fn).setName(this, 'flatMapErrors');
3556};
3557
3558// Combine two observables
3559// -----------------------------------------------------------------------------
3560
3561// (Stream, Stream|Property) -> Stream
3562// (Property, Stream|Property) -> Property
3563Observable.prototype.filterBy = function (other) {
3564 return filterBy(this, other);
3565};
3566
3567// (Stream, Stream|Property, Function|undefiend) -> Stream
3568// (Property, Stream|Property, Function|undefiend) -> Property
3569Observable.prototype.sampledBy = function (other, combinator) {
3570 return sampledBy(this, other, combinator);
3571};
3572
3573// (Stream, Stream|Property) -> Stream
3574// (Property, Stream|Property) -> Property
3575Observable.prototype.skipUntilBy = function (other) {
3576 return skipUntilBy(this, other);
3577};
3578
3579// (Stream, Stream|Property) -> Stream
3580// (Property, Stream|Property) -> Property
3581Observable.prototype.takeUntilBy = function (other) {
3582 return takeUntilBy(this, other);
3583};
3584
3585// Options = {flushOnEnd: boolean|undefined}
3586// (Stream, Stream|Property, Options|undefined) -> Stream
3587// (Property, Stream|Property, Options|undefined) -> Property
3588Observable.prototype.bufferBy = function (other, options) {
3589 return bufferBy(this, other, options);
3590};
3591
3592// Options = {flushOnEnd: boolean|undefined}
3593// (Stream, Stream|Property, Options|undefined) -> Stream
3594// (Property, Stream|Property, Options|undefined) -> Property
3595Observable.prototype.bufferWhileBy = function (other, options) {
3596 return bufferWhileBy(this, other, options);
3597};
3598
3599// Deprecated
3600// -----------------------------------------------------------------------------
3601
3602var DEPRECATION_WARNINGS = true;
3603function dissableDeprecationWarnings() {
3604 DEPRECATION_WARNINGS = false;
3605}
3606
3607function warn(msg) {
3608 if (DEPRECATION_WARNINGS && console && typeof console.warn === 'function') {
3609 var msg2 = '\nHere is an Error object for you containing the call stack:';
3610 console.warn(msg, msg2, new Error());
3611 }
3612}
3613
3614// (Stream|Property, Stream|Property) -> Property
3615Observable.prototype.awaiting = function (other) {
3616 warn('You are using deprecated .awaiting() method, see https://github.com/kefirjs/kefir/issues/145');
3617 return awaiting(this, other);
3618};
3619
3620// (Stream, Function|undefined) -> Stream
3621// (Property, Function|undefined) -> Property
3622Observable.prototype.valuesToErrors = function (fn) {
3623 warn('You are using deprecated .valuesToErrors() method, see https://github.com/kefirjs/kefir/issues/149');
3624 return valuesToErrors(this, fn);
3625};
3626
3627// (Stream, Function|undefined) -> Stream
3628// (Property, Function|undefined) -> Property
3629Observable.prototype.errorsToValues = function (fn) {
3630 warn('You are using deprecated .errorsToValues() method, see https://github.com/kefirjs/kefir/issues/149');
3631 return errorsToValues(this, fn);
3632};
3633
3634// (Stream) -> Stream
3635// (Property) -> Property
3636Observable.prototype.endOnError = function () {
3637 warn('You are using deprecated .endOnError() method, see https://github.com/kefirjs/kefir/issues/150');
3638 return endOnError(this);
3639};
3640
3641// Exports
3642// --------------------------------------------------------------------------
3643
3644var Kefir = {
3645 Observable: Observable,
3646 Stream: Stream,
3647 Property: Property,
3648 never: never,
3649 later: later,
3650 interval: interval,
3651 sequentially: sequentially,
3652 fromPoll: fromPoll,
3653 withInterval: withInterval,
3654 fromCallback: fromCallback,
3655 fromNodeCallback: fromNodeCallback,
3656 fromEvents: fromEvents,
3657 stream: stream,
3658 constant: constant,
3659 constantError: constantError,
3660 fromPromise: fromPromise,
3661 fromESObservable: fromESObservable,
3662 combine: combine,
3663 zip: zip,
3664 merge: merge,
3665 concat: concat$1,
3666 Pool: Pool,
3667 pool: pool,
3668 repeat: repeat,
3669 staticLand: staticLand
3670};
3671
3672Kefir.Kefir = Kefir;
3673
3674exports.dissableDeprecationWarnings = dissableDeprecationWarnings;
3675exports.Kefir = Kefir;
3676exports.Observable = Observable;
3677exports.Stream = Stream;
3678exports.Property = Property;
3679exports.never = never;
3680exports.later = later;
3681exports.interval = interval;
3682exports.sequentially = sequentially;
3683exports.fromPoll = fromPoll;
3684exports.withInterval = withInterval;
3685exports.fromCallback = fromCallback;
3686exports.fromNodeCallback = fromNodeCallback;
3687exports.fromEvents = fromEvents;
3688exports.stream = stream;
3689exports.constant = constant;
3690exports.constantError = constantError;
3691exports.fromPromise = fromPromise;
3692exports.fromESObservable = fromESObservable;
3693exports.combine = combine;
3694exports.zip = zip;
3695exports.merge = merge;
3696exports.concat = concat$1;
3697exports.Pool = Pool;
3698exports.pool = pool;
3699exports.repeat = repeat;
3700exports.staticLand = staticLand;
3701exports['default'] = Kefir;
3702
3703Object.defineProperty(exports, '__esModule', { value: true });
3704
3705})));