UNPKG

46.1 kBJavaScriptView Raw
1// Copyright (c) Microsoft, All rights reserved. See License.txt in the project root for license information.
2
3;(function (factory) {
4 var objectTypes = {
5 'function': true,
6 'object': true
7 };
8
9 function checkGlobal(value) {
10 return (value && value.Object === Object) ? value : null;
11 }
12
13 var freeExports = (objectTypes[typeof exports] && exports && !exports.nodeType) ? exports : null;
14 var freeModule = (objectTypes[typeof module] && module && !module.nodeType) ? module : null;
15 var freeGlobal = checkGlobal(freeExports && freeModule && typeof global === 'object' && global);
16 var freeSelf = checkGlobal(objectTypes[typeof self] && self);
17 var freeWindow = checkGlobal(objectTypes[typeof window] && window);
18 var moduleExports = (freeModule && freeModule.exports === freeExports) ? freeExports : null;
19 var thisGlobal = checkGlobal(objectTypes[typeof this] && this);
20 var root = freeGlobal || ((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) || freeSelf || thisGlobal || Function('return this')();
21
22 // Because of build optimizers
23 if (typeof define === 'function' && define.amd) {
24 define(['./rx'], function (Rx, exports) {
25 return factory(root, exports, Rx);
26 });
27 } else if (typeof module === 'object' && module && module.exports === freeExports) {
28 module.exports = factory(root, module.exports, require('./rx'));
29 } else {
30 root.Rx = factory(root, {}, root.Rx);
31 }
32}.call(this, function (root, exp, Rx, undefined) {
33
34 // References
35 var Observable = Rx.Observable,
36 observableProto = Observable.prototype,
37 BinaryDisposable = Rx.BinaryDisposable,
38 AnonymousObservable = Rx.AnonymousObservable,
39 AbstractObserver = Rx.internals.AbstractObserver,
40 disposableEmpty = Rx.Disposable.empty,
41 helpers = Rx.helpers,
42 defaultComparer = helpers.defaultComparer,
43 identity = helpers.identity,
44 defaultSubComparer = helpers.defaultSubComparer,
45 isFunction = helpers.isFunction,
46 isPromise = helpers.isPromise,
47 isArrayLike = helpers.isArrayLike,
48 isIterable = helpers.isIterable,
49 inherits = Rx.internals.inherits,
50 observableFromPromise = Observable.fromPromise,
51 observableFrom = Observable.from,
52 bindCallback = Rx.internals.bindCallback,
53 EmptyError = Rx.EmptyError,
54 ObservableBase = Rx.ObservableBase,
55 ArgumentOutOfRangeError = Rx.ArgumentOutOfRangeError;
56
57 var errorObj = {e: {}};
58
59 function tryCatcherGen(tryCatchTarget) {
60 return function tryCatcher() {
61 try {
62 return tryCatchTarget.apply(this, arguments);
63 } catch (e) {
64 errorObj.e = e;
65 return errorObj;
66 }
67 };
68 }
69
70 var tryCatch = Rx.internals.tryCatch = function tryCatch(fn) {
71 if (!isFunction(fn)) { throw new TypeError('fn must be a function'); }
72 return tryCatcherGen(fn);
73 };
74
75 function thrower(e) {
76 throw e;
77 }
78
79 var ExtremaByObservable = (function (__super__) {
80 inherits(ExtremaByObservable, __super__);
81 function ExtremaByObservable(source, k, c) {
82 this.source = source;
83 this._k = k;
84 this._c = c;
85 __super__.call(this);
86 }
87
88 ExtremaByObservable.prototype.subscribeCore = function (o) {
89 return this.source.subscribe(new ExtremaByObserver(o, this._k, this._c));
90 };
91
92 return ExtremaByObservable;
93 }(ObservableBase));
94
95 var ExtremaByObserver = (function (__super__) {
96 inherits(ExtremaByObserver, __super__);
97 function ExtremaByObserver(o, k, c) {
98 this._o = o;
99 this._k = k;
100 this._c = c;
101 this._v = null;
102 this._hv = false;
103 this._l = [];
104 __super__.call(this);
105 }
106
107 ExtremaByObserver.prototype.next = function (x) {
108 var key = tryCatch(this._k)(x);
109 if (key === errorObj) { return this._o.onError(key.e); }
110 var comparison = 0;
111 if (!this._hv) {
112 this._hv = true;
113 this._v = key;
114 } else {
115 comparison = tryCatch(this._c)(key, this._v);
116 if (comparison === errorObj) { return this._o.onError(comparison.e); }
117 }
118 if (comparison > 0) {
119 this._v = key;
120 this._l = [];
121 }
122 if (comparison >= 0) { this._l.push(x); }
123 };
124
125 ExtremaByObserver.prototype.error = function (e) {
126 this._o.onError(e);
127 };
128
129 ExtremaByObserver.prototype.completed = function () {
130 this._o.onNext(this._l);
131 this._o.onCompleted();
132 };
133
134 return ExtremaByObserver;
135 }(AbstractObserver));
136
137 function firstOnly(x) {
138 if (x.length === 0) { throw new EmptyError(); }
139 return x[0];
140 }
141
142 var ReduceObservable = (function(__super__) {
143 inherits(ReduceObservable, __super__);
144 function ReduceObservable(source, accumulator, hasSeed, seed) {
145 this.source = source;
146 this.accumulator = accumulator;
147 this.hasSeed = hasSeed;
148 this.seed = seed;
149 __super__.call(this);
150 }
151
152 ReduceObservable.prototype.subscribeCore = function(observer) {
153 return this.source.subscribe(new ReduceObserver(observer,this));
154 };
155
156 return ReduceObservable;
157 }(ObservableBase));
158
159 var ReduceObserver = (function (__super__) {
160 inherits(ReduceObserver, __super__);
161 function ReduceObserver(o, parent) {
162 this._o = o;
163 this._p = parent;
164 this._fn = parent.accumulator;
165 this._hs = parent.hasSeed;
166 this._s = parent.seed;
167 this._ha = false;
168 this._a = null;
169 this._hv = false;
170 this._i = 0;
171 __super__.call(this);
172 }
173
174 ReduceObserver.prototype.next = function (x) {
175 !this._hv && (this._hv = true);
176 if (this._ha) {
177 this._a = tryCatch(this._fn)(this._a, x, this._i, this._p);
178 } else {
179 this._a = this._hs ? tryCatch(this._fn)(this._s, x, this._i, this._p) : x;
180 this._ha = true;
181 }
182 if (this._a === errorObj) { return this._o.onError(this._a.e); }
183 this._i++;
184 };
185
186 ReduceObserver.prototype.error = function (e) {
187 this._o.onError(e);
188 };
189
190 ReduceObserver.prototype.completed = function () {
191 this._hv && this._o.onNext(this._a);
192 !this._hv && this._hs && this._o.onNext(this._s);
193 !this._hv && !this._hs && this._o.onError(new EmptyError());
194 this._o.onCompleted();
195 };
196
197 return ReduceObserver;
198 }(AbstractObserver));
199
200 /**
201 * Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified seed value is used as the initial accumulator value.
202 * For aggregation behavior with incremental intermediate results, see Observable.scan.
203 * @param {Function} accumulator An accumulator function to be invoked on each element.
204 * @param {Any} [seed] The initial accumulator value.
205 * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
206 */
207 observableProto.reduce = function () {
208 var hasSeed = false, seed, accumulator = arguments[0];
209 if (arguments.length === 2) {
210 hasSeed = true;
211 seed = arguments[1];
212 }
213 return new ReduceObservable(this, accumulator, hasSeed, seed);
214 };
215
216 var SomeObservable = (function (__super__) {
217 inherits(SomeObservable, __super__);
218 function SomeObservable(source, fn) {
219 this.source = source;
220 this._fn = fn;
221 __super__.call(this);
222 }
223
224 SomeObservable.prototype.subscribeCore = function (o) {
225 return this.source.subscribe(new SomeObserver(o, this._fn, this.source));
226 };
227
228 return SomeObservable;
229 }(ObservableBase));
230
231 var SomeObserver = (function (__super__) {
232 inherits(SomeObserver, __super__);
233
234 function SomeObserver(o, fn, s) {
235 this._o = o;
236 this._fn = fn;
237 this._s = s;
238 this._i = 0;
239 __super__.call(this);
240 }
241
242 SomeObserver.prototype.next = function (x) {
243 var result = tryCatch(this._fn)(x, this._i++, this._s);
244 if (result === errorObj) { return this._o.onError(result.e); }
245 if (Boolean(result)) {
246 this._o.onNext(true);
247 this._o.onCompleted();
248 }
249 };
250 SomeObserver.prototype.error = function (e) { this._o.onError(e); };
251 SomeObserver.prototype.completed = function () {
252 this._o.onNext(false);
253 this._o.onCompleted();
254 };
255
256 return SomeObserver;
257 }(AbstractObserver));
258
259 /**
260 * Determines whether any element of an observable sequence satisfies a condition if present, else if any items are in the sequence.
261 * @param {Function} [predicate] A function to test each element for a condition.
262 * @returns {Observable} An observable sequence containing a single element determining whether any elements in the source sequence pass the test in the specified predicate if given, else if any items are in the sequence.
263 */
264 observableProto.some = function (predicate, thisArg) {
265 var fn = bindCallback(predicate, thisArg, 3);
266 return new SomeObservable(this, fn);
267 };
268
269 var IsEmptyObservable = (function (__super__) {
270 inherits(IsEmptyObservable, __super__);
271 function IsEmptyObservable(source) {
272 this.source = source;
273 __super__.call(this);
274 }
275
276 IsEmptyObservable.prototype.subscribeCore = function (o) {
277 return this.source.subscribe(new IsEmptyObserver(o));
278 };
279
280 return IsEmptyObservable;
281 }(ObservableBase));
282
283 var IsEmptyObserver = (function(__super__) {
284 inherits(IsEmptyObserver, __super__);
285 function IsEmptyObserver(o) {
286 this._o = o;
287 __super__.call(this);
288 }
289
290 IsEmptyObserver.prototype.next = function () {
291 this._o.onNext(false);
292 this._o.onCompleted();
293 };
294 IsEmptyObserver.prototype.error = function (e) { this._o.onError(e); };
295 IsEmptyObserver.prototype.completed = function () {
296 this._o.onNext(true);
297 this._o.onCompleted();
298 };
299
300 return IsEmptyObserver;
301 }(AbstractObserver));
302
303 /**
304 * Determines whether an observable sequence is empty.
305 * @returns {Observable} An observable sequence containing a single element determining whether the source sequence is empty.
306 */
307 observableProto.isEmpty = function () {
308 return new IsEmptyObservable(this);
309 };
310
311 var EveryObservable = (function (__super__) {
312 inherits(EveryObservable, __super__);
313 function EveryObservable(source, fn) {
314 this.source = source;
315 this._fn = fn;
316 __super__.call(this);
317 }
318
319 EveryObservable.prototype.subscribeCore = function (o) {
320 return this.source.subscribe(new EveryObserver(o, this._fn, this.source));
321 };
322
323 return EveryObservable;
324 }(ObservableBase));
325
326 var EveryObserver = (function (__super__) {
327 inherits(EveryObserver, __super__);
328
329 function EveryObserver(o, fn, s) {
330 this._o = o;
331 this._fn = fn;
332 this._s = s;
333 this._i = 0;
334 __super__.call(this);
335 }
336
337 EveryObserver.prototype.next = function (x) {
338 var result = tryCatch(this._fn)(x, this._i++, this._s);
339 if (result === errorObj) { return this._o.onError(result.e); }
340 if (!Boolean(result)) {
341 this._o.onNext(false);
342 this._o.onCompleted();
343 }
344 };
345 EveryObserver.prototype.error = function (e) { this._o.onError(e); };
346 EveryObserver.prototype.completed = function () {
347 this._o.onNext(true);
348 this._o.onCompleted();
349 };
350
351 return EveryObserver;
352 }(AbstractObserver));
353
354 /**
355 * Determines whether all elements of an observable sequence satisfy a condition.
356 * @param {Function} [predicate] A function to test each element for a condition.
357 * @param {Any} [thisArg] Object to use as this when executing callback.
358 * @returns {Observable} An observable sequence containing a single element determining whether all elements in the source sequence pass the test in the specified predicate.
359 */
360 observableProto.every = function (predicate, thisArg) {
361 var fn = bindCallback(predicate, thisArg, 3);
362 return new EveryObservable(this, fn);
363 };
364
365 var IncludesObservable = (function (__super__) {
366 inherits(IncludesObservable, __super__);
367 function IncludesObservable(source, elem, idx) {
368 var n = +idx || 0;
369 Math.abs(n) === Infinity && (n = 0);
370
371 this.source = source;
372 this._elem = elem;
373 this._n = n;
374 __super__.call(this);
375 }
376
377 IncludesObservable.prototype.subscribeCore = function (o) {
378 if (this._n < 0) {
379 o.onNext(false);
380 o.onCompleted();
381 return disposableEmpty;
382 }
383
384 return this.source.subscribe(new IncludesObserver(o, this._elem, this._n));
385 };
386
387 return IncludesObservable;
388 }(ObservableBase));
389
390 var IncludesObserver = (function (__super__) {
391 inherits(IncludesObserver, __super__);
392 function IncludesObserver(o, elem, n) {
393 this._o = o;
394 this._elem = elem;
395 this._n = n;
396 this._i = 0;
397 __super__.call(this);
398 }
399
400 function comparer(a, b) {
401 return (a === 0 && b === 0) || (a === b || (isNaN(a) && isNaN(b)));
402 }
403
404 IncludesObserver.prototype.next = function (x) {
405 if (this._i++ >= this._n && comparer(x, this._elem)) {
406 this._o.onNext(true);
407 this._o.onCompleted();
408 }
409 };
410 IncludesObserver.prototype.error = function (e) { this._o.onError(e); };
411 IncludesObserver.prototype.completed = function () { this._o.onNext(false); this._o.onCompleted(); };
412
413 return IncludesObserver;
414 }(AbstractObserver));
415
416 /**
417 * Determines whether an observable sequence includes a specified element with an optional equality comparer.
418 * @param searchElement The value to locate in the source sequence.
419 * @param {Number} [fromIndex] An equality comparer to compare elements.
420 * @returns {Observable} An observable sequence containing a single element determining whether the source sequence includes an element that has the specified value from the given index.
421 */
422 observableProto.includes = function (searchElement, fromIndex) {
423 return new IncludesObservable(this, searchElement, fromIndex);
424 };
425
426 var CountObservable = (function (__super__) {
427 inherits(CountObservable, __super__);
428 function CountObservable(source, fn) {
429 this.source = source;
430 this._fn = fn;
431 __super__.call(this);
432 }
433
434 CountObservable.prototype.subscribeCore = function (o) {
435 return this.source.subscribe(new CountObserver(o, this._fn, this.source));
436 };
437
438 return CountObservable;
439 }(ObservableBase));
440
441 var CountObserver = (function (__super__) {
442 inherits(CountObserver, __super__);
443
444 function CountObserver(o, fn, s) {
445 this._o = o;
446 this._fn = fn;
447 this._s = s;
448 this._i = 0;
449 this._c = 0;
450 __super__.call(this);
451 }
452
453 CountObserver.prototype.next = function (x) {
454 if (this._fn) {
455 var result = tryCatch(this._fn)(x, this._i++, this._s);
456 if (result === errorObj) { return this._o.onError(result.e); }
457 Boolean(result) && (this._c++);
458 } else {
459 this._c++;
460 }
461 };
462 CountObserver.prototype.error = function (e) { this._o.onError(e); };
463 CountObserver.prototype.completed = function () {
464 this._o.onNext(this._c);
465 this._o.onCompleted();
466 };
467
468 return CountObserver;
469 }(AbstractObserver));
470
471 /**
472 * Returns an observable sequence containing a value that represents how many elements in the specified observable sequence satisfy a condition if provided, else the count of items.
473 * @example
474 * res = source.count();
475 * res = source.count(function (x) { return x > 3; });
476 * @param {Function} [predicate]A function to test each element for a condition.
477 * @param {Any} [thisArg] Object to use as this when executing callback.
478 * @returns {Observable} An observable sequence containing a single element with a number that represents how many elements in the input sequence satisfy the condition in the predicate function if provided, else the count of items in the sequence.
479 */
480 observableProto.count = function (predicate, thisArg) {
481 var fn = bindCallback(predicate, thisArg, 3);
482 return new CountObservable(this, fn);
483 };
484
485 var IndexOfObservable = (function (__super__) {
486 inherits(IndexOfObservable, __super__);
487 function IndexOfObservable(source, e, n) {
488 this.source = source;
489 this._e = e;
490 this._n = n;
491 __super__.call(this);
492 }
493
494 IndexOfObservable.prototype.subscribeCore = function (o) {
495 if (this._n < 0) {
496 o.onNext(-1);
497 o.onCompleted();
498 return disposableEmpty;
499 }
500
501 return this.source.subscribe(new IndexOfObserver(o, this._e, this._n));
502 };
503
504 return IndexOfObservable;
505 }(ObservableBase));
506
507 var IndexOfObserver = (function (__super__) {
508 inherits(IndexOfObserver, __super__);
509 function IndexOfObserver(o, e, n) {
510 this._o = o;
511 this._e = e;
512 this._n = n;
513 this._i = 0;
514 __super__.call(this);
515 }
516
517 IndexOfObserver.prototype.next = function (x) {
518 if (this._i >= this._n && x === this._e) {
519 this._o.onNext(this._i);
520 this._o.onCompleted();
521 }
522 this._i++;
523 };
524 IndexOfObserver.prototype.error = function (e) { this._o.onError(e); };
525 IndexOfObserver.prototype.completed = function () { this._o.onNext(-1); this._o.onCompleted(); };
526
527 return IndexOfObserver;
528 }(AbstractObserver));
529
530 /**
531 * Returns the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
532 * @param {Any} searchElement Element to locate in the array.
533 * @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
534 * @returns {Observable} And observable sequence containing the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
535 */
536 observableProto.indexOf = function(searchElement, fromIndex) {
537 var n = +fromIndex || 0;
538 Math.abs(n) === Infinity && (n = 0);
539 return new IndexOfObservable(this, searchElement, n);
540 };
541
542 var SumObservable = (function (__super__) {
543 inherits(SumObservable, __super__);
544 function SumObservable(source, fn) {
545 this.source = source;
546 this._fn = fn;
547 __super__.call(this);
548 }
549
550 SumObservable.prototype.subscribeCore = function (o) {
551 return this.source.subscribe(new SumObserver(o, this._fn, this.source));
552 };
553
554 return SumObservable;
555 }(ObservableBase));
556
557 var SumObserver = (function (__super__) {
558 inherits(SumObserver, __super__);
559
560 function SumObserver(o, fn, s) {
561 this._o = o;
562 this._fn = fn;
563 this._s = s;
564 this._i = 0;
565 this._c = 0;
566 __super__.call(this);
567 }
568
569 SumObserver.prototype.next = function (x) {
570 if (this._fn) {
571 var result = tryCatch(this._fn)(x, this._i++, this._s);
572 if (result === errorObj) { return this._o.onError(result.e); }
573 this._c += result;
574 } else {
575 this._c += x;
576 }
577 };
578 SumObserver.prototype.error = function (e) { this._o.onError(e); };
579 SumObserver.prototype.completed = function () {
580 this._o.onNext(this._c);
581 this._o.onCompleted();
582 };
583
584 return SumObserver;
585 }(AbstractObserver));
586
587 /**
588 * Computes the sum of a sequence of values that are obtained by invoking an optional transform function on each element of the input sequence, else if not specified computes the sum on each item in the sequence.
589 * @param {Function} [selector] A transform function to apply to each element.
590 * @param {Any} [thisArg] Object to use as this when executing callback.
591 * @returns {Observable} An observable sequence containing a single element with the sum of the values in the source sequence.
592 */
593 observableProto.sum = function (keySelector, thisArg) {
594 var fn = bindCallback(keySelector, thisArg, 3);
595 return new SumObservable(this, fn);
596 };
597
598 /**
599 * Returns the elements in an observable sequence with the minimum key value according to the specified comparer.
600 * @example
601 * var res = source.minBy(function (x) { return x.value; });
602 * var res = source.minBy(function (x) { return x.value; }, function (x, y) { return x - y; });
603 * @param {Function} keySelector Key selector function.
604 * @param {Function} [comparer] Comparer used to compare key values.
605 * @returns {Observable} An observable sequence containing a list of zero or more elements that have a minimum key value.
606 */
607 observableProto.minBy = function (keySelector, comparer) {
608 comparer || (comparer = defaultSubComparer);
609 return new ExtremaByObservable(this, keySelector, function (x, y) { return comparer(x, y) * -1; });
610 };
611
612 /**
613 * Returns the minimum element in an observable sequence according to the optional comparer else a default greater than less than check.
614 * @example
615 * var res = source.min();
616 * var res = source.min(function (x, y) { return x.value - y.value; });
617 * @param {Function} [comparer] Comparer used to compare elements.
618 * @returns {Observable} An observable sequence containing a single element with the minimum element in the source sequence.
619 */
620 observableProto.min = function (comparer) {
621 return this.minBy(identity, comparer).map(function (x) { return firstOnly(x); });
622 };
623
624 /**
625 * Returns the elements in an observable sequence with the maximum key value according to the specified comparer.
626 * @example
627 * var res = source.maxBy(function (x) { return x.value; });
628 * var res = source.maxBy(function (x) { return x.value; }, function (x, y) { return x - y;; });
629 * @param {Function} keySelector Key selector function.
630 * @param {Function} [comparer] Comparer used to compare key values.
631 * @returns {Observable} An observable sequence containing a list of zero or more elements that have a maximum key value.
632 */
633 observableProto.maxBy = function (keySelector, comparer) {
634 comparer || (comparer = defaultSubComparer);
635 return new ExtremaByObservable(this, keySelector, comparer);
636 };
637
638 /**
639 * Returns the maximum value in an observable sequence according to the specified comparer.
640 * @example
641 * var res = source.max();
642 * var res = source.max(function (x, y) { return x.value - y.value; });
643 * @param {Function} [comparer] Comparer used to compare elements.
644 * @returns {Observable} An observable sequence containing a single element with the maximum element in the source sequence.
645 */
646 observableProto.max = function (comparer) {
647 return this.maxBy(identity, comparer).map(function (x) { return firstOnly(x); });
648 };
649
650 var AverageObservable = (function (__super__) {
651 inherits(AverageObservable, __super__);
652 function AverageObservable(source, fn) {
653 this.source = source;
654 this._fn = fn;
655 __super__.call(this);
656 }
657
658 AverageObservable.prototype.subscribeCore = function (o) {
659 return this.source.subscribe(new AverageObserver(o, this._fn, this.source));
660 };
661
662 return AverageObservable;
663 }(ObservableBase));
664
665 var AverageObserver = (function(__super__) {
666 inherits(AverageObserver, __super__);
667 function AverageObserver(o, fn, s) {
668 this._o = o;
669 this._fn = fn;
670 this._s = s;
671 this._c = 0;
672 this._t = 0;
673 __super__.call(this);
674 }
675
676 AverageObserver.prototype.next = function (x) {
677 if(this._fn) {
678 var r = tryCatch(this._fn)(x, this._c++, this._s);
679 if (r === errorObj) { return this._o.onError(r.e); }
680 this._t += r;
681 } else {
682 this._c++;
683 this._t += x;
684 }
685 };
686 AverageObserver.prototype.error = function (e) { this._o.onError(e); };
687 AverageObserver.prototype.completed = function () {
688 if (this._c === 0) { return this._o.onError(new EmptyError()); }
689 this._o.onNext(this._t / this._c);
690 this._o.onCompleted();
691 };
692
693 return AverageObserver;
694 }(AbstractObserver));
695
696 /**
697 * Computes the average of an observable sequence of values that are in the sequence or obtained by invoking a transform function on each element of the input sequence if present.
698 * @param {Function} [selector] A transform function to apply to each element.
699 * @param {Any} [thisArg] Object to use as this when executing callback.
700 * @returns {Observable} An observable sequence containing a single element with the average of the sequence of values.
701 */
702 observableProto.average = function (keySelector, thisArg) {
703 var source = this, fn;
704 if (isFunction(keySelector)) {
705 fn = bindCallback(keySelector, thisArg, 3);
706 }
707 return new AverageObservable(source, fn);
708 };
709
710 /**
711 * Determines whether two sequences are equal by comparing the elements pairwise using a specified equality comparer.
712 *
713 * @example
714 * var res = res = source.sequenceEqual([1,2,3]);
715 * var res = res = source.sequenceEqual([{ value: 42 }], function (x, y) { return x.value === y.value; });
716 * 3 - res = source.sequenceEqual(Rx.Observable.returnValue(42));
717 * 4 - res = source.sequenceEqual(Rx.Observable.returnValue({ value: 42 }), function (x, y) { return x.value === y.value; });
718 * @param {Observable} second Second observable sequence or array to compare.
719 * @param {Function} [comparer] Comparer used to compare elements of both sequences.
720 * @returns {Observable} An observable sequence that contains a single element which indicates whether both sequences are of equal length and their corresponding elements are equal according to the specified equality comparer.
721 */
722 observableProto.sequenceEqual = function (second, comparer) {
723 var first = this;
724 comparer || (comparer = defaultComparer);
725 return new AnonymousObservable(function (o) {
726 var donel = false, doner = false, ql = [], qr = [];
727 var subscription1 = first.subscribe(function (x) {
728 if (qr.length > 0) {
729 var v = qr.shift();
730 var equal = tryCatch(comparer)(v, x);
731 if (equal === errorObj) { return o.onError(equal.e); }
732 if (!equal) {
733 o.onNext(false);
734 o.onCompleted();
735 }
736 } else if (doner) {
737 o.onNext(false);
738 o.onCompleted();
739 } else {
740 ql.push(x);
741 }
742 }, function(e) { o.onError(e); }, function () {
743 donel = true;
744 if (ql.length === 0) {
745 if (qr.length > 0) {
746 o.onNext(false);
747 o.onCompleted();
748 } else if (doner) {
749 o.onNext(true);
750 o.onCompleted();
751 }
752 }
753 });
754
755 (isArrayLike(second) || isIterable(second)) && (second = observableFrom(second));
756 isPromise(second) && (second = observableFromPromise(second));
757 var subscription2 = second.subscribe(function (x) {
758 if (ql.length > 0) {
759 var v = ql.shift();
760 var equal = tryCatch(comparer)(v, x);
761 if (equal === errorObj) { return o.onError(equal.e); }
762 if (!equal) {
763 o.onNext(false);
764 o.onCompleted();
765 }
766 } else if (donel) {
767 o.onNext(false);
768 o.onCompleted();
769 } else {
770 qr.push(x);
771 }
772 }, function(e) { o.onError(e); }, function () {
773 doner = true;
774 if (qr.length === 0) {
775 if (ql.length > 0) {
776 o.onNext(false);
777 o.onCompleted();
778 } else if (donel) {
779 o.onNext(true);
780 o.onCompleted();
781 }
782 }
783 });
784 return new BinaryDisposable(subscription1, subscription2);
785 }, first);
786 };
787
788 var ElementAtObservable = (function (__super__) {
789 inherits(ElementAtObservable, __super__);
790 function ElementAtObservable(source, i, d) {
791 this.source = source;
792 this._i = i;
793 this._d = d;
794 __super__.call(this);
795 }
796
797 ElementAtObservable.prototype.subscribeCore = function (o) {
798 return this.source.subscribe(new ElementAtObserver(o, this._i, this._d));
799 };
800
801 return ElementAtObservable;
802 }(ObservableBase));
803
804 var ElementAtObserver = (function (__super__) {
805 inherits(ElementAtObserver, __super__);
806
807 function ElementAtObserver(o, i, d) {
808 this._o = o;
809 this._i = i;
810 this._d = d;
811 __super__.call(this);
812 }
813
814 ElementAtObserver.prototype.next = function (x) {
815 if (this._i-- === 0) {
816 this._o.onNext(x);
817 this._o.onCompleted();
818 }
819 };
820 ElementAtObserver.prototype.error = function (e) { this._o.onError(e); };
821 ElementAtObserver.prototype.completed = function () {
822 if (this._d === undefined) {
823 this._o.onError(new ArgumentOutOfRangeError());
824 } else {
825 this._o.onNext(this._d);
826 this._o.onCompleted();
827 }
828 };
829
830 return ElementAtObserver;
831 }(AbstractObserver));
832
833 /**
834 * Returns the element at a specified index in a sequence or default value if not found.
835 * @param {Number} index The zero-based index of the element to retrieve.
836 * @param {Any} [defaultValue] The default value to use if elementAt does not find a value.
837 * @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence.
838 */
839 observableProto.elementAt = function (index, defaultValue) {
840 if (index < 0) { throw new ArgumentOutOfRangeError(); }
841 return new ElementAtObservable(this, index, defaultValue);
842 };
843
844 var SingleObserver = (function(__super__) {
845 inherits(SingleObserver, __super__);
846 function SingleObserver(o, obj, s) {
847 this._o = o;
848 this._obj = obj;
849 this._s = s;
850 this._i = 0;
851 this._hv = false;
852 this._v = null;
853 __super__.call(this);
854 }
855
856 SingleObserver.prototype.next = function (x) {
857 var shouldYield = false;
858 if (this._obj.predicate) {
859 var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
860 if (res === errorObj) { return this._o.onError(res.e); }
861 Boolean(res) && (shouldYield = true);
862 } else if (!this._obj.predicate) {
863 shouldYield = true;
864 }
865 if (shouldYield) {
866 if (this._hv) {
867 return this._o.onError(new Error('Sequence contains more than one matching element'));
868 }
869 this._hv = true;
870 this._v = x;
871 }
872 };
873 SingleObserver.prototype.error = function (e) { this._o.onError(e); };
874 SingleObserver.prototype.completed = function () {
875 if (this._hv) {
876 this._o.onNext(this._v);
877 this._o.onCompleted();
878 }
879 else if (this._obj.defaultValue === undefined) {
880 this._o.onError(new EmptyError());
881 } else {
882 this._o.onNext(this._obj.defaultValue);
883 this._o.onCompleted();
884 }
885 };
886
887 return SingleObserver;
888 }(AbstractObserver));
889
890
891 /**
892 * Returns the only element of an observable sequence that satisfies the condition in the optional predicate, and reports an exception if there is not exactly one element in the observable sequence.
893 * @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.
894 */
895 observableProto.single = function (predicate, thisArg) {
896 var obj = {}, source = this;
897 if (typeof arguments[0] === 'object') {
898 obj = arguments[0];
899 } else {
900 obj = {
901 predicate: arguments[0],
902 thisArg: arguments[1],
903 defaultValue: arguments[2]
904 };
905 }
906 if (isFunction (obj.predicate)) {
907 var fn = obj.predicate;
908 obj.predicate = bindCallback(fn, obj.thisArg, 3);
909 }
910 return new AnonymousObservable(function (o) {
911 return source.subscribe(new SingleObserver(o, obj, source));
912 }, source);
913 };
914
915 var FirstObservable = (function (__super__) {
916 inherits(FirstObservable, __super__);
917 function FirstObservable(source, obj) {
918 this.source = source;
919 this._obj = obj;
920 __super__.call(this);
921 }
922
923 FirstObservable.prototype.subscribeCore = function (o) {
924 return this.source.subscribe(new FirstObserver(o, this._obj, this.source));
925 };
926
927 return FirstObservable;
928 }(ObservableBase));
929
930 var FirstObserver = (function(__super__) {
931 inherits(FirstObserver, __super__);
932 function FirstObserver(o, obj, s) {
933 this._o = o;
934 this._obj = obj;
935 this._s = s;
936 this._i = 0;
937 __super__.call(this);
938 }
939
940 FirstObserver.prototype.next = function (x) {
941 if (this._obj.predicate) {
942 var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
943 if (res === errorObj) { return this._o.onError(res.e); }
944 if (Boolean(res)) {
945 this._o.onNext(x);
946 this._o.onCompleted();
947 }
948 } else if (!this._obj.predicate) {
949 this._o.onNext(x);
950 this._o.onCompleted();
951 }
952 };
953 FirstObserver.prototype.error = function (e) { this._o.onError(e); };
954 FirstObserver.prototype.completed = function () {
955 if (this._obj.defaultValue === undefined) {
956 this._o.onError(new EmptyError());
957 } else {
958 this._o.onNext(this._obj.defaultValue);
959 this._o.onCompleted();
960 }
961 };
962
963 return FirstObserver;
964 }(AbstractObserver));
965
966 /**
967 * Returns the first element of an observable sequence that satisfies the condition in the predicate if present else the first item in the sequence.
968 * @returns {Observable} Sequence containing the first element in the observable sequence that satisfies the condition in the predicate if provided, else the first item in the sequence.
969 */
970 observableProto.first = function () {
971 var obj = {}, source = this;
972 if (typeof arguments[0] === 'object') {
973 obj = arguments[0];
974 } else {
975 obj = {
976 predicate: arguments[0],
977 thisArg: arguments[1],
978 defaultValue: arguments[2]
979 };
980 }
981 if (isFunction (obj.predicate)) {
982 var fn = obj.predicate;
983 obj.predicate = bindCallback(fn, obj.thisArg, 3);
984 }
985 return new FirstObservable(this, obj);
986 };
987
988 var LastObservable = (function (__super__) {
989 inherits(LastObservable, __super__);
990 function LastObservable(source, obj) {
991 this.source = source;
992 this._obj = obj;
993 __super__.call(this);
994 }
995
996 LastObservable.prototype.subscribeCore = function (o) {
997 return this.source.subscribe(new LastObserver(o, this._obj, this.source));
998 };
999
1000 return LastObservable;
1001 }(ObservableBase));
1002
1003 var LastObserver = (function(__super__) {
1004 inherits(LastObserver, __super__);
1005 function LastObserver(o, obj, s) {
1006 this._o = o;
1007 this._obj = obj;
1008 this._s = s;
1009 this._i = 0;
1010 this._hv = false;
1011 this._v = null;
1012 __super__.call(this);
1013 }
1014
1015 LastObserver.prototype.next = function (x) {
1016 var shouldYield = false;
1017 if (this._obj.predicate) {
1018 var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
1019 if (res === errorObj) { return this._o.onError(res.e); }
1020 Boolean(res) && (shouldYield = true);
1021 } else if (!this._obj.predicate) {
1022 shouldYield = true;
1023 }
1024 if (shouldYield) {
1025 this._hv = true;
1026 this._v = x;
1027 }
1028 };
1029 LastObserver.prototype.error = function (e) { this._o.onError(e); };
1030 LastObserver.prototype.completed = function () {
1031 if (this._hv) {
1032 this._o.onNext(this._v);
1033 this._o.onCompleted();
1034 }
1035 else if (this._obj.defaultValue === undefined) {
1036 this._o.onError(new EmptyError());
1037 } else {
1038 this._o.onNext(this._obj.defaultValue);
1039 this._o.onCompleted();
1040 }
1041 };
1042
1043 return LastObserver;
1044 }(AbstractObserver));
1045
1046 /**
1047 * Returns the last element of an observable sequence that satisfies the condition in the predicate if specified, else the last element.
1048 * @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate.
1049 */
1050 observableProto.last = function () {
1051 var obj = {}, source = this;
1052 if (typeof arguments[0] === 'object') {
1053 obj = arguments[0];
1054 } else {
1055 obj = {
1056 predicate: arguments[0],
1057 thisArg: arguments[1],
1058 defaultValue: arguments[2]
1059 };
1060 }
1061 if (isFunction (obj.predicate)) {
1062 var fn = obj.predicate;
1063 obj.predicate = bindCallback(fn, obj.thisArg, 3);
1064 }
1065 return new LastObservable(this, obj);
1066 };
1067
1068 var FindValueObserver = (function(__super__) {
1069 inherits(FindValueObserver, __super__);
1070 function FindValueObserver(observer, source, callback, yieldIndex) {
1071 this._o = observer;
1072 this._s = source;
1073 this._cb = callback;
1074 this._y = yieldIndex;
1075 this._i = 0;
1076 __super__.call(this);
1077 }
1078
1079 FindValueObserver.prototype.next = function (x) {
1080 var shouldRun = tryCatch(this._cb)(x, this._i, this._s);
1081 if (shouldRun === errorObj) { return this._o.onError(shouldRun.e); }
1082 if (shouldRun) {
1083 this._o.onNext(this._y ? this._i : x);
1084 this._o.onCompleted();
1085 } else {
1086 this._i++;
1087 }
1088 };
1089
1090 FindValueObserver.prototype.error = function (e) {
1091 this._o.onError(e);
1092 };
1093
1094 FindValueObserver.prototype.completed = function () {
1095 this._y && this._o.onNext(-1);
1096 this._o.onCompleted();
1097 };
1098
1099 return FindValueObserver;
1100 }(AbstractObserver));
1101
1102 function findValue (source, predicate, thisArg, yieldIndex) {
1103 var callback = bindCallback(predicate, thisArg, 3);
1104 return new AnonymousObservable(function (o) {
1105 return source.subscribe(new FindValueObserver(o, source, callback, yieldIndex));
1106 }, source);
1107 }
1108
1109 /**
1110 * Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Observable sequence.
1111 * @param {Function} predicate The predicate that defines the conditions of the element to search for.
1112 * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
1113 * @returns {Observable} An Observable sequence with the first element that matches the conditions defined by the specified predicate, if found; otherwise, undefined.
1114 */
1115 observableProto.find = function (predicate, thisArg) {
1116 return findValue(this, predicate, thisArg, false);
1117 };
1118
1119 /**
1120 * Searches for an element that matches the conditions defined by the specified predicate, and returns
1121 * an Observable sequence with the zero-based index of the first occurrence within the entire Observable sequence.
1122 * @param {Function} predicate The predicate that defines the conditions of the element to search for.
1123 * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
1124 * @returns {Observable} An Observable sequence with the zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.
1125 */
1126 observableProto.findIndex = function (predicate, thisArg) {
1127 return findValue(this, predicate, thisArg, true);
1128 };
1129
1130 var ToSetObservable = (function (__super__) {
1131 inherits(ToSetObservable, __super__);
1132 function ToSetObservable(source) {
1133 this.source = source;
1134 __super__.call(this);
1135 }
1136
1137 ToSetObservable.prototype.subscribeCore = function (o) {
1138 return this.source.subscribe(new ToSetObserver(o));
1139 };
1140
1141 return ToSetObservable;
1142 }(ObservableBase));
1143
1144 var ToSetObserver = (function (__super__) {
1145 inherits(ToSetObserver, __super__);
1146 function ToSetObserver(o) {
1147 this._o = o;
1148 this._s = new root.Set();
1149 __super__.call(this);
1150 }
1151
1152 ToSetObserver.prototype.next = function (x) {
1153 this._s.add(x);
1154 };
1155
1156 ToSetObserver.prototype.error = function (e) {
1157 this._o.onError(e);
1158 };
1159
1160 ToSetObserver.prototype.completed = function () {
1161 this._o.onNext(this._s);
1162 this._o.onCompleted();
1163 };
1164
1165 return ToSetObserver;
1166 }(AbstractObserver));
1167
1168 /**
1169 * Converts the observable sequence to a Set if it exists.
1170 * @returns {Observable} An observable sequence with a single value of a Set containing the values from the observable sequence.
1171 */
1172 observableProto.toSet = function () {
1173 if (typeof root.Set === 'undefined') { throw new TypeError(); }
1174 return new ToSetObservable(this);
1175 };
1176
1177 var ToMapObservable = (function (__super__) {
1178 inherits(ToMapObservable, __super__);
1179 function ToMapObservable(source, k, e) {
1180 this.source = source;
1181 this._k = k;
1182 this._e = e;
1183 __super__.call(this);
1184 }
1185
1186 ToMapObservable.prototype.subscribeCore = function (o) {
1187 return this.source.subscribe(new ToMapObserver(o, this._k, this._e));
1188 };
1189
1190 return ToMapObservable;
1191 }(ObservableBase));
1192
1193 var ToMapObserver = (function (__super__) {
1194 inherits(ToMapObserver, __super__);
1195 function ToMapObserver(o, k, e) {
1196 this._o = o;
1197 this._k = k;
1198 this._e = e;
1199 this._m = new root.Map();
1200 __super__.call(this);
1201 }
1202
1203 ToMapObserver.prototype.next = function (x) {
1204 var key = tryCatch(this._k)(x);
1205 if (key === errorObj) { return this._o.onError(key.e); }
1206 var elem = x;
1207 if (this._e) {
1208 elem = tryCatch(this._e)(x);
1209 if (elem === errorObj) { return this._o.onError(elem.e); }
1210 }
1211
1212 this._m.set(key, elem);
1213 };
1214
1215 ToMapObserver.prototype.error = function (e) {
1216 this._o.onError(e);
1217 };
1218
1219 ToMapObserver.prototype.completed = function () {
1220 this._o.onNext(this._m);
1221 this._o.onCompleted();
1222 };
1223
1224 return ToMapObserver;
1225 }(AbstractObserver));
1226
1227 /**
1228 * Converts the observable sequence to a Map if it exists.
1229 * @param {Function} keySelector A function which produces the key for the Map.
1230 * @param {Function} [elementSelector] An optional function which produces the element for the Map. If not present, defaults to the value from the observable sequence.
1231 * @returns {Observable} An observable sequence with a single value of a Map containing the values from the observable sequence.
1232 */
1233 observableProto.toMap = function (keySelector, elementSelector) {
1234 if (typeof root.Map === 'undefined') { throw new TypeError(); }
1235 return new ToMapObservable(this, keySelector, elementSelector);
1236 };
1237
1238 var SliceObservable = (function (__super__) {
1239 inherits(SliceObservable, __super__);
1240 function SliceObservable(source, b, e) {
1241 this.source = source;
1242 this._b = b;
1243 this._e = e;
1244 __super__.call(this);
1245 }
1246
1247 SliceObservable.prototype.subscribeCore = function (o) {
1248 return this.source.subscribe(new SliceObserver(o, this._b, this._e));
1249 };
1250
1251 return SliceObservable;
1252 }(ObservableBase));
1253
1254 var SliceObserver = (function (__super__) {
1255 inherits(SliceObserver, __super__);
1256
1257 function SliceObserver(o, b, e) {
1258 this._o = o;
1259 this._b = b;
1260 this._e = e;
1261 this._i = 0;
1262 __super__.call(this);
1263 }
1264
1265 SliceObserver.prototype.next = function (x) {
1266 if (this._i >= this._b) {
1267 if (this._e === this._i) {
1268 this._o.onCompleted();
1269 } else {
1270 this._o.onNext(x);
1271 }
1272 }
1273 this._i++;
1274 };
1275 SliceObserver.prototype.error = function (e) { this._o.onError(e); };
1276 SliceObserver.prototype.completed = function () { this._o.onCompleted(); };
1277
1278 return SliceObserver;
1279 }(AbstractObserver));
1280
1281 /*
1282 * The slice() method returns a shallow copy of a portion of an Observable into a new Observable object.
1283 * Unlike the array version, this does not support negative numbers for being or end.
1284 * @param {Number} [begin] Zero-based index at which to begin extraction. If omitted, this will default to zero.
1285 * @param {Number} [end] Zero-based index at which to end extraction. slice extracts up to but not including end.
1286 * If omitted, this will emit the rest of the Observable object.
1287 * @returns {Observable} A shallow copy of a portion of an Observable into a new Observable object.
1288 */
1289 observableProto.slice = function (begin, end) {
1290 var start = begin || 0;
1291 if (start < 0) { throw new Rx.ArgumentOutOfRangeError(); }
1292 if (typeof end === 'number' && end < start) {
1293 throw new Rx.ArgumentOutOfRangeError();
1294 }
1295 return new SliceObservable(this, start, end);
1296 };
1297
1298 var LastIndexOfObservable = (function (__super__) {
1299 inherits(LastIndexOfObservable, __super__);
1300 function LastIndexOfObservable(source, e, n) {
1301 this.source = source;
1302 this._e = e;
1303 this._n = n;
1304 __super__.call(this);
1305 }
1306
1307 LastIndexOfObservable.prototype.subscribeCore = function (o) {
1308 if (this._n < 0) {
1309 o.onNext(-1);
1310 o.onCompleted();
1311 return disposableEmpty;
1312 }
1313
1314 return this.source.subscribe(new LastIndexOfObserver(o, this._e, this._n));
1315 };
1316
1317 return LastIndexOfObservable;
1318 }(ObservableBase));
1319
1320 var LastIndexOfObserver = (function (__super__) {
1321 inherits(LastIndexOfObserver, __super__);
1322 function LastIndexOfObserver(o, e, n) {
1323 this._o = o;
1324 this._e = e;
1325 this._n = n;
1326 this._v = 0;
1327 this._hv = false;
1328 this._i = 0;
1329 __super__.call(this);
1330 }
1331
1332 LastIndexOfObserver.prototype.next = function (x) {
1333 if (this._i >= this._n && x === this._e) {
1334 this._hv = true;
1335 this._v = this._i;
1336 }
1337 this._i++;
1338 };
1339 LastIndexOfObserver.prototype.error = function (e) { this._o.onError(e); };
1340 LastIndexOfObserver.prototype.completed = function () {
1341 if (this._hv) {
1342 this._o.onNext(this._v);
1343 } else {
1344 this._o.onNext(-1);
1345 }
1346 this._o.onCompleted();
1347 };
1348
1349 return LastIndexOfObserver;
1350 }(AbstractObserver));
1351
1352 /**
1353 * Returns the last index at which a given element can be found in the observable sequence, or -1 if it is not present.
1354 * @param {Any} searchElement Element to locate in the array.
1355 * @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
1356 * @returns {Observable} And observable sequence containing the last index at which a given element can be found in the observable sequence, or -1 if it is not present.
1357 */
1358 observableProto.lastIndexOf = function(searchElement, fromIndex) {
1359 var n = +fromIndex || 0;
1360 Math.abs(n) === Infinity && (n = 0);
1361 return new LastIndexOfObservable(this, searchElement, n);
1362 };
1363
1364 return Rx;
1365}));