UNPKG

432 kBJavaScriptView Raw
1// Copyright (c) Microsoft, All rights reserved. See License.txt in the project root for license information.
2
3;(function (undefined) {
4
5 var objectTypes = {
6 'function': true,
7 'object': true
8 };
9
10 function checkGlobal(value) {
11 return (value && value.Object === Object) ? value : null;
12 }
13
14 var freeExports = (objectTypes[typeof exports] && exports && !exports.nodeType) ? exports : null;
15 var freeModule = (objectTypes[typeof module] && module && !module.nodeType) ? module : null;
16 var freeGlobal = checkGlobal(freeExports && freeModule && typeof global === 'object' && global);
17 var freeSelf = checkGlobal(objectTypes[typeof self] && self);
18 var freeWindow = checkGlobal(objectTypes[typeof window] && window);
19 var moduleExports = (freeModule && freeModule.exports === freeExports) ? freeExports : null;
20 var thisGlobal = checkGlobal(objectTypes[typeof this] && this);
21 var root = freeGlobal || ((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) || freeSelf || thisGlobal || Function('return this')();
22
23 var Rx = {
24 internals: {},
25 config: {
26 Promise: root.Promise
27 },
28 helpers: { }
29 };
30
31 // Defaults
32 var noop = Rx.helpers.noop = function () { },
33 identity = Rx.helpers.identity = function (x) { return x; },
34 defaultNow = Rx.helpers.defaultNow = Date.now,
35 defaultComparer = Rx.helpers.defaultComparer = function (x, y) { return isEqual(x, y); },
36 defaultSubComparer = Rx.helpers.defaultSubComparer = function (x, y) { return x > y ? 1 : (x < y ? -1 : 0); },
37 defaultKeySerializer = Rx.helpers.defaultKeySerializer = function (x) { return x.toString(); },
38 defaultError = Rx.helpers.defaultError = function (err) { throw err; },
39 isPromise = Rx.helpers.isPromise = function (p) { return !!p && typeof p.subscribe !== 'function' && typeof p.then === 'function'; },
40 isFunction = Rx.helpers.isFunction = (function () {
41
42 var isFn = function (value) {
43 return typeof value == 'function' || false;
44 };
45
46 // fallback for older versions of Chrome and Safari
47 if (isFn(/x/)) {
48 isFn = function(value) {
49 return typeof value == 'function' && toString.call(value) == '[object Function]';
50 };
51 }
52
53 return isFn;
54 }());
55
56 function cloneArray(arr) { for(var a = [], i = 0, len = arr.length; i < len; i++) { a.push(arr[i]); } return a;}
57
58 var errorObj = {e: {}};
59
60 function tryCatcherGen(tryCatchTarget) {
61 return function tryCatcher() {
62 try {
63 return tryCatchTarget.apply(this, arguments);
64 } catch (e) {
65 errorObj.e = e;
66 return errorObj;
67 }
68 };
69 }
70
71 var tryCatch = Rx.internals.tryCatch = function tryCatch(fn) {
72 if (!isFunction(fn)) { throw new TypeError('fn must be a function'); }
73 return tryCatcherGen(fn);
74 };
75
76 function thrower(e) {
77 throw e;
78 }
79
80 Rx.config.longStackSupport = false;
81 var hasStacks = false, stacks = tryCatch(function () { throw new Error(); })();
82 hasStacks = !!stacks.e && !!stacks.e.stack;
83
84 // All code after this point will be filtered from stack traces reported by RxJS
85 var rStartingLine = captureLine(), rFileName;
86
87 var STACK_JUMP_SEPARATOR = 'From previous event:';
88
89 function makeStackTraceLong(error, observable) {
90 // If possible, transform the error stack trace by removing Node and RxJS
91 // cruft, then concatenating with the stack trace of `observable`.
92 if (hasStacks &&
93 observable.stack &&
94 typeof error === 'object' &&
95 error !== null &&
96 error.stack &&
97 error.stack.indexOf(STACK_JUMP_SEPARATOR) === -1
98 ) {
99 var stacks = [];
100 for (var o = observable; !!o; o = o.source) {
101 if (o.stack) {
102 stacks.unshift(o.stack);
103 }
104 }
105 stacks.unshift(error.stack);
106
107 var concatedStacks = stacks.join('\n' + STACK_JUMP_SEPARATOR + '\n');
108 error.stack = filterStackString(concatedStacks);
109 }
110 }
111
112 function filterStackString(stackString) {
113 var lines = stackString.split('\n'), desiredLines = [];
114 for (var i = 0, len = lines.length; i < len; i++) {
115 var line = lines[i];
116
117 if (!isInternalFrame(line) && !isNodeFrame(line) && line) {
118 desiredLines.push(line);
119 }
120 }
121 return desiredLines.join('\n');
122 }
123
124 function isInternalFrame(stackLine) {
125 var fileNameAndLineNumber = getFileNameAndLineNumber(stackLine);
126 if (!fileNameAndLineNumber) {
127 return false;
128 }
129 var fileName = fileNameAndLineNumber[0], lineNumber = fileNameAndLineNumber[1];
130
131 return fileName === rFileName &&
132 lineNumber >= rStartingLine &&
133 lineNumber <= rEndingLine;
134 }
135
136 function isNodeFrame(stackLine) {
137 return stackLine.indexOf('(module.js:') !== -1 ||
138 stackLine.indexOf('(node.js:') !== -1;
139 }
140
141 function captureLine() {
142 if (!hasStacks) { return; }
143
144 try {
145 throw new Error();
146 } catch (e) {
147 var lines = e.stack.split('\n');
148 var firstLine = lines[0].indexOf('@') > 0 ? lines[1] : lines[2];
149 var fileNameAndLineNumber = getFileNameAndLineNumber(firstLine);
150 if (!fileNameAndLineNumber) { return; }
151
152 rFileName = fileNameAndLineNumber[0];
153 return fileNameAndLineNumber[1];
154 }
155 }
156
157 function getFileNameAndLineNumber(stackLine) {
158 // Named functions: 'at functionName (filename:lineNumber:columnNumber)'
159 var attempt1 = /at .+ \((.+):(\d+):(?:\d+)\)$/.exec(stackLine);
160 if (attempt1) { return [attempt1[1], Number(attempt1[2])]; }
161
162 // Anonymous functions: 'at filename:lineNumber:columnNumber'
163 var attempt2 = /at ([^ ]+):(\d+):(?:\d+)$/.exec(stackLine);
164 if (attempt2) { return [attempt2[1], Number(attempt2[2])]; }
165
166 // Firefox style: 'function@filename:lineNumber or @filename:lineNumber'
167 var attempt3 = /.*@(.+):(\d+)$/.exec(stackLine);
168 if (attempt3) { return [attempt3[1], Number(attempt3[2])]; }
169 }
170
171 var EmptyError = Rx.EmptyError = function() {
172 this.message = 'Sequence contains no elements.';
173 Error.call(this);
174 };
175 EmptyError.prototype = Object.create(Error.prototype);
176 EmptyError.prototype.name = 'EmptyError';
177
178 var ObjectDisposedError = Rx.ObjectDisposedError = function() {
179 this.message = 'Object has been disposed';
180 Error.call(this);
181 };
182 ObjectDisposedError.prototype = Object.create(Error.prototype);
183 ObjectDisposedError.prototype.name = 'ObjectDisposedError';
184
185 var ArgumentOutOfRangeError = Rx.ArgumentOutOfRangeError = function () {
186 this.message = 'Argument out of range';
187 Error.call(this);
188 };
189 ArgumentOutOfRangeError.prototype = Object.create(Error.prototype);
190 ArgumentOutOfRangeError.prototype.name = 'ArgumentOutOfRangeError';
191
192 var NotSupportedError = Rx.NotSupportedError = function (message) {
193 this.message = message || 'This operation is not supported';
194 Error.call(this);
195 };
196 NotSupportedError.prototype = Object.create(Error.prototype);
197 NotSupportedError.prototype.name = 'NotSupportedError';
198
199 var NotImplementedError = Rx.NotImplementedError = function (message) {
200 this.message = message || 'This operation is not implemented';
201 Error.call(this);
202 };
203 NotImplementedError.prototype = Object.create(Error.prototype);
204 NotImplementedError.prototype.name = 'NotImplementedError';
205
206 var notImplemented = Rx.helpers.notImplemented = function () {
207 throw new NotImplementedError();
208 };
209
210 var notSupported = Rx.helpers.notSupported = function () {
211 throw new NotSupportedError();
212 };
213
214 // Shim in iterator support
215 var $iterator$ = (typeof Symbol === 'function' && Symbol.iterator) ||
216 '_es6shim_iterator_';
217 // Bug for mozilla version
218 if (root.Set && typeof new root.Set()['@@iterator'] === 'function') {
219 $iterator$ = '@@iterator';
220 }
221
222 var doneEnumerator = Rx.doneEnumerator = { done: true, value: undefined };
223
224 var isIterable = Rx.helpers.isIterable = function (o) {
225 return o && o[$iterator$] !== undefined;
226 };
227
228 var isArrayLike = Rx.helpers.isArrayLike = function (o) {
229 return o && o.length !== undefined;
230 };
231
232 Rx.helpers.iterator = $iterator$;
233
234 var bindCallback = Rx.internals.bindCallback = function (func, thisArg, argCount) {
235 if (typeof thisArg === 'undefined') { return func; }
236 switch(argCount) {
237 case 0:
238 return function() {
239 return func.call(thisArg)
240 };
241 case 1:
242 return function(arg) {
243 return func.call(thisArg, arg);
244 };
245 case 2:
246 return function(value, index) {
247 return func.call(thisArg, value, index);
248 };
249 case 3:
250 return function(value, index, collection) {
251 return func.call(thisArg, value, index, collection);
252 };
253 }
254
255 return function() {
256 return func.apply(thisArg, arguments);
257 };
258 };
259
260 /** Used to determine if values are of the language type Object */
261 var dontEnums = ['toString',
262 'toLocaleString',
263 'valueOf',
264 'hasOwnProperty',
265 'isPrototypeOf',
266 'propertyIsEnumerable',
267 'constructor'],
268 dontEnumsLength = dontEnums.length;
269
270var argsTag = '[object Arguments]',
271 arrayTag = '[object Array]',
272 boolTag = '[object Boolean]',
273 dateTag = '[object Date]',
274 errorTag = '[object Error]',
275 funcTag = '[object Function]',
276 mapTag = '[object Map]',
277 numberTag = '[object Number]',
278 objectTag = '[object Object]',
279 regexpTag = '[object RegExp]',
280 setTag = '[object Set]',
281 stringTag = '[object String]',
282 weakMapTag = '[object WeakMap]';
283
284var arrayBufferTag = '[object ArrayBuffer]',
285 float32Tag = '[object Float32Array]',
286 float64Tag = '[object Float64Array]',
287 int8Tag = '[object Int8Array]',
288 int16Tag = '[object Int16Array]',
289 int32Tag = '[object Int32Array]',
290 uint8Tag = '[object Uint8Array]',
291 uint8ClampedTag = '[object Uint8ClampedArray]',
292 uint16Tag = '[object Uint16Array]',
293 uint32Tag = '[object Uint32Array]';
294
295var typedArrayTags = {};
296typedArrayTags[float32Tag] = typedArrayTags[float64Tag] =
297typedArrayTags[int8Tag] = typedArrayTags[int16Tag] =
298typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =
299typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =
300typedArrayTags[uint32Tag] = true;
301typedArrayTags[argsTag] = typedArrayTags[arrayTag] =
302typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =
303typedArrayTags[dateTag] = typedArrayTags[errorTag] =
304typedArrayTags[funcTag] = typedArrayTags[mapTag] =
305typedArrayTags[numberTag] = typedArrayTags[objectTag] =
306typedArrayTags[regexpTag] = typedArrayTags[setTag] =
307typedArrayTags[stringTag] = typedArrayTags[weakMapTag] = false;
308
309var objectProto = Object.prototype,
310 hasOwnProperty = objectProto.hasOwnProperty,
311 objToString = objectProto.toString,
312 MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
313
314var keys = Object.keys || (function() {
315 var hasOwnProperty = Object.prototype.hasOwnProperty,
316 hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
317 dontEnums = [
318 'toString',
319 'toLocaleString',
320 'valueOf',
321 'hasOwnProperty',
322 'isPrototypeOf',
323 'propertyIsEnumerable',
324 'constructor'
325 ],
326 dontEnumsLength = dontEnums.length;
327
328 return function(obj) {
329 if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
330 throw new TypeError('Object.keys called on non-object');
331 }
332
333 var result = [], prop, i;
334
335 for (prop in obj) {
336 if (hasOwnProperty.call(obj, prop)) {
337 result.push(prop);
338 }
339 }
340
341 if (hasDontEnumBug) {
342 for (i = 0; i < dontEnumsLength; i++) {
343 if (hasOwnProperty.call(obj, dontEnums[i])) {
344 result.push(dontEnums[i]);
345 }
346 }
347 }
348 return result;
349 };
350 }());
351
352function equalObjects(object, other, equalFunc, isLoose, stackA, stackB) {
353 var objProps = keys(object),
354 objLength = objProps.length,
355 othProps = keys(other),
356 othLength = othProps.length;
357
358 if (objLength !== othLength && !isLoose) {
359 return false;
360 }
361 var index = objLength, key;
362 while (index--) {
363 key = objProps[index];
364 if (!(isLoose ? key in other : hasOwnProperty.call(other, key))) {
365 return false;
366 }
367 }
368 var skipCtor = isLoose;
369 while (++index < objLength) {
370 key = objProps[index];
371 var objValue = object[key],
372 othValue = other[key],
373 result;
374
375 if (!(result === undefined ? equalFunc(objValue, othValue, isLoose, stackA, stackB) : result)) {
376 return false;
377 }
378 skipCtor || (skipCtor = key === 'constructor');
379 }
380 if (!skipCtor) {
381 var objCtor = object.constructor,
382 othCtor = other.constructor;
383
384 if (objCtor !== othCtor &&
385 ('constructor' in object && 'constructor' in other) &&
386 !(typeof objCtor === 'function' && objCtor instanceof objCtor &&
387 typeof othCtor === 'function' && othCtor instanceof othCtor)) {
388 return false;
389 }
390 }
391 return true;
392}
393
394function equalByTag(object, other, tag) {
395 switch (tag) {
396 case boolTag:
397 case dateTag:
398 return +object === +other;
399
400 case errorTag:
401 return object.name === other.name && object.message === other.message;
402
403 case numberTag:
404 return (object !== +object) ?
405 other !== +other :
406 object === +other;
407
408 case regexpTag:
409 case stringTag:
410 return object === (other + '');
411 }
412 return false;
413}
414
415var isObject = Rx.internals.isObject = function(value) {
416 var type = typeof value;
417 return !!value && (type === 'object' || type === 'function');
418};
419
420function isObjectLike(value) {
421 return !!value && typeof value === 'object';
422}
423
424function isLength(value) {
425 return typeof value === 'number' && value > -1 && value % 1 === 0 && value <= MAX_SAFE_INTEGER;
426}
427
428var isHostObject = (function() {
429 try {
430 Object({ 'toString': 0 } + '');
431 } catch(e) {
432 return function() { return false; };
433 }
434 return function(value) {
435 return typeof value.toString !== 'function' && typeof (value + '') === 'string';
436 };
437}());
438
439function isTypedArray(value) {
440 return isObjectLike(value) && isLength(value.length) && !!typedArrayTags[objToString.call(value)];
441}
442
443var isArray = Array.isArray || function(value) {
444 return isObjectLike(value) && isLength(value.length) && objToString.call(value) === arrayTag;
445};
446
447function arraySome (array, predicate) {
448 var index = -1,
449 length = array.length;
450
451 while (++index < length) {
452 if (predicate(array[index], index, array)) {
453 return true;
454 }
455 }
456 return false;
457}
458
459function equalArrays(array, other, equalFunc, isLoose, stackA, stackB) {
460 var index = -1,
461 arrLength = array.length,
462 othLength = other.length;
463
464 if (arrLength !== othLength && !(isLoose && othLength > arrLength)) {
465 return false;
466 }
467 // Ignore non-index properties.
468 while (++index < arrLength) {
469 var arrValue = array[index],
470 othValue = other[index],
471 result;
472
473 if (result !== undefined) {
474 if (result) {
475 continue;
476 }
477 return false;
478 }
479 // Recursively compare arrays (susceptible to call stack limits).
480 if (isLoose) {
481 if (!arraySome(other, function(othValue) {
482 return arrValue === othValue || equalFunc(arrValue, othValue, isLoose, stackA, stackB);
483 })) {
484 return false;
485 }
486 } else if (!(arrValue === othValue || equalFunc(arrValue, othValue, isLoose, stackA, stackB))) {
487 return false;
488 }
489 }
490 return true;
491}
492
493function baseIsEqualDeep(object, other, equalFunc, isLoose, stackA, stackB) {
494 var objIsArr = isArray(object),
495 othIsArr = isArray(other),
496 objTag = arrayTag,
497 othTag = arrayTag;
498
499 if (!objIsArr) {
500 objTag = objToString.call(object);
501 if (objTag === argsTag) {
502 objTag = objectTag;
503 } else if (objTag !== objectTag) {
504 objIsArr = isTypedArray(object);
505 }
506 }
507 if (!othIsArr) {
508 othTag = objToString.call(other);
509 if (othTag === argsTag) {
510 othTag = objectTag;
511 }
512 }
513 var objIsObj = objTag === objectTag && !isHostObject(object),
514 othIsObj = othTag === objectTag && !isHostObject(other),
515 isSameTag = objTag === othTag;
516
517 if (isSameTag && !(objIsArr || objIsObj)) {
518 return equalByTag(object, other, objTag);
519 }
520 if (!isLoose) {
521 var objIsWrapped = objIsObj && hasOwnProperty.call(object, '__wrapped__'),
522 othIsWrapped = othIsObj && hasOwnProperty.call(other, '__wrapped__');
523
524 if (objIsWrapped || othIsWrapped) {
525 return equalFunc(objIsWrapped ? object.value() : object, othIsWrapped ? other.value() : other, isLoose, stackA, stackB);
526 }
527 }
528 if (!isSameTag) {
529 return false;
530 }
531 // Assume cyclic values are equal.
532 // For more information on detecting circular references see https://es5.github.io/#JO.
533 stackA || (stackA = []);
534 stackB || (stackB = []);
535
536 var length = stackA.length;
537 while (length--) {
538 if (stackA[length] === object) {
539 return stackB[length] === other;
540 }
541 }
542 // Add `object` and `other` to the stack of traversed objects.
543 stackA.push(object);
544 stackB.push(other);
545
546 var result = (objIsArr ? equalArrays : equalObjects)(object, other, equalFunc, isLoose, stackA, stackB);
547
548 stackA.pop();
549 stackB.pop();
550
551 return result;
552}
553
554function baseIsEqual(value, other, isLoose, stackA, stackB) {
555 if (value === other) {
556 return true;
557 }
558 if (value == null || other == null || (!isObject(value) && !isObjectLike(other))) {
559 return value !== value && other !== other;
560 }
561 return baseIsEqualDeep(value, other, baseIsEqual, isLoose, stackA, stackB);
562}
563
564var isEqual = Rx.internals.isEqual = function (value, other) {
565 return baseIsEqual(value, other);
566};
567
568 var hasProp = {}.hasOwnProperty,
569 slice = Array.prototype.slice;
570
571 var inherits = Rx.internals.inherits = function (child, parent) {
572 function __() { this.constructor = child; }
573 __.prototype = parent.prototype;
574 child.prototype = new __();
575 };
576
577 var addProperties = Rx.internals.addProperties = function (obj) {
578 for(var sources = [], i = 1, len = arguments.length; i < len; i++) { sources.push(arguments[i]); }
579 for (var idx = 0, ln = sources.length; idx < ln; idx++) {
580 var source = sources[idx];
581 for (var prop in source) {
582 obj[prop] = source[prop];
583 }
584 }
585 };
586
587 // Rx Utils
588 var addRef = Rx.internals.addRef = function (xs, r) {
589 return new AnonymousObservable(function (observer) {
590 return new BinaryDisposable(r.getDisposable(), xs.subscribe(observer));
591 });
592 };
593
594 function arrayInitialize(count, factory) {
595 var a = new Array(count);
596 for (var i = 0; i < count; i++) {
597 a[i] = factory();
598 }
599 return a;
600 }
601
602 function IndexedItem(id, value) {
603 this.id = id;
604 this.value = value;
605 }
606
607 IndexedItem.prototype.compareTo = function (other) {
608 var c = this.value.compareTo(other.value);
609 c === 0 && (c = this.id - other.id);
610 return c;
611 };
612
613 var PriorityQueue = Rx.internals.PriorityQueue = function (capacity) {
614 this.items = new Array(capacity);
615 this.length = 0;
616 };
617
618 var priorityProto = PriorityQueue.prototype;
619 priorityProto.isHigherPriority = function (left, right) {
620 return this.items[left].compareTo(this.items[right]) < 0;
621 };
622
623 priorityProto.percolate = function (index) {
624 if (index >= this.length || index < 0) { return; }
625 var parent = index - 1 >> 1;
626 if (parent < 0 || parent === index) { return; }
627 if (this.isHigherPriority(index, parent)) {
628 var temp = this.items[index];
629 this.items[index] = this.items[parent];
630 this.items[parent] = temp;
631 this.percolate(parent);
632 }
633 };
634
635 priorityProto.heapify = function (index) {
636 +index || (index = 0);
637 if (index >= this.length || index < 0) { return; }
638 var left = 2 * index + 1,
639 right = 2 * index + 2,
640 first = index;
641 if (left < this.length && this.isHigherPriority(left, first)) {
642 first = left;
643 }
644 if (right < this.length && this.isHigherPriority(right, first)) {
645 first = right;
646 }
647 if (first !== index) {
648 var temp = this.items[index];
649 this.items[index] = this.items[first];
650 this.items[first] = temp;
651 this.heapify(first);
652 }
653 };
654
655 priorityProto.peek = function () { return this.items[0].value; };
656
657 priorityProto.removeAt = function (index) {
658 this.items[index] = this.items[--this.length];
659 this.items[this.length] = undefined;
660 this.heapify();
661 };
662
663 priorityProto.dequeue = function () {
664 var result = this.peek();
665 this.removeAt(0);
666 return result;
667 };
668
669 priorityProto.enqueue = function (item) {
670 var index = this.length++;
671 this.items[index] = new IndexedItem(PriorityQueue.count++, item);
672 this.percolate(index);
673 };
674
675 priorityProto.remove = function (item) {
676 for (var i = 0; i < this.length; i++) {
677 if (this.items[i].value === item) {
678 this.removeAt(i);
679 return true;
680 }
681 }
682 return false;
683 };
684 PriorityQueue.count = 0;
685
686 /**
687 * Represents a group of disposable resources that are disposed together.
688 * @constructor
689 */
690 var CompositeDisposable = Rx.CompositeDisposable = function () {
691 var args = [], i, len;
692 if (Array.isArray(arguments[0])) {
693 args = arguments[0];
694 } else {
695 len = arguments.length;
696 args = new Array(len);
697 for(i = 0; i < len; i++) { args[i] = arguments[i]; }
698 }
699 this.disposables = args;
700 this.isDisposed = false;
701 this.length = args.length;
702 };
703
704 var CompositeDisposablePrototype = CompositeDisposable.prototype;
705
706 /**
707 * Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.
708 * @param {Mixed} item Disposable to add.
709 */
710 CompositeDisposablePrototype.add = function (item) {
711 if (this.isDisposed) {
712 item.dispose();
713 } else {
714 this.disposables.push(item);
715 this.length++;
716 }
717 };
718
719 /**
720 * Removes and disposes the first occurrence of a disposable from the CompositeDisposable.
721 * @param {Mixed} item Disposable to remove.
722 * @returns {Boolean} true if found; false otherwise.
723 */
724 CompositeDisposablePrototype.remove = function (item) {
725 var shouldDispose = false;
726 if (!this.isDisposed) {
727 var idx = this.disposables.indexOf(item);
728 if (idx !== -1) {
729 shouldDispose = true;
730 this.disposables.splice(idx, 1);
731 this.length--;
732 item.dispose();
733 }
734 }
735 return shouldDispose;
736 };
737
738 /**
739 * Disposes all disposables in the group and removes them from the group.
740 */
741 CompositeDisposablePrototype.dispose = function () {
742 if (!this.isDisposed) {
743 this.isDisposed = true;
744 var len = this.disposables.length, currentDisposables = new Array(len);
745 for(var i = 0; i < len; i++) { currentDisposables[i] = this.disposables[i]; }
746 this.disposables = [];
747 this.length = 0;
748
749 for (i = 0; i < len; i++) {
750 currentDisposables[i].dispose();
751 }
752 }
753 };
754
755 /**
756 * Provides a set of static methods for creating Disposables.
757 * @param {Function} dispose Action to run during the first call to dispose. The action is guaranteed to be run at most once.
758 */
759 var Disposable = Rx.Disposable = function (action) {
760 this.isDisposed = false;
761 this.action = action || noop;
762 };
763
764 /** Performs the task of cleaning up resources. */
765 Disposable.prototype.dispose = function () {
766 if (!this.isDisposed) {
767 this.action();
768 this.isDisposed = true;
769 }
770 };
771
772 /**
773 * Creates a disposable object that invokes the specified action when disposed.
774 * @param {Function} dispose Action to run during the first call to dispose. The action is guaranteed to be run at most once.
775 * @return {Disposable} The disposable object that runs the given action upon disposal.
776 */
777 var disposableCreate = Disposable.create = function (action) { return new Disposable(action); };
778
779 /**
780 * Gets the disposable that does nothing when disposed.
781 */
782 var disposableEmpty = Disposable.empty = { dispose: noop };
783
784 /**
785 * Validates whether the given object is a disposable
786 * @param {Object} Object to test whether it has a dispose method
787 * @returns {Boolean} true if a disposable object, else false.
788 */
789 var isDisposable = Disposable.isDisposable = function (d) {
790 return d && isFunction(d.dispose);
791 };
792
793 var checkDisposed = Disposable.checkDisposed = function (disposable) {
794 if (disposable.isDisposed) { throw new ObjectDisposedError(); }
795 };
796
797 var disposableFixup = Disposable._fixup = function (result) {
798 return isDisposable(result) ? result : disposableEmpty;
799 };
800
801 // Single assignment
802 var SingleAssignmentDisposable = Rx.SingleAssignmentDisposable = function () {
803 this.isDisposed = false;
804 this.current = null;
805 };
806 SingleAssignmentDisposable.prototype.getDisposable = function () {
807 return this.current;
808 };
809 SingleAssignmentDisposable.prototype.setDisposable = function (value) {
810 if (this.current) { throw new Error('Disposable has already been assigned'); }
811 var shouldDispose = this.isDisposed;
812 !shouldDispose && (this.current = value);
813 shouldDispose && value && value.dispose();
814 };
815 SingleAssignmentDisposable.prototype.dispose = function () {
816 if (!this.isDisposed) {
817 this.isDisposed = true;
818 var old = this.current;
819 this.current = null;
820 old && old.dispose();
821 }
822 };
823
824 // Multiple assignment disposable
825 var SerialDisposable = Rx.SerialDisposable = function () {
826 this.isDisposed = false;
827 this.current = null;
828 };
829 SerialDisposable.prototype.getDisposable = function () {
830 return this.current;
831 };
832 SerialDisposable.prototype.setDisposable = function (value) {
833 var shouldDispose = this.isDisposed;
834 if (!shouldDispose) {
835 var old = this.current;
836 this.current = value;
837 }
838 old && old.dispose();
839 shouldDispose && value && value.dispose();
840 };
841 SerialDisposable.prototype.dispose = function () {
842 if (!this.isDisposed) {
843 this.isDisposed = true;
844 var old = this.current;
845 this.current = null;
846 }
847 old && old.dispose();
848 };
849
850 var BinaryDisposable = Rx.BinaryDisposable = function (first, second) {
851 this._first = first;
852 this._second = second;
853 this.isDisposed = false;
854 };
855
856 BinaryDisposable.prototype.dispose = function () {
857 if (!this.isDisposed) {
858 this.isDisposed = true;
859 var old1 = this._first;
860 this._first = null;
861 old1 && old1.dispose();
862 var old2 = this._second;
863 this._second = null;
864 old2 && old2.dispose();
865 }
866 };
867
868 var NAryDisposable = Rx.NAryDisposable = function (disposables) {
869 this._disposables = disposables;
870 this.isDisposed = false;
871 };
872
873 NAryDisposable.prototype.dispose = function () {
874 if (!this.isDisposed) {
875 this.isDisposed = true;
876 for (var i = 0, len = this._disposables.length; i < len; i++) {
877 this._disposables[i].dispose();
878 }
879 this._disposables.length = 0;
880 }
881 };
882
883 /**
884 * Represents a disposable resource that only disposes its underlying disposable resource when all dependent disposable objects have been disposed.
885 */
886 var RefCountDisposable = Rx.RefCountDisposable = (function () {
887
888 function InnerDisposable(disposable) {
889 this.disposable = disposable;
890 this.disposable.count++;
891 this.isInnerDisposed = false;
892 }
893
894 InnerDisposable.prototype.dispose = function () {
895 if (!this.disposable.isDisposed && !this.isInnerDisposed) {
896 this.isInnerDisposed = true;
897 this.disposable.count--;
898 if (this.disposable.count === 0 && this.disposable.isPrimaryDisposed) {
899 this.disposable.isDisposed = true;
900 this.disposable.underlyingDisposable.dispose();
901 }
902 }
903 };
904
905 /**
906 * Initializes a new instance of the RefCountDisposable with the specified disposable.
907 * @constructor
908 * @param {Disposable} disposable Underlying disposable.
909 */
910 function RefCountDisposable(disposable) {
911 this.underlyingDisposable = disposable;
912 this.isDisposed = false;
913 this.isPrimaryDisposed = false;
914 this.count = 0;
915 }
916
917 /**
918 * Disposes the underlying disposable only when all dependent disposables have been disposed
919 */
920 RefCountDisposable.prototype.dispose = function () {
921 if (!this.isDisposed && !this.isPrimaryDisposed) {
922 this.isPrimaryDisposed = true;
923 if (this.count === 0) {
924 this.isDisposed = true;
925 this.underlyingDisposable.dispose();
926 }
927 }
928 };
929
930 /**
931 * Returns a dependent disposable that when disposed decreases the refcount on the underlying disposable.
932 * @returns {Disposable} A dependent disposable contributing to the reference count that manages the underlying disposable's lifetime.
933 */
934 RefCountDisposable.prototype.getDisposable = function () {
935 return this.isDisposed ? disposableEmpty : new InnerDisposable(this);
936 };
937
938 return RefCountDisposable;
939 })();
940
941 function ScheduledDisposable(scheduler, disposable) {
942 this.scheduler = scheduler;
943 this.disposable = disposable;
944 this.isDisposed = false;
945 }
946
947 function scheduleItem(s, self) {
948 if (!self.isDisposed) {
949 self.isDisposed = true;
950 self.disposable.dispose();
951 }
952 }
953
954 ScheduledDisposable.prototype.dispose = function () {
955 this.scheduler.schedule(this, scheduleItem);
956 };
957
958 var ScheduledItem = Rx.internals.ScheduledItem = function (scheduler, state, action, dueTime, comparer) {
959 this.scheduler = scheduler;
960 this.state = state;
961 this.action = action;
962 this.dueTime = dueTime;
963 this.comparer = comparer || defaultSubComparer;
964 this.disposable = new SingleAssignmentDisposable();
965 };
966
967 ScheduledItem.prototype.invoke = function () {
968 this.disposable.setDisposable(this.invokeCore());
969 };
970
971 ScheduledItem.prototype.compareTo = function (other) {
972 return this.comparer(this.dueTime, other.dueTime);
973 };
974
975 ScheduledItem.prototype.isCancelled = function () {
976 return this.disposable.isDisposed;
977 };
978
979 ScheduledItem.prototype.invokeCore = function () {
980 return disposableFixup(this.action(this.scheduler, this.state));
981 };
982
983 /** Provides a set of static properties to access commonly used schedulers. */
984 var Scheduler = Rx.Scheduler = (function () {
985
986 function Scheduler() { }
987
988 /** Determines whether the given object is a scheduler */
989 Scheduler.isScheduler = function (s) {
990 return s instanceof Scheduler;
991 };
992
993 var schedulerProto = Scheduler.prototype;
994
995 /**
996 * Schedules an action to be executed.
997 * @param state State passed to the action to be executed.
998 * @param {Function} action Action to be executed.
999 * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
1000 */
1001 schedulerProto.schedule = function (state, action) {
1002 throw new NotImplementedError();
1003 };
1004
1005 /**
1006 * Schedules an action to be executed after dueTime.
1007 * @param state State passed to the action to be executed.
1008 * @param {Function} action Action to be executed.
1009 * @param {Number} dueTime Relative time after which to execute the action.
1010 * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
1011 */
1012 schedulerProto.scheduleFuture = function (state, dueTime, action) {
1013 var dt = dueTime;
1014 dt instanceof Date && (dt = dt - this.now());
1015 dt = Scheduler.normalize(dt);
1016
1017 if (dt === 0) { return this.schedule(state, action); }
1018
1019 return this._scheduleFuture(state, dt, action);
1020 };
1021
1022 schedulerProto._scheduleFuture = function (state, dueTime, action) {
1023 throw new NotImplementedError();
1024 };
1025
1026 /** Gets the current time according to the local machine's system clock. */
1027 Scheduler.now = defaultNow;
1028
1029 /** Gets the current time according to the local machine's system clock. */
1030 Scheduler.prototype.now = defaultNow;
1031
1032 /**
1033 * Normalizes the specified TimeSpan value to a positive value.
1034 * @param {Number} timeSpan The time span value to normalize.
1035 * @returns {Number} The specified TimeSpan value if it is zero or positive; otherwise, 0
1036 */
1037 Scheduler.normalize = function (timeSpan) {
1038 timeSpan < 0 && (timeSpan = 0);
1039 return timeSpan;
1040 };
1041
1042 return Scheduler;
1043 }());
1044
1045 var normalizeTime = Scheduler.normalize, isScheduler = Scheduler.isScheduler;
1046
1047 (function (schedulerProto) {
1048
1049 function invokeRecImmediate(scheduler, pair) {
1050 var state = pair[0], action = pair[1], group = new CompositeDisposable();
1051 action(state, innerAction);
1052 return group;
1053
1054 function innerAction(state2) {
1055 var isAdded = false, isDone = false;
1056
1057 var d = scheduler.schedule(state2, scheduleWork);
1058 if (!isDone) {
1059 group.add(d);
1060 isAdded = true;
1061 }
1062
1063 function scheduleWork(_, state3) {
1064 if (isAdded) {
1065 group.remove(d);
1066 } else {
1067 isDone = true;
1068 }
1069 action(state3, innerAction);
1070 return disposableEmpty;
1071 }
1072 }
1073 }
1074
1075 function invokeRecDate(scheduler, pair) {
1076 var state = pair[0], action = pair[1], group = new CompositeDisposable();
1077 action(state, innerAction);
1078 return group;
1079
1080 function innerAction(state2, dueTime1) {
1081 var isAdded = false, isDone = false;
1082
1083 var d = scheduler.scheduleFuture(state2, dueTime1, scheduleWork);
1084 if (!isDone) {
1085 group.add(d);
1086 isAdded = true;
1087 }
1088
1089 function scheduleWork(_, state3) {
1090 if (isAdded) {
1091 group.remove(d);
1092 } else {
1093 isDone = true;
1094 }
1095 action(state3, innerAction);
1096 return disposableEmpty;
1097 }
1098 }
1099 }
1100
1101 /**
1102 * Schedules an action to be executed recursively.
1103 * @param {Mixed} state State passed to the action to be executed.
1104 * @param {Function} action Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state.
1105 * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
1106 */
1107 schedulerProto.scheduleRecursive = function (state, action) {
1108 return this.schedule([state, action], invokeRecImmediate);
1109 };
1110
1111 /**
1112 * Schedules an action to be executed recursively after a specified relative or absolute due time.
1113 * @param {Mixed} state State passed to the action to be executed.
1114 * @param {Function} action Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state.
1115 * @param {Number | Date} dueTime Relative or absolute time after which to execute the action for the first time.
1116 * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
1117 */
1118 schedulerProto.scheduleRecursiveFuture = function (state, dueTime, action) {
1119 return this.scheduleFuture([state, action], dueTime, invokeRecDate);
1120 };
1121
1122 }(Scheduler.prototype));
1123
1124 (function (schedulerProto) {
1125
1126 /**
1127 * Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. The periodic task will be scheduled using window.setInterval for the base implementation.
1128 * @param {Mixed} state Initial state passed to the action upon the first iteration.
1129 * @param {Number} period Period for running the work periodically.
1130 * @param {Function} action Action to be executed, potentially updating the state.
1131 * @returns {Disposable} The disposable object used to cancel the scheduled recurring action (best effort).
1132 */
1133 schedulerProto.schedulePeriodic = function(state, period, action) {
1134 if (typeof root.setInterval === 'undefined') { throw new NotSupportedError(); }
1135 period = normalizeTime(period);
1136 var s = state, id = root.setInterval(function () { s = action(s); }, period);
1137 return disposableCreate(function () { root.clearInterval(id); });
1138 };
1139
1140 }(Scheduler.prototype));
1141
1142 (function (schedulerProto) {
1143 /**
1144 * Returns a scheduler that wraps the original scheduler, adding exception handling for scheduled actions.
1145 * @param {Function} handler Handler that's run if an exception is caught. The exception will be rethrown if the handler returns false.
1146 * @returns {Scheduler} Wrapper around the original scheduler, enforcing exception handling.
1147 */
1148 schedulerProto.catchError = schedulerProto['catch'] = function (handler) {
1149 return new CatchScheduler(this, handler);
1150 };
1151 }(Scheduler.prototype));
1152
1153 var SchedulePeriodicRecursive = Rx.internals.SchedulePeriodicRecursive = (function () {
1154 function createTick(self) {
1155 return function tick(command, recurse) {
1156 recurse(0, self._period);
1157 var state = tryCatch(self._action)(self._state);
1158 if (state === errorObj) {
1159 self._cancel.dispose();
1160 thrower(state.e);
1161 }
1162 self._state = state;
1163 };
1164 }
1165
1166 function SchedulePeriodicRecursive(scheduler, state, period, action) {
1167 this._scheduler = scheduler;
1168 this._state = state;
1169 this._period = period;
1170 this._action = action;
1171 }
1172
1173 SchedulePeriodicRecursive.prototype.start = function () {
1174 var d = new SingleAssignmentDisposable();
1175 this._cancel = d;
1176 d.setDisposable(this._scheduler.scheduleRecursiveFuture(0, this._period, createTick(this)));
1177
1178 return d;
1179 };
1180
1181 return SchedulePeriodicRecursive;
1182 }());
1183
1184 /** Gets a scheduler that schedules work immediately on the current thread. */
1185 var ImmediateScheduler = (function (__super__) {
1186 inherits(ImmediateScheduler, __super__);
1187 function ImmediateScheduler() {
1188 __super__.call(this);
1189 }
1190
1191 ImmediateScheduler.prototype.schedule = function (state, action) {
1192 return disposableFixup(action(this, state));
1193 };
1194
1195 return ImmediateScheduler;
1196 }(Scheduler));
1197
1198 var immediateScheduler = Scheduler.immediate = new ImmediateScheduler();
1199
1200 /**
1201 * Gets a scheduler that schedules work as soon as possible on the current thread.
1202 */
1203 var CurrentThreadScheduler = (function (__super__) {
1204 var queue;
1205
1206 function runTrampoline () {
1207 while (queue.length > 0) {
1208 var item = queue.dequeue();
1209 !item.isCancelled() && item.invoke();
1210 }
1211 }
1212
1213 inherits(CurrentThreadScheduler, __super__);
1214 function CurrentThreadScheduler() {
1215 __super__.call(this);
1216 }
1217
1218 CurrentThreadScheduler.prototype.schedule = function (state, action) {
1219 var si = new ScheduledItem(this, state, action, this.now());
1220
1221 if (!queue) {
1222 queue = new PriorityQueue(4);
1223 queue.enqueue(si);
1224
1225 var result = tryCatch(runTrampoline)();
1226 queue = null;
1227 if (result === errorObj) { thrower(result.e); }
1228 } else {
1229 queue.enqueue(si);
1230 }
1231 return si.disposable;
1232 };
1233
1234 CurrentThreadScheduler.prototype.scheduleRequired = function () { return !queue; };
1235
1236 return CurrentThreadScheduler;
1237 }(Scheduler));
1238
1239 var currentThreadScheduler = Scheduler.currentThread = new CurrentThreadScheduler();
1240
1241 var scheduleMethod, clearMethod;
1242
1243 var localTimer = (function () {
1244 var localSetTimeout, localClearTimeout = noop;
1245 if (!!root.setTimeout) {
1246 localSetTimeout = root.setTimeout;
1247 localClearTimeout = root.clearTimeout;
1248 } else if (!!root.WScript) {
1249 localSetTimeout = function (fn, time) {
1250 root.WScript.Sleep(time);
1251 fn();
1252 };
1253 } else {
1254 throw new NotSupportedError();
1255 }
1256
1257 return {
1258 setTimeout: localSetTimeout,
1259 clearTimeout: localClearTimeout
1260 };
1261 }());
1262 var localSetTimeout = localTimer.setTimeout,
1263 localClearTimeout = localTimer.clearTimeout;
1264
1265 (function () {
1266
1267 var nextHandle = 1, tasksByHandle = {}, currentlyRunning = false;
1268
1269 clearMethod = function (handle) {
1270 delete tasksByHandle[handle];
1271 };
1272
1273 function runTask(handle) {
1274 if (currentlyRunning) {
1275 localSetTimeout(function () { runTask(handle); }, 0);
1276 } else {
1277 var task = tasksByHandle[handle];
1278 if (task) {
1279 currentlyRunning = true;
1280 var result = tryCatch(task)();
1281 clearMethod(handle);
1282 currentlyRunning = false;
1283 if (result === errorObj) { thrower(result.e); }
1284 }
1285 }
1286 }
1287
1288 var reNative = new RegExp('^' +
1289 String(toString)
1290 .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
1291 .replace(/toString| for [^\]]+/g, '.*?') + '$'
1292 );
1293
1294 var setImmediate = typeof (setImmediate = freeGlobal && moduleExports && freeGlobal.setImmediate) == 'function' &&
1295 !reNative.test(setImmediate) && setImmediate;
1296
1297 function postMessageSupported () {
1298 // Ensure not in a worker
1299 if (!root.postMessage || root.importScripts) { return false; }
1300 var isAsync = false, oldHandler = root.onmessage;
1301 // Test for async
1302 root.onmessage = function () { isAsync = true; };
1303 root.postMessage('', '*');
1304 root.onmessage = oldHandler;
1305
1306 return isAsync;
1307 }
1308
1309 // Use in order, setImmediate, nextTick, postMessage, MessageChannel, script readystatechanged, setTimeout
1310 if (isFunction(setImmediate)) {
1311 scheduleMethod = function (action) {
1312 var id = nextHandle++;
1313 tasksByHandle[id] = action;
1314 setImmediate(function () { runTask(id); });
1315
1316 return id;
1317 };
1318 } else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
1319 scheduleMethod = function (action) {
1320 var id = nextHandle++;
1321 tasksByHandle[id] = action;
1322 process.nextTick(function () { runTask(id); });
1323
1324 return id;
1325 };
1326 } else if (postMessageSupported()) {
1327 var MSG_PREFIX = 'ms.rx.schedule' + Math.random();
1328
1329 var onGlobalPostMessage = function (event) {
1330 // Only if we're a match to avoid any other global events
1331 if (typeof event.data === 'string' && event.data.substring(0, MSG_PREFIX.length) === MSG_PREFIX) {
1332 runTask(event.data.substring(MSG_PREFIX.length));
1333 }
1334 };
1335
1336 root.addEventListener('message', onGlobalPostMessage, false);
1337
1338 scheduleMethod = function (action) {
1339 var id = nextHandle++;
1340 tasksByHandle[id] = action;
1341 root.postMessage(MSG_PREFIX + currentId, '*');
1342 return id;
1343 };
1344 } else if (!!root.MessageChannel) {
1345 var channel = new root.MessageChannel();
1346
1347 channel.port1.onmessage = function (e) { runTask(e.data); };
1348
1349 scheduleMethod = function (action) {
1350 var id = nextHandle++;
1351 tasksByHandle[id] = action;
1352 channel.port2.postMessage(id);
1353 return id;
1354 };
1355 } else if ('document' in root && 'onreadystatechange' in root.document.createElement('script')) {
1356
1357 scheduleMethod = function (action) {
1358 var scriptElement = root.document.createElement('script');
1359 var id = nextHandle++;
1360 tasksByHandle[id] = action;
1361
1362 scriptElement.onreadystatechange = function () {
1363 runTask(id);
1364 scriptElement.onreadystatechange = null;
1365 scriptElement.parentNode.removeChild(scriptElement);
1366 scriptElement = null;
1367 };
1368 root.document.documentElement.appendChild(scriptElement);
1369 return id;
1370 };
1371
1372 } else {
1373 scheduleMethod = function (action) {
1374 var id = nextHandle++;
1375 tasksByHandle[id] = action;
1376 localSetTimeout(function () {
1377 runTask(id);
1378 }, 0);
1379
1380 return id;
1381 };
1382 }
1383 }());
1384
1385 /**
1386 * Gets a scheduler that schedules work via a timed callback based upon platform.
1387 */
1388 var DefaultScheduler = (function (__super__) {
1389 inherits(DefaultScheduler, __super__);
1390 function DefaultScheduler() {
1391 __super__.call(this);
1392 }
1393
1394 function scheduleAction(disposable, action, scheduler, state) {
1395 return function schedule() {
1396 disposable.setDisposable(Disposable._fixup(action(scheduler, state)));
1397 };
1398 }
1399
1400 function ClearDisposable(id) {
1401 this._id = id;
1402 this.isDisposed = false;
1403 }
1404
1405 ClearDisposable.prototype.dispose = function () {
1406 if (!this.isDisposed) {
1407 this.isDisposed = true;
1408 clearMethod(this._id);
1409 }
1410 };
1411
1412 function LocalClearDisposable(id) {
1413 this._id = id;
1414 this.isDisposed = false;
1415 }
1416
1417 LocalClearDisposable.prototype.dispose = function () {
1418 if (!this.isDisposed) {
1419 this.isDisposed = true;
1420 localClearTimeout(this._id);
1421 }
1422 };
1423
1424 DefaultScheduler.prototype.schedule = function (state, action) {
1425 var disposable = new SingleAssignmentDisposable(),
1426 id = scheduleMethod(scheduleAction(disposable, action, this, state));
1427 return new BinaryDisposable(disposable, new ClearDisposable(id));
1428 };
1429
1430 DefaultScheduler.prototype._scheduleFuture = function (state, dueTime, action) {
1431 if (dueTime === 0) { return this.schedule(state, action); }
1432 var disposable = new SingleAssignmentDisposable(),
1433 id = localSetTimeout(scheduleAction(disposable, action, this, state), dueTime);
1434 return new BinaryDisposable(disposable, new LocalClearDisposable(id));
1435 };
1436
1437 return DefaultScheduler;
1438 }(Scheduler));
1439
1440 var defaultScheduler = Scheduler['default'] = Scheduler.async = new DefaultScheduler();
1441
1442 var CatchScheduler = (function (__super__) {
1443 inherits(CatchScheduler, __super__);
1444
1445 function CatchScheduler(scheduler, handler) {
1446 this._scheduler = scheduler;
1447 this._handler = handler;
1448 this._recursiveOriginal = null;
1449 this._recursiveWrapper = null;
1450 __super__.call(this);
1451 }
1452
1453 CatchScheduler.prototype.schedule = function (state, action) {
1454 return this._scheduler.schedule(state, this._wrap(action));
1455 };
1456
1457 CatchScheduler.prototype._scheduleFuture = function (state, dueTime, action) {
1458 return this._scheduler.schedule(state, dueTime, this._wrap(action));
1459 };
1460
1461 CatchScheduler.prototype.now = function () { return this._scheduler.now(); };
1462
1463 CatchScheduler.prototype._clone = function (scheduler) {
1464 return new CatchScheduler(scheduler, this._handler);
1465 };
1466
1467 CatchScheduler.prototype._wrap = function (action) {
1468 var parent = this;
1469 return function (self, state) {
1470 var res = tryCatch(action)(parent._getRecursiveWrapper(self), state);
1471 if (res === errorObj) {
1472 if (!parent._handler(res.e)) { thrower(res.e); }
1473 return disposableEmpty;
1474 }
1475 return disposableFixup(res);
1476 };
1477 };
1478
1479 CatchScheduler.prototype._getRecursiveWrapper = function (scheduler) {
1480 if (this._recursiveOriginal !== scheduler) {
1481 this._recursiveOriginal = scheduler;
1482 var wrapper = this._clone(scheduler);
1483 wrapper._recursiveOriginal = scheduler;
1484 wrapper._recursiveWrapper = wrapper;
1485 this._recursiveWrapper = wrapper;
1486 }
1487 return this._recursiveWrapper;
1488 };
1489
1490 CatchScheduler.prototype.schedulePeriodic = function (state, period, action) {
1491 var self = this, failed = false, d = new SingleAssignmentDisposable();
1492
1493 d.setDisposable(this._scheduler.schedulePeriodic(state, period, function (state1) {
1494 if (failed) { return null; }
1495 var res = tryCatch(action)(state1);
1496 if (res === errorObj) {
1497 failed = true;
1498 if (!self._handler(res.e)) { thrower(res.e); }
1499 d.dispose();
1500 return null;
1501 }
1502 return res;
1503 }));
1504
1505 return d;
1506 };
1507
1508 return CatchScheduler;
1509 }(Scheduler));
1510
1511 /**
1512 * Represents a notification to an observer.
1513 */
1514 var Notification = Rx.Notification = (function () {
1515 function Notification() {
1516
1517 }
1518
1519 Notification.prototype._accept = function (onNext, onError, onCompleted) {
1520 throw new NotImplementedError();
1521 };
1522
1523 Notification.prototype._acceptObserver = function (onNext, onError, onCompleted) {
1524 throw new NotImplementedError();
1525 };
1526
1527 /**
1528 * Invokes the delegate corresponding to the notification or the observer's method corresponding to the notification and returns the produced result.
1529 * @param {Function | Observer} observerOrOnNext Function to invoke for an OnNext notification or Observer to invoke the notification on..
1530 * @param {Function} onError Function to invoke for an OnError notification.
1531 * @param {Function} onCompleted Function to invoke for an OnCompleted notification.
1532 * @returns {Any} Result produced by the observation.
1533 */
1534 Notification.prototype.accept = function (observerOrOnNext, onError, onCompleted) {
1535 return observerOrOnNext && typeof observerOrOnNext === 'object' ?
1536 this._acceptObserver(observerOrOnNext) :
1537 this._accept(observerOrOnNext, onError, onCompleted);
1538 };
1539
1540 /**
1541 * Returns an observable sequence with a single notification.
1542 *
1543 * @memberOf Notifications
1544 * @param {Scheduler} [scheduler] Scheduler to send out the notification calls on.
1545 * @returns {Observable} The observable sequence that surfaces the behavior of the notification upon subscription.
1546 */
1547 Notification.prototype.toObservable = function (scheduler) {
1548 var self = this;
1549 isScheduler(scheduler) || (scheduler = immediateScheduler);
1550 return new AnonymousObservable(function (o) {
1551 return scheduler.schedule(self, function (_, notification) {
1552 notification._acceptObserver(o);
1553 notification.kind === 'N' && o.onCompleted();
1554 });
1555 });
1556 };
1557
1558 return Notification;
1559 })();
1560
1561 var OnNextNotification = (function (__super__) {
1562 inherits(OnNextNotification, __super__);
1563 function OnNextNotification(value) {
1564 this.value = value;
1565 this.kind = 'N';
1566 }
1567
1568 OnNextNotification.prototype._accept = function (onNext) {
1569 return onNext(this.value);
1570 };
1571
1572 OnNextNotification.prototype._acceptObserver = function (o) {
1573 return o.onNext(this.value);
1574 };
1575
1576 OnNextNotification.prototype.toString = function () {
1577 return 'OnNext(' + this.value + ')';
1578 };
1579
1580 return OnNextNotification;
1581 }(Notification));
1582
1583 var OnErrorNotification = (function (__super__) {
1584 inherits(OnErrorNotification, __super__);
1585 function OnErrorNotification(error) {
1586 this.error = error;
1587 this.kind = 'E';
1588 }
1589
1590 OnErrorNotification.prototype._accept = function (onNext, onError) {
1591 return onError(this.error);
1592 };
1593
1594 OnErrorNotification.prototype._acceptObserver = function (o) {
1595 return o.onError(this.error);
1596 };
1597
1598 OnErrorNotification.prototype.toString = function () {
1599 return 'OnError(' + this.error + ')';
1600 };
1601
1602 return OnErrorNotification;
1603 }(Notification));
1604
1605 var OnCompletedNotification = (function (__super__) {
1606 inherits(OnCompletedNotification, __super__);
1607 function OnCompletedNotification() {
1608 this.kind = 'C';
1609 }
1610
1611 OnCompletedNotification.prototype._accept = function (onNext, onError, onCompleted) {
1612 return onCompleted();
1613 };
1614
1615 OnCompletedNotification.prototype._acceptObserver = function (o) {
1616 return o.onCompleted();
1617 };
1618
1619 OnCompletedNotification.prototype.toString = function () {
1620 return 'OnCompleted()';
1621 };
1622
1623 return OnCompletedNotification;
1624 }(Notification));
1625
1626 /**
1627 * Creates an object that represents an OnNext notification to an observer.
1628 * @param {Any} value The value contained in the notification.
1629 * @returns {Notification} The OnNext notification containing the value.
1630 */
1631 var notificationCreateOnNext = Notification.createOnNext = function (value) {
1632 return new OnNextNotification(value);
1633 };
1634
1635 /**
1636 * Creates an object that represents an OnError notification to an observer.
1637 * @param {Any} error The exception contained in the notification.
1638 * @returns {Notification} The OnError notification containing the exception.
1639 */
1640 var notificationCreateOnError = Notification.createOnError = function (error) {
1641 return new OnErrorNotification(error);
1642 };
1643
1644 /**
1645 * Creates an object that represents an OnCompleted notification to an observer.
1646 * @returns {Notification} The OnCompleted notification.
1647 */
1648 var notificationCreateOnCompleted = Notification.createOnCompleted = function () {
1649 return new OnCompletedNotification();
1650 };
1651
1652 /**
1653 * Supports push-style iteration over an observable sequence.
1654 */
1655 var Observer = Rx.Observer = function () { };
1656
1657 /**
1658 * Creates a notification callback from an observer.
1659 * @returns The action that forwards its input notification to the underlying observer.
1660 */
1661 Observer.prototype.toNotifier = function () {
1662 var observer = this;
1663 return function (n) { return n.accept(observer); };
1664 };
1665
1666 /**
1667 * Hides the identity of an observer.
1668 * @returns An observer that hides the identity of the specified observer.
1669 */
1670 Observer.prototype.asObserver = function () {
1671 var self = this;
1672 return new AnonymousObserver(
1673 function (x) { self.onNext(x); },
1674 function (err) { self.onError(err); },
1675 function () { self.onCompleted(); });
1676 };
1677
1678 /**
1679 * Checks access to the observer for grammar violations. This includes checking for multiple OnError or OnCompleted calls, as well as reentrancy in any of the observer methods.
1680 * If a violation is detected, an Error is thrown from the offending observer method call.
1681 * @returns An observer that checks callbacks invocations against the observer grammar and, if the checks pass, forwards those to the specified observer.
1682 */
1683 Observer.prototype.checked = function () { return new CheckedObserver(this); };
1684
1685 /**
1686 * Creates an observer from the specified OnNext, along with optional OnError, and OnCompleted actions.
1687 * @param {Function} [onNext] Observer's OnNext action implementation.
1688 * @param {Function} [onError] Observer's OnError action implementation.
1689 * @param {Function} [onCompleted] Observer's OnCompleted action implementation.
1690 * @returns {Observer} The observer object implemented using the given actions.
1691 */
1692 var observerCreate = Observer.create = function (onNext, onError, onCompleted) {
1693 onNext || (onNext = noop);
1694 onError || (onError = defaultError);
1695 onCompleted || (onCompleted = noop);
1696 return new AnonymousObserver(onNext, onError, onCompleted);
1697 };
1698
1699 /**
1700 * Creates an observer from a notification callback.
1701 * @param {Function} handler Action that handles a notification.
1702 * @returns The observer object that invokes the specified handler using a notification corresponding to each message it receives.
1703 */
1704 Observer.fromNotifier = function (handler, thisArg) {
1705 var cb = bindCallback(handler, thisArg, 1);
1706 return new AnonymousObserver(function (x) {
1707 return cb(notificationCreateOnNext(x));
1708 }, function (e) {
1709 return cb(notificationCreateOnError(e));
1710 }, function () {
1711 return cb(notificationCreateOnCompleted());
1712 });
1713 };
1714
1715 /**
1716 * Schedules the invocation of observer methods on the given scheduler.
1717 * @param {Scheduler} scheduler Scheduler to schedule observer messages on.
1718 * @returns {Observer} Observer whose messages are scheduled on the given scheduler.
1719 */
1720 Observer.prototype.notifyOn = function (scheduler) {
1721 return new ObserveOnObserver(scheduler, this);
1722 };
1723
1724 Observer.prototype.makeSafe = function(disposable) {
1725 return new AnonymousSafeObserver(this._onNext, this._onError, this._onCompleted, disposable);
1726 };
1727
1728 /**
1729 * Abstract base class for implementations of the Observer class.
1730 * This base class enforces the grammar of observers where OnError and OnCompleted are terminal messages.
1731 */
1732 var AbstractObserver = Rx.internals.AbstractObserver = (function (__super__) {
1733 inherits(AbstractObserver, __super__);
1734
1735 /**
1736 * Creates a new observer in a non-stopped state.
1737 */
1738 function AbstractObserver() {
1739 this.isStopped = false;
1740 }
1741
1742 // Must be implemented by other observers
1743 AbstractObserver.prototype.next = notImplemented;
1744 AbstractObserver.prototype.error = notImplemented;
1745 AbstractObserver.prototype.completed = notImplemented;
1746
1747 /**
1748 * Notifies the observer of a new element in the sequence.
1749 * @param {Any} value Next element in the sequence.
1750 */
1751 AbstractObserver.prototype.onNext = function (value) {
1752 !this.isStopped && this.next(value);
1753 };
1754
1755 /**
1756 * Notifies the observer that an exception has occurred.
1757 * @param {Any} error The error that has occurred.
1758 */
1759 AbstractObserver.prototype.onError = function (error) {
1760 if (!this.isStopped) {
1761 this.isStopped = true;
1762 this.error(error);
1763 }
1764 };
1765
1766 /**
1767 * Notifies the observer of the end of the sequence.
1768 */
1769 AbstractObserver.prototype.onCompleted = function () {
1770 if (!this.isStopped) {
1771 this.isStopped = true;
1772 this.completed();
1773 }
1774 };
1775
1776 /**
1777 * Disposes the observer, causing it to transition to the stopped state.
1778 */
1779 AbstractObserver.prototype.dispose = function () { this.isStopped = true; };
1780
1781 AbstractObserver.prototype.fail = function (e) {
1782 if (!this.isStopped) {
1783 this.isStopped = true;
1784 this.error(e);
1785 return true;
1786 }
1787
1788 return false;
1789 };
1790
1791 return AbstractObserver;
1792 }(Observer));
1793
1794 /**
1795 * Class to create an Observer instance from delegate-based implementations of the on* methods.
1796 */
1797 var AnonymousObserver = Rx.AnonymousObserver = (function (__super__) {
1798 inherits(AnonymousObserver, __super__);
1799
1800 /**
1801 * Creates an observer from the specified OnNext, OnError, and OnCompleted actions.
1802 * @param {Any} onNext Observer's OnNext action implementation.
1803 * @param {Any} onError Observer's OnError action implementation.
1804 * @param {Any} onCompleted Observer's OnCompleted action implementation.
1805 */
1806 function AnonymousObserver(onNext, onError, onCompleted) {
1807 __super__.call(this);
1808 this._onNext = onNext;
1809 this._onError = onError;
1810 this._onCompleted = onCompleted;
1811 }
1812
1813 /**
1814 * Calls the onNext action.
1815 * @param {Any} value Next element in the sequence.
1816 */
1817 AnonymousObserver.prototype.next = function (value) {
1818 this._onNext(value);
1819 };
1820
1821 /**
1822 * Calls the onError action.
1823 * @param {Any} error The error that has occurred.
1824 */
1825 AnonymousObserver.prototype.error = function (error) {
1826 this._onError(error);
1827 };
1828
1829 /**
1830 * Calls the onCompleted action.
1831 */
1832 AnonymousObserver.prototype.completed = function () {
1833 this._onCompleted();
1834 };
1835
1836 return AnonymousObserver;
1837 }(AbstractObserver));
1838
1839 var CheckedObserver = (function (__super__) {
1840 inherits(CheckedObserver, __super__);
1841
1842 function CheckedObserver(observer) {
1843 __super__.call(this);
1844 this._observer = observer;
1845 this._state = 0; // 0 - idle, 1 - busy, 2 - done
1846 }
1847
1848 var CheckedObserverPrototype = CheckedObserver.prototype;
1849
1850 CheckedObserverPrototype.onNext = function (value) {
1851 this.checkAccess();
1852 var res = tryCatch(this._observer.onNext).call(this._observer, value);
1853 this._state = 0;
1854 res === errorObj && thrower(res.e);
1855 };
1856
1857 CheckedObserverPrototype.onError = function (err) {
1858 this.checkAccess();
1859 var res = tryCatch(this._observer.onError).call(this._observer, err);
1860 this._state = 2;
1861 res === errorObj && thrower(res.e);
1862 };
1863
1864 CheckedObserverPrototype.onCompleted = function () {
1865 this.checkAccess();
1866 var res = tryCatch(this._observer.onCompleted).call(this._observer);
1867 this._state = 2;
1868 res === errorObj && thrower(res.e);
1869 };
1870
1871 CheckedObserverPrototype.checkAccess = function () {
1872 if (this._state === 1) { throw new Error('Re-entrancy detected'); }
1873 if (this._state === 2) { throw new Error('Observer completed'); }
1874 if (this._state === 0) { this._state = 1; }
1875 };
1876
1877 return CheckedObserver;
1878 }(Observer));
1879
1880 var ScheduledObserver = Rx.internals.ScheduledObserver = (function (__super__) {
1881 inherits(ScheduledObserver, __super__);
1882
1883 function ScheduledObserver(scheduler, observer) {
1884 __super__.call(this);
1885 this.scheduler = scheduler;
1886 this.observer = observer;
1887 this.isAcquired = false;
1888 this.hasFaulted = false;
1889 this.queue = [];
1890 this.disposable = new SerialDisposable();
1891 }
1892
1893 function enqueueNext(observer, x) { return function () { observer.onNext(x); }; }
1894 function enqueueError(observer, e) { return function () { observer.onError(e); }; }
1895 function enqueueCompleted(observer) { return function () { observer.onCompleted(); }; }
1896
1897 ScheduledObserver.prototype.next = function (x) {
1898 this.queue.push(enqueueNext(this.observer, x));
1899 };
1900
1901 ScheduledObserver.prototype.error = function (e) {
1902 this.queue.push(enqueueError(this.observer, e));
1903 };
1904
1905 ScheduledObserver.prototype.completed = function () {
1906 this.queue.push(enqueueCompleted(this.observer));
1907 };
1908
1909
1910 function scheduleMethod(state, recurse) {
1911 var work;
1912 if (state.queue.length > 0) {
1913 work = state.queue.shift();
1914 } else {
1915 state.isAcquired = false;
1916 return;
1917 }
1918 var res = tryCatch(work)();
1919 if (res === errorObj) {
1920 state.queue = [];
1921 state.hasFaulted = true;
1922 return thrower(res.e);
1923 }
1924 recurse(state);
1925 }
1926
1927 ScheduledObserver.prototype.ensureActive = function () {
1928 var isOwner = false;
1929 if (!this.hasFaulted && this.queue.length > 0) {
1930 isOwner = !this.isAcquired;
1931 this.isAcquired = true;
1932 }
1933 isOwner &&
1934 this.disposable.setDisposable(this.scheduler.scheduleRecursive(this, scheduleMethod));
1935 };
1936
1937 ScheduledObserver.prototype.dispose = function () {
1938 __super__.prototype.dispose.call(this);
1939 this.disposable.dispose();
1940 };
1941
1942 return ScheduledObserver;
1943 }(AbstractObserver));
1944
1945 var ObserveOnObserver = (function (__super__) {
1946 inherits(ObserveOnObserver, __super__);
1947
1948 function ObserveOnObserver(scheduler, observer, cancel) {
1949 __super__.call(this, scheduler, observer);
1950 this._cancel = cancel;
1951 }
1952
1953 ObserveOnObserver.prototype.next = function (value) {
1954 __super__.prototype.next.call(this, value);
1955 this.ensureActive();
1956 };
1957
1958 ObserveOnObserver.prototype.error = function (e) {
1959 __super__.prototype.error.call(this, e);
1960 this.ensureActive();
1961 };
1962
1963 ObserveOnObserver.prototype.completed = function () {
1964 __super__.prototype.completed.call(this);
1965 this.ensureActive();
1966 };
1967
1968 ObserveOnObserver.prototype.dispose = function () {
1969 __super__.prototype.dispose.call(this);
1970 this._cancel && this._cancel.dispose();
1971 this._cancel = null;
1972 };
1973
1974 return ObserveOnObserver;
1975 })(ScheduledObserver);
1976
1977 var observableProto;
1978
1979 /**
1980 * Represents a push-style collection.
1981 */
1982 var Observable = Rx.Observable = (function () {
1983
1984 function makeSubscribe(self, subscribe) {
1985 return function (o) {
1986 var oldOnError = o.onError;
1987 o.onError = function (e) {
1988 makeStackTraceLong(e, self);
1989 oldOnError.call(o, e);
1990 };
1991
1992 return subscribe.call(self, o);
1993 };
1994 }
1995
1996 function Observable() {
1997 if (Rx.config.longStackSupport && hasStacks) {
1998 var oldSubscribe = this._subscribe;
1999 var e = tryCatch(thrower)(new Error()).e;
2000 this.stack = e.stack.substring(e.stack.indexOf('\n') + 1);
2001 this._subscribe = makeSubscribe(this, oldSubscribe);
2002 }
2003 }
2004
2005 observableProto = Observable.prototype;
2006
2007 /**
2008 * Determines whether the given object is an Observable
2009 * @param {Any} An object to determine whether it is an Observable
2010 * @returns {Boolean} true if an Observable, else false.
2011 */
2012 Observable.isObservable = function (o) {
2013 return o && isFunction(o.subscribe);
2014 };
2015
2016 /**
2017 * Subscribes an o to the observable sequence.
2018 * @param {Mixed} [oOrOnNext] The object that is to receive notifications or an action to invoke for each element in the observable sequence.
2019 * @param {Function} [onError] Action to invoke upon exceptional termination of the observable sequence.
2020 * @param {Function} [onCompleted] Action to invoke upon graceful termination of the observable sequence.
2021 * @returns {Diposable} A disposable handling the subscriptions and unsubscriptions.
2022 */
2023 observableProto.subscribe = observableProto.forEach = function (oOrOnNext, onError, onCompleted) {
2024 return this._subscribe(typeof oOrOnNext === 'object' ?
2025 oOrOnNext :
2026 observerCreate(oOrOnNext, onError, onCompleted));
2027 };
2028
2029 /**
2030 * Subscribes to the next value in the sequence with an optional "this" argument.
2031 * @param {Function} onNext The function to invoke on each element in the observable sequence.
2032 * @param {Any} [thisArg] Object to use as this when executing callback.
2033 * @returns {Disposable} A disposable handling the subscriptions and unsubscriptions.
2034 */
2035 observableProto.subscribeOnNext = function (onNext, thisArg) {
2036 return this._subscribe(observerCreate(typeof thisArg !== 'undefined' ? function(x) { onNext.call(thisArg, x); } : onNext));
2037 };
2038
2039 /**
2040 * Subscribes to an exceptional condition in the sequence with an optional "this" argument.
2041 * @param {Function} onError The function to invoke upon exceptional termination of the observable sequence.
2042 * @param {Any} [thisArg] Object to use as this when executing callback.
2043 * @returns {Disposable} A disposable handling the subscriptions and unsubscriptions.
2044 */
2045 observableProto.subscribeOnError = function (onError, thisArg) {
2046 return this._subscribe(observerCreate(null, typeof thisArg !== 'undefined' ? function(e) { onError.call(thisArg, e); } : onError));
2047 };
2048
2049 /**
2050 * Subscribes to the next value in the sequence with an optional "this" argument.
2051 * @param {Function} onCompleted The function to invoke upon graceful termination of the observable sequence.
2052 * @param {Any} [thisArg] Object to use as this when executing callback.
2053 * @returns {Disposable} A disposable handling the subscriptions and unsubscriptions.
2054 */
2055 observableProto.subscribeOnCompleted = function (onCompleted, thisArg) {
2056 return this._subscribe(observerCreate(null, null, typeof thisArg !== 'undefined' ? function() { onCompleted.call(thisArg); } : onCompleted));
2057 };
2058
2059 return Observable;
2060 })();
2061
2062 var ObservableBase = Rx.ObservableBase = (function (__super__) {
2063 inherits(ObservableBase, __super__);
2064
2065 function fixSubscriber(subscriber) {
2066 return subscriber && isFunction(subscriber.dispose) ? subscriber :
2067 isFunction(subscriber) ? disposableCreate(subscriber) : disposableEmpty;
2068 }
2069
2070 function setDisposable(s, state) {
2071 var ado = state[0], self = state[1];
2072 var sub = tryCatch(self.subscribeCore).call(self, ado);
2073 if (sub === errorObj && !ado.fail(errorObj.e)) { thrower(errorObj.e); }
2074 ado.setDisposable(fixSubscriber(sub));
2075 }
2076
2077 function ObservableBase() {
2078 __super__.call(this);
2079 }
2080
2081 ObservableBase.prototype._subscribe = function (o) {
2082 var ado = new AutoDetachObserver(o), state = [ado, this];
2083
2084 if (currentThreadScheduler.scheduleRequired()) {
2085 currentThreadScheduler.schedule(state, setDisposable);
2086 } else {
2087 setDisposable(null, state);
2088 }
2089 return ado;
2090 };
2091
2092 ObservableBase.prototype.subscribeCore = notImplemented;
2093
2094 return ObservableBase;
2095 }(Observable));
2096
2097var FlatMapObservable = Rx.FlatMapObservable = (function(__super__) {
2098
2099 inherits(FlatMapObservable, __super__);
2100
2101 function FlatMapObservable(source, selector, resultSelector, thisArg) {
2102 this.resultSelector = isFunction(resultSelector) ? resultSelector : null;
2103 this.selector = bindCallback(isFunction(selector) ? selector : function() { return selector; }, thisArg, 3);
2104 this.source = source;
2105 __super__.call(this);
2106 }
2107
2108 FlatMapObservable.prototype.subscribeCore = function(o) {
2109 return this.source.subscribe(new InnerObserver(o, this.selector, this.resultSelector, this));
2110 };
2111
2112 inherits(InnerObserver, AbstractObserver);
2113 function InnerObserver(observer, selector, resultSelector, source) {
2114 this.i = 0;
2115 this.selector = selector;
2116 this.resultSelector = resultSelector;
2117 this.source = source;
2118 this.o = observer;
2119 AbstractObserver.call(this);
2120 }
2121
2122 InnerObserver.prototype._wrapResult = function(result, x, i) {
2123 return this.resultSelector ?
2124 result.map(function(y, i2) { return this.resultSelector(x, y, i, i2); }, this) :
2125 result;
2126 };
2127
2128 InnerObserver.prototype.next = function(x) {
2129 var i = this.i++;
2130 var result = tryCatch(this.selector)(x, i, this.source);
2131 if (result === errorObj) { return this.o.onError(result.e); }
2132
2133 isPromise(result) && (result = observableFromPromise(result));
2134 (isArrayLike(result) || isIterable(result)) && (result = Observable.from(result));
2135 this.o.onNext(this._wrapResult(result, x, i));
2136 };
2137
2138 InnerObserver.prototype.error = function(e) { this.o.onError(e); };
2139
2140 InnerObserver.prototype.completed = function() { this.o.onCompleted(); };
2141
2142 return FlatMapObservable;
2143
2144}(ObservableBase));
2145
2146 var Enumerable = Rx.internals.Enumerable = function () { };
2147
2148 function IsDisposedDisposable(state) {
2149 this._s = state;
2150 this.isDisposed = false;
2151 }
2152
2153 IsDisposedDisposable.prototype.dispose = function () {
2154 if (!this.isDisposed) {
2155 this.isDisposed = true;
2156 this._s.isDisposed = true;
2157 }
2158 };
2159
2160 var ConcatEnumerableObservable = (function(__super__) {
2161 inherits(ConcatEnumerableObservable, __super__);
2162 function ConcatEnumerableObservable(sources) {
2163 this.sources = sources;
2164 __super__.call(this);
2165 }
2166
2167 function scheduleMethod(state, recurse) {
2168 if (state.isDisposed) { return; }
2169 var currentItem = tryCatch(state.e.next).call(state.e);
2170 if (currentItem === errorObj) { return state.o.onError(currentItem.e); }
2171 if (currentItem.done) { return state.o.onCompleted(); }
2172
2173 // Check if promise
2174 var currentValue = currentItem.value;
2175 isPromise(currentValue) && (currentValue = observableFromPromise(currentValue));
2176
2177 var d = new SingleAssignmentDisposable();
2178 state.subscription.setDisposable(d);
2179 d.setDisposable(currentValue.subscribe(new InnerObserver(state, recurse)));
2180 }
2181
2182 ConcatEnumerableObservable.prototype.subscribeCore = function (o) {
2183 var subscription = new SerialDisposable();
2184 var state = {
2185 isDisposed: false,
2186 o: o,
2187 subscription: subscription,
2188 e: this.sources[$iterator$]()
2189 };
2190
2191 var cancelable = currentThreadScheduler.scheduleRecursive(state, scheduleMethod);
2192 return new NAryDisposable([subscription, cancelable, new IsDisposedDisposable(state)]);
2193 };
2194
2195 function InnerObserver(state, recurse) {
2196 this._state = state;
2197 this._recurse = recurse;
2198 AbstractObserver.call(this);
2199 }
2200
2201 inherits(InnerObserver, AbstractObserver);
2202
2203 InnerObserver.prototype.next = function (x) { this._state.o.onNext(x); };
2204 InnerObserver.prototype.error = function (e) { this._state.o.onError(e); };
2205 InnerObserver.prototype.completed = function () { this._recurse(this._state); };
2206
2207 return ConcatEnumerableObservable;
2208 }(ObservableBase));
2209
2210 Enumerable.prototype.concat = function () {
2211 return new ConcatEnumerableObservable(this);
2212 };
2213
2214 var CatchErrorObservable = (function(__super__) {
2215 function CatchErrorObservable(sources) {
2216 this.sources = sources;
2217 __super__.call(this);
2218 }
2219
2220 inherits(CatchErrorObservable, __super__);
2221
2222 function scheduleMethod(state, recurse) {
2223 if (state.isDisposed) { return; }
2224 var currentItem = tryCatch(state.e.next).call(state.e);
2225 if (currentItem === errorObj) { return state.o.onError(currentItem.e); }
2226 if (currentItem.done) { return state.lastError !== null ? state.o.onError(state.lastError) : state.o.onCompleted(); }
2227
2228 var currentValue = currentItem.value;
2229 isPromise(currentValue) && (currentValue = observableFromPromise(currentValue));
2230
2231 var d = new SingleAssignmentDisposable();
2232 state.subscription.setDisposable(d);
2233 d.setDisposable(currentValue.subscribe(new InnerObserver(state, recurse)));
2234 }
2235
2236 CatchErrorObservable.prototype.subscribeCore = function (o) {
2237 var subscription = new SerialDisposable();
2238 var state = {
2239 isDisposed: false,
2240 e: this.sources[$iterator$](),
2241 subscription: subscription,
2242 lastError: null,
2243 o: o
2244 };
2245
2246 var cancelable = currentThreadScheduler.scheduleRecursive(state, scheduleMethod);
2247 return new NAryDisposable([subscription, cancelable, new IsDisposedDisposable(state)]);
2248 };
2249
2250 function InnerObserver(state, recurse) {
2251 this._state = state;
2252 this._recurse = recurse;
2253 AbstractObserver.call(this);
2254 }
2255
2256 inherits(InnerObserver, AbstractObserver);
2257
2258 InnerObserver.prototype.next = function (x) { this._state.o.onNext(x); };
2259 InnerObserver.prototype.error = function (e) { this._state.lastError = e; this._recurse(this._state); };
2260 InnerObserver.prototype.completed = function () { this._state.o.onCompleted(); };
2261
2262 return CatchErrorObservable;
2263 }(ObservableBase));
2264
2265 Enumerable.prototype.catchError = function () {
2266 return new CatchErrorObservable(this);
2267 };
2268
2269 Enumerable.prototype.catchErrorWhen = function (notificationHandler) {
2270 var sources = this;
2271 return new AnonymousObservable(function (o) {
2272 var exceptions = new Subject(),
2273 notifier = new Subject(),
2274 handled = notificationHandler(exceptions),
2275 notificationDisposable = handled.subscribe(notifier);
2276
2277 var e = sources[$iterator$]();
2278
2279 var state = { isDisposed: false },
2280 lastError,
2281 subscription = new SerialDisposable();
2282 var cancelable = currentThreadScheduler.scheduleRecursive(null, function (_, self) {
2283 if (state.isDisposed) { return; }
2284 var currentItem = tryCatch(e.next).call(e);
2285 if (currentItem === errorObj) { return o.onError(currentItem.e); }
2286
2287 if (currentItem.done) {
2288 if (lastError) {
2289 o.onError(lastError);
2290 } else {
2291 o.onCompleted();
2292 }
2293 return;
2294 }
2295
2296 // Check if promise
2297 var currentValue = currentItem.value;
2298 isPromise(currentValue) && (currentValue = observableFromPromise(currentValue));
2299
2300 var outer = new SingleAssignmentDisposable();
2301 var inner = new SingleAssignmentDisposable();
2302 subscription.setDisposable(new BinaryDisposable(inner, outer));
2303 outer.setDisposable(currentValue.subscribe(
2304 function(x) { o.onNext(x); },
2305 function (exn) {
2306 inner.setDisposable(notifier.subscribe(self, function(ex) {
2307 o.onError(ex);
2308 }, function() {
2309 o.onCompleted();
2310 }));
2311
2312 exceptions.onNext(exn);
2313 },
2314 function() { o.onCompleted(); }));
2315 });
2316
2317 return new NAryDisposable([notificationDisposable, subscription, cancelable, new IsDisposedDisposable(state)]);
2318 });
2319 };
2320
2321 var RepeatEnumerable = (function (__super__) {
2322 inherits(RepeatEnumerable, __super__);
2323 function RepeatEnumerable(v, c) {
2324 this.v = v;
2325 this.c = c == null ? -1 : c;
2326 }
2327
2328 RepeatEnumerable.prototype[$iterator$] = function () {
2329 return new RepeatEnumerator(this);
2330 };
2331
2332 function RepeatEnumerator(p) {
2333 this.v = p.v;
2334 this.l = p.c;
2335 }
2336
2337 RepeatEnumerator.prototype.next = function () {
2338 if (this.l === 0) { return doneEnumerator; }
2339 if (this.l > 0) { this.l--; }
2340 return { done: false, value: this.v };
2341 };
2342
2343 return RepeatEnumerable;
2344 }(Enumerable));
2345
2346 var enumerableRepeat = Enumerable.repeat = function (value, repeatCount) {
2347 return new RepeatEnumerable(value, repeatCount);
2348 };
2349
2350 var OfEnumerable = (function(__super__) {
2351 inherits(OfEnumerable, __super__);
2352 function OfEnumerable(s, fn, thisArg) {
2353 this.s = s;
2354 this.fn = fn ? bindCallback(fn, thisArg, 3) : null;
2355 }
2356 OfEnumerable.prototype[$iterator$] = function () {
2357 return new OfEnumerator(this);
2358 };
2359
2360 function OfEnumerator(p) {
2361 this.i = -1;
2362 this.s = p.s;
2363 this.l = this.s.length;
2364 this.fn = p.fn;
2365 }
2366
2367 OfEnumerator.prototype.next = function () {
2368 return ++this.i < this.l ?
2369 { done: false, value: !this.fn ? this.s[this.i] : this.fn(this.s[this.i], this.i, this.s) } :
2370 doneEnumerator;
2371 };
2372
2373 return OfEnumerable;
2374 }(Enumerable));
2375
2376 var enumerableOf = Enumerable.of = function (source, selector, thisArg) {
2377 return new OfEnumerable(source, selector, thisArg);
2378 };
2379
2380var ObserveOnObservable = (function (__super__) {
2381 inherits(ObserveOnObservable, __super__);
2382 function ObserveOnObservable(source, s) {
2383 this.source = source;
2384 this._s = s;
2385 __super__.call(this);
2386 }
2387
2388 ObserveOnObservable.prototype.subscribeCore = function (o) {
2389 return this.source.subscribe(new ObserveOnObserver(this._s, o));
2390 };
2391
2392 return ObserveOnObservable;
2393}(ObservableBase));
2394
2395 /**
2396 * Wraps the source sequence in order to run its observer callbacks on the specified scheduler.
2397 *
2398 * This only invokes observer callbacks on a scheduler. In case the subscription and/or unsubscription actions have side-effects
2399 * that require to be run on a scheduler, use subscribeOn.
2400 *
2401 * @param {Scheduler} scheduler Scheduler to notify observers on.
2402 * @returns {Observable} The source sequence whose observations happen on the specified scheduler.
2403 */
2404 observableProto.observeOn = function (scheduler) {
2405 return new ObserveOnObservable(this, scheduler);
2406 };
2407
2408 var SubscribeOnObservable = (function (__super__) {
2409 inherits(SubscribeOnObservable, __super__);
2410 function SubscribeOnObservable(source, s) {
2411 this.source = source;
2412 this._s = s;
2413 __super__.call(this);
2414 }
2415
2416 function scheduleMethod(scheduler, state) {
2417 var source = state[0], d = state[1], o = state[2];
2418 d.setDisposable(new ScheduledDisposable(scheduler, source.subscribe(o)));
2419 }
2420
2421 SubscribeOnObservable.prototype.subscribeCore = function (o) {
2422 var m = new SingleAssignmentDisposable(), d = new SerialDisposable();
2423 d.setDisposable(m);
2424 m.setDisposable(this._s.schedule([this.source, d, o], scheduleMethod));
2425 return d;
2426 };
2427
2428 return SubscribeOnObservable;
2429 }(ObservableBase));
2430
2431 /**
2432 * Wraps the source sequence in order to run its subscription and unsubscription logic on the specified scheduler. This operation is not commonly used;
2433 * see the remarks section for more information on the distinction between subscribeOn and observeOn.
2434
2435 * This only performs the side-effects of subscription and unsubscription on the specified scheduler. In order to invoke observer
2436 * callbacks on a scheduler, use observeOn.
2437
2438 * @param {Scheduler} scheduler Scheduler to perform subscription and unsubscription actions on.
2439 * @returns {Observable} The source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.
2440 */
2441 observableProto.subscribeOn = function (scheduler) {
2442 return new SubscribeOnObservable(this, scheduler);
2443 };
2444
2445 var FromPromiseObservable = (function(__super__) {
2446 inherits(FromPromiseObservable, __super__);
2447 function FromPromiseObservable(p, s) {
2448 this._p = p;
2449 this._s = s;
2450 __super__.call(this);
2451 }
2452
2453 function scheduleNext(s, state) {
2454 var o = state[0], data = state[1];
2455 o.onNext(data);
2456 o.onCompleted();
2457 }
2458
2459 function scheduleError(s, state) {
2460 var o = state[0], err = state[1];
2461 o.onError(err);
2462 }
2463
2464 FromPromiseObservable.prototype.subscribeCore = function(o) {
2465 var sad = new SingleAssignmentDisposable(), self = this;
2466
2467 this._p
2468 .then(function (data) {
2469 sad.setDisposable(self._s.schedule([o, data], scheduleNext));
2470 }, function (err) {
2471 sad.setDisposable(self._s.schedule([o, err], scheduleError));
2472 });
2473
2474 return sad;
2475 };
2476
2477 return FromPromiseObservable;
2478 }(ObservableBase));
2479
2480 /**
2481 * Converts a Promise to an Observable sequence
2482 * @param {Promise} An ES6 Compliant promise.
2483 * @returns {Observable} An Observable sequence which wraps the existing promise success and failure.
2484 */
2485 var observableFromPromise = Observable.fromPromise = function (promise, scheduler) {
2486 scheduler || (scheduler = defaultScheduler);
2487 return new FromPromiseObservable(promise, scheduler);
2488 };
2489
2490 /*
2491 * Converts an existing observable sequence to an ES6 Compatible Promise
2492 * @example
2493 * var promise = Rx.Observable.return(42).toPromise(RSVP.Promise);
2494 *
2495 * // With config
2496 * Rx.config.Promise = RSVP.Promise;
2497 * var promise = Rx.Observable.return(42).toPromise();
2498 * @param {Function} [promiseCtor] The constructor of the promise. If not provided, it looks for it in Rx.config.Promise.
2499 * @returns {Promise} An ES6 compatible promise with the last value from the observable sequence.
2500 */
2501 observableProto.toPromise = function (promiseCtor) {
2502 promiseCtor || (promiseCtor = Rx.config.Promise);
2503 if (!promiseCtor) { throw new NotSupportedError('Promise type not provided nor in Rx.config.Promise'); }
2504 var source = this;
2505 return new promiseCtor(function (resolve, reject) {
2506 // No cancellation can be done
2507 var value;
2508 source.subscribe(function (v) {
2509 value = v;
2510 }, reject, function () {
2511 resolve(value);
2512 });
2513 });
2514 };
2515
2516 var ToArrayObservable = (function(__super__) {
2517 inherits(ToArrayObservable, __super__);
2518 function ToArrayObservable(source) {
2519 this.source = source;
2520 __super__.call(this);
2521 }
2522
2523 ToArrayObservable.prototype.subscribeCore = function(o) {
2524 return this.source.subscribe(new InnerObserver(o));
2525 };
2526
2527 inherits(InnerObserver, AbstractObserver);
2528 function InnerObserver(o) {
2529 this.o = o;
2530 this.a = [];
2531 AbstractObserver.call(this);
2532 }
2533
2534 InnerObserver.prototype.next = function (x) { this.a.push(x); };
2535 InnerObserver.prototype.error = function (e) { this.o.onError(e); };
2536 InnerObserver.prototype.completed = function () { this.o.onNext(this.a); this.o.onCompleted(); };
2537
2538 return ToArrayObservable;
2539 }(ObservableBase));
2540
2541 /**
2542 * Creates an array from an observable sequence.
2543 * @returns {Observable} An observable sequence containing a single element with a list containing all the elements of the source sequence.
2544 */
2545 observableProto.toArray = function () {
2546 return new ToArrayObservable(this);
2547 };
2548
2549 /**
2550 * Creates an observable sequence from a specified subscribe method implementation.
2551 * @example
2552 * var res = Rx.Observable.create(function (observer) { return function () { } );
2553 * var res = Rx.Observable.create(function (observer) { return Rx.Disposable.empty; } );
2554 * var res = Rx.Observable.create(function (observer) { } );
2555 * @param {Function} subscribe Implementation of the resulting observable sequence's subscribe method, returning a function that will be wrapped in a Disposable.
2556 * @returns {Observable} The observable sequence with the specified implementation for the Subscribe method.
2557 */
2558 Observable.create = function (subscribe, parent) {
2559 return new AnonymousObservable(subscribe, parent);
2560 };
2561
2562 var Defer = (function(__super__) {
2563 inherits(Defer, __super__);
2564 function Defer(factory) {
2565 this._f = factory;
2566 __super__.call(this);
2567 }
2568
2569 Defer.prototype.subscribeCore = function (o) {
2570 var result = tryCatch(this._f)();
2571 if (result === errorObj) { return observableThrow(result.e).subscribe(o);}
2572 isPromise(result) && (result = observableFromPromise(result));
2573 return result.subscribe(o);
2574 };
2575
2576 return Defer;
2577 }(ObservableBase));
2578
2579 /**
2580 * Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.
2581 *
2582 * @example
2583 * var res = Rx.Observable.defer(function () { return Rx.Observable.fromArray([1,2,3]); });
2584 * @param {Function} observableFactory Observable factory function to invoke for each observer that subscribes to the resulting sequence or Promise.
2585 * @returns {Observable} An observable sequence whose observers trigger an invocation of the given observable factory function.
2586 */
2587 var observableDefer = Observable.defer = function (observableFactory) {
2588 return new Defer(observableFactory);
2589 };
2590
2591 var EmptyObservable = (function(__super__) {
2592 inherits(EmptyObservable, __super__);
2593 function EmptyObservable(scheduler) {
2594 this.scheduler = scheduler;
2595 __super__.call(this);
2596 }
2597
2598 EmptyObservable.prototype.subscribeCore = function (observer) {
2599 var sink = new EmptySink(observer, this.scheduler);
2600 return sink.run();
2601 };
2602
2603 function EmptySink(observer, scheduler) {
2604 this.observer = observer;
2605 this.scheduler = scheduler;
2606 }
2607
2608 function scheduleItem(s, state) {
2609 state.onCompleted();
2610 return disposableEmpty;
2611 }
2612
2613 EmptySink.prototype.run = function () {
2614 var state = this.observer;
2615 return this.scheduler === immediateScheduler ?
2616 scheduleItem(null, state) :
2617 this.scheduler.schedule(state, scheduleItem);
2618 };
2619
2620 return EmptyObservable;
2621 }(ObservableBase));
2622
2623 var EMPTY_OBSERVABLE = new EmptyObservable(immediateScheduler);
2624
2625 /**
2626 * Returns an empty observable sequence, using the specified scheduler to send out the single OnCompleted message.
2627 *
2628 * @example
2629 * var res = Rx.Observable.empty();
2630 * var res = Rx.Observable.empty(Rx.Scheduler.timeout);
2631 * @param {Scheduler} [scheduler] Scheduler to send the termination call on.
2632 * @returns {Observable} An observable sequence with no elements.
2633 */
2634 var observableEmpty = Observable.empty = function (scheduler) {
2635 isScheduler(scheduler) || (scheduler = immediateScheduler);
2636 return scheduler === immediateScheduler ? EMPTY_OBSERVABLE : new EmptyObservable(scheduler);
2637 };
2638
2639 var FromObservable = (function(__super__) {
2640 inherits(FromObservable, __super__);
2641 function FromObservable(iterable, fn, scheduler) {
2642 this._iterable = iterable;
2643 this._fn = fn;
2644 this._scheduler = scheduler;
2645 __super__.call(this);
2646 }
2647
2648 function createScheduleMethod(o, it, fn) {
2649 return function loopRecursive(i, recurse) {
2650 var next = tryCatch(it.next).call(it);
2651 if (next === errorObj) { return o.onError(next.e); }
2652 if (next.done) { return o.onCompleted(); }
2653
2654 var result = next.value;
2655
2656 if (isFunction(fn)) {
2657 result = tryCatch(fn)(result, i);
2658 if (result === errorObj) { return o.onError(result.e); }
2659 }
2660
2661 o.onNext(result);
2662 recurse(i + 1);
2663 };
2664 }
2665
2666 FromObservable.prototype.subscribeCore = function (o) {
2667 var list = Object(this._iterable),
2668 it = getIterable(list);
2669
2670 return this._scheduler.scheduleRecursive(0, createScheduleMethod(o, it, this._fn));
2671 };
2672
2673 return FromObservable;
2674 }(ObservableBase));
2675
2676 var maxSafeInteger = Math.pow(2, 53) - 1;
2677
2678 function StringIterable(s) {
2679 this._s = s;
2680 }
2681
2682 StringIterable.prototype[$iterator$] = function () {
2683 return new StringIterator(this._s);
2684 };
2685
2686 function StringIterator(s) {
2687 this._s = s;
2688 this._l = s.length;
2689 this._i = 0;
2690 }
2691
2692 StringIterator.prototype[$iterator$] = function () {
2693 return this;
2694 };
2695
2696 StringIterator.prototype.next = function () {
2697 return this._i < this._l ? { done: false, value: this._s.charAt(this._i++) } : doneEnumerator;
2698 };
2699
2700 function ArrayIterable(a) {
2701 this._a = a;
2702 }
2703
2704 ArrayIterable.prototype[$iterator$] = function () {
2705 return new ArrayIterator(this._a);
2706 };
2707
2708 function ArrayIterator(a) {
2709 this._a = a;
2710 this._l = toLength(a);
2711 this._i = 0;
2712 }
2713
2714 ArrayIterator.prototype[$iterator$] = function () {
2715 return this;
2716 };
2717
2718 ArrayIterator.prototype.next = function () {
2719 return this._i < this._l ? { done: false, value: this._a[this._i++] } : doneEnumerator;
2720 };
2721
2722 function numberIsFinite(value) {
2723 return typeof value === 'number' && root.isFinite(value);
2724 }
2725
2726 function isNan(n) {
2727 return n !== n;
2728 }
2729
2730 function getIterable(o) {
2731 var i = o[$iterator$], it;
2732 if (!i && typeof o === 'string') {
2733 it = new StringIterable(o);
2734 return it[$iterator$]();
2735 }
2736 if (!i && o.length !== undefined) {
2737 it = new ArrayIterable(o);
2738 return it[$iterator$]();
2739 }
2740 if (!i) { throw new TypeError('Object is not iterable'); }
2741 return o[$iterator$]();
2742 }
2743
2744 function sign(value) {
2745 var number = +value;
2746 if (number === 0) { return number; }
2747 if (isNaN(number)) { return number; }
2748 return number < 0 ? -1 : 1;
2749 }
2750
2751 function toLength(o) {
2752 var len = +o.length;
2753 if (isNaN(len)) { return 0; }
2754 if (len === 0 || !numberIsFinite(len)) { return len; }
2755 len = sign(len) * Math.floor(Math.abs(len));
2756 if (len <= 0) { return 0; }
2757 if (len > maxSafeInteger) { return maxSafeInteger; }
2758 return len;
2759 }
2760
2761 /**
2762 * This method creates a new Observable sequence from an array-like or iterable object.
2763 * @param {Any} arrayLike An array-like or iterable object to convert to an Observable sequence.
2764 * @param {Function} [mapFn] Map function to call on every element of the array.
2765 * @param {Any} [thisArg] The context to use calling the mapFn if provided.
2766 * @param {Scheduler} [scheduler] Optional scheduler to use for scheduling. If not provided, defaults to Scheduler.currentThread.
2767 */
2768 var observableFrom = Observable.from = function (iterable, mapFn, thisArg, scheduler) {
2769 if (iterable == null) {
2770 throw new Error('iterable cannot be null.')
2771 }
2772 if (mapFn && !isFunction(mapFn)) {
2773 throw new Error('mapFn when provided must be a function');
2774 }
2775 if (mapFn) {
2776 var mapper = bindCallback(mapFn, thisArg, 2);
2777 }
2778 isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2779 return new FromObservable(iterable, mapper, scheduler);
2780 }
2781
2782 var FromArrayObservable = (function(__super__) {
2783 inherits(FromArrayObservable, __super__);
2784 function FromArrayObservable(args, scheduler) {
2785 this._args = args;
2786 this._scheduler = scheduler;
2787 __super__.call(this);
2788 }
2789
2790 function scheduleMethod(o, args) {
2791 var len = args.length;
2792 return function loopRecursive (i, recurse) {
2793 if (i < len) {
2794 o.onNext(args[i]);
2795 recurse(i + 1);
2796 } else {
2797 o.onCompleted();
2798 }
2799 };
2800 }
2801
2802 FromArrayObservable.prototype.subscribeCore = function (o) {
2803 return this._scheduler.scheduleRecursive(0, scheduleMethod(o, this._args));
2804 };
2805
2806 return FromArrayObservable;
2807 }(ObservableBase));
2808
2809 /**
2810 * Converts an array to an observable sequence, using an optional scheduler to enumerate the array.
2811 * @deprecated use Observable.from or Observable.of
2812 * @param {Scheduler} [scheduler] Scheduler to run the enumeration of the input sequence on.
2813 * @returns {Observable} The observable sequence whose elements are pulled from the given enumerable sequence.
2814 */
2815 var observableFromArray = Observable.fromArray = function (array, scheduler) {
2816 isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2817 return new FromArrayObservable(array, scheduler)
2818 };
2819
2820 var GenerateObservable = (function (__super__) {
2821 inherits(GenerateObservable, __super__);
2822 function GenerateObservable(state, cndFn, itrFn, resFn, s) {
2823 this._state = state;
2824 this._cndFn = cndFn;
2825 this._itrFn = itrFn;
2826 this._resFn = resFn;
2827 this._s = s;
2828 this._first = true;
2829 __super__.call(this);
2830 }
2831
2832 function scheduleRecursive(self, recurse) {
2833 if (self._first) {
2834 self._first = false;
2835 } else {
2836 self._state = tryCatch(self._itrFn)(self._state);
2837 if (self._state === errorObj) { return self._o.onError(self._state.e); }
2838 }
2839 var hasResult = tryCatch(self._cndFn)(self._state);
2840 if (hasResult === errorObj) { return self._o.onError(hasResult.e); }
2841 if (hasResult) {
2842 var result = tryCatch(self._resFn)(self._state);
2843 if (result === errorObj) { return self._o.onError(result.e); }
2844 self._o.onNext(result);
2845 recurse(self);
2846 } else {
2847 self._o.onCompleted();
2848 }
2849 }
2850
2851 GenerateObservable.prototype.subscribeCore = function (o) {
2852 this._o = o;
2853 return this._s.scheduleRecursive(this, scheduleRecursive);
2854 };
2855
2856 return GenerateObservable;
2857 }(ObservableBase));
2858
2859 /**
2860 * Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler to send out observer messages.
2861 *
2862 * @example
2863 * var res = Rx.Observable.generate(0, function (x) { return x < 10; }, function (x) { return x + 1; }, function (x) { return x; });
2864 * var res = Rx.Observable.generate(0, function (x) { return x < 10; }, function (x) { return x + 1; }, function (x) { return x; }, Rx.Scheduler.timeout);
2865 * @param {Mixed} initialState Initial state.
2866 * @param {Function} condition Condition to terminate generation (upon returning false).
2867 * @param {Function} iterate Iteration step function.
2868 * @param {Function} resultSelector Selector function for results produced in the sequence.
2869 * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not provided, defaults to Scheduler.currentThread.
2870 * @returns {Observable} The generated sequence.
2871 */
2872 Observable.generate = function (initialState, condition, iterate, resultSelector, scheduler) {
2873 isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2874 return new GenerateObservable(initialState, condition, iterate, resultSelector, scheduler);
2875 };
2876
2877 function observableOf (scheduler, array) {
2878 isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2879 return new FromArrayObservable(array, scheduler);
2880 }
2881
2882 /**
2883 * This method creates a new Observable instance with a variable number of arguments, regardless of number or type of the arguments.
2884 * @returns {Observable} The observable sequence whose elements are pulled from the given arguments.
2885 */
2886 Observable.of = function () {
2887 var len = arguments.length, args = new Array(len);
2888 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
2889 return new FromArrayObservable(args, currentThreadScheduler);
2890 };
2891
2892 /**
2893 * This method creates a new Observable instance with a variable number of arguments, regardless of number or type of the arguments.
2894 * @param {Scheduler} scheduler A scheduler to use for scheduling the arguments.
2895 * @returns {Observable} The observable sequence whose elements are pulled from the given arguments.
2896 */
2897 Observable.ofWithScheduler = function (scheduler) {
2898 var len = arguments.length, args = new Array(len - 1);
2899 for(var i = 1; i < len; i++) { args[i - 1] = arguments[i]; }
2900 return new FromArrayObservable(args, scheduler);
2901 };
2902
2903 /**
2904 * Creates an Observable sequence from changes to an array using Array.observe.
2905 * @param {Array} array An array to observe changes.
2906 * @returns {Observable} An observable sequence containing changes to an array from Array.observe.
2907 */
2908 Observable.ofArrayChanges = function(array) {
2909 if (!Array.isArray(array)) { throw new TypeError('Array.observe only accepts arrays.'); }
2910 if (typeof Array.observe !== 'function' && typeof Array.unobserve !== 'function') { throw new TypeError('Array.observe is not supported on your platform') }
2911 return new AnonymousObservable(function(observer) {
2912 function observerFn(changes) {
2913 for(var i = 0, len = changes.length; i < len; i++) {
2914 observer.onNext(changes[i]);
2915 }
2916 }
2917
2918 Array.observe(array, observerFn);
2919
2920 return function () {
2921 Array.unobserve(array, observerFn);
2922 };
2923 });
2924 };
2925
2926 /**
2927 * Creates an Observable sequence from changes to an object using Object.observe.
2928 * @param {Object} obj An object to observe changes.
2929 * @returns {Observable} An observable sequence containing changes to an object from Object.observe.
2930 */
2931 Observable.ofObjectChanges = function(obj) {
2932 if (obj == null) { throw new TypeError('object must not be null or undefined.'); }
2933 if (typeof Object.observe !== 'function' && typeof Object.unobserve !== 'function') { throw new TypeError('Object.observe is not supported on your platform') }
2934 return new AnonymousObservable(function(observer) {
2935 function observerFn(changes) {
2936 for(var i = 0, len = changes.length; i < len; i++) {
2937 observer.onNext(changes[i]);
2938 }
2939 }
2940
2941 Object.observe(obj, observerFn);
2942
2943 return function () {
2944 Object.unobserve(obj, observerFn);
2945 };
2946 });
2947 };
2948
2949 var NeverObservable = (function(__super__) {
2950 inherits(NeverObservable, __super__);
2951 function NeverObservable() {
2952 __super__.call(this);
2953 }
2954
2955 NeverObservable.prototype.subscribeCore = function (observer) {
2956 return disposableEmpty;
2957 };
2958
2959 return NeverObservable;
2960 }(ObservableBase));
2961
2962 var NEVER_OBSERVABLE = new NeverObservable();
2963
2964 /**
2965 * Returns a non-terminating observable sequence, which can be used to denote an infinite duration (e.g. when using reactive joins).
2966 * @returns {Observable} An observable sequence whose observers will never get called.
2967 */
2968 var observableNever = Observable.never = function () {
2969 return NEVER_OBSERVABLE;
2970 };
2971
2972 var PairsObservable = (function(__super__) {
2973 inherits(PairsObservable, __super__);
2974 function PairsObservable(o, scheduler) {
2975 this._o = o;
2976 this._keys = Object.keys(o);
2977 this._scheduler = scheduler;
2978 __super__.call(this);
2979 }
2980
2981 function scheduleMethod(o, obj, keys) {
2982 return function loopRecursive(i, recurse) {
2983 if (i < keys.length) {
2984 var key = keys[i];
2985 o.onNext([key, obj[key]]);
2986 recurse(i + 1);
2987 } else {
2988 o.onCompleted();
2989 }
2990 };
2991 }
2992
2993 PairsObservable.prototype.subscribeCore = function (o) {
2994 return this._scheduler.scheduleRecursive(0, scheduleMethod(o, this._o, this._keys));
2995 };
2996
2997 return PairsObservable;
2998 }(ObservableBase));
2999
3000 /**
3001 * Convert an object into an observable sequence of [key, value] pairs.
3002 * @param {Object} obj The object to inspect.
3003 * @param {Scheduler} [scheduler] Scheduler to run the enumeration of the input sequence on.
3004 * @returns {Observable} An observable sequence of [key, value] pairs from the object.
3005 */
3006 Observable.pairs = function (obj, scheduler) {
3007 scheduler || (scheduler = currentThreadScheduler);
3008 return new PairsObservable(obj, scheduler);
3009 };
3010
3011 var RangeObservable = (function(__super__) {
3012 inherits(RangeObservable, __super__);
3013 function RangeObservable(start, count, scheduler) {
3014 this.start = start;
3015 this.rangeCount = count;
3016 this.scheduler = scheduler;
3017 __super__.call(this);
3018 }
3019
3020 function loopRecursive(start, count, o) {
3021 return function loop (i, recurse) {
3022 if (i < count) {
3023 o.onNext(start + i);
3024 recurse(i + 1);
3025 } else {
3026 o.onCompleted();
3027 }
3028 };
3029 }
3030
3031 RangeObservable.prototype.subscribeCore = function (o) {
3032 return this.scheduler.scheduleRecursive(
3033 0,
3034 loopRecursive(this.start, this.rangeCount, o)
3035 );
3036 };
3037
3038 return RangeObservable;
3039 }(ObservableBase));
3040
3041 /**
3042 * Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to send out observer messages.
3043 * @param {Number} start The value of the first integer in the sequence.
3044 * @param {Number} count The number of sequential integers to generate.
3045 * @param {Scheduler} [scheduler] Scheduler to run the generator loop on. If not specified, defaults to Scheduler.currentThread.
3046 * @returns {Observable} An observable sequence that contains a range of sequential integral numbers.
3047 */
3048 Observable.range = function (start, count, scheduler) {
3049 isScheduler(scheduler) || (scheduler = currentThreadScheduler);
3050 return new RangeObservable(start, count, scheduler);
3051 };
3052
3053 var RepeatObservable = (function(__super__) {
3054 inherits(RepeatObservable, __super__);
3055 function RepeatObservable(value, repeatCount, scheduler) {
3056 this.value = value;
3057 this.repeatCount = repeatCount == null ? -1 : repeatCount;
3058 this.scheduler = scheduler;
3059 __super__.call(this);
3060 }
3061
3062 RepeatObservable.prototype.subscribeCore = function (observer) {
3063 var sink = new RepeatSink(observer, this);
3064 return sink.run();
3065 };
3066
3067 return RepeatObservable;
3068 }(ObservableBase));
3069
3070 function RepeatSink(observer, parent) {
3071 this.observer = observer;
3072 this.parent = parent;
3073 }
3074
3075 RepeatSink.prototype.run = function () {
3076 var observer = this.observer, value = this.parent.value;
3077 function loopRecursive(i, recurse) {
3078 if (i === -1 || i > 0) {
3079 observer.onNext(value);
3080 i > 0 && i--;
3081 }
3082 if (i === 0) { return observer.onCompleted(); }
3083 recurse(i);
3084 }
3085
3086 return this.parent.scheduler.scheduleRecursive(this.parent.repeatCount, loopRecursive);
3087 };
3088
3089 /**
3090 * Generates an observable sequence that repeats the given element the specified number of times, using the specified scheduler to send out observer messages.
3091 * @param {Mixed} value Element to repeat.
3092 * @param {Number} repeatCount [Optiona] Number of times to repeat the element. If not specified, repeats indefinitely.
3093 * @param {Scheduler} scheduler Scheduler to run the producer loop on. If not specified, defaults to Scheduler.immediate.
3094 * @returns {Observable} An observable sequence that repeats the given element the specified number of times.
3095 */
3096 Observable.repeat = function (value, repeatCount, scheduler) {
3097 isScheduler(scheduler) || (scheduler = currentThreadScheduler);
3098 return new RepeatObservable(value, repeatCount, scheduler);
3099 };
3100
3101 var JustObservable = (function(__super__) {
3102 inherits(JustObservable, __super__);
3103 function JustObservable(value, scheduler) {
3104 this._value = value;
3105 this._scheduler = scheduler;
3106 __super__.call(this);
3107 }
3108
3109 JustObservable.prototype.subscribeCore = function (o) {
3110 var state = [this._value, o];
3111 return this._scheduler === immediateScheduler ?
3112 scheduleItem(null, state) :
3113 this._scheduler.schedule(state, scheduleItem);
3114 };
3115
3116 function scheduleItem(s, state) {
3117 var value = state[0], observer = state[1];
3118 observer.onNext(value);
3119 observer.onCompleted();
3120 return disposableEmpty;
3121 }
3122
3123 return JustObservable;
3124 }(ObservableBase));
3125
3126 /**
3127 * Returns an observable sequence that contains a single element, using the specified scheduler to send out observer messages.
3128 * There is an alias called 'just' or browsers <IE9.
3129 * @param {Mixed} value Single element in the resulting observable sequence.
3130 * @param {Scheduler} scheduler Scheduler to send the single element on. If not specified, defaults to Scheduler.immediate.
3131 * @returns {Observable} An observable sequence containing the single specified element.
3132 */
3133 var observableReturn = Observable['return'] = Observable.just = function (value, scheduler) {
3134 isScheduler(scheduler) || (scheduler = immediateScheduler);
3135 return new JustObservable(value, scheduler);
3136 };
3137
3138 var ThrowObservable = (function(__super__) {
3139 inherits(ThrowObservable, __super__);
3140 function ThrowObservable(error, scheduler) {
3141 this._error = error;
3142 this._scheduler = scheduler;
3143 __super__.call(this);
3144 }
3145
3146 ThrowObservable.prototype.subscribeCore = function (o) {
3147 var state = [this._error, o];
3148 return this._scheduler === immediateScheduler ?
3149 scheduleItem(null, state) :
3150 this._scheduler.schedule(state, scheduleItem);
3151 };
3152
3153 function scheduleItem(s, state) {
3154 var e = state[0], o = state[1];
3155 o.onError(e);
3156 return disposableEmpty;
3157 }
3158
3159 return ThrowObservable;
3160 }(ObservableBase));
3161
3162 /**
3163 * Returns an observable sequence that terminates with an exception, using the specified scheduler to send out the single onError message.
3164 * There is an alias to this method called 'throwError' for browsers <IE9.
3165 * @param {Mixed} error An object used for the sequence's termination.
3166 * @param {Scheduler} scheduler Scheduler to send the exceptional termination call on. If not specified, defaults to Scheduler.immediate.
3167 * @returns {Observable} The observable sequence that terminates exceptionally with the specified exception object.
3168 */
3169 var observableThrow = Observable['throw'] = function (error, scheduler) {
3170 isScheduler(scheduler) || (scheduler = immediateScheduler);
3171 return new ThrowObservable(error, scheduler);
3172 };
3173
3174 var UsingObservable = (function (__super__) {
3175 inherits(UsingObservable, __super__);
3176 function UsingObservable(resFn, obsFn) {
3177 this._resFn = resFn;
3178 this._obsFn = obsFn;
3179 __super__.call(this);
3180 }
3181
3182 UsingObservable.prototype.subscribeCore = function (o) {
3183 var disposable = disposableEmpty;
3184 var resource = tryCatch(this._resFn)();
3185 if (resource === errorObj) {
3186 return new BinaryDisposable(observableThrow(resource.e).subscribe(o), disposable);
3187 }
3188 resource && (disposable = resource);
3189 var source = tryCatch(this._obsFn)(resource);
3190 if (source === errorObj) {
3191 return new BinaryDisposable(observableThrow(source.e).subscribe(o), disposable);
3192 }
3193 return new BinaryDisposable(source.subscribe(o), disposable);
3194 };
3195
3196 return UsingObservable;
3197 }(ObservableBase));
3198
3199 /**
3200 * Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime.
3201 * @param {Function} resourceFactory Factory function to obtain a resource object.
3202 * @param {Function} observableFactory Factory function to obtain an observable sequence that depends on the obtained resource.
3203 * @returns {Observable} An observable sequence whose lifetime controls the lifetime of the dependent resource object.
3204 */
3205 Observable.using = function (resourceFactory, observableFactory) {
3206 return new UsingObservable(resourceFactory, observableFactory);
3207 };
3208
3209 /**
3210 * Propagates the observable sequence or Promise that reacts first.
3211 * @param {Observable} rightSource Second observable sequence or Promise.
3212 * @returns {Observable} {Observable} An observable sequence that surfaces either of the given sequences, whichever reacted first.
3213 */
3214 observableProto.amb = function (rightSource) {
3215 var leftSource = this;
3216 return new AnonymousObservable(function (observer) {
3217 var choice,
3218 leftChoice = 'L', rightChoice = 'R',
3219 leftSubscription = new SingleAssignmentDisposable(),
3220 rightSubscription = new SingleAssignmentDisposable();
3221
3222 isPromise(rightSource) && (rightSource = observableFromPromise(rightSource));
3223
3224 function choiceL() {
3225 if (!choice) {
3226 choice = leftChoice;
3227 rightSubscription.dispose();
3228 }
3229 }
3230
3231 function choiceR() {
3232 if (!choice) {
3233 choice = rightChoice;
3234 leftSubscription.dispose();
3235 }
3236 }
3237
3238 var leftSubscribe = observerCreate(
3239 function (left) {
3240 choiceL();
3241 choice === leftChoice && observer.onNext(left);
3242 },
3243 function (e) {
3244 choiceL();
3245 choice === leftChoice && observer.onError(e);
3246 },
3247 function () {
3248 choiceL();
3249 choice === leftChoice && observer.onCompleted();
3250 }
3251 );
3252 var rightSubscribe = observerCreate(
3253 function (right) {
3254 choiceR();
3255 choice === rightChoice && observer.onNext(right);
3256 },
3257 function (e) {
3258 choiceR();
3259 choice === rightChoice && observer.onError(e);
3260 },
3261 function () {
3262 choiceR();
3263 choice === rightChoice && observer.onCompleted();
3264 }
3265 );
3266
3267 leftSubscription.setDisposable(leftSource.subscribe(leftSubscribe));
3268 rightSubscription.setDisposable(rightSource.subscribe(rightSubscribe));
3269
3270 return new BinaryDisposable(leftSubscription, rightSubscription);
3271 });
3272 };
3273
3274 function amb(p, c) { return p.amb(c); }
3275
3276 /**
3277 * Propagates the observable sequence or Promise that reacts first.
3278 * @returns {Observable} An observable sequence that surfaces any of the given sequences, whichever reacted first.
3279 */
3280 Observable.amb = function () {
3281 var acc = observableNever(), items;
3282 if (Array.isArray(arguments[0])) {
3283 items = arguments[0];
3284 } else {
3285 var len = arguments.length;
3286 items = new Array(items);
3287 for(var i = 0; i < len; i++) { items[i] = arguments[i]; }
3288 }
3289 for (var i = 0, len = items.length; i < len; i++) {
3290 acc = amb(acc, items[i]);
3291 }
3292 return acc;
3293 };
3294
3295 var CatchObservable = (function (__super__) {
3296 inherits(CatchObservable, __super__);
3297 function CatchObservable(source, fn) {
3298 this.source = source;
3299 this._fn = fn;
3300 __super__.call(this);
3301 }
3302
3303 CatchObservable.prototype.subscribeCore = function (o) {
3304 var d1 = new SingleAssignmentDisposable(), subscription = new SerialDisposable();
3305 subscription.setDisposable(d1);
3306 d1.setDisposable(this.source.subscribe(new CatchObserver(o, subscription, this._fn)));
3307 return subscription;
3308 };
3309
3310 return CatchObservable;
3311 }(ObservableBase));
3312
3313 var CatchObserver = (function(__super__) {
3314 inherits(CatchObserver, __super__);
3315 function CatchObserver(o, s, fn) {
3316 this._o = o;
3317 this._s = s;
3318 this._fn = fn;
3319 __super__.call(this);
3320 }
3321
3322 CatchObserver.prototype.next = function (x) { this._o.onNext(x); };
3323 CatchObserver.prototype.completed = function () { return this._o.onCompleted(); };
3324 CatchObserver.prototype.error = function (e) {
3325 var result = tryCatch(this._fn)(e);
3326 if (result === errorObj) { return this._o.onError(result.e); }
3327 isPromise(result) && (result = observableFromPromise(result));
3328
3329 var d = new SingleAssignmentDisposable();
3330 this._s.setDisposable(d);
3331 d.setDisposable(result.subscribe(this._o));
3332 };
3333
3334 return CatchObserver;
3335 }(AbstractObserver));
3336
3337 /**
3338 * Continues an observable sequence that is terminated by an exception with the next observable sequence.
3339 * @param {Mixed} handlerOrSecond Exception handler function that returns an observable sequence given the error that occurred in the first sequence, or a second observable sequence used to produce results when an error occurred in the first sequence.
3340 * @returns {Observable} An observable sequence containing the first sequence's elements, followed by the elements of the handler sequence in case an exception occurred.
3341 */
3342 observableProto['catch'] = function (handlerOrSecond) {
3343 return isFunction(handlerOrSecond) ? new CatchObservable(this, handlerOrSecond) : observableCatch([this, handlerOrSecond]);
3344 };
3345
3346 /**
3347 * Continues an observable sequence that is terminated by an exception with the next observable sequence.
3348 * @param {Array | Arguments} args Arguments or an array to use as the next sequence if an error occurs.
3349 * @returns {Observable} An observable sequence containing elements from consecutive source sequences until a source sequence terminates successfully.
3350 */
3351 var observableCatch = Observable['catch'] = function () {
3352 var items;
3353 if (Array.isArray(arguments[0])) {
3354 items = arguments[0];
3355 } else {
3356 var len = arguments.length;
3357 items = new Array(len);
3358 for(var i = 0; i < len; i++) { items[i] = arguments[i]; }
3359 }
3360 return enumerableOf(items).catchError();
3361 };
3362
3363 /**
3364 * Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences or Promises produces an element.
3365 * This can be in the form of an argument list of observables or an array.
3366 *
3367 * @example
3368 * 1 - obs = observable.combineLatest(obs1, obs2, obs3, function (o1, o2, o3) { return o1 + o2 + o3; });
3369 * 2 - obs = observable.combineLatest([obs1, obs2, obs3], function (o1, o2, o3) { return o1 + o2 + o3; });
3370 * @returns {Observable} An observable sequence containing the result of combining elements of the sources using the specified result selector function.
3371 */
3372 observableProto.combineLatest = function () {
3373 var len = arguments.length, args = new Array(len);
3374 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
3375 if (Array.isArray(args[0])) {
3376 args[0].unshift(this);
3377 } else {
3378 args.unshift(this);
3379 }
3380 return combineLatest.apply(this, args);
3381 };
3382
3383 function falseFactory() { return false; }
3384 function argumentsToArray() {
3385 var len = arguments.length, args = new Array(len);
3386 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
3387 return args;
3388 }
3389
3390 var CombineLatestObservable = (function(__super__) {
3391 inherits(CombineLatestObservable, __super__);
3392 function CombineLatestObservable(params, cb) {
3393 this._params = params;
3394 this._cb = cb;
3395 __super__.call(this);
3396 }
3397
3398 CombineLatestObservable.prototype.subscribeCore = function(observer) {
3399 var len = this._params.length,
3400 subscriptions = new Array(len);
3401
3402 var state = {
3403 hasValue: arrayInitialize(len, falseFactory),
3404 hasValueAll: false,
3405 isDone: arrayInitialize(len, falseFactory),
3406 values: new Array(len)
3407 };
3408
3409 for (var i = 0; i < len; i++) {
3410 var source = this._params[i], sad = new SingleAssignmentDisposable();
3411 subscriptions[i] = sad;
3412 isPromise(source) && (source = observableFromPromise(source));
3413 sad.setDisposable(source.subscribe(new CombineLatestObserver(observer, i, this._cb, state)));
3414 }
3415
3416 return new NAryDisposable(subscriptions);
3417 };
3418
3419 return CombineLatestObservable;
3420 }(ObservableBase));
3421
3422 var CombineLatestObserver = (function (__super__) {
3423 inherits(CombineLatestObserver, __super__);
3424 function CombineLatestObserver(o, i, cb, state) {
3425 this._o = o;
3426 this._i = i;
3427 this._cb = cb;
3428 this._state = state;
3429 __super__.call(this);
3430 }
3431
3432 function notTheSame(i) {
3433 return function (x, j) {
3434 return j !== i;
3435 };
3436 }
3437
3438 CombineLatestObserver.prototype.next = function (x) {
3439 this._state.values[this._i] = x;
3440 this._state.hasValue[this._i] = true;
3441 if (this._state.hasValueAll || (this._state.hasValueAll = this._state.hasValue.every(identity))) {
3442 var res = tryCatch(this._cb).apply(null, this._state.values);
3443 if (res === errorObj) { return this._o.onError(res.e); }
3444 this._o.onNext(res);
3445 } else if (this._state.isDone.filter(notTheSame(this._i)).every(identity)) {
3446 this._o.onCompleted();
3447 }
3448 };
3449
3450 CombineLatestObserver.prototype.error = function (e) {
3451 this._o.onError(e);
3452 };
3453
3454 CombineLatestObserver.prototype.completed = function () {
3455 this._state.isDone[this._i] = true;
3456 this._state.isDone.every(identity) && this._o.onCompleted();
3457 };
3458
3459 return CombineLatestObserver;
3460 }(AbstractObserver));
3461
3462 /**
3463 * Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences or Promises produces an element.
3464 *
3465 * @example
3466 * 1 - obs = Rx.Observable.combineLatest(obs1, obs2, obs3, function (o1, o2, o3) { return o1 + o2 + o3; });
3467 * 2 - obs = Rx.Observable.combineLatest([obs1, obs2, obs3], function (o1, o2, o3) { return o1 + o2 + o3; });
3468 * @returns {Observable} An observable sequence containing the result of combining elements of the sources using the specified result selector function.
3469 */
3470 var combineLatest = Observable.combineLatest = function () {
3471 var len = arguments.length, args = new Array(len);
3472 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
3473 var resultSelector = isFunction(args[len - 1]) ? args.pop() : argumentsToArray;
3474 Array.isArray(args[0]) && (args = args[0]);
3475 return new CombineLatestObservable(args, resultSelector);
3476 };
3477
3478 /**
3479 * Concatenates all the observable sequences. This takes in either an array or variable arguments to concatenate.
3480 * @returns {Observable} An observable sequence that contains the elements of each given sequence, in sequential order.
3481 */
3482 observableProto.concat = function () {
3483 for(var args = [], i = 0, len = arguments.length; i < len; i++) { args.push(arguments[i]); }
3484 args.unshift(this);
3485 return observableConcat.apply(null, args);
3486 };
3487
3488 var ConcatObserver = (function(__super__) {
3489 inherits(ConcatObserver, __super__);
3490 function ConcatObserver(s, fn) {
3491 this._s = s;
3492 this._fn = fn;
3493 __super__.call(this);
3494 }
3495
3496 ConcatObserver.prototype.next = function (x) { this._s.o.onNext(x); };
3497 ConcatObserver.prototype.error = function (e) { this._s.o.onError(e); };
3498 ConcatObserver.prototype.completed = function () { this._s.i++; this._fn(this._s); };
3499
3500 return ConcatObserver;
3501 }(AbstractObserver));
3502
3503 var ConcatObservable = (function(__super__) {
3504 inherits(ConcatObservable, __super__);
3505 function ConcatObservable(sources) {
3506 this._sources = sources;
3507 __super__.call(this);
3508 }
3509
3510 function scheduleRecursive (state, recurse) {
3511 if (state.disposable.isDisposed) { return; }
3512 if (state.i === state.sources.length) { return state.o.onCompleted(); }
3513
3514 // Check if promise
3515 var currentValue = state.sources[state.i];
3516 isPromise(currentValue) && (currentValue = observableFromPromise(currentValue));
3517
3518 var d = new SingleAssignmentDisposable();
3519 state.subscription.setDisposable(d);
3520 d.setDisposable(currentValue.subscribe(new ConcatObserver(state, recurse)));
3521 }
3522
3523 ConcatObservable.prototype.subscribeCore = function(o) {
3524 var subscription = new SerialDisposable();
3525 var disposable = disposableCreate(noop);
3526 var state = {
3527 o: o,
3528 i: 0,
3529 subscription: subscription,
3530 disposable: disposable,
3531 sources: this._sources
3532 };
3533
3534 var cancelable = immediateScheduler.scheduleRecursive(state, scheduleRecursive);
3535 return new NAryDisposable([subscription, disposable, cancelable]);
3536 };
3537
3538 return ConcatObservable;
3539 }(ObservableBase));
3540
3541 /**
3542 * Concatenates all the observable sequences.
3543 * @param {Array | Arguments} args Arguments or an array to concat to the observable sequence.
3544 * @returns {Observable} An observable sequence that contains the elements of each given sequence, in sequential order.
3545 */
3546 var observableConcat = Observable.concat = function () {
3547 var args;
3548 if (Array.isArray(arguments[0])) {
3549 args = arguments[0];
3550 } else {
3551 args = new Array(arguments.length);
3552 for(var i = 0, len = arguments.length; i < len; i++) { args[i] = arguments[i]; }
3553 }
3554 return new ConcatObservable(args);
3555 };
3556
3557 /**
3558 * Concatenates an observable sequence of observable sequences.
3559 * @returns {Observable} An observable sequence that contains the elements of each observed inner sequence, in sequential order.
3560 */
3561 observableProto.concatAll = function () {
3562 return this.merge(1);
3563 };
3564
3565 var MergeObservable = (function (__super__) {
3566 inherits(MergeObservable, __super__);
3567
3568 function MergeObservable(source, maxConcurrent) {
3569 this.source = source;
3570 this.maxConcurrent = maxConcurrent;
3571 __super__.call(this);
3572 }
3573
3574 MergeObservable.prototype.subscribeCore = function(observer) {
3575 var g = new CompositeDisposable();
3576 g.add(this.source.subscribe(new MergeObserver(observer, this.maxConcurrent, g)));
3577 return g;
3578 };
3579
3580 return MergeObservable;
3581
3582 }(ObservableBase));
3583
3584 var MergeObserver = (function (__super__) {
3585 function MergeObserver(o, max, g) {
3586 this.o = o;
3587 this.max = max;
3588 this.g = g;
3589 this.done = false;
3590 this.q = [];
3591 this.activeCount = 0;
3592 __super__.call(this);
3593 }
3594
3595 inherits(MergeObserver, __super__);
3596
3597 MergeObserver.prototype.handleSubscribe = function (xs) {
3598 var sad = new SingleAssignmentDisposable();
3599 this.g.add(sad);
3600 isPromise(xs) && (xs = observableFromPromise(xs));
3601 sad.setDisposable(xs.subscribe(new InnerObserver(this, sad)));
3602 };
3603
3604 MergeObserver.prototype.next = function (innerSource) {
3605 if(this.activeCount < this.max) {
3606 this.activeCount++;
3607 this.handleSubscribe(innerSource);
3608 } else {
3609 this.q.push(innerSource);
3610 }
3611 };
3612 MergeObserver.prototype.error = function (e) { this.o.onError(e); };
3613 MergeObserver.prototype.completed = function () { this.done = true; this.activeCount === 0 && this.o.onCompleted(); };
3614
3615 function InnerObserver(parent, sad) {
3616 this.parent = parent;
3617 this.sad = sad;
3618 __super__.call(this);
3619 }
3620
3621 inherits(InnerObserver, __super__);
3622
3623 InnerObserver.prototype.next = function (x) { this.parent.o.onNext(x); };
3624 InnerObserver.prototype.error = function (e) { this.parent.o.onError(e); };
3625 InnerObserver.prototype.completed = function () {
3626 this.parent.g.remove(this.sad);
3627 if (this.parent.q.length > 0) {
3628 this.parent.handleSubscribe(this.parent.q.shift());
3629 } else {
3630 this.parent.activeCount--;
3631 this.parent.done && this.parent.activeCount === 0 && this.parent.o.onCompleted();
3632 }
3633 };
3634
3635 return MergeObserver;
3636 }(AbstractObserver));
3637
3638 /**
3639 * Merges an observable sequence of observable sequences into an observable sequence, limiting the number of concurrent subscriptions to inner sequences.
3640 * Or merges two observable sequences into a single observable sequence.
3641 * @param {Mixed} [maxConcurrentOrOther] Maximum number of inner observable sequences being subscribed to concurrently or the second observable sequence.
3642 * @returns {Observable} The observable sequence that merges the elements of the inner sequences.
3643 */
3644 observableProto.merge = function (maxConcurrentOrOther) {
3645 return typeof maxConcurrentOrOther !== 'number' ?
3646 observableMerge(this, maxConcurrentOrOther) :
3647 new MergeObservable(this, maxConcurrentOrOther);
3648 };
3649
3650 /**
3651 * Merges all the observable sequences into a single observable sequence.
3652 * The scheduler is optional and if not specified, the immediate scheduler is used.
3653 * @returns {Observable} The observable sequence that merges the elements of the observable sequences.
3654 */
3655 var observableMerge = Observable.merge = function () {
3656 var scheduler, sources = [], i, len = arguments.length;
3657 if (!arguments[0]) {
3658 scheduler = immediateScheduler;
3659 for(i = 1; i < len; i++) { sources.push(arguments[i]); }
3660 } else if (isScheduler(arguments[0])) {
3661 scheduler = arguments[0];
3662 for(i = 1; i < len; i++) { sources.push(arguments[i]); }
3663 } else {
3664 scheduler = immediateScheduler;
3665 for(i = 0; i < len; i++) { sources.push(arguments[i]); }
3666 }
3667 if (Array.isArray(sources[0])) {
3668 sources = sources[0];
3669 }
3670 return observableOf(scheduler, sources).mergeAll();
3671 };
3672
3673 var MergeAllObservable = (function (__super__) {
3674 inherits(MergeAllObservable, __super__);
3675
3676 function MergeAllObservable(source) {
3677 this.source = source;
3678 __super__.call(this);
3679 }
3680
3681 MergeAllObservable.prototype.subscribeCore = function (o) {
3682 var g = new CompositeDisposable(), m = new SingleAssignmentDisposable();
3683 g.add(m);
3684 m.setDisposable(this.source.subscribe(new MergeAllObserver(o, g)));
3685 return g;
3686 };
3687
3688 return MergeAllObservable;
3689 }(ObservableBase));
3690
3691 var MergeAllObserver = (function (__super__) {
3692 function MergeAllObserver(o, g) {
3693 this.o = o;
3694 this.g = g;
3695 this.done = false;
3696 __super__.call(this);
3697 }
3698
3699 inherits(MergeAllObserver, __super__);
3700
3701 MergeAllObserver.prototype.next = function(innerSource) {
3702 var sad = new SingleAssignmentDisposable();
3703 this.g.add(sad);
3704 isPromise(innerSource) && (innerSource = observableFromPromise(innerSource));
3705 sad.setDisposable(innerSource.subscribe(new InnerObserver(this, sad)));
3706 };
3707
3708 MergeAllObserver.prototype.error = function (e) {
3709 this.o.onError(e);
3710 };
3711
3712 MergeAllObserver.prototype.completed = function () {
3713 this.done = true;
3714 this.g.length === 1 && this.o.onCompleted();
3715 };
3716
3717 function InnerObserver(parent, sad) {
3718 this.parent = parent;
3719 this.sad = sad;
3720 __super__.call(this);
3721 }
3722
3723 inherits(InnerObserver, __super__);
3724
3725 InnerObserver.prototype.next = function (x) {
3726 this.parent.o.onNext(x);
3727 };
3728 InnerObserver.prototype.error = function (e) {
3729 this.parent.o.onError(e);
3730 };
3731 InnerObserver.prototype.completed = function () {
3732 this.parent.g.remove(this.sad);
3733 this.parent.done && this.parent.g.length === 1 && this.parent.o.onCompleted();
3734 };
3735
3736 return MergeAllObserver;
3737 }(AbstractObserver));
3738
3739 /**
3740 * Merges an observable sequence of observable sequences into an observable sequence.
3741 * @returns {Observable} The observable sequence that merges the elements of the inner sequences.
3742 */
3743 observableProto.mergeAll = function () {
3744 return new MergeAllObservable(this);
3745 };
3746
3747 var CompositeError = Rx.CompositeError = function(errors) {
3748 this.innerErrors = errors;
3749 this.message = 'This contains multiple errors. Check the innerErrors';
3750 Error.call(this);
3751 };
3752 CompositeError.prototype = Object.create(Error.prototype);
3753 CompositeError.prototype.name = 'CompositeError';
3754
3755 var MergeDelayErrorObservable = (function(__super__) {
3756 inherits(MergeDelayErrorObservable, __super__);
3757 function MergeDelayErrorObservable(source) {
3758 this.source = source;
3759 __super__.call(this);
3760 }
3761
3762 MergeDelayErrorObservable.prototype.subscribeCore = function (o) {
3763 var group = new CompositeDisposable(),
3764 m = new SingleAssignmentDisposable(),
3765 state = { isStopped: false, errors: [], o: o };
3766
3767 group.add(m);
3768 m.setDisposable(this.source.subscribe(new MergeDelayErrorObserver(group, state)));
3769
3770 return group;
3771 };
3772
3773 return MergeDelayErrorObservable;
3774 }(ObservableBase));
3775
3776 var MergeDelayErrorObserver = (function(__super__) {
3777 inherits(MergeDelayErrorObserver, __super__);
3778 function MergeDelayErrorObserver(group, state) {
3779 this._group = group;
3780 this._state = state;
3781 __super__.call(this);
3782 }
3783
3784 function setCompletion(o, errors) {
3785 if (errors.length === 0) {
3786 o.onCompleted();
3787 } else if (errors.length === 1) {
3788 o.onError(errors[0]);
3789 } else {
3790 o.onError(new CompositeError(errors));
3791 }
3792 }
3793
3794 MergeDelayErrorObserver.prototype.next = function (x) {
3795 var inner = new SingleAssignmentDisposable();
3796 this._group.add(inner);
3797
3798 // Check for promises support
3799 isPromise(x) && (x = observableFromPromise(x));
3800 inner.setDisposable(x.subscribe(new InnerObserver(inner, this._group, this._state)));
3801 };
3802
3803 MergeDelayErrorObserver.prototype.error = function (e) {
3804 this._state.errors.push(e);
3805 this._state.isStopped = true;
3806 this._group.length === 1 && setCompletion(this._state.o, this._state.errors);
3807 };
3808
3809 MergeDelayErrorObserver.prototype.completed = function () {
3810 this._state.isStopped = true;
3811 this._group.length === 1 && setCompletion(this._state.o, this._state.errors);
3812 };
3813
3814 inherits(InnerObserver, __super__);
3815 function InnerObserver(inner, group, state) {
3816 this._inner = inner;
3817 this._group = group;
3818 this._state = state;
3819 __super__.call(this);
3820 }
3821
3822 InnerObserver.prototype.next = function (x) { this._state.o.onNext(x); };
3823 InnerObserver.prototype.error = function (e) {
3824 this._state.errors.push(e);
3825 this._group.remove(this._inner);
3826 this._state.isStopped && this._group.length === 1 && setCompletion(this._state.o, this._state.errors);
3827 };
3828 InnerObserver.prototype.completed = function () {
3829 this._group.remove(this._inner);
3830 this._state.isStopped && this._group.length === 1 && setCompletion(this._state.o, this._state.errors);
3831 };
3832
3833 return MergeDelayErrorObserver;
3834 }(AbstractObserver));
3835
3836 /**
3837 * Flattens an Observable that emits Observables into one Observable, in a way that allows an Observer to
3838 * receive all successfully emitted items from all of the source Observables without being interrupted by
3839 * an error notification from one of them.
3840 *
3841 * This behaves like Observable.prototype.mergeAll except that if any of the merged Observables notify of an
3842 * error via the Observer's onError, mergeDelayError will refrain from propagating that
3843 * error notification until all of the merged Observables have finished emitting items.
3844 * @param {Array | Arguments} args Arguments or an array to merge.
3845 * @returns {Observable} an Observable that emits all of the items emitted by the Observables emitted by the Observable
3846 */
3847 Observable.mergeDelayError = function() {
3848 var args;
3849 if (Array.isArray(arguments[0])) {
3850 args = arguments[0];
3851 } else {
3852 var len = arguments.length;
3853 args = new Array(len);
3854 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
3855 }
3856 var source = observableOf(null, args);
3857 return new MergeDelayErrorObservable(source);
3858 };
3859
3860 /**
3861 * Continues an observable sequence that is terminated normally or by an exception with the next observable sequence.
3862 * @param {Observable} second Second observable sequence used to produce results after the first sequence terminates.
3863 * @returns {Observable} An observable sequence that concatenates the first and second sequence, even if the first sequence terminates exceptionally.
3864 */
3865 observableProto.onErrorResumeNext = function (second) {
3866 if (!second) { throw new Error('Second observable is required'); }
3867 return onErrorResumeNext([this, second]);
3868 };
3869
3870 var OnErrorResumeNextObservable = (function(__super__) {
3871 inherits(OnErrorResumeNextObservable, __super__);
3872 function OnErrorResumeNextObservable(sources) {
3873 this.sources = sources;
3874 __super__.call(this);
3875 }
3876
3877 function scheduleMethod(state, recurse) {
3878 if (state.pos < state.sources.length) {
3879 var current = state.sources[state.pos++];
3880 isPromise(current) && (current = observableFromPromise(current));
3881 var d = new SingleAssignmentDisposable();
3882 state.subscription.setDisposable(d);
3883 d.setDisposable(current.subscribe(new OnErrorResumeNextObserver(state, recurse)));
3884 } else {
3885 state.o.onCompleted();
3886 }
3887 }
3888
3889 OnErrorResumeNextObservable.prototype.subscribeCore = function (o) {
3890 var subscription = new SerialDisposable(),
3891 state = {pos: 0, subscription: subscription, o: o, sources: this.sources },
3892 cancellable = immediateScheduler.scheduleRecursive(state, scheduleMethod);
3893
3894 return new BinaryDisposable(subscription, cancellable);
3895 };
3896
3897 return OnErrorResumeNextObservable;
3898 }(ObservableBase));
3899
3900 var OnErrorResumeNextObserver = (function(__super__) {
3901 inherits(OnErrorResumeNextObserver, __super__);
3902 function OnErrorResumeNextObserver(state, recurse) {
3903 this._state = state;
3904 this._recurse = recurse;
3905 __super__.call(this);
3906 }
3907
3908 OnErrorResumeNextObserver.prototype.next = function (x) { this._state.o.onNext(x); };
3909 OnErrorResumeNextObserver.prototype.error = function () { this._recurse(this._state); };
3910 OnErrorResumeNextObserver.prototype.completed = function () { this._recurse(this._state); };
3911
3912 return OnErrorResumeNextObserver;
3913 }(AbstractObserver));
3914
3915 /**
3916 * Continues an observable sequence that is terminated normally or by an exception with the next observable sequence.
3917 * @returns {Observable} An observable sequence that concatenates the source sequences, even if a sequence terminates exceptionally.
3918 */
3919 var onErrorResumeNext = Observable.onErrorResumeNext = function () {
3920 var sources = [];
3921 if (Array.isArray(arguments[0])) {
3922 sources = arguments[0];
3923 } else {
3924 var len = arguments.length;
3925 sources = new Array(len);
3926 for(var i = 0; i < len; i++) { sources[i] = arguments[i]; }
3927 }
3928 return new OnErrorResumeNextObservable(sources);
3929 };
3930
3931 var SkipUntilObservable = (function(__super__) {
3932 inherits(SkipUntilObservable, __super__);
3933
3934 function SkipUntilObservable(source, other) {
3935 this._s = source;
3936 this._o = isPromise(other) ? observableFromPromise(other) : other;
3937 this._open = false;
3938 __super__.call(this);
3939 }
3940
3941 SkipUntilObservable.prototype.subscribeCore = function(o) {
3942 var leftSubscription = new SingleAssignmentDisposable();
3943 leftSubscription.setDisposable(this._s.subscribe(new SkipUntilSourceObserver(o, this)));
3944
3945 isPromise(this._o) && (this._o = observableFromPromise(this._o));
3946
3947 var rightSubscription = new SingleAssignmentDisposable();
3948 rightSubscription.setDisposable(this._o.subscribe(new SkipUntilOtherObserver(o, this, rightSubscription)));
3949
3950 return new BinaryDisposable(leftSubscription, rightSubscription);
3951 };
3952
3953 return SkipUntilObservable;
3954 }(ObservableBase));
3955
3956 var SkipUntilSourceObserver = (function(__super__) {
3957 inherits(SkipUntilSourceObserver, __super__);
3958 function SkipUntilSourceObserver(o, p) {
3959 this._o = o;
3960 this._p = p;
3961 __super__.call(this);
3962 }
3963
3964 SkipUntilSourceObserver.prototype.next = function (x) {
3965 this._p._open && this._o.onNext(x);
3966 };
3967
3968 SkipUntilSourceObserver.prototype.error = function (err) {
3969 this._o.onError(err);
3970 };
3971
3972 SkipUntilSourceObserver.prototype.onCompleted = function () {
3973 this._p._open && this._o.onCompleted();
3974 };
3975
3976 return SkipUntilSourceObserver;
3977 }(AbstractObserver));
3978
3979 var SkipUntilOtherObserver = (function(__super__) {
3980 inherits(SkipUntilOtherObserver, __super__);
3981 function SkipUntilOtherObserver(o, p, r) {
3982 this._o = o;
3983 this._p = p;
3984 this._r = r;
3985 __super__.call(this);
3986 }
3987
3988 SkipUntilOtherObserver.prototype.next = function () {
3989 this._p._open = true;
3990 this._r.dispose();
3991 };
3992
3993 SkipUntilOtherObserver.prototype.error = function (err) {
3994 this._o.onError(err);
3995 };
3996
3997 SkipUntilOtherObserver.prototype.onCompleted = function () {
3998 this._r.dispose();
3999 };
4000
4001 return SkipUntilOtherObserver;
4002 }(AbstractObserver));
4003
4004 /**
4005 * Returns the values from the source observable sequence only after the other observable sequence produces a value.
4006 * @param {Observable | Promise} other The observable sequence or Promise that triggers propagation of elements of the source sequence.
4007 * @returns {Observable} An observable sequence containing the elements of the source sequence starting from the point the other sequence triggered propagation.
4008 */
4009 observableProto.skipUntil = function (other) {
4010 return new SkipUntilObservable(this, other);
4011 };
4012
4013 var SwitchObservable = (function(__super__) {
4014 inherits(SwitchObservable, __super__);
4015 function SwitchObservable(source) {
4016 this.source = source;
4017 __super__.call(this);
4018 }
4019
4020 SwitchObservable.prototype.subscribeCore = function (o) {
4021 var inner = new SerialDisposable(), s = this.source.subscribe(new SwitchObserver(o, inner));
4022 return new BinaryDisposable(s, inner);
4023 };
4024
4025 inherits(SwitchObserver, AbstractObserver);
4026 function SwitchObserver(o, inner) {
4027 this.o = o;
4028 this.inner = inner;
4029 this.stopped = false;
4030 this.latest = 0;
4031 this.hasLatest = false;
4032 AbstractObserver.call(this);
4033 }
4034
4035 SwitchObserver.prototype.next = function (innerSource) {
4036 var d = new SingleAssignmentDisposable(), id = ++this.latest;
4037 this.hasLatest = true;
4038 this.inner.setDisposable(d);
4039 isPromise(innerSource) && (innerSource = observableFromPromise(innerSource));
4040 d.setDisposable(innerSource.subscribe(new InnerObserver(this, id)));
4041 };
4042
4043 SwitchObserver.prototype.error = function (e) {
4044 this.o.onError(e);
4045 };
4046
4047 SwitchObserver.prototype.completed = function () {
4048 this.stopped = true;
4049 !this.hasLatest && this.o.onCompleted();
4050 };
4051
4052 inherits(InnerObserver, AbstractObserver);
4053 function InnerObserver(parent, id) {
4054 this.parent = parent;
4055 this.id = id;
4056 AbstractObserver.call(this);
4057 }
4058 InnerObserver.prototype.next = function (x) {
4059 this.parent.latest === this.id && this.parent.o.onNext(x);
4060 };
4061
4062 InnerObserver.prototype.error = function (e) {
4063 this.parent.latest === this.id && this.parent.o.onError(e);
4064 };
4065
4066 InnerObserver.prototype.completed = function () {
4067 if (this.parent.latest === this.id) {
4068 this.parent.hasLatest = false;
4069 this.parent.stopped && this.parent.o.onCompleted();
4070 }
4071 };
4072
4073 return SwitchObservable;
4074 }(ObservableBase));
4075
4076 /**
4077 * Transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.
4078 * @returns {Observable} The observable sequence that at any point in time produces the elements of the most recent inner observable sequence that has been received.
4079 */
4080 observableProto['switch'] = observableProto.switchLatest = function () {
4081 return new SwitchObservable(this);
4082 };
4083
4084 var TakeUntilObservable = (function(__super__) {
4085 inherits(TakeUntilObservable, __super__);
4086
4087 function TakeUntilObservable(source, other) {
4088 this.source = source;
4089 this.other = isPromise(other) ? observableFromPromise(other) : other;
4090 __super__.call(this);
4091 }
4092
4093 TakeUntilObservable.prototype.subscribeCore = function(o) {
4094 return new BinaryDisposable(
4095 this.source.subscribe(o),
4096 this.other.subscribe(new TakeUntilObserver(o))
4097 );
4098 };
4099
4100 return TakeUntilObservable;
4101 }(ObservableBase));
4102
4103 var TakeUntilObserver = (function(__super__) {
4104 inherits(TakeUntilObserver, __super__);
4105 function TakeUntilObserver(o) {
4106 this._o = o;
4107 __super__.call(this);
4108 }
4109
4110 TakeUntilObserver.prototype.next = function () {
4111 this._o.onCompleted();
4112 };
4113
4114 TakeUntilObserver.prototype.error = function (err) {
4115 this._o.onError(err);
4116 };
4117
4118 TakeUntilObserver.prototype.onCompleted = noop;
4119
4120 return TakeUntilObserver;
4121 }(AbstractObserver));
4122
4123 /**
4124 * Returns the values from the source observable sequence until the other observable sequence produces a value.
4125 * @param {Observable | Promise} other Observable sequence or Promise that terminates propagation of elements of the source sequence.
4126 * @returns {Observable} An observable sequence containing the elements of the source sequence up to the point the other sequence interrupted further propagation.
4127 */
4128 observableProto.takeUntil = function (other) {
4129 return new TakeUntilObservable(this, other);
4130 };
4131
4132 function falseFactory() { return false; }
4133 function argumentsToArray() {
4134 var len = arguments.length, args = new Array(len);
4135 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
4136 return args;
4137 }
4138
4139 var WithLatestFromObservable = (function(__super__) {
4140 inherits(WithLatestFromObservable, __super__);
4141 function WithLatestFromObservable(source, sources, resultSelector) {
4142 this._s = source;
4143 this._ss = sources;
4144 this._cb = resultSelector;
4145 __super__.call(this);
4146 }
4147
4148 WithLatestFromObservable.prototype.subscribeCore = function (o) {
4149 var len = this._ss.length;
4150 var state = {
4151 hasValue: arrayInitialize(len, falseFactory),
4152 hasValueAll: false,
4153 values: new Array(len)
4154 };
4155
4156 var n = this._ss.length, subscriptions = new Array(n + 1);
4157 for (var i = 0; i < n; i++) {
4158 var other = this._ss[i], sad = new SingleAssignmentDisposable();
4159 isPromise(other) && (other = observableFromPromise(other));
4160 sad.setDisposable(other.subscribe(new WithLatestFromOtherObserver(o, i, state)));
4161 subscriptions[i] = sad;
4162 }
4163
4164 var outerSad = new SingleAssignmentDisposable();
4165 outerSad.setDisposable(this._s.subscribe(new WithLatestFromSourceObserver(o, this._cb, state)));
4166 subscriptions[n] = outerSad;
4167
4168 return new NAryDisposable(subscriptions);
4169 };
4170
4171 return WithLatestFromObservable;
4172 }(ObservableBase));
4173
4174 var WithLatestFromOtherObserver = (function (__super__) {
4175 inherits(WithLatestFromOtherObserver, __super__);
4176 function WithLatestFromOtherObserver(o, i, state) {
4177 this._o = o;
4178 this._i = i;
4179 this._state = state;
4180 __super__.call(this);
4181 }
4182
4183 WithLatestFromOtherObserver.prototype.next = function (x) {
4184 this._state.values[this._i] = x;
4185 this._state.hasValue[this._i] = true;
4186 this._state.hasValueAll = this._state.hasValue.every(identity);
4187 };
4188
4189 WithLatestFromOtherObserver.prototype.error = function (e) {
4190 this._o.onError(e);
4191 };
4192
4193 WithLatestFromOtherObserver.prototype.completed = noop;
4194
4195 return WithLatestFromOtherObserver;
4196 }(AbstractObserver));
4197
4198 var WithLatestFromSourceObserver = (function (__super__) {
4199 inherits(WithLatestFromSourceObserver, __super__);
4200 function WithLatestFromSourceObserver(o, cb, state) {
4201 this._o = o;
4202 this._cb = cb;
4203 this._state = state;
4204 __super__.call(this);
4205 }
4206
4207 WithLatestFromSourceObserver.prototype.next = function (x) {
4208 var allValues = [x].concat(this._state.values);
4209 if (!this._state.hasValueAll) { return; }
4210 var res = tryCatch(this._cb).apply(null, allValues);
4211 if (res === errorObj) { return this._o.onError(res.e); }
4212 this._o.onNext(res);
4213 };
4214
4215 WithLatestFromSourceObserver.prototype.error = function (e) {
4216 this._o.onError(e);
4217 };
4218
4219 WithLatestFromSourceObserver.prototype.completed = function () {
4220 this._o.onCompleted();
4221 };
4222
4223 return WithLatestFromSourceObserver;
4224 }(AbstractObserver));
4225
4226 /**
4227 * Merges the specified observable sequences into one observable sequence by using the selector function only when the (first) source observable sequence produces an element.
4228 * @returns {Observable} An observable sequence containing the result of combining elements of the sources using the specified result selector function.
4229 */
4230 observableProto.withLatestFrom = function () {
4231 if (arguments.length === 0) { throw new Error('invalid arguments'); }
4232
4233 var len = arguments.length, args = new Array(len);
4234 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
4235 var resultSelector = isFunction(args[len - 1]) ? args.pop() : argumentsToArray;
4236 Array.isArray(args[0]) && (args = args[0]);
4237
4238 return new WithLatestFromObservable(this, args, resultSelector);
4239 };
4240
4241 function falseFactory() { return false; }
4242 function emptyArrayFactory() { return []; }
4243
4244 var ZipObservable = (function(__super__) {
4245 inherits(ZipObservable, __super__);
4246 function ZipObservable(sources, resultSelector) {
4247 this._s = sources;
4248 this._cb = resultSelector;
4249 __super__.call(this);
4250 }
4251
4252 ZipObservable.prototype.subscribeCore = function(observer) {
4253 var n = this._s.length,
4254 subscriptions = new Array(n),
4255 done = arrayInitialize(n, falseFactory),
4256 q = arrayInitialize(n, emptyArrayFactory);
4257
4258 for (var i = 0; i < n; i++) {
4259 var source = this._s[i], sad = new SingleAssignmentDisposable();
4260 subscriptions[i] = sad;
4261 isPromise(source) && (source = observableFromPromise(source));
4262 sad.setDisposable(source.subscribe(new ZipObserver(observer, i, this, q, done)));
4263 }
4264
4265 return new NAryDisposable(subscriptions);
4266 };
4267
4268 return ZipObservable;
4269 }(ObservableBase));
4270
4271 var ZipObserver = (function (__super__) {
4272 inherits(ZipObserver, __super__);
4273 function ZipObserver(o, i, p, q, d) {
4274 this._o = o;
4275 this._i = i;
4276 this._p = p;
4277 this._q = q;
4278 this._d = d;
4279 __super__.call(this);
4280 }
4281
4282 function notEmpty(x) { return x.length > 0; }
4283 function shiftEach(x) { return x.shift(); }
4284 function notTheSame(i) {
4285 return function (x, j) {
4286 return j !== i;
4287 };
4288 }
4289
4290 ZipObserver.prototype.next = function (x) {
4291 this._q[this._i].push(x);
4292 if (this._q.every(notEmpty)) {
4293 var queuedValues = this._q.map(shiftEach);
4294 var res = tryCatch(this._p._cb).apply(null, queuedValues);
4295 if (res === errorObj) { return this._o.onError(res.e); }
4296 this._o.onNext(res);
4297 } else if (this._d.filter(notTheSame(this._i)).every(identity)) {
4298 this._o.onCompleted();
4299 }
4300 };
4301
4302 ZipObserver.prototype.error = function (e) {
4303 this._o.onError(e);
4304 };
4305
4306 ZipObserver.prototype.completed = function () {
4307 this._d[this._i] = true;
4308 this._d.every(identity) && this._o.onCompleted();
4309 };
4310
4311 return ZipObserver;
4312 }(AbstractObserver));
4313
4314 /**
4315 * Merges the specified observable sequences into one observable sequence by using the selector function whenever all of the observable sequences or an array have produced an element at a corresponding index.
4316 * The last element in the arguments must be a function to invoke for each series of elements at corresponding indexes in the args.
4317 * @returns {Observable} An observable sequence containing the result of combining elements of the args using the specified result selector function.
4318 */
4319 observableProto.zip = function () {
4320 if (arguments.length === 0) { throw new Error('invalid arguments'); }
4321
4322 var len = arguments.length, args = new Array(len);
4323 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
4324 var resultSelector = isFunction(args[len - 1]) ? args.pop() : argumentsToArray;
4325 Array.isArray(args[0]) && (args = args[0]);
4326
4327 var parent = this;
4328 args.unshift(parent);
4329
4330 return new ZipObservable(args, resultSelector);
4331 };
4332
4333 /**
4334 * Merges the specified observable sequences into one observable sequence by using the selector function whenever all of the observable sequences have produced an element at a corresponding index.
4335 * @param arguments Observable sources.
4336 * @param {Function} resultSelector Function to invoke for each series of elements at corresponding indexes in the sources.
4337 * @returns {Observable} An observable sequence containing the result of combining elements of the sources using the specified result selector function.
4338 */
4339 Observable.zip = function () {
4340 var len = arguments.length, args = new Array(len);
4341 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
4342 if (Array.isArray(args[0])) {
4343 args = isFunction(args[1]) ? args[0].concat(args[1]) : args[0];
4344 }
4345 var first = args.shift();
4346 return first.zip.apply(first, args);
4347 };
4348
4349function falseFactory() { return false; }
4350function emptyArrayFactory() { return []; }
4351function argumentsToArray() {
4352 var len = arguments.length, args = new Array(len);
4353 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
4354 return args;
4355}
4356
4357var ZipIterableObservable = (function(__super__) {
4358 inherits(ZipIterableObservable, __super__);
4359 function ZipIterableObservable(sources, cb) {
4360 this.sources = sources;
4361 this._cb = cb;
4362 __super__.call(this);
4363 }
4364
4365 ZipIterableObservable.prototype.subscribeCore = function (o) {
4366 var sources = this.sources, len = sources.length, subscriptions = new Array(len);
4367
4368 var state = {
4369 q: arrayInitialize(len, emptyArrayFactory),
4370 done: arrayInitialize(len, falseFactory),
4371 cb: this._cb,
4372 o: o
4373 };
4374
4375 for (var i = 0; i < len; i++) {
4376 (function (i) {
4377 var source = sources[i], sad = new SingleAssignmentDisposable();
4378 (isArrayLike(source) || isIterable(source)) && (source = observableFrom(source));
4379
4380 subscriptions[i] = sad;
4381 sad.setDisposable(source.subscribe(new ZipIterableObserver(state, i)));
4382 }(i));
4383 }
4384
4385 return new NAryDisposable(subscriptions);
4386 };
4387
4388 return ZipIterableObservable;
4389}(ObservableBase));
4390
4391var ZipIterableObserver = (function (__super__) {
4392 inherits(ZipIterableObserver, __super__);
4393 function ZipIterableObserver(s, i) {
4394 this._s = s;
4395 this._i = i;
4396 __super__.call(this);
4397 }
4398
4399 function notEmpty(x) { return x.length > 0; }
4400 function shiftEach(x) { return x.shift(); }
4401 function notTheSame(i) {
4402 return function (x, j) {
4403 return j !== i;
4404 };
4405 }
4406
4407 ZipIterableObserver.prototype.next = function (x) {
4408 this._s.q[this._i].push(x);
4409 if (this._s.q.every(notEmpty)) {
4410 var queuedValues = this._s.q.map(shiftEach),
4411 res = tryCatch(this._s.cb).apply(null, queuedValues);
4412 if (res === errorObj) { return this._s.o.onError(res.e); }
4413 this._s.o.onNext(res);
4414 } else if (this._s.done.filter(notTheSame(this._i)).every(identity)) {
4415 this._s.o.onCompleted();
4416 }
4417 };
4418
4419 ZipIterableObserver.prototype.error = function (e) { this._s.o.onError(e); };
4420
4421 ZipIterableObserver.prototype.completed = function () {
4422 this._s.done[this._i] = true;
4423 this._s.done.every(identity) && this._s.o.onCompleted();
4424 };
4425
4426 return ZipIterableObserver;
4427}(AbstractObserver));
4428
4429/**
4430 * Merges the specified observable sequences into one observable sequence by using the selector function whenever all of the observable sequences or an array have produced an element at a corresponding index.
4431 * The last element in the arguments must be a function to invoke for each series of elements at corresponding indexes in the args.
4432 * @returns {Observable} An observable sequence containing the result of combining elements of the args using the specified result selector function.
4433 */
4434observableProto.zipIterable = function () {
4435 if (arguments.length === 0) { throw new Error('invalid arguments'); }
4436
4437 var len = arguments.length, args = new Array(len);
4438 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
4439 var resultSelector = isFunction(args[len - 1]) ? args.pop() : argumentsToArray;
4440
4441 var parent = this;
4442 args.unshift(parent);
4443 return new ZipIterableObservable(args, resultSelector);
4444};
4445
4446 function asObservable(source) {
4447 return function subscribe(o) { return source.subscribe(o); };
4448 }
4449
4450 /**
4451 * Hides the identity of an observable sequence.
4452 * @returns {Observable} An observable sequence that hides the identity of the source sequence.
4453 */
4454 observableProto.asObservable = function () {
4455 return new AnonymousObservable(asObservable(this), this);
4456 };
4457
4458 function toArray(x) { return x.toArray(); }
4459 function notEmpty(x) { return x.length > 0; }
4460
4461 /**
4462 * Projects each element of an observable sequence into zero or more buffers which are produced based on element count information.
4463 * @param {Number} count Length of each buffer.
4464 * @param {Number} [skip] Number of elements to skip between creation of consecutive buffers. If not provided, defaults to the count.
4465 * @returns {Observable} An observable sequence of buffers.
4466 */
4467 observableProto.bufferWithCount = function (count, skip) {
4468 typeof skip !== 'number' && (skip = count);
4469 return this.windowWithCount(count, skip)
4470 .flatMap(toArray)
4471 .filter(notEmpty);
4472 };
4473
4474 var DematerializeObservable = (function (__super__) {
4475 inherits(DematerializeObservable, __super__);
4476 function DematerializeObservable(source) {
4477 this.source = source;
4478 __super__.call(this);
4479 }
4480
4481 DematerializeObservable.prototype.subscribeCore = function (o) {
4482 return this.source.subscribe(new DematerializeObserver(o));
4483 };
4484
4485 return DematerializeObservable;
4486 }(ObservableBase));
4487
4488 var DematerializeObserver = (function (__super__) {
4489 inherits(DematerializeObserver, __super__);
4490
4491 function DematerializeObserver(o) {
4492 this._o = o;
4493 __super__.call(this);
4494 }
4495
4496 DematerializeObserver.prototype.next = function (x) { x.accept(this._o); };
4497 DematerializeObserver.prototype.error = function (e) { this._o.onError(e); };
4498 DematerializeObserver.prototype.completed = function () { this._o.onCompleted(); };
4499
4500 return DematerializeObserver;
4501 }(AbstractObserver));
4502
4503 /**
4504 * Dematerializes the explicit notification values of an observable sequence as implicit notifications.
4505 * @returns {Observable} An observable sequence exhibiting the behavior corresponding to the source sequence's notification values.
4506 */
4507 observableProto.dematerialize = function () {
4508 return new DematerializeObservable(this);
4509 };
4510
4511 var DistinctUntilChangedObservable = (function(__super__) {
4512 inherits(DistinctUntilChangedObservable, __super__);
4513 function DistinctUntilChangedObservable(source, keyFn, comparer) {
4514 this.source = source;
4515 this.keyFn = keyFn;
4516 this.comparer = comparer;
4517 __super__.call(this);
4518 }
4519
4520 DistinctUntilChangedObservable.prototype.subscribeCore = function (o) {
4521 return this.source.subscribe(new DistinctUntilChangedObserver(o, this.keyFn, this.comparer));
4522 };
4523
4524 return DistinctUntilChangedObservable;
4525 }(ObservableBase));
4526
4527 var DistinctUntilChangedObserver = (function(__super__) {
4528 inherits(DistinctUntilChangedObserver, __super__);
4529 function DistinctUntilChangedObserver(o, keyFn, comparer) {
4530 this.o = o;
4531 this.keyFn = keyFn;
4532 this.comparer = comparer;
4533 this.hasCurrentKey = false;
4534 this.currentKey = null;
4535 __super__.call(this);
4536 }
4537
4538 DistinctUntilChangedObserver.prototype.next = function (x) {
4539 var key = x, comparerEquals;
4540 if (isFunction(this.keyFn)) {
4541 key = tryCatch(this.keyFn)(x);
4542 if (key === errorObj) { return this.o.onError(key.e); }
4543 }
4544 if (this.hasCurrentKey) {
4545 comparerEquals = tryCatch(this.comparer)(this.currentKey, key);
4546 if (comparerEquals === errorObj) { return this.o.onError(comparerEquals.e); }
4547 }
4548 if (!this.hasCurrentKey || !comparerEquals) {
4549 this.hasCurrentKey = true;
4550 this.currentKey = key;
4551 this.o.onNext(x);
4552 }
4553 };
4554 DistinctUntilChangedObserver.prototype.error = function(e) {
4555 this.o.onError(e);
4556 };
4557 DistinctUntilChangedObserver.prototype.completed = function () {
4558 this.o.onCompleted();
4559 };
4560
4561 return DistinctUntilChangedObserver;
4562 }(AbstractObserver));
4563
4564 /**
4565 * Returns an observable sequence that contains only distinct contiguous elements according to the keyFn and the comparer.
4566 * @param {Function} [keyFn] A function to compute the comparison key for each element. If not provided, it projects the value.
4567 * @param {Function} [comparer] Equality comparer for computed key values. If not provided, defaults to an equality comparer function.
4568 * @returns {Observable} An observable sequence only containing the distinct contiguous elements, based on a computed key value, from the source sequence.
4569 */
4570 observableProto.distinctUntilChanged = function (keyFn, comparer) {
4571 comparer || (comparer = defaultComparer);
4572 return new DistinctUntilChangedObservable(this, keyFn, comparer);
4573 };
4574
4575 var TapObservable = (function(__super__) {
4576 inherits(TapObservable,__super__);
4577 function TapObservable(source, observerOrOnNext, onError, onCompleted) {
4578 this.source = source;
4579 this._oN = observerOrOnNext;
4580 this._oE = onError;
4581 this._oC = onCompleted;
4582 __super__.call(this);
4583 }
4584
4585 TapObservable.prototype.subscribeCore = function(o) {
4586 return this.source.subscribe(new InnerObserver(o, this));
4587 };
4588
4589 inherits(InnerObserver, AbstractObserver);
4590 function InnerObserver(o, p) {
4591 this.o = o;
4592 this.t = !p._oN || isFunction(p._oN) ?
4593 observerCreate(p._oN || noop, p._oE || noop, p._oC || noop) :
4594 p._oN;
4595 this.isStopped = false;
4596 AbstractObserver.call(this);
4597 }
4598 InnerObserver.prototype.next = function(x) {
4599 var res = tryCatch(this.t.onNext).call(this.t, x);
4600 if (res === errorObj) { this.o.onError(res.e); }
4601 this.o.onNext(x);
4602 };
4603 InnerObserver.prototype.error = function(err) {
4604 var res = tryCatch(this.t.onError).call(this.t, err);
4605 if (res === errorObj) { return this.o.onError(res.e); }
4606 this.o.onError(err);
4607 };
4608 InnerObserver.prototype.completed = function() {
4609 var res = tryCatch(this.t.onCompleted).call(this.t);
4610 if (res === errorObj) { return this.o.onError(res.e); }
4611 this.o.onCompleted();
4612 };
4613
4614 return TapObservable;
4615 }(ObservableBase));
4616
4617 /**
4618 * Invokes an action for each element in the observable sequence and invokes an action upon graceful or exceptional termination of the observable sequence.
4619 * This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
4620 * @param {Function | Observer} observerOrOnNext Action to invoke for each element in the observable sequence or an o.
4621 * @param {Function} [onError] Action to invoke upon exceptional termination of the observable sequence. Used if only the observerOrOnNext parameter is also a function.
4622 * @param {Function} [onCompleted] Action to invoke upon graceful termination of the observable sequence. Used if only the observerOrOnNext parameter is also a function.
4623 * @returns {Observable} The source sequence with the side-effecting behavior applied.
4624 */
4625 observableProto['do'] = observableProto.tap = observableProto.doAction = function (observerOrOnNext, onError, onCompleted) {
4626 return new TapObservable(this, observerOrOnNext, onError, onCompleted);
4627 };
4628
4629 /**
4630 * Invokes an action for each element in the observable sequence.
4631 * This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
4632 * @param {Function} onNext Action to invoke for each element in the observable sequence.
4633 * @param {Any} [thisArg] Object to use as this when executing callback.
4634 * @returns {Observable} The source sequence with the side-effecting behavior applied.
4635 */
4636 observableProto.doOnNext = observableProto.tapOnNext = function (onNext, thisArg) {
4637 return this.tap(typeof thisArg !== 'undefined' ? function (x) { onNext.call(thisArg, x); } : onNext);
4638 };
4639
4640 /**
4641 * Invokes an action upon exceptional termination of the observable sequence.
4642 * This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
4643 * @param {Function} onError Action to invoke upon exceptional termination of the observable sequence.
4644 * @param {Any} [thisArg] Object to use as this when executing callback.
4645 * @returns {Observable} The source sequence with the side-effecting behavior applied.
4646 */
4647 observableProto.doOnError = observableProto.tapOnError = function (onError, thisArg) {
4648 return this.tap(noop, typeof thisArg !== 'undefined' ? function (e) { onError.call(thisArg, e); } : onError);
4649 };
4650
4651 /**
4652 * Invokes an action upon graceful termination of the observable sequence.
4653 * This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
4654 * @param {Function} onCompleted Action to invoke upon graceful termination of the observable sequence.
4655 * @param {Any} [thisArg] Object to use as this when executing callback.
4656 * @returns {Observable} The source sequence with the side-effecting behavior applied.
4657 */
4658 observableProto.doOnCompleted = observableProto.tapOnCompleted = function (onCompleted, thisArg) {
4659 return this.tap(noop, null, typeof thisArg !== 'undefined' ? function () { onCompleted.call(thisArg); } : onCompleted);
4660 };
4661
4662 var FinallyObservable = (function (__super__) {
4663 inherits(FinallyObservable, __super__);
4664 function FinallyObservable(source, fn, thisArg) {
4665 this.source = source;
4666 this._fn = bindCallback(fn, thisArg, 0);
4667 __super__.call(this);
4668 }
4669
4670 FinallyObservable.prototype.subscribeCore = function (o) {
4671 var d = tryCatch(this.source.subscribe).call(this.source, o);
4672 if (d === errorObj) {
4673 this._fn();
4674 thrower(d.e);
4675 }
4676
4677 return new FinallyDisposable(d, this._fn);
4678 };
4679
4680 function FinallyDisposable(s, fn) {
4681 this.isDisposed = false;
4682 this._s = s;
4683 this._fn = fn;
4684 }
4685 FinallyDisposable.prototype.dispose = function () {
4686 if (!this.isDisposed) {
4687 var res = tryCatch(this._s.dispose).call(this._s);
4688 this._fn();
4689 res === errorObj && thrower(res.e);
4690 }
4691 };
4692
4693 return FinallyObservable;
4694
4695 }(ObservableBase));
4696
4697 /**
4698 * Invokes a specified action after the source observable sequence terminates gracefully or exceptionally.
4699 * @param {Function} finallyAction Action to invoke after the source observable sequence terminates.
4700 * @returns {Observable} Source sequence with the action-invoking termination behavior applied.
4701 */
4702 observableProto['finally'] = function (action, thisArg) {
4703 return new FinallyObservable(this, action, thisArg);
4704 };
4705
4706 var IgnoreElementsObservable = (function(__super__) {
4707 inherits(IgnoreElementsObservable, __super__);
4708
4709 function IgnoreElementsObservable(source) {
4710 this.source = source;
4711 __super__.call(this);
4712 }
4713
4714 IgnoreElementsObservable.prototype.subscribeCore = function (o) {
4715 return this.source.subscribe(new InnerObserver(o));
4716 };
4717
4718 function InnerObserver(o) {
4719 this.o = o;
4720 this.isStopped = false;
4721 }
4722 InnerObserver.prototype.onNext = noop;
4723 InnerObserver.prototype.onError = function (err) {
4724 if(!this.isStopped) {
4725 this.isStopped = true;
4726 this.o.onError(err);
4727 }
4728 };
4729 InnerObserver.prototype.onCompleted = function () {
4730 if(!this.isStopped) {
4731 this.isStopped = true;
4732 this.o.onCompleted();
4733 }
4734 };
4735 InnerObserver.prototype.dispose = function() { this.isStopped = true; };
4736 InnerObserver.prototype.fail = function (e) {
4737 if (!this.isStopped) {
4738 this.isStopped = true;
4739 this.observer.onError(e);
4740 return true;
4741 }
4742
4743 return false;
4744 };
4745
4746 return IgnoreElementsObservable;
4747 }(ObservableBase));
4748
4749 /**
4750 * Ignores all elements in an observable sequence leaving only the termination messages.
4751 * @returns {Observable} An empty observable sequence that signals termination, successful or exceptional, of the source sequence.
4752 */
4753 observableProto.ignoreElements = function () {
4754 return new IgnoreElementsObservable(this);
4755 };
4756
4757 var MaterializeObservable = (function (__super__) {
4758 inherits(MaterializeObservable, __super__);
4759 function MaterializeObservable(source, fn) {
4760 this.source = source;
4761 __super__.call(this);
4762 }
4763
4764 MaterializeObservable.prototype.subscribeCore = function (o) {
4765 return this.source.subscribe(new MaterializeObserver(o));
4766 };
4767
4768 return MaterializeObservable;
4769 }(ObservableBase));
4770
4771 var MaterializeObserver = (function (__super__) {
4772 inherits(MaterializeObserver, __super__);
4773
4774 function MaterializeObserver(o) {
4775 this._o = o;
4776 __super__.call(this);
4777 }
4778
4779 MaterializeObserver.prototype.next = function (x) { this._o.onNext(notificationCreateOnNext(x)) };
4780 MaterializeObserver.prototype.error = function (e) { this._o.onNext(notificationCreateOnError(e)); this._o.onCompleted(); };
4781 MaterializeObserver.prototype.completed = function () { this._o.onNext(notificationCreateOnCompleted()); this._o.onCompleted(); };
4782
4783 return MaterializeObserver;
4784 }(AbstractObserver));
4785
4786 /**
4787 * Materializes the implicit notifications of an observable sequence as explicit notification values.
4788 * @returns {Observable} An observable sequence containing the materialized notification values from the source sequence.
4789 */
4790 observableProto.materialize = function () {
4791 return new MaterializeObservable(this);
4792 };
4793
4794 /**
4795 * Repeats the observable sequence a specified number of times. If the repeat count is not specified, the sequence repeats indefinitely.
4796 * @param {Number} [repeatCount] Number of times to repeat the sequence. If not provided, repeats the sequence indefinitely.
4797 * @returns {Observable} The observable sequence producing the elements of the given sequence repeatedly.
4798 */
4799 observableProto.repeat = function (repeatCount) {
4800 return enumerableRepeat(this, repeatCount).concat();
4801 };
4802
4803 /**
4804 * Repeats the source observable sequence the specified number of times or until it successfully terminates. If the retry count is not specified, it retries indefinitely.
4805 * Note if you encounter an error and want it to retry once, then you must use .retry(2);
4806 *
4807 * @example
4808 * var res = retried = retry.repeat();
4809 * var res = retried = retry.repeat(2);
4810 * @param {Number} [retryCount] Number of times to retry the sequence. If not provided, retry the sequence indefinitely.
4811 * @returns {Observable} An observable sequence producing the elements of the given sequence repeatedly until it terminates successfully.
4812 */
4813 observableProto.retry = function (retryCount) {
4814 return enumerableRepeat(this, retryCount).catchError();
4815 };
4816
4817 /**
4818 * Repeats the source observable sequence upon error each time the notifier emits or until it successfully terminates.
4819 * if the notifier completes, the observable sequence completes.
4820 *
4821 * @example
4822 * var timer = Observable.timer(500);
4823 * var source = observable.retryWhen(timer);
4824 * @param {Observable} [notifier] An observable that triggers the retries or completes the observable with onNext or onCompleted respectively.
4825 * @returns {Observable} An observable sequence producing the elements of the given sequence repeatedly until it terminates successfully.
4826 */
4827 observableProto.retryWhen = function (notifier) {
4828 return enumerableRepeat(this).catchErrorWhen(notifier);
4829 };
4830 var ScanObservable = (function(__super__) {
4831 inherits(ScanObservable, __super__);
4832 function ScanObservable(source, accumulator, hasSeed, seed) {
4833 this.source = source;
4834 this.accumulator = accumulator;
4835 this.hasSeed = hasSeed;
4836 this.seed = seed;
4837 __super__.call(this);
4838 }
4839
4840 ScanObservable.prototype.subscribeCore = function(o) {
4841 return this.source.subscribe(new ScanObserver(o,this));
4842 };
4843
4844 return ScanObservable;
4845 }(ObservableBase));
4846
4847 var ScanObserver = (function (__super__) {
4848 inherits(ScanObserver, __super__);
4849 function ScanObserver(o, parent) {
4850 this._o = o;
4851 this._p = parent;
4852 this._fn = parent.accumulator;
4853 this._hs = parent.hasSeed;
4854 this._s = parent.seed;
4855 this._ha = false;
4856 this._a = null;
4857 this._hv = false;
4858 this._i = 0;
4859 __super__.call(this);
4860 }
4861
4862 ScanObserver.prototype.next = function (x) {
4863 !this._hv && (this._hv = true);
4864 if (this._ha) {
4865 this._a = tryCatch(this._fn)(this._a, x, this._i, this._p);
4866 } else {
4867 this._a = this._hs ? tryCatch(this._fn)(this._s, x, this._i, this._p) : x;
4868 this._ha = true;
4869 }
4870 if (this._a === errorObj) { return this._o.onError(this._a.e); }
4871 this._o.onNext(this._a);
4872 this._i++;
4873 };
4874
4875 ScanObserver.prototype.error = function (e) {
4876 this._o.onError(e);
4877 };
4878
4879 ScanObserver.prototype.completed = function () {
4880 !this._hv && this._hs && this._o.onNext(this._s);
4881 this._o.onCompleted();
4882 };
4883
4884 return ScanObserver;
4885 }(AbstractObserver));
4886
4887 /**
4888 * Applies an accumulator function over an observable sequence and returns each intermediate result. The optional seed value is used as the initial accumulator value.
4889 * For aggregation behavior with no intermediate results, see Observable.aggregate.
4890 * @param {Mixed} [seed] The initial accumulator value.
4891 * @param {Function} accumulator An accumulator function to be invoked on each element.
4892 * @returns {Observable} An observable sequence containing the accumulated values.
4893 */
4894 observableProto.scan = function () {
4895 var hasSeed = false, seed, accumulator = arguments[0];
4896 if (arguments.length === 2) {
4897 hasSeed = true;
4898 seed = arguments[1];
4899 }
4900 return new ScanObservable(this, accumulator, hasSeed, seed);
4901 };
4902
4903 var SkipLastObservable = (function (__super__) {
4904 inherits(SkipLastObservable, __super__);
4905 function SkipLastObservable(source, c) {
4906 this.source = source;
4907 this._c = c;
4908 __super__.call(this);
4909 }
4910
4911 SkipLastObservable.prototype.subscribeCore = function (o) {
4912 return this.source.subscribe(new SkipLastObserver(o, this._c));
4913 };
4914
4915 return SkipLastObservable;
4916 }(ObservableBase));
4917
4918 var SkipLastObserver = (function (__super__) {
4919 inherits(SkipLastObserver, __super__);
4920 function SkipLastObserver(o, c) {
4921 this._o = o;
4922 this._c = c;
4923 this._q = [];
4924 __super__.call(this);
4925 }
4926
4927 SkipLastObserver.prototype.next = function (x) {
4928 this._q.push(x);
4929 this._q.length > this._c && this._o.onNext(this._q.shift());
4930 };
4931
4932 SkipLastObserver.prototype.error = function (e) {
4933 this._o.onError(e);
4934 };
4935
4936 SkipLastObserver.prototype.completed = function () {
4937 this._o.onCompleted();
4938 };
4939
4940 return SkipLastObserver;
4941 }(AbstractObserver));
4942
4943 /**
4944 * Bypasses a specified number of elements at the end of an observable sequence.
4945 * @description
4946 * This operator accumulates a queue with a length enough to store the first `count` elements. As more elements are
4947 * received, elements are taken from the front of the queue and produced on the result sequence. This causes elements to be delayed.
4948 * @param count Number of elements to bypass at the end of the source sequence.
4949 * @returns {Observable} An observable sequence containing the source sequence elements except for the bypassed ones at the end.
4950 */
4951 observableProto.skipLast = function (count) {
4952 if (count < 0) { throw new ArgumentOutOfRangeError(); }
4953 return new SkipLastObservable(this, count);
4954 };
4955
4956 /**
4957 * Prepends a sequence of values to an observable sequence with an optional scheduler and an argument list of values to prepend.
4958 * @example
4959 * var res = source.startWith(1, 2, 3);
4960 * var res = source.startWith(Rx.Scheduler.timeout, 1, 2, 3);
4961 * @param {Arguments} args The specified values to prepend to the observable sequence
4962 * @returns {Observable} The source sequence prepended with the specified values.
4963 */
4964 observableProto.startWith = function () {
4965 var values, scheduler, start = 0;
4966 if (!!arguments.length && isScheduler(arguments[0])) {
4967 scheduler = arguments[0];
4968 start = 1;
4969 } else {
4970 scheduler = immediateScheduler;
4971 }
4972 for(var args = [], i = start, len = arguments.length; i < len; i++) { args.push(arguments[i]); }
4973 return enumerableOf([observableFromArray(args, scheduler), this]).concat();
4974 };
4975
4976 var TakeLastObserver = (function (__super__) {
4977 inherits(TakeLastObserver, __super__);
4978 function TakeLastObserver(o, c) {
4979 this._o = o;
4980 this._c = c;
4981 this._q = [];
4982 __super__.call(this);
4983 }
4984
4985 TakeLastObserver.prototype.next = function (x) {
4986 this._q.push(x);
4987 this._q.length > this._c && this._q.shift();
4988 };
4989
4990 TakeLastObserver.prototype.error = function (e) {
4991 this._o.onError(e);
4992 };
4993
4994 TakeLastObserver.prototype.completed = function () {
4995 while (this._q.length > 0) { this._o.onNext(this._q.shift()); }
4996 this._o.onCompleted();
4997 };
4998
4999 return TakeLastObserver;
5000 }(AbstractObserver));
5001
5002 /**
5003 * Returns a specified number of contiguous elements from the end of an observable sequence.
5004 * @description
5005 * This operator accumulates a buffer with a length enough to store elements count elements. Upon completion of
5006 * the source sequence, this buffer is drained on the result sequence. This causes the elements to be delayed.
5007 * @param {Number} count Number of elements to take from the end of the source sequence.
5008 * @returns {Observable} An observable sequence containing the specified number of elements from the end of the source sequence.
5009 */
5010 observableProto.takeLast = function (count) {
5011 if (count < 0) { throw new ArgumentOutOfRangeError(); }
5012 var source = this;
5013 return new AnonymousObservable(function (o) {
5014 return source.subscribe(new TakeLastObserver(o, count));
5015 }, source);
5016 };
5017
5018 var TakeLastBufferObserver = (function (__super__) {
5019 inherits(TakeLastBufferObserver, __super__);
5020 function TakeLastBufferObserver(o, c) {
5021 this._o = o;
5022 this._c = c;
5023 this._q = [];
5024 __super__.call(this);
5025 }
5026
5027 TakeLastBufferObserver.prototype.next = function (x) {
5028 this._q.push(x);
5029 this._q.length > this._c && this._q.shift();
5030 };
5031
5032 TakeLastBufferObserver.prototype.error = function (e) {
5033 this._o.onError(e);
5034 };
5035
5036 TakeLastBufferObserver.prototype.completed = function () {
5037 this._o.onNext(this._q);
5038 this._o.onCompleted();
5039 };
5040
5041 return TakeLastBufferObserver;
5042 }(AbstractObserver));
5043
5044 /**
5045 * Returns an array with the specified number of contiguous elements from the end of an observable sequence.
5046 *
5047 * @description
5048 * This operator accumulates a buffer with a length enough to store count elements. Upon completion of the
5049 * source sequence, this buffer is produced on the result sequence.
5050 * @param {Number} count Number of elements to take from the end of the source sequence.
5051 * @returns {Observable} An observable sequence containing a single array with the specified number of elements from the end of the source sequence.
5052 */
5053 observableProto.takeLastBuffer = function (count) {
5054 if (count < 0) { throw new ArgumentOutOfRangeError(); }
5055 var source = this;
5056 return new AnonymousObservable(function (o) {
5057 return source.subscribe(new TakeLastBufferObserver(o, count));
5058 }, source);
5059 };
5060
5061 /**
5062 * Projects each element of an observable sequence into zero or more windows which are produced based on element count information.
5063 * @param {Number} count Length of each window.
5064 * @param {Number} [skip] Number of elements to skip between creation of consecutive windows. If not specified, defaults to the count.
5065 * @returns {Observable} An observable sequence of windows.
5066 */
5067 observableProto.windowWithCount = function (count, skip) {
5068 var source = this;
5069 +count || (count = 0);
5070 Math.abs(count) === Infinity && (count = 0);
5071 if (count <= 0) { throw new ArgumentOutOfRangeError(); }
5072 skip == null && (skip = count);
5073 +skip || (skip = 0);
5074 Math.abs(skip) === Infinity && (skip = 0);
5075
5076 if (skip <= 0) { throw new ArgumentOutOfRangeError(); }
5077 return new AnonymousObservable(function (observer) {
5078 var m = new SingleAssignmentDisposable(),
5079 refCountDisposable = new RefCountDisposable(m),
5080 n = 0,
5081 q = [];
5082
5083 function createWindow () {
5084 var s = new Subject();
5085 q.push(s);
5086 observer.onNext(addRef(s, refCountDisposable));
5087 }
5088
5089 createWindow();
5090
5091 m.setDisposable(source.subscribe(
5092 function (x) {
5093 for (var i = 0, len = q.length; i < len; i++) { q[i].onNext(x); }
5094 var c = n - count + 1;
5095 c >= 0 && c % skip === 0 && q.shift().onCompleted();
5096 ++n % skip === 0 && createWindow();
5097 },
5098 function (e) {
5099 while (q.length > 0) { q.shift().onError(e); }
5100 observer.onError(e);
5101 },
5102 function () {
5103 while (q.length > 0) { q.shift().onCompleted(); }
5104 observer.onCompleted();
5105 }
5106 ));
5107 return refCountDisposable;
5108 }, source);
5109 };
5110
5111 function concatMap(source, selector, thisArg) {
5112 var selectorFunc = bindCallback(selector, thisArg, 3);
5113 return source.map(function (x, i) {
5114 var result = selectorFunc(x, i, source);
5115 isPromise(result) && (result = observableFromPromise(result));
5116 (isArrayLike(result) || isIterable(result)) && (result = observableFrom(result));
5117 return result;
5118 }).concatAll();
5119 }
5120
5121 /**
5122 * One of the Following:
5123 * Projects each element of an observable sequence to an observable sequence and merges the resulting observable sequences into one observable sequence.
5124 *
5125 * @example
5126 * var res = source.concatMap(function (x) { return Rx.Observable.range(0, x); });
5127 * Or:
5128 * Projects each element of an observable sequence to an observable sequence, invokes the result selector for the source element and each of the corresponding inner sequence's elements, and merges the results into one observable sequence.
5129 *
5130 * var res = source.concatMap(function (x) { return Rx.Observable.range(0, x); }, function (x, y) { return x + y; });
5131 * Or:
5132 * Projects each element of the source observable sequence to the other observable sequence and merges the resulting observable sequences into one observable sequence.
5133 *
5134 * var res = source.concatMap(Rx.Observable.fromArray([1,2,3]));
5135 * @param {Function} selector A transform function to apply to each element or an observable sequence to project each element from the
5136 * source sequence onto which could be either an observable or Promise.
5137 * @param {Function} [resultSelector] A transform function to apply to each element of the intermediate sequence.
5138 * @returns {Observable} An observable sequence whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of the input sequence and then mapping each of those sequence elements and their corresponding source element to a result element.
5139 */
5140 observableProto.selectConcat = observableProto.concatMap = function (selector, resultSelector, thisArg) {
5141 if (isFunction(selector) && isFunction(resultSelector)) {
5142 return this.concatMap(function (x, i) {
5143 var selectorResult = selector(x, i);
5144 isPromise(selectorResult) && (selectorResult = observableFromPromise(selectorResult));
5145 (isArrayLike(selectorResult) || isIterable(selectorResult)) && (selectorResult = observableFrom(selectorResult));
5146
5147 return selectorResult.map(function (y, i2) {
5148 return resultSelector(x, y, i, i2);
5149 });
5150 });
5151 }
5152 return isFunction(selector) ?
5153 concatMap(this, selector, thisArg) :
5154 concatMap(this, function () { return selector; });
5155 };
5156
5157 /**
5158 * Projects each notification of an observable sequence to an observable sequence and concats the resulting observable sequences into one observable sequence.
5159 * @param {Function} onNext A transform function to apply to each element; the second parameter of the function represents the index of the source element.
5160 * @param {Function} onError A transform function to apply when an error occurs in the source sequence.
5161 * @param {Function} onCompleted A transform function to apply when the end of the source sequence is reached.
5162 * @param {Any} [thisArg] An optional "this" to use to invoke each transform.
5163 * @returns {Observable} An observable sequence whose elements are the result of invoking the one-to-many transform function corresponding to each notification in the input sequence.
5164 */
5165 observableProto.concatMapObserver = observableProto.selectConcatObserver = function(onNext, onError, onCompleted, thisArg) {
5166 var source = this,
5167 onNextFunc = bindCallback(onNext, thisArg, 2),
5168 onErrorFunc = bindCallback(onError, thisArg, 1),
5169 onCompletedFunc = bindCallback(onCompleted, thisArg, 0);
5170 return new AnonymousObservable(function (observer) {
5171 var index = 0;
5172 return source.subscribe(
5173 function (x) {
5174 var result;
5175 try {
5176 result = onNextFunc(x, index++);
5177 } catch (e) {
5178 observer.onError(e);
5179 return;
5180 }
5181 isPromise(result) && (result = observableFromPromise(result));
5182 observer.onNext(result);
5183 },
5184 function (err) {
5185 var result;
5186 try {
5187 result = onErrorFunc(err);
5188 } catch (e) {
5189 observer.onError(e);
5190 return;
5191 }
5192 isPromise(result) && (result = observableFromPromise(result));
5193 observer.onNext(result);
5194 observer.onCompleted();
5195 },
5196 function () {
5197 var result;
5198 try {
5199 result = onCompletedFunc();
5200 } catch (e) {
5201 observer.onError(e);
5202 return;
5203 }
5204 isPromise(result) && (result = observableFromPromise(result));
5205 observer.onNext(result);
5206 observer.onCompleted();
5207 });
5208 }, this).concatAll();
5209 };
5210
5211 var DefaultIfEmptyObserver = (function (__super__) {
5212 inherits(DefaultIfEmptyObserver, __super__);
5213 function DefaultIfEmptyObserver(o, d) {
5214 this._o = o;
5215 this._d = d;
5216 this._f = false;
5217 __super__.call(this);
5218 }
5219
5220 DefaultIfEmptyObserver.prototype.next = function (x) {
5221 this._f = true;
5222 this._o.onNext(x);
5223 };
5224
5225 DefaultIfEmptyObserver.prototype.error = function (e) {
5226 this._o.onError(e);
5227 };
5228
5229 DefaultIfEmptyObserver.prototype.completed = function () {
5230 !this._f && this._o.onNext(this._d);
5231 this._o.onCompleted();
5232 };
5233
5234 return DefaultIfEmptyObserver;
5235 }(AbstractObserver));
5236
5237 /**
5238 * Returns the elements of the specified sequence or the specified value in a singleton sequence if the sequence is empty.
5239 *
5240 * var res = obs = xs.defaultIfEmpty();
5241 * 2 - obs = xs.defaultIfEmpty(false);
5242 *
5243 * @memberOf Observable#
5244 * @param defaultValue The value to return if the sequence is empty. If not provided, this defaults to null.
5245 * @returns {Observable} An observable sequence that contains the specified default value if the source is empty; otherwise, the elements of the source itself.
5246 */
5247 observableProto.defaultIfEmpty = function (defaultValue) {
5248 var source = this;
5249 defaultValue === undefined && (defaultValue = null);
5250 return new AnonymousObservable(function (o) {
5251 return source.subscribe(new DefaultIfEmptyObserver(o, defaultValue));
5252 }, source);
5253 };
5254
5255 // Swap out for Array.findIndex
5256 function arrayIndexOfComparer(array, item, comparer) {
5257 for (var i = 0, len = array.length; i < len; i++) {
5258 if (comparer(array[i], item)) { return i; }
5259 }
5260 return -1;
5261 }
5262
5263 function HashSet(comparer) {
5264 this.comparer = comparer;
5265 this.set = [];
5266 }
5267 HashSet.prototype.push = function(value) {
5268 var retValue = arrayIndexOfComparer(this.set, value, this.comparer) === -1;
5269 retValue && this.set.push(value);
5270 return retValue;
5271 };
5272
5273 var DistinctObservable = (function (__super__) {
5274 inherits(DistinctObservable, __super__);
5275 function DistinctObservable(source, keyFn, cmpFn) {
5276 this.source = source;
5277 this._keyFn = keyFn;
5278 this._cmpFn = cmpFn;
5279 __super__.call(this);
5280 }
5281
5282 DistinctObservable.prototype.subscribeCore = function (o) {
5283 return this.source.subscribe(new DistinctObserver(o, this._keyFn, this._cmpFn));
5284 };
5285
5286 return DistinctObservable;
5287 }(ObservableBase));
5288
5289 var DistinctObserver = (function (__super__) {
5290 inherits(DistinctObserver, __super__);
5291 function DistinctObserver(o, keyFn, cmpFn) {
5292 this._o = o;
5293 this._keyFn = keyFn;
5294 this._h = new HashSet(cmpFn);
5295 __super__.call(this);
5296 }
5297
5298 DistinctObserver.prototype.next = function (x) {
5299 var key = x;
5300 if (isFunction(this._keyFn)) {
5301 key = tryCatch(this._keyFn)(x);
5302 if (key === errorObj) { return this._o.onError(key.e); }
5303 }
5304 this._h.push(key) && this._o.onNext(x);
5305 };
5306
5307 DistinctObserver.prototype.error = function (e) { this._o.onError(e); };
5308 DistinctObserver.prototype.completed = function () { this._o.onCompleted(); };
5309
5310 return DistinctObserver;
5311 }(AbstractObserver));
5312
5313 /**
5314 * Returns an observable sequence that contains only distinct elements according to the keySelector and the comparer.
5315 * Usage of this operator should be considered carefully due to the maintenance of an internal lookup structure which can grow large.
5316 *
5317 * @example
5318 * var res = obs = xs.distinct();
5319 * 2 - obs = xs.distinct(function (x) { return x.id; });
5320 * 2 - obs = xs.distinct(function (x) { return x.id; }, function (a,b) { return a === b; });
5321 * @param {Function} [keySelector] A function to compute the comparison key for each element.
5322 * @param {Function} [comparer] Used to compare items in the collection.
5323 * @returns {Observable} An observable sequence only containing the distinct elements, based on a computed key value, from the source sequence.
5324 */
5325 observableProto.distinct = function (keySelector, comparer) {
5326 comparer || (comparer = defaultComparer);
5327 return new DistinctObservable(this, keySelector, comparer);
5328 };
5329
5330 /**
5331 * Groups the elements of an observable sequence according to a specified key selector function and comparer and selects the resulting elements by using a specified function.
5332 *
5333 * @example
5334 * var res = observable.groupBy(function (x) { return x.id; });
5335 * 2 - observable.groupBy(function (x) { return x.id; }), function (x) { return x.name; });
5336 * 3 - observable.groupBy(function (x) { return x.id; }), function (x) { return x.name; }, function (x) { return x.toString(); });
5337 * @param {Function} keySelector A function to extract the key for each element.
5338 * @param {Function} [elementSelector] A function to map each source element to an element in an observable group.
5339 * @returns {Observable} A sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.
5340 */
5341 observableProto.groupBy = function (keySelector, elementSelector) {
5342 return this.groupByUntil(keySelector, elementSelector, observableNever);
5343 };
5344
5345 /**
5346 * Groups the elements of an observable sequence according to a specified key selector function.
5347 * A duration selector function is used to control the lifetime of groups. When a group expires, it receives an OnCompleted notification. When a new element with the same
5348 * key value as a reclaimed group occurs, the group will be reborn with a new lifetime request.
5349 *
5350 * @example
5351 * var res = observable.groupByUntil(function (x) { return x.id; }, null, function () { return Rx.Observable.never(); });
5352 * 2 - observable.groupBy(function (x) { return x.id; }), function (x) { return x.name; }, function () { return Rx.Observable.never(); });
5353 * 3 - observable.groupBy(function (x) { return x.id; }), function (x) { return x.name; }, function () { return Rx.Observable.never(); }, function (x) { return x.toString(); });
5354 * @param {Function} keySelector A function to extract the key for each element.
5355 * @param {Function} durationSelector A function to signal the expiration of a group.
5356 * @returns {Observable}
5357 * A sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.
5358 * If a group's lifetime expires, a new group with the same key value can be created once an element with such a key value is encoutered.
5359 *
5360 */
5361 observableProto.groupByUntil = function (keySelector, elementSelector, durationSelector) {
5362 var source = this;
5363 return new AnonymousObservable(function (o) {
5364 var map = new Map(),
5365 groupDisposable = new CompositeDisposable(),
5366 refCountDisposable = new RefCountDisposable(groupDisposable),
5367 handleError = function (e) { return function (item) { item.onError(e); }; };
5368
5369 groupDisposable.add(
5370 source.subscribe(function (x) {
5371 var key = tryCatch(keySelector)(x);
5372 if (key === errorObj) {
5373 map.forEach(handleError(key.e));
5374 return o.onError(key.e);
5375 }
5376
5377 var fireNewMapEntry = false, writer = map.get(key);
5378 if (writer === undefined) {
5379 writer = new Subject();
5380 map.set(key, writer);
5381 fireNewMapEntry = true;
5382 }
5383
5384 if (fireNewMapEntry) {
5385 var group = new GroupedObservable(key, writer, refCountDisposable),
5386 durationGroup = new GroupedObservable(key, writer);
5387 var duration = tryCatch(durationSelector)(durationGroup);
5388 if (duration === errorObj) {
5389 map.forEach(handleError(duration.e));
5390 return o.onError(duration.e);
5391 }
5392
5393 o.onNext(group);
5394
5395 var md = new SingleAssignmentDisposable();
5396 groupDisposable.add(md);
5397
5398 md.setDisposable(duration.take(1).subscribe(
5399 noop,
5400 function (e) {
5401 map.forEach(handleError(e));
5402 o.onError(e);
5403 },
5404 function () {
5405 if (map['delete'](key)) { writer.onCompleted(); }
5406 groupDisposable.remove(md);
5407 }));
5408 }
5409
5410 var element = x;
5411 if (isFunction(elementSelector)) {
5412 element = tryCatch(elementSelector)(x);
5413 if (element === errorObj) {
5414 map.forEach(handleError(element.e));
5415 return o.onError(element.e);
5416 }
5417 }
5418
5419 writer.onNext(element);
5420 }, function (e) {
5421 map.forEach(handleError(e));
5422 o.onError(e);
5423 }, function () {
5424 map.forEach(function (item) { item.onCompleted(); });
5425 o.onCompleted();
5426 }));
5427
5428 return refCountDisposable;
5429 }, source);
5430 };
5431
5432 var MapObservable = (function (__super__) {
5433 inherits(MapObservable, __super__);
5434
5435 function MapObservable(source, selector, thisArg) {
5436 this.source = source;
5437 this.selector = bindCallback(selector, thisArg, 3);
5438 __super__.call(this);
5439 }
5440
5441 function innerMap(selector, self) {
5442 return function (x, i, o) { return selector.call(this, self.selector(x, i, o), i, o); };
5443 }
5444
5445 MapObservable.prototype.internalMap = function (selector, thisArg) {
5446 return new MapObservable(this.source, innerMap(selector, this), thisArg);
5447 };
5448
5449 MapObservable.prototype.subscribeCore = function (o) {
5450 return this.source.subscribe(new InnerObserver(o, this.selector, this));
5451 };
5452
5453 inherits(InnerObserver, AbstractObserver);
5454 function InnerObserver(o, selector, source) {
5455 this.o = o;
5456 this.selector = selector;
5457 this.source = source;
5458 this.i = 0;
5459 AbstractObserver.call(this);
5460 }
5461
5462 InnerObserver.prototype.next = function(x) {
5463 var result = tryCatch(this.selector)(x, this.i++, this.source);
5464 if (result === errorObj) { return this.o.onError(result.e); }
5465 this.o.onNext(result);
5466 };
5467
5468 InnerObserver.prototype.error = function (e) {
5469 this.o.onError(e);
5470 };
5471
5472 InnerObserver.prototype.completed = function () {
5473 this.o.onCompleted();
5474 };
5475
5476 return MapObservable;
5477
5478 }(ObservableBase));
5479
5480 /**
5481 * Projects each element of an observable sequence into a new form by incorporating the element's index.
5482 * @param {Function} selector A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
5483 * @param {Any} [thisArg] Object to use as this when executing callback.
5484 * @returns {Observable} An observable sequence whose elements are the result of invoking the transform function on each element of source.
5485 */
5486 observableProto.map = observableProto.select = function (selector, thisArg) {
5487 var selectorFn = typeof selector === 'function' ? selector : function () { return selector; };
5488 return this instanceof MapObservable ?
5489 this.internalMap(selectorFn, thisArg) :
5490 new MapObservable(this, selectorFn, thisArg);
5491 };
5492
5493 function plucker(args, len) {
5494 return function mapper(x) {
5495 var currentProp = x;
5496 for (var i = 0; i < len; i++) {
5497 var p = currentProp[args[i]];
5498 if (typeof p !== 'undefined') {
5499 currentProp = p;
5500 } else {
5501 return undefined;
5502 }
5503 }
5504 return currentProp;
5505 }
5506 }
5507
5508 /**
5509 * Retrieves the value of a specified nested property from all elements in
5510 * the Observable sequence.
5511 * @param {Arguments} arguments The nested properties to pluck.
5512 * @returns {Observable} Returns a new Observable sequence of property values.
5513 */
5514 observableProto.pluck = function () {
5515 var len = arguments.length, args = new Array(len);
5516 if (len === 0) { throw new Error('List of properties cannot be empty.'); }
5517 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
5518 return this.map(plucker(args, len));
5519 };
5520
5521observableProto.flatMap = observableProto.selectMany = function(selector, resultSelector, thisArg) {
5522 return new FlatMapObservable(this, selector, resultSelector, thisArg).mergeAll();
5523};
5524
5525 /**
5526 * Projects each notification of an observable sequence to an observable sequence and merges the resulting observable sequences into one observable sequence.
5527 * @param {Function} onNext A transform function to apply to each element; the second parameter of the function represents the index of the source element.
5528 * @param {Function} onError A transform function to apply when an error occurs in the source sequence.
5529 * @param {Function} onCompleted A transform function to apply when the end of the source sequence is reached.
5530 * @param {Any} [thisArg] An optional "this" to use to invoke each transform.
5531 * @returns {Observable} An observable sequence whose elements are the result of invoking the one-to-many transform function corresponding to each notification in the input sequence.
5532 */
5533 observableProto.flatMapObserver = observableProto.selectManyObserver = function (onNext, onError, onCompleted, thisArg) {
5534 var source = this;
5535 return new AnonymousObservable(function (observer) {
5536 var index = 0;
5537
5538 return source.subscribe(
5539 function (x) {
5540 var result;
5541 try {
5542 result = onNext.call(thisArg, x, index++);
5543 } catch (e) {
5544 observer.onError(e);
5545 return;
5546 }
5547 isPromise(result) && (result = observableFromPromise(result));
5548 observer.onNext(result);
5549 },
5550 function (err) {
5551 var result;
5552 try {
5553 result = onError.call(thisArg, err);
5554 } catch (e) {
5555 observer.onError(e);
5556 return;
5557 }
5558 isPromise(result) && (result = observableFromPromise(result));
5559 observer.onNext(result);
5560 observer.onCompleted();
5561 },
5562 function () {
5563 var result;
5564 try {
5565 result = onCompleted.call(thisArg);
5566 } catch (e) {
5567 observer.onError(e);
5568 return;
5569 }
5570 isPromise(result) && (result = observableFromPromise(result));
5571 observer.onNext(result);
5572 observer.onCompleted();
5573 });
5574 }, source).mergeAll();
5575 };
5576
5577Rx.Observable.prototype.flatMapLatest = function(selector, resultSelector, thisArg) {
5578 return new FlatMapObservable(this, selector, resultSelector, thisArg).switchLatest();
5579};
5580 var SkipObservable = (function(__super__) {
5581 inherits(SkipObservable, __super__);
5582 function SkipObservable(source, count) {
5583 this.source = source;
5584 this._count = count;
5585 __super__.call(this);
5586 }
5587
5588 SkipObservable.prototype.subscribeCore = function (o) {
5589 return this.source.subscribe(new SkipObserver(o, this._count));
5590 };
5591
5592 function SkipObserver(o, c) {
5593 this._o = o;
5594 this._r = c;
5595 AbstractObserver.call(this);
5596 }
5597
5598 inherits(SkipObserver, AbstractObserver);
5599
5600 SkipObserver.prototype.next = function (x) {
5601 if (this._r <= 0) {
5602 this._o.onNext(x);
5603 } else {
5604 this._r--;
5605 }
5606 };
5607 SkipObserver.prototype.error = function(e) { this._o.onError(e); };
5608 SkipObserver.prototype.completed = function() { this._o.onCompleted(); };
5609
5610 return SkipObservable;
5611 }(ObservableBase));
5612
5613 /**
5614 * Bypasses a specified number of elements in an observable sequence and then returns the remaining elements.
5615 * @param {Number} count The number of elements to skip before returning the remaining elements.
5616 * @returns {Observable} An observable sequence that contains the elements that occur after the specified index in the input sequence.
5617 */
5618 observableProto.skip = function (count) {
5619 if (count < 0) { throw new ArgumentOutOfRangeError(); }
5620 return new SkipObservable(this, count);
5621 };
5622
5623 var SkipWhileObservable = (function (__super__) {
5624 inherits(SkipWhileObservable, __super__);
5625 function SkipWhileObservable(source, fn) {
5626 this.source = source;
5627 this._fn = fn;
5628 __super__.call(this);
5629 }
5630
5631 SkipWhileObservable.prototype.subscribeCore = function (o) {
5632 return this.source.subscribe(new SkipWhileObserver(o, this));
5633 };
5634
5635 return SkipWhileObservable;
5636 }(ObservableBase));
5637
5638 var SkipWhileObserver = (function (__super__) {
5639 inherits(SkipWhileObserver, __super__);
5640
5641 function SkipWhileObserver(o, p) {
5642 this._o = o;
5643 this._p = p;
5644 this._i = 0;
5645 this._r = false;
5646 __super__.call(this);
5647 }
5648
5649 SkipWhileObserver.prototype.next = function (x) {
5650 if (!this._r) {
5651 var res = tryCatch(this._p._fn)(x, this._i++, this._p);
5652 if (res === errorObj) { return this._o.onError(res.e); }
5653 this._r = !res;
5654 }
5655 this._r && this._o.onNext(x);
5656 };
5657 SkipWhileObserver.prototype.error = function (e) { this._o.onError(e); };
5658 SkipWhileObserver.prototype.completed = function () { this._o.onCompleted(); };
5659
5660 return SkipWhileObserver;
5661 }(AbstractObserver));
5662
5663 /**
5664 * Bypasses elements in an observable sequence as long as a specified condition is true and then returns the remaining elements.
5665 * The element's index is used in the logic of the predicate function.
5666 *
5667 * var res = source.skipWhile(function (value) { return value < 10; });
5668 * var res = source.skipWhile(function (value, index) { return value < 10 || index < 10; });
5669 * @param {Function} predicate A function to test each element for a condition; the second parameter of the function represents the index of the source element.
5670 * @param {Any} [thisArg] Object to use as this when executing callback.
5671 * @returns {Observable} An observable sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate.
5672 */
5673 observableProto.skipWhile = function (predicate, thisArg) {
5674 var fn = bindCallback(predicate, thisArg, 3);
5675 return new SkipWhileObservable(this, fn);
5676 };
5677
5678 var TakeObservable = (function(__super__) {
5679 inherits(TakeObservable, __super__);
5680 function TakeObservable(source, count) {
5681 this.source = source;
5682 this._count = count;
5683 __super__.call(this);
5684 }
5685
5686 TakeObservable.prototype.subscribeCore = function (o) {
5687 return this.source.subscribe(new TakeObserver(o, this._count));
5688 };
5689
5690 function TakeObserver(o, c) {
5691 this._o = o;
5692 this._c = c;
5693 this._r = c;
5694 AbstractObserver.call(this);
5695 }
5696
5697 inherits(TakeObserver, AbstractObserver);
5698
5699 TakeObserver.prototype.next = function (x) {
5700 if (this._r-- > 0) {
5701 this._o.onNext(x);
5702 this._r <= 0 && this._o.onCompleted();
5703 }
5704 };
5705
5706 TakeObserver.prototype.error = function (e) { this._o.onError(e); };
5707 TakeObserver.prototype.completed = function () { this._o.onCompleted(); };
5708
5709 return TakeObservable;
5710 }(ObservableBase));
5711
5712 /**
5713 * Returns a specified number of contiguous elements from the start of an observable sequence, using the specified scheduler for the edge case of take(0).
5714 * @param {Number} count The number of elements to return.
5715 * @param {Scheduler} [scheduler] Scheduler used to produce an OnCompleted message in case <paramref name="count count</paramref> is set to 0.
5716 * @returns {Observable} An observable sequence that contains the specified number of elements from the start of the input sequence.
5717 */
5718 observableProto.take = function (count, scheduler) {
5719 if (count < 0) { throw new ArgumentOutOfRangeError(); }
5720 if (count === 0) { return observableEmpty(scheduler); }
5721 return new TakeObservable(this, count);
5722 };
5723
5724 var TakeWhileObservable = (function (__super__) {
5725 inherits(TakeWhileObservable, __super__);
5726 function TakeWhileObservable(source, fn) {
5727 this.source = source;
5728 this._fn = fn;
5729 __super__.call(this);
5730 }
5731
5732 TakeWhileObservable.prototype.subscribeCore = function (o) {
5733 return this.source.subscribe(new TakeWhileObserver(o, this));
5734 };
5735
5736 return TakeWhileObservable;
5737 }(ObservableBase));
5738
5739 var TakeWhileObserver = (function (__super__) {
5740 inherits(TakeWhileObserver, __super__);
5741
5742 function TakeWhileObserver(o, p) {
5743 this._o = o;
5744 this._p = p;
5745 this._i = 0;
5746 this._r = true;
5747 __super__.call(this);
5748 }
5749
5750 TakeWhileObserver.prototype.next = function (x) {
5751 if (this._r) {
5752 this._r = tryCatch(this._p._fn)(x, this._i++, this._p);
5753 if (this._r === errorObj) { return this._o.onError(this._r.e); }
5754 }
5755 if (this._r) {
5756 this._o.onNext(x);
5757 } else {
5758 this._o.onCompleted();
5759 }
5760 };
5761 TakeWhileObserver.prototype.error = function (e) { this._o.onError(e); };
5762 TakeWhileObserver.prototype.completed = function () { this._o.onCompleted(); };
5763
5764 return TakeWhileObserver;
5765 }(AbstractObserver));
5766
5767 /**
5768 * Returns elements from an observable sequence as long as a specified condition is true.
5769 * The element's index is used in the logic of the predicate function.
5770 * @param {Function} predicate A function to test each element for a condition; the second parameter of the function represents the index of the source element.
5771 * @param {Any} [thisArg] Object to use as this when executing callback.
5772 * @returns {Observable} An observable sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.
5773 */
5774 observableProto.takeWhile = function (predicate, thisArg) {
5775 var fn = bindCallback(predicate, thisArg, 3);
5776 return new TakeWhileObservable(this, fn);
5777 };
5778
5779 var FilterObservable = (function (__super__) {
5780 inherits(FilterObservable, __super__);
5781
5782 function FilterObservable(source, predicate, thisArg) {
5783 this.source = source;
5784 this.predicate = bindCallback(predicate, thisArg, 3);
5785 __super__.call(this);
5786 }
5787
5788 FilterObservable.prototype.subscribeCore = function (o) {
5789 return this.source.subscribe(new InnerObserver(o, this.predicate, this));
5790 };
5791
5792 function innerPredicate(predicate, self) {
5793 return function(x, i, o) { return self.predicate(x, i, o) && predicate.call(this, x, i, o); }
5794 }
5795
5796 FilterObservable.prototype.internalFilter = function(predicate, thisArg) {
5797 return new FilterObservable(this.source, innerPredicate(predicate, this), thisArg);
5798 };
5799
5800 inherits(InnerObserver, AbstractObserver);
5801 function InnerObserver(o, predicate, source) {
5802 this.o = o;
5803 this.predicate = predicate;
5804 this.source = source;
5805 this.i = 0;
5806 AbstractObserver.call(this);
5807 }
5808
5809 InnerObserver.prototype.next = function(x) {
5810 var shouldYield = tryCatch(this.predicate)(x, this.i++, this.source);
5811 if (shouldYield === errorObj) {
5812 return this.o.onError(shouldYield.e);
5813 }
5814 shouldYield && this.o.onNext(x);
5815 };
5816
5817 InnerObserver.prototype.error = function (e) {
5818 this.o.onError(e);
5819 };
5820
5821 InnerObserver.prototype.completed = function () {
5822 this.o.onCompleted();
5823 };
5824
5825 return FilterObservable;
5826
5827 }(ObservableBase));
5828
5829 /**
5830 * Filters the elements of an observable sequence based on a predicate by incorporating the element's index.
5831 * @param {Function} predicate A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
5832 * @param {Any} [thisArg] Object to use as this when executing callback.
5833 * @returns {Observable} An observable sequence that contains elements from the input sequence that satisfy the condition.
5834 */
5835 observableProto.filter = observableProto.where = function (predicate, thisArg) {
5836 return this instanceof FilterObservable ? this.internalFilter(predicate, thisArg) :
5837 new FilterObservable(this, predicate, thisArg);
5838 };
5839
5840 var ExtremaByObservable = (function (__super__) {
5841 inherits(ExtremaByObservable, __super__);
5842 function ExtremaByObservable(source, k, c) {
5843 this.source = source;
5844 this._k = k;
5845 this._c = c;
5846 __super__.call(this);
5847 }
5848
5849 ExtremaByObservable.prototype.subscribeCore = function (o) {
5850 return this.source.subscribe(new ExtremaByObserver(o, this._k, this._c));
5851 };
5852
5853 return ExtremaByObservable;
5854 }(ObservableBase));
5855
5856 var ExtremaByObserver = (function (__super__) {
5857 inherits(ExtremaByObserver, __super__);
5858 function ExtremaByObserver(o, k, c) {
5859 this._o = o;
5860 this._k = k;
5861 this._c = c;
5862 this._v = null;
5863 this._hv = false;
5864 this._l = [];
5865 __super__.call(this);
5866 }
5867
5868 ExtremaByObserver.prototype.next = function (x) {
5869 var key = tryCatch(this._k)(x);
5870 if (key === errorObj) { return this._o.onError(key.e); }
5871 var comparison = 0;
5872 if (!this._hv) {
5873 this._hv = true;
5874 this._v = key;
5875 } else {
5876 comparison = tryCatch(this._c)(key, this._v);
5877 if (comparison === errorObj) { return this._o.onError(comparison.e); }
5878 }
5879 if (comparison > 0) {
5880 this._v = key;
5881 this._l = [];
5882 }
5883 if (comparison >= 0) { this._l.push(x); }
5884 };
5885
5886 ExtremaByObserver.prototype.error = function (e) {
5887 this._o.onError(e);
5888 };
5889
5890 ExtremaByObserver.prototype.completed = function () {
5891 this._o.onNext(this._l);
5892 this._o.onCompleted();
5893 };
5894
5895 return ExtremaByObserver;
5896 }(AbstractObserver));
5897
5898 function firstOnly(x) {
5899 if (x.length === 0) { throw new EmptyError(); }
5900 return x[0];
5901 }
5902
5903 var ReduceObservable = (function(__super__) {
5904 inherits(ReduceObservable, __super__);
5905 function ReduceObservable(source, accumulator, hasSeed, seed) {
5906 this.source = source;
5907 this.accumulator = accumulator;
5908 this.hasSeed = hasSeed;
5909 this.seed = seed;
5910 __super__.call(this);
5911 }
5912
5913 ReduceObservable.prototype.subscribeCore = function(observer) {
5914 return this.source.subscribe(new ReduceObserver(observer,this));
5915 };
5916
5917 return ReduceObservable;
5918 }(ObservableBase));
5919
5920 var ReduceObserver = (function (__super__) {
5921 inherits(ReduceObserver, __super__);
5922 function ReduceObserver(o, parent) {
5923 this._o = o;
5924 this._p = parent;
5925 this._fn = parent.accumulator;
5926 this._hs = parent.hasSeed;
5927 this._s = parent.seed;
5928 this._ha = false;
5929 this._a = null;
5930 this._hv = false;
5931 this._i = 0;
5932 __super__.call(this);
5933 }
5934
5935 ReduceObserver.prototype.next = function (x) {
5936 !this._hv && (this._hv = true);
5937 if (this._ha) {
5938 this._a = tryCatch(this._fn)(this._a, x, this._i, this._p);
5939 } else {
5940 this._a = this._hs ? tryCatch(this._fn)(this._s, x, this._i, this._p) : x;
5941 this._ha = true;
5942 }
5943 if (this._a === errorObj) { return this._o.onError(this._a.e); }
5944 this._i++;
5945 };
5946
5947 ReduceObserver.prototype.error = function (e) {
5948 this._o.onError(e);
5949 };
5950
5951 ReduceObserver.prototype.completed = function () {
5952 this._hv && this._o.onNext(this._a);
5953 !this._hv && this._hs && this._o.onNext(this._s);
5954 !this._hv && !this._hs && this._o.onError(new EmptyError());
5955 this._o.onCompleted();
5956 };
5957
5958 return ReduceObserver;
5959 }(AbstractObserver));
5960
5961 /**
5962 * 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.
5963 * For aggregation behavior with incremental intermediate results, see Observable.scan.
5964 * @param {Function} accumulator An accumulator function to be invoked on each element.
5965 * @param {Any} [seed] The initial accumulator value.
5966 * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
5967 */
5968 observableProto.reduce = function () {
5969 var hasSeed = false, seed, accumulator = arguments[0];
5970 if (arguments.length === 2) {
5971 hasSeed = true;
5972 seed = arguments[1];
5973 }
5974 return new ReduceObservable(this, accumulator, hasSeed, seed);
5975 };
5976
5977 var SomeObservable = (function (__super__) {
5978 inherits(SomeObservable, __super__);
5979 function SomeObservable(source, fn) {
5980 this.source = source;
5981 this._fn = fn;
5982 __super__.call(this);
5983 }
5984
5985 SomeObservable.prototype.subscribeCore = function (o) {
5986 return this.source.subscribe(new SomeObserver(o, this._fn, this.source));
5987 };
5988
5989 return SomeObservable;
5990 }(ObservableBase));
5991
5992 var SomeObserver = (function (__super__) {
5993 inherits(SomeObserver, __super__);
5994
5995 function SomeObserver(o, fn, s) {
5996 this._o = o;
5997 this._fn = fn;
5998 this._s = s;
5999 this._i = 0;
6000 __super__.call(this);
6001 }
6002
6003 SomeObserver.prototype.next = function (x) {
6004 var result = tryCatch(this._fn)(x, this._i++, this._s);
6005 if (result === errorObj) { return this._o.onError(result.e); }
6006 if (Boolean(result)) {
6007 this._o.onNext(true);
6008 this._o.onCompleted();
6009 }
6010 };
6011 SomeObserver.prototype.error = function (e) { this._o.onError(e); };
6012 SomeObserver.prototype.completed = function () {
6013 this._o.onNext(false);
6014 this._o.onCompleted();
6015 };
6016
6017 return SomeObserver;
6018 }(AbstractObserver));
6019
6020 /**
6021 * Determines whether any element of an observable sequence satisfies a condition if present, else if any items are in the sequence.
6022 * @param {Function} [predicate] A function to test each element for a condition.
6023 * @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.
6024 */
6025 observableProto.some = function (predicate, thisArg) {
6026 var fn = bindCallback(predicate, thisArg, 3);
6027 return new SomeObservable(this, fn);
6028 };
6029
6030 var IsEmptyObservable = (function (__super__) {
6031 inherits(IsEmptyObservable, __super__);
6032 function IsEmptyObservable(source) {
6033 this.source = source;
6034 __super__.call(this);
6035 }
6036
6037 IsEmptyObservable.prototype.subscribeCore = function (o) {
6038 return this.source.subscribe(new IsEmptyObserver(o));
6039 };
6040
6041 return IsEmptyObservable;
6042 }(ObservableBase));
6043
6044 var IsEmptyObserver = (function(__super__) {
6045 inherits(IsEmptyObserver, __super__);
6046 function IsEmptyObserver(o) {
6047 this._o = o;
6048 __super__.call(this);
6049 }
6050
6051 IsEmptyObserver.prototype.next = function () {
6052 this._o.onNext(false);
6053 this._o.onCompleted();
6054 };
6055 IsEmptyObserver.prototype.error = function (e) { this._o.onError(e); };
6056 IsEmptyObserver.prototype.completed = function () {
6057 this._o.onNext(true);
6058 this._o.onCompleted();
6059 };
6060
6061 return IsEmptyObserver;
6062 }(AbstractObserver));
6063
6064 /**
6065 * Determines whether an observable sequence is empty.
6066 * @returns {Observable} An observable sequence containing a single element determining whether the source sequence is empty.
6067 */
6068 observableProto.isEmpty = function () {
6069 return new IsEmptyObservable(this);
6070 };
6071
6072 var EveryObservable = (function (__super__) {
6073 inherits(EveryObservable, __super__);
6074 function EveryObservable(source, fn) {
6075 this.source = source;
6076 this._fn = fn;
6077 __super__.call(this);
6078 }
6079
6080 EveryObservable.prototype.subscribeCore = function (o) {
6081 return this.source.subscribe(new EveryObserver(o, this._fn, this.source));
6082 };
6083
6084 return EveryObservable;
6085 }(ObservableBase));
6086
6087 var EveryObserver = (function (__super__) {
6088 inherits(EveryObserver, __super__);
6089
6090 function EveryObserver(o, fn, s) {
6091 this._o = o;
6092 this._fn = fn;
6093 this._s = s;
6094 this._i = 0;
6095 __super__.call(this);
6096 }
6097
6098 EveryObserver.prototype.next = function (x) {
6099 var result = tryCatch(this._fn)(x, this._i++, this._s);
6100 if (result === errorObj) { return this._o.onError(result.e); }
6101 if (!Boolean(result)) {
6102 this._o.onNext(false);
6103 this._o.onCompleted();
6104 }
6105 };
6106 EveryObserver.prototype.error = function (e) { this._o.onError(e); };
6107 EveryObserver.prototype.completed = function () {
6108 this._o.onNext(true);
6109 this._o.onCompleted();
6110 };
6111
6112 return EveryObserver;
6113 }(AbstractObserver));
6114
6115 /**
6116 * Determines whether all elements of an observable sequence satisfy a condition.
6117 * @param {Function} [predicate] A function to test each element for a condition.
6118 * @param {Any} [thisArg] Object to use as this when executing callback.
6119 * @returns {Observable} An observable sequence containing a single element determining whether all elements in the source sequence pass the test in the specified predicate.
6120 */
6121 observableProto.every = function (predicate, thisArg) {
6122 var fn = bindCallback(predicate, thisArg, 3);
6123 return new EveryObservable(this, fn);
6124 };
6125
6126 var IncludesObservable = (function (__super__) {
6127 inherits(IncludesObservable, __super__);
6128 function IncludesObservable(source, elem, idx) {
6129 var n = +idx || 0;
6130 Math.abs(n) === Infinity && (n = 0);
6131
6132 this.source = source;
6133 this._elem = elem;
6134 this._n = n;
6135 __super__.call(this);
6136 }
6137
6138 IncludesObservable.prototype.subscribeCore = function (o) {
6139 if (this._n < 0) {
6140 o.onNext(false);
6141 o.onCompleted();
6142 return disposableEmpty;
6143 }
6144
6145 return this.source.subscribe(new IncludesObserver(o, this._elem, this._n));
6146 };
6147
6148 return IncludesObservable;
6149 }(ObservableBase));
6150
6151 var IncludesObserver = (function (__super__) {
6152 inherits(IncludesObserver, __super__);
6153 function IncludesObserver(o, elem, n) {
6154 this._o = o;
6155 this._elem = elem;
6156 this._n = n;
6157 this._i = 0;
6158 __super__.call(this);
6159 }
6160
6161 function comparer(a, b) {
6162 return (a === 0 && b === 0) || (a === b || (isNaN(a) && isNaN(b)));
6163 }
6164
6165 IncludesObserver.prototype.next = function (x) {
6166 if (this._i++ >= this._n && comparer(x, this._elem)) {
6167 this._o.onNext(true);
6168 this._o.onCompleted();
6169 }
6170 };
6171 IncludesObserver.prototype.error = function (e) { this._o.onError(e); };
6172 IncludesObserver.prototype.completed = function () { this._o.onNext(false); this._o.onCompleted(); };
6173
6174 return IncludesObserver;
6175 }(AbstractObserver));
6176
6177 /**
6178 * Determines whether an observable sequence includes a specified element with an optional equality comparer.
6179 * @param searchElement The value to locate in the source sequence.
6180 * @param {Number} [fromIndex] An equality comparer to compare elements.
6181 * @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.
6182 */
6183 observableProto.includes = function (searchElement, fromIndex) {
6184 return new IncludesObservable(this, searchElement, fromIndex);
6185 };
6186
6187 var CountObservable = (function (__super__) {
6188 inherits(CountObservable, __super__);
6189 function CountObservable(source, fn) {
6190 this.source = source;
6191 this._fn = fn;
6192 __super__.call(this);
6193 }
6194
6195 CountObservable.prototype.subscribeCore = function (o) {
6196 return this.source.subscribe(new CountObserver(o, this._fn, this.source));
6197 };
6198
6199 return CountObservable;
6200 }(ObservableBase));
6201
6202 var CountObserver = (function (__super__) {
6203 inherits(CountObserver, __super__);
6204
6205 function CountObserver(o, fn, s) {
6206 this._o = o;
6207 this._fn = fn;
6208 this._s = s;
6209 this._i = 0;
6210 this._c = 0;
6211 __super__.call(this);
6212 }
6213
6214 CountObserver.prototype.next = function (x) {
6215 if (this._fn) {
6216 var result = tryCatch(this._fn)(x, this._i++, this._s);
6217 if (result === errorObj) { return this._o.onError(result.e); }
6218 Boolean(result) && (this._c++);
6219 } else {
6220 this._c++;
6221 }
6222 };
6223 CountObserver.prototype.error = function (e) { this._o.onError(e); };
6224 CountObserver.prototype.completed = function () {
6225 this._o.onNext(this._c);
6226 this._o.onCompleted();
6227 };
6228
6229 return CountObserver;
6230 }(AbstractObserver));
6231
6232 /**
6233 * 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.
6234 * @example
6235 * res = source.count();
6236 * res = source.count(function (x) { return x > 3; });
6237 * @param {Function} [predicate]A function to test each element for a condition.
6238 * @param {Any} [thisArg] Object to use as this when executing callback.
6239 * @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.
6240 */
6241 observableProto.count = function (predicate, thisArg) {
6242 var fn = bindCallback(predicate, thisArg, 3);
6243 return new CountObservable(this, fn);
6244 };
6245
6246 var IndexOfObservable = (function (__super__) {
6247 inherits(IndexOfObservable, __super__);
6248 function IndexOfObservable(source, e, n) {
6249 this.source = source;
6250 this._e = e;
6251 this._n = n;
6252 __super__.call(this);
6253 }
6254
6255 IndexOfObservable.prototype.subscribeCore = function (o) {
6256 if (this._n < 0) {
6257 o.onNext(-1);
6258 o.onCompleted();
6259 return disposableEmpty;
6260 }
6261
6262 return this.source.subscribe(new IndexOfObserver(o, this._e, this._n));
6263 };
6264
6265 return IndexOfObservable;
6266 }(ObservableBase));
6267
6268 var IndexOfObserver = (function (__super__) {
6269 inherits(IndexOfObserver, __super__);
6270 function IndexOfObserver(o, e, n) {
6271 this._o = o;
6272 this._e = e;
6273 this._n = n;
6274 this._i = 0;
6275 __super__.call(this);
6276 }
6277
6278 IndexOfObserver.prototype.next = function (x) {
6279 if (this._i >= this._n && x === this._e) {
6280 this._o.onNext(this._i);
6281 this._o.onCompleted();
6282 }
6283 this._i++;
6284 };
6285 IndexOfObserver.prototype.error = function (e) { this._o.onError(e); };
6286 IndexOfObserver.prototype.completed = function () { this._o.onNext(-1); this._o.onCompleted(); };
6287
6288 return IndexOfObserver;
6289 }(AbstractObserver));
6290
6291 /**
6292 * Returns the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
6293 * @param {Any} searchElement Element to locate in the array.
6294 * @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
6295 * @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.
6296 */
6297 observableProto.indexOf = function(searchElement, fromIndex) {
6298 var n = +fromIndex || 0;
6299 Math.abs(n) === Infinity && (n = 0);
6300 return new IndexOfObservable(this, searchElement, n);
6301 };
6302
6303 var SumObservable = (function (__super__) {
6304 inherits(SumObservable, __super__);
6305 function SumObservable(source, fn) {
6306 this.source = source;
6307 this._fn = fn;
6308 __super__.call(this);
6309 }
6310
6311 SumObservable.prototype.subscribeCore = function (o) {
6312 return this.source.subscribe(new SumObserver(o, this._fn, this.source));
6313 };
6314
6315 return SumObservable;
6316 }(ObservableBase));
6317
6318 var SumObserver = (function (__super__) {
6319 inherits(SumObserver, __super__);
6320
6321 function SumObserver(o, fn, s) {
6322 this._o = o;
6323 this._fn = fn;
6324 this._s = s;
6325 this._i = 0;
6326 this._c = 0;
6327 __super__.call(this);
6328 }
6329
6330 SumObserver.prototype.next = function (x) {
6331 if (this._fn) {
6332 var result = tryCatch(this._fn)(x, this._i++, this._s);
6333 if (result === errorObj) { return this._o.onError(result.e); }
6334 this._c += result;
6335 } else {
6336 this._c += x;
6337 }
6338 };
6339 SumObserver.prototype.error = function (e) { this._o.onError(e); };
6340 SumObserver.prototype.completed = function () {
6341 this._o.onNext(this._c);
6342 this._o.onCompleted();
6343 };
6344
6345 return SumObserver;
6346 }(AbstractObserver));
6347
6348 /**
6349 * 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.
6350 * @param {Function} [selector] A transform function to apply to each element.
6351 * @param {Any} [thisArg] Object to use as this when executing callback.
6352 * @returns {Observable} An observable sequence containing a single element with the sum of the values in the source sequence.
6353 */
6354 observableProto.sum = function (keySelector, thisArg) {
6355 var fn = bindCallback(keySelector, thisArg, 3);
6356 return new SumObservable(this, fn);
6357 };
6358
6359 /**
6360 * Returns the elements in an observable sequence with the minimum key value according to the specified comparer.
6361 * @example
6362 * var res = source.minBy(function (x) { return x.value; });
6363 * var res = source.minBy(function (x) { return x.value; }, function (x, y) { return x - y; });
6364 * @param {Function} keySelector Key selector function.
6365 * @param {Function} [comparer] Comparer used to compare key values.
6366 * @returns {Observable} An observable sequence containing a list of zero or more elements that have a minimum key value.
6367 */
6368 observableProto.minBy = function (keySelector, comparer) {
6369 comparer || (comparer = defaultSubComparer);
6370 return new ExtremaByObservable(this, keySelector, function (x, y) { return comparer(x, y) * -1; });
6371 };
6372
6373 /**
6374 * Returns the minimum element in an observable sequence according to the optional comparer else a default greater than less than check.
6375 * @example
6376 * var res = source.min();
6377 * var res = source.min(function (x, y) { return x.value - y.value; });
6378 * @param {Function} [comparer] Comparer used to compare elements.
6379 * @returns {Observable} An observable sequence containing a single element with the minimum element in the source sequence.
6380 */
6381 observableProto.min = function (comparer) {
6382 return this.minBy(identity, comparer).map(function (x) { return firstOnly(x); });
6383 };
6384
6385 /**
6386 * Returns the elements in an observable sequence with the maximum key value according to the specified comparer.
6387 * @example
6388 * var res = source.maxBy(function (x) { return x.value; });
6389 * var res = source.maxBy(function (x) { return x.value; }, function (x, y) { return x - y;; });
6390 * @param {Function} keySelector Key selector function.
6391 * @param {Function} [comparer] Comparer used to compare key values.
6392 * @returns {Observable} An observable sequence containing a list of zero or more elements that have a maximum key value.
6393 */
6394 observableProto.maxBy = function (keySelector, comparer) {
6395 comparer || (comparer = defaultSubComparer);
6396 return new ExtremaByObservable(this, keySelector, comparer);
6397 };
6398
6399 /**
6400 * Returns the maximum value in an observable sequence according to the specified comparer.
6401 * @example
6402 * var res = source.max();
6403 * var res = source.max(function (x, y) { return x.value - y.value; });
6404 * @param {Function} [comparer] Comparer used to compare elements.
6405 * @returns {Observable} An observable sequence containing a single element with the maximum element in the source sequence.
6406 */
6407 observableProto.max = function (comparer) {
6408 return this.maxBy(identity, comparer).map(function (x) { return firstOnly(x); });
6409 };
6410
6411 var AverageObservable = (function (__super__) {
6412 inherits(AverageObservable, __super__);
6413 function AverageObservable(source, fn) {
6414 this.source = source;
6415 this._fn = fn;
6416 __super__.call(this);
6417 }
6418
6419 AverageObservable.prototype.subscribeCore = function (o) {
6420 return this.source.subscribe(new AverageObserver(o, this._fn, this.source));
6421 };
6422
6423 return AverageObservable;
6424 }(ObservableBase));
6425
6426 var AverageObserver = (function(__super__) {
6427 inherits(AverageObserver, __super__);
6428 function AverageObserver(o, fn, s) {
6429 this._o = o;
6430 this._fn = fn;
6431 this._s = s;
6432 this._c = 0;
6433 this._t = 0;
6434 __super__.call(this);
6435 }
6436
6437 AverageObserver.prototype.next = function (x) {
6438 if(this._fn) {
6439 var r = tryCatch(this._fn)(x, this._c++, this._s);
6440 if (r === errorObj) { return this._o.onError(r.e); }
6441 this._t += r;
6442 } else {
6443 this._c++;
6444 this._t += x;
6445 }
6446 };
6447 AverageObserver.prototype.error = function (e) { this._o.onError(e); };
6448 AverageObserver.prototype.completed = function () {
6449 if (this._c === 0) { return this._o.onError(new EmptyError()); }
6450 this._o.onNext(this._t / this._c);
6451 this._o.onCompleted();
6452 };
6453
6454 return AverageObserver;
6455 }(AbstractObserver));
6456
6457 /**
6458 * 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.
6459 * @param {Function} [selector] A transform function to apply to each element.
6460 * @param {Any} [thisArg] Object to use as this when executing callback.
6461 * @returns {Observable} An observable sequence containing a single element with the average of the sequence of values.
6462 */
6463 observableProto.average = function (keySelector, thisArg) {
6464 var source = this, fn;
6465 if (isFunction(keySelector)) {
6466 fn = bindCallback(keySelector, thisArg, 3);
6467 }
6468 return new AverageObservable(source, fn);
6469 };
6470
6471 /**
6472 * Determines whether two sequences are equal by comparing the elements pairwise using a specified equality comparer.
6473 *
6474 * @example
6475 * var res = res = source.sequenceEqual([1,2,3]);
6476 * var res = res = source.sequenceEqual([{ value: 42 }], function (x, y) { return x.value === y.value; });
6477 * 3 - res = source.sequenceEqual(Rx.Observable.returnValue(42));
6478 * 4 - res = source.sequenceEqual(Rx.Observable.returnValue({ value: 42 }), function (x, y) { return x.value === y.value; });
6479 * @param {Observable} second Second observable sequence or array to compare.
6480 * @param {Function} [comparer] Comparer used to compare elements of both sequences.
6481 * @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.
6482 */
6483 observableProto.sequenceEqual = function (second, comparer) {
6484 var first = this;
6485 comparer || (comparer = defaultComparer);
6486 return new AnonymousObservable(function (o) {
6487 var donel = false, doner = false, ql = [], qr = [];
6488 var subscription1 = first.subscribe(function (x) {
6489 if (qr.length > 0) {
6490 var v = qr.shift();
6491 var equal = tryCatch(comparer)(v, x);
6492 if (equal === errorObj) { return o.onError(equal.e); }
6493 if (!equal) {
6494 o.onNext(false);
6495 o.onCompleted();
6496 }
6497 } else if (doner) {
6498 o.onNext(false);
6499 o.onCompleted();
6500 } else {
6501 ql.push(x);
6502 }
6503 }, function(e) { o.onError(e); }, function () {
6504 donel = true;
6505 if (ql.length === 0) {
6506 if (qr.length > 0) {
6507 o.onNext(false);
6508 o.onCompleted();
6509 } else if (doner) {
6510 o.onNext(true);
6511 o.onCompleted();
6512 }
6513 }
6514 });
6515
6516 (isArrayLike(second) || isIterable(second)) && (second = observableFrom(second));
6517 isPromise(second) && (second = observableFromPromise(second));
6518 var subscription2 = second.subscribe(function (x) {
6519 if (ql.length > 0) {
6520 var v = ql.shift();
6521 var equal = tryCatch(comparer)(v, x);
6522 if (equal === errorObj) { return o.onError(equal.e); }
6523 if (!equal) {
6524 o.onNext(false);
6525 o.onCompleted();
6526 }
6527 } else if (donel) {
6528 o.onNext(false);
6529 o.onCompleted();
6530 } else {
6531 qr.push(x);
6532 }
6533 }, function(e) { o.onError(e); }, function () {
6534 doner = true;
6535 if (qr.length === 0) {
6536 if (ql.length > 0) {
6537 o.onNext(false);
6538 o.onCompleted();
6539 } else if (donel) {
6540 o.onNext(true);
6541 o.onCompleted();
6542 }
6543 }
6544 });
6545 return new BinaryDisposable(subscription1, subscription2);
6546 }, first);
6547 };
6548
6549 var ElementAtObservable = (function (__super__) {
6550 inherits(ElementAtObservable, __super__);
6551 function ElementAtObservable(source, i, d) {
6552 this.source = source;
6553 this._i = i;
6554 this._d = d;
6555 __super__.call(this);
6556 }
6557
6558 ElementAtObservable.prototype.subscribeCore = function (o) {
6559 return this.source.subscribe(new ElementAtObserver(o, this._i, this._d));
6560 };
6561
6562 return ElementAtObservable;
6563 }(ObservableBase));
6564
6565 var ElementAtObserver = (function (__super__) {
6566 inherits(ElementAtObserver, __super__);
6567
6568 function ElementAtObserver(o, i, d) {
6569 this._o = o;
6570 this._i = i;
6571 this._d = d;
6572 __super__.call(this);
6573 }
6574
6575 ElementAtObserver.prototype.next = function (x) {
6576 if (this._i-- === 0) {
6577 this._o.onNext(x);
6578 this._o.onCompleted();
6579 }
6580 };
6581 ElementAtObserver.prototype.error = function (e) { this._o.onError(e); };
6582 ElementAtObserver.prototype.completed = function () {
6583 if (this._d === undefined) {
6584 this._o.onError(new ArgumentOutOfRangeError());
6585 } else {
6586 this._o.onNext(this._d);
6587 this._o.onCompleted();
6588 }
6589 };
6590
6591 return ElementAtObserver;
6592 }(AbstractObserver));
6593
6594 /**
6595 * Returns the element at a specified index in a sequence or default value if not found.
6596 * @param {Number} index The zero-based index of the element to retrieve.
6597 * @param {Any} [defaultValue] The default value to use if elementAt does not find a value.
6598 * @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence.
6599 */
6600 observableProto.elementAt = function (index, defaultValue) {
6601 if (index < 0) { throw new ArgumentOutOfRangeError(); }
6602 return new ElementAtObservable(this, index, defaultValue);
6603 };
6604
6605 var SingleObserver = (function(__super__) {
6606 inherits(SingleObserver, __super__);
6607 function SingleObserver(o, obj, s) {
6608 this._o = o;
6609 this._obj = obj;
6610 this._s = s;
6611 this._i = 0;
6612 this._hv = false;
6613 this._v = null;
6614 __super__.call(this);
6615 }
6616
6617 SingleObserver.prototype.next = function (x) {
6618 var shouldYield = false;
6619 if (this._obj.predicate) {
6620 var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
6621 if (res === errorObj) { return this._o.onError(res.e); }
6622 Boolean(res) && (shouldYield = true);
6623 } else if (!this._obj.predicate) {
6624 shouldYield = true;
6625 }
6626 if (shouldYield) {
6627 if (this._hv) {
6628 return this._o.onError(new Error('Sequence contains more than one matching element'));
6629 }
6630 this._hv = true;
6631 this._v = x;
6632 }
6633 };
6634 SingleObserver.prototype.error = function (e) { this._o.onError(e); };
6635 SingleObserver.prototype.completed = function () {
6636 if (this._hv) {
6637 this._o.onNext(this._v);
6638 this._o.onCompleted();
6639 }
6640 else if (this._obj.defaultValue === undefined) {
6641 this._o.onError(new EmptyError());
6642 } else {
6643 this._o.onNext(this._obj.defaultValue);
6644 this._o.onCompleted();
6645 }
6646 };
6647
6648 return SingleObserver;
6649 }(AbstractObserver));
6650
6651
6652 /**
6653 * 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.
6654 * @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.
6655 */
6656 observableProto.single = function (predicate, thisArg) {
6657 var obj = {}, source = this;
6658 if (typeof arguments[0] === 'object') {
6659 obj = arguments[0];
6660 } else {
6661 obj = {
6662 predicate: arguments[0],
6663 thisArg: arguments[1],
6664 defaultValue: arguments[2]
6665 };
6666 }
6667 if (isFunction (obj.predicate)) {
6668 var fn = obj.predicate;
6669 obj.predicate = bindCallback(fn, obj.thisArg, 3);
6670 }
6671 return new AnonymousObservable(function (o) {
6672 return source.subscribe(new SingleObserver(o, obj, source));
6673 }, source);
6674 };
6675
6676 var FirstObservable = (function (__super__) {
6677 inherits(FirstObservable, __super__);
6678 function FirstObservable(source, obj) {
6679 this.source = source;
6680 this._obj = obj;
6681 __super__.call(this);
6682 }
6683
6684 FirstObservable.prototype.subscribeCore = function (o) {
6685 return this.source.subscribe(new FirstObserver(o, this._obj, this.source));
6686 };
6687
6688 return FirstObservable;
6689 }(ObservableBase));
6690
6691 var FirstObserver = (function(__super__) {
6692 inherits(FirstObserver, __super__);
6693 function FirstObserver(o, obj, s) {
6694 this._o = o;
6695 this._obj = obj;
6696 this._s = s;
6697 this._i = 0;
6698 __super__.call(this);
6699 }
6700
6701 FirstObserver.prototype.next = function (x) {
6702 if (this._obj.predicate) {
6703 var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
6704 if (res === errorObj) { return this._o.onError(res.e); }
6705 if (Boolean(res)) {
6706 this._o.onNext(x);
6707 this._o.onCompleted();
6708 }
6709 } else if (!this._obj.predicate) {
6710 this._o.onNext(x);
6711 this._o.onCompleted();
6712 }
6713 };
6714 FirstObserver.prototype.error = function (e) { this._o.onError(e); };
6715 FirstObserver.prototype.completed = function () {
6716 if (this._obj.defaultValue === undefined) {
6717 this._o.onError(new EmptyError());
6718 } else {
6719 this._o.onNext(this._obj.defaultValue);
6720 this._o.onCompleted();
6721 }
6722 };
6723
6724 return FirstObserver;
6725 }(AbstractObserver));
6726
6727 /**
6728 * Returns the first element of an observable sequence that satisfies the condition in the predicate if present else the first item in the sequence.
6729 * @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.
6730 */
6731 observableProto.first = function () {
6732 var obj = {}, source = this;
6733 if (typeof arguments[0] === 'object') {
6734 obj = arguments[0];
6735 } else {
6736 obj = {
6737 predicate: arguments[0],
6738 thisArg: arguments[1],
6739 defaultValue: arguments[2]
6740 };
6741 }
6742 if (isFunction (obj.predicate)) {
6743 var fn = obj.predicate;
6744 obj.predicate = bindCallback(fn, obj.thisArg, 3);
6745 }
6746 return new FirstObservable(this, obj);
6747 };
6748
6749 var LastObservable = (function (__super__) {
6750 inherits(LastObservable, __super__);
6751 function LastObservable(source, obj) {
6752 this.source = source;
6753 this._obj = obj;
6754 __super__.call(this);
6755 }
6756
6757 LastObservable.prototype.subscribeCore = function (o) {
6758 return this.source.subscribe(new LastObserver(o, this._obj, this.source));
6759 };
6760
6761 return LastObservable;
6762 }(ObservableBase));
6763
6764 var LastObserver = (function(__super__) {
6765 inherits(LastObserver, __super__);
6766 function LastObserver(o, obj, s) {
6767 this._o = o;
6768 this._obj = obj;
6769 this._s = s;
6770 this._i = 0;
6771 this._hv = false;
6772 this._v = null;
6773 __super__.call(this);
6774 }
6775
6776 LastObserver.prototype.next = function (x) {
6777 var shouldYield = false;
6778 if (this._obj.predicate) {
6779 var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
6780 if (res === errorObj) { return this._o.onError(res.e); }
6781 Boolean(res) && (shouldYield = true);
6782 } else if (!this._obj.predicate) {
6783 shouldYield = true;
6784 }
6785 if (shouldYield) {
6786 this._hv = true;
6787 this._v = x;
6788 }
6789 };
6790 LastObserver.prototype.error = function (e) { this._o.onError(e); };
6791 LastObserver.prototype.completed = function () {
6792 if (this._hv) {
6793 this._o.onNext(this._v);
6794 this._o.onCompleted();
6795 }
6796 else if (this._obj.defaultValue === undefined) {
6797 this._o.onError(new EmptyError());
6798 } else {
6799 this._o.onNext(this._obj.defaultValue);
6800 this._o.onCompleted();
6801 }
6802 };
6803
6804 return LastObserver;
6805 }(AbstractObserver));
6806
6807 /**
6808 * Returns the last element of an observable sequence that satisfies the condition in the predicate if specified, else the last element.
6809 * @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate.
6810 */
6811 observableProto.last = function () {
6812 var obj = {}, source = this;
6813 if (typeof arguments[0] === 'object') {
6814 obj = arguments[0];
6815 } else {
6816 obj = {
6817 predicate: arguments[0],
6818 thisArg: arguments[1],
6819 defaultValue: arguments[2]
6820 };
6821 }
6822 if (isFunction (obj.predicate)) {
6823 var fn = obj.predicate;
6824 obj.predicate = bindCallback(fn, obj.thisArg, 3);
6825 }
6826 return new LastObservable(this, obj);
6827 };
6828
6829 var FindValueObserver = (function(__super__) {
6830 inherits(FindValueObserver, __super__);
6831 function FindValueObserver(observer, source, callback, yieldIndex) {
6832 this._o = observer;
6833 this._s = source;
6834 this._cb = callback;
6835 this._y = yieldIndex;
6836 this._i = 0;
6837 __super__.call(this);
6838 }
6839
6840 FindValueObserver.prototype.next = function (x) {
6841 var shouldRun = tryCatch(this._cb)(x, this._i, this._s);
6842 if (shouldRun === errorObj) { return this._o.onError(shouldRun.e); }
6843 if (shouldRun) {
6844 this._o.onNext(this._y ? this._i : x);
6845 this._o.onCompleted();
6846 } else {
6847 this._i++;
6848 }
6849 };
6850
6851 FindValueObserver.prototype.error = function (e) {
6852 this._o.onError(e);
6853 };
6854
6855 FindValueObserver.prototype.completed = function () {
6856 this._y && this._o.onNext(-1);
6857 this._o.onCompleted();
6858 };
6859
6860 return FindValueObserver;
6861 }(AbstractObserver));
6862
6863 function findValue (source, predicate, thisArg, yieldIndex) {
6864 var callback = bindCallback(predicate, thisArg, 3);
6865 return new AnonymousObservable(function (o) {
6866 return source.subscribe(new FindValueObserver(o, source, callback, yieldIndex));
6867 }, source);
6868 }
6869
6870 /**
6871 * Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Observable sequence.
6872 * @param {Function} predicate The predicate that defines the conditions of the element to search for.
6873 * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
6874 * @returns {Observable} An Observable sequence with the first element that matches the conditions defined by the specified predicate, if found; otherwise, undefined.
6875 */
6876 observableProto.find = function (predicate, thisArg) {
6877 return findValue(this, predicate, thisArg, false);
6878 };
6879
6880 /**
6881 * Searches for an element that matches the conditions defined by the specified predicate, and returns
6882 * an Observable sequence with the zero-based index of the first occurrence within the entire Observable sequence.
6883 * @param {Function} predicate The predicate that defines the conditions of the element to search for.
6884 * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
6885 * @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.
6886 */
6887 observableProto.findIndex = function (predicate, thisArg) {
6888 return findValue(this, predicate, thisArg, true);
6889 };
6890
6891 var ToSetObservable = (function (__super__) {
6892 inherits(ToSetObservable, __super__);
6893 function ToSetObservable(source) {
6894 this.source = source;
6895 __super__.call(this);
6896 }
6897
6898 ToSetObservable.prototype.subscribeCore = function (o) {
6899 return this.source.subscribe(new ToSetObserver(o));
6900 };
6901
6902 return ToSetObservable;
6903 }(ObservableBase));
6904
6905 var ToSetObserver = (function (__super__) {
6906 inherits(ToSetObserver, __super__);
6907 function ToSetObserver(o) {
6908 this._o = o;
6909 this._s = new root.Set();
6910 __super__.call(this);
6911 }
6912
6913 ToSetObserver.prototype.next = function (x) {
6914 this._s.add(x);
6915 };
6916
6917 ToSetObserver.prototype.error = function (e) {
6918 this._o.onError(e);
6919 };
6920
6921 ToSetObserver.prototype.completed = function () {
6922 this._o.onNext(this._s);
6923 this._o.onCompleted();
6924 };
6925
6926 return ToSetObserver;
6927 }(AbstractObserver));
6928
6929 /**
6930 * Converts the observable sequence to a Set if it exists.
6931 * @returns {Observable} An observable sequence with a single value of a Set containing the values from the observable sequence.
6932 */
6933 observableProto.toSet = function () {
6934 if (typeof root.Set === 'undefined') { throw new TypeError(); }
6935 return new ToSetObservable(this);
6936 };
6937
6938 var ToMapObservable = (function (__super__) {
6939 inherits(ToMapObservable, __super__);
6940 function ToMapObservable(source, k, e) {
6941 this.source = source;
6942 this._k = k;
6943 this._e = e;
6944 __super__.call(this);
6945 }
6946
6947 ToMapObservable.prototype.subscribeCore = function (o) {
6948 return this.source.subscribe(new ToMapObserver(o, this._k, this._e));
6949 };
6950
6951 return ToMapObservable;
6952 }(ObservableBase));
6953
6954 var ToMapObserver = (function (__super__) {
6955 inherits(ToMapObserver, __super__);
6956 function ToMapObserver(o, k, e) {
6957 this._o = o;
6958 this._k = k;
6959 this._e = e;
6960 this._m = new root.Map();
6961 __super__.call(this);
6962 }
6963
6964 ToMapObserver.prototype.next = function (x) {
6965 var key = tryCatch(this._k)(x);
6966 if (key === errorObj) { return this._o.onError(key.e); }
6967 var elem = x;
6968 if (this._e) {
6969 elem = tryCatch(this._e)(x);
6970 if (elem === errorObj) { return this._o.onError(elem.e); }
6971 }
6972
6973 this._m.set(key, elem);
6974 };
6975
6976 ToMapObserver.prototype.error = function (e) {
6977 this._o.onError(e);
6978 };
6979
6980 ToMapObserver.prototype.completed = function () {
6981 this._o.onNext(this._m);
6982 this._o.onCompleted();
6983 };
6984
6985 return ToMapObserver;
6986 }(AbstractObserver));
6987
6988 /**
6989 * Converts the observable sequence to a Map if it exists.
6990 * @param {Function} keySelector A function which produces the key for the Map.
6991 * @param {Function} [elementSelector] An optional function which produces the element for the Map. If not present, defaults to the value from the observable sequence.
6992 * @returns {Observable} An observable sequence with a single value of a Map containing the values from the observable sequence.
6993 */
6994 observableProto.toMap = function (keySelector, elementSelector) {
6995 if (typeof root.Map === 'undefined') { throw new TypeError(); }
6996 return new ToMapObservable(this, keySelector, elementSelector);
6997 };
6998
6999 var SliceObservable = (function (__super__) {
7000 inherits(SliceObservable, __super__);
7001 function SliceObservable(source, b, e) {
7002 this.source = source;
7003 this._b = b;
7004 this._e = e;
7005 __super__.call(this);
7006 }
7007
7008 SliceObservable.prototype.subscribeCore = function (o) {
7009 return this.source.subscribe(new SliceObserver(o, this._b, this._e));
7010 };
7011
7012 return SliceObservable;
7013 }(ObservableBase));
7014
7015 var SliceObserver = (function (__super__) {
7016 inherits(SliceObserver, __super__);
7017
7018 function SliceObserver(o, b, e) {
7019 this._o = o;
7020 this._b = b;
7021 this._e = e;
7022 this._i = 0;
7023 __super__.call(this);
7024 }
7025
7026 SliceObserver.prototype.next = function (x) {
7027 if (this._i >= this._b) {
7028 if (this._e === this._i) {
7029 this._o.onCompleted();
7030 } else {
7031 this._o.onNext(x);
7032 }
7033 }
7034 this._i++;
7035 };
7036 SliceObserver.prototype.error = function (e) { this._o.onError(e); };
7037 SliceObserver.prototype.completed = function () { this._o.onCompleted(); };
7038
7039 return SliceObserver;
7040 }(AbstractObserver));
7041
7042 /*
7043 * The slice() method returns a shallow copy of a portion of an Observable into a new Observable object.
7044 * Unlike the array version, this does not support negative numbers for being or end.
7045 * @param {Number} [begin] Zero-based index at which to begin extraction. If omitted, this will default to zero.
7046 * @param {Number} [end] Zero-based index at which to end extraction. slice extracts up to but not including end.
7047 * If omitted, this will emit the rest of the Observable object.
7048 * @returns {Observable} A shallow copy of a portion of an Observable into a new Observable object.
7049 */
7050 observableProto.slice = function (begin, end) {
7051 var start = begin || 0;
7052 if (start < 0) { throw new Rx.ArgumentOutOfRangeError(); }
7053 if (typeof end === 'number' && end < start) {
7054 throw new Rx.ArgumentOutOfRangeError();
7055 }
7056 return new SliceObservable(this, start, end);
7057 };
7058
7059 var LastIndexOfObservable = (function (__super__) {
7060 inherits(LastIndexOfObservable, __super__);
7061 function LastIndexOfObservable(source, e, n) {
7062 this.source = source;
7063 this._e = e;
7064 this._n = n;
7065 __super__.call(this);
7066 }
7067
7068 LastIndexOfObservable.prototype.subscribeCore = function (o) {
7069 if (this._n < 0) {
7070 o.onNext(-1);
7071 o.onCompleted();
7072 return disposableEmpty;
7073 }
7074
7075 return this.source.subscribe(new LastIndexOfObserver(o, this._e, this._n));
7076 };
7077
7078 return LastIndexOfObservable;
7079 }(ObservableBase));
7080
7081 var LastIndexOfObserver = (function (__super__) {
7082 inherits(LastIndexOfObserver, __super__);
7083 function LastIndexOfObserver(o, e, n) {
7084 this._o = o;
7085 this._e = e;
7086 this._n = n;
7087 this._v = 0;
7088 this._hv = false;
7089 this._i = 0;
7090 __super__.call(this);
7091 }
7092
7093 LastIndexOfObserver.prototype.next = function (x) {
7094 if (this._i >= this._n && x === this._e) {
7095 this._hv = true;
7096 this._v = this._i;
7097 }
7098 this._i++;
7099 };
7100 LastIndexOfObserver.prototype.error = function (e) { this._o.onError(e); };
7101 LastIndexOfObserver.prototype.completed = function () {
7102 if (this._hv) {
7103 this._o.onNext(this._v);
7104 } else {
7105 this._o.onNext(-1);
7106 }
7107 this._o.onCompleted();
7108 };
7109
7110 return LastIndexOfObserver;
7111 }(AbstractObserver));
7112
7113 /**
7114 * Returns the last index at which a given element can be found in the observable sequence, or -1 if it is not present.
7115 * @param {Any} searchElement Element to locate in the array.
7116 * @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
7117 * @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.
7118 */
7119 observableProto.lastIndexOf = function(searchElement, fromIndex) {
7120 var n = +fromIndex || 0;
7121 Math.abs(n) === Infinity && (n = 0);
7122 return new LastIndexOfObservable(this, searchElement, n);
7123 };
7124
7125 Observable.wrap = function (fn) {
7126 function createObservable() {
7127 return Observable.spawn.call(this, fn.apply(this, arguments));
7128 }
7129
7130 createObservable.__generatorFunction__ = fn;
7131 return createObservable;
7132 };
7133
7134 var spawn = Observable.spawn = function () {
7135 var gen = arguments[0], self = this, args = [];
7136 for (var i = 1, len = arguments.length; i < len; i++) { args.push(arguments[i]); }
7137
7138 return new AnonymousObservable(function (o) {
7139 var g = new CompositeDisposable();
7140
7141 if (isFunction(gen)) { gen = gen.apply(self, args); }
7142 if (!gen || !isFunction(gen.next)) {
7143 o.onNext(gen);
7144 return o.onCompleted();
7145 }
7146
7147 function processGenerator(res) {
7148 var ret = tryCatch(gen.next).call(gen, res);
7149 if (ret === errorObj) { return o.onError(ret.e); }
7150 next(ret);
7151 }
7152
7153 processGenerator();
7154
7155 function onError(err) {
7156 var ret = tryCatch(gen.next).call(gen, err);
7157 if (ret === errorObj) { return o.onError(ret.e); }
7158 next(ret);
7159 }
7160
7161 function next(ret) {
7162 if (ret.done) {
7163 o.onNext(ret.value);
7164 o.onCompleted();
7165 return;
7166 }
7167 var obs = toObservable.call(self, ret.value);
7168 var value = null;
7169 var hasValue = false;
7170 if (Observable.isObservable(obs)) {
7171 g.add(obs.subscribe(function(val) {
7172 hasValue = true;
7173 value = val;
7174 }, onError, function() {
7175 hasValue && processGenerator(value);
7176 }));
7177 } else {
7178 onError(new TypeError('type not supported'));
7179 }
7180 }
7181
7182 return g;
7183 });
7184 };
7185
7186 function toObservable(obj) {
7187 if (!obj) { return obj; }
7188 if (Observable.isObservable(obj)) { return obj; }
7189 if (isPromise(obj)) { return Observable.fromPromise(obj); }
7190 if (isGeneratorFunction(obj) || isGenerator(obj)) { return spawn.call(this, obj); }
7191 if (isFunction(obj)) { return thunkToObservable.call(this, obj); }
7192 if (isArrayLike(obj) || isIterable(obj)) { return arrayToObservable.call(this, obj); }
7193 if (isObject(obj)) {return objectToObservable.call(this, obj);}
7194 return obj;
7195 }
7196
7197 function arrayToObservable (obj) {
7198 return Observable.from(obj).concatMap(function(o) {
7199 if(Observable.isObservable(o) || isObject(o)) {
7200 return toObservable.call(null, o);
7201 } else {
7202 return Rx.Observable.just(o);
7203 }
7204 }).toArray();
7205 }
7206
7207 function objectToObservable (obj) {
7208 var results = new obj.constructor(), keys = Object.keys(obj), observables = [];
7209 for (var i = 0, len = keys.length; i < len; i++) {
7210 var key = keys[i];
7211 var observable = toObservable.call(this, obj[key]);
7212
7213 if(observable && Observable.isObservable(observable)) {
7214 defer(observable, key);
7215 } else {
7216 results[key] = obj[key];
7217 }
7218 }
7219
7220 return Observable.forkJoin.apply(Observable, observables).map(function() {
7221 return results;
7222 });
7223
7224
7225 function defer (observable, key) {
7226 results[key] = undefined;
7227 observables.push(observable.map(function (next) {
7228 results[key] = next;
7229 }));
7230 }
7231 }
7232
7233 function thunkToObservable(fn) {
7234 var self = this;
7235 return new AnonymousObservable(function (o) {
7236 fn.call(self, function () {
7237 var err = arguments[0], res = arguments[1];
7238 if (err) { return o.onError(err); }
7239 if (arguments.length > 2) {
7240 var args = [];
7241 for (var i = 1, len = arguments.length; i < len; i++) { args.push(arguments[i]); }
7242 res = args;
7243 }
7244 o.onNext(res);
7245 o.onCompleted();
7246 });
7247 });
7248 }
7249
7250 function isGenerator(obj) {
7251 return isFunction (obj.next) && isFunction (obj['throw']);
7252 }
7253
7254 function isGeneratorFunction(obj) {
7255 var ctor = obj.constructor;
7256 if (!ctor) { return false; }
7257 if (ctor.name === 'GeneratorFunction' || ctor.displayName === 'GeneratorFunction') { return true; }
7258 return isGenerator(ctor.prototype);
7259 }
7260
7261 function isObject(val) {
7262 return Object == val.constructor;
7263 }
7264
7265 /**
7266 * Invokes the specified function asynchronously on the specified scheduler, surfacing the result through an observable sequence.
7267 *
7268 * @example
7269 * var res = Rx.Observable.start(function () { console.log('hello'); });
7270 * var res = Rx.Observable.start(function () { console.log('hello'); }, Rx.Scheduler.timeout);
7271 * var res = Rx.Observable.start(function () { this.log('hello'); }, Rx.Scheduler.timeout, console);
7272 *
7273 * @param {Function} func Function to run asynchronously.
7274 * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
7275 * @param [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
7276 * @returns {Observable} An observable sequence exposing the function's result value, or an exception.
7277 *
7278 * Remarks
7279 * * The function is called immediately, not during the subscription of the resulting sequence.
7280 * * Multiple subscriptions to the resulting sequence can observe the function's result.
7281 */
7282 Observable.start = function (func, context, scheduler) {
7283 return observableToAsync(func, context, scheduler)();
7284 };
7285
7286 /**
7287 * Converts the function into an asynchronous function. Each invocation of the resulting asynchronous function causes an invocation of the original synchronous function on the specified scheduler.
7288 * @param {Function} function Function to convert to an asynchronous function.
7289 * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
7290 * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
7291 * @returns {Function} Asynchronous function.
7292 */
7293 var observableToAsync = Observable.toAsync = function (func, context, scheduler) {
7294 isScheduler(scheduler) || (scheduler = defaultScheduler);
7295 return function () {
7296 var args = arguments,
7297 subject = new AsyncSubject();
7298
7299 scheduler.schedule(null, function () {
7300 var result;
7301 try {
7302 result = func.apply(context, args);
7303 } catch (e) {
7304 subject.onError(e);
7305 return;
7306 }
7307 subject.onNext(result);
7308 subject.onCompleted();
7309 });
7310 return subject.asObservable();
7311 };
7312 };
7313
7314function createCbObservable(fn, ctx, selector, args) {
7315 var o = new AsyncSubject();
7316
7317 args.push(createCbHandler(o, ctx, selector));
7318 fn.apply(ctx, args);
7319
7320 return o.asObservable();
7321}
7322
7323function createCbHandler(o, ctx, selector) {
7324 return function handler () {
7325 var len = arguments.length, results = new Array(len);
7326 for(var i = 0; i < len; i++) { results[i] = arguments[i]; }
7327
7328 if (isFunction(selector)) {
7329 results = tryCatch(selector).apply(ctx, results);
7330 if (results === errorObj) { return o.onError(results.e); }
7331 o.onNext(results);
7332 } else {
7333 if (results.length <= 1) {
7334 o.onNext(results[0]);
7335 } else {
7336 o.onNext(results);
7337 }
7338 }
7339
7340 o.onCompleted();
7341 };
7342}
7343
7344/**
7345 * Converts a callback function to an observable sequence.
7346 *
7347 * @param {Function} fn Function with a callback as the last parameter to convert to an Observable sequence.
7348 * @param {Mixed} [ctx] The context for the func parameter to be executed. If not specified, defaults to undefined.
7349 * @param {Function} [selector] A selector which takes the arguments from the callback to produce a single item to yield on next.
7350 * @returns {Function} A function, when executed with the required parameters minus the callback, produces an Observable sequence with a single value of the arguments to the callback as an array.
7351 */
7352Observable.fromCallback = function (fn, ctx, selector) {
7353 return function () {
7354 typeof ctx === 'undefined' && (ctx = this);
7355
7356 var len = arguments.length, args = new Array(len)
7357 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
7358 return createCbObservable(fn, ctx, selector, args);
7359 };
7360};
7361
7362function createNodeObservable(fn, ctx, selector, args) {
7363 var o = new AsyncSubject();
7364
7365 args.push(createNodeHandler(o, ctx, selector));
7366 fn.apply(ctx, args);
7367
7368 return o.asObservable();
7369}
7370
7371function createNodeHandler(o, ctx, selector) {
7372 return function handler () {
7373 var err = arguments[0];
7374 if (err) { return o.onError(err); }
7375
7376 var len = arguments.length, results = [];
7377 for(var i = 1; i < len; i++) { results[i - 1] = arguments[i]; }
7378
7379 if (isFunction(selector)) {
7380 var results = tryCatch(selector).apply(ctx, results);
7381 if (results === errorObj) { return o.onError(results.e); }
7382 o.onNext(results);
7383 } else {
7384 if (results.length <= 1) {
7385 o.onNext(results[0]);
7386 } else {
7387 o.onNext(results);
7388 }
7389 }
7390
7391 o.onCompleted();
7392 };
7393}
7394
7395/**
7396 * Converts a Node.js callback style function to an observable sequence. This must be in function (err, ...) format.
7397 * @param {Function} fn The function to call
7398 * @param {Mixed} [ctx] The context for the func parameter to be executed. If not specified, defaults to undefined.
7399 * @param {Function} [selector] A selector which takes the arguments from the callback minus the error to produce a single item to yield on next.
7400 * @returns {Function} An async function which when applied, returns an observable sequence with the callback arguments as an array.
7401 */
7402Observable.fromNodeCallback = function (fn, ctx, selector) {
7403 return function () {
7404 typeof ctx === 'undefined' && (ctx = this);
7405 var len = arguments.length, args = new Array(len);
7406 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
7407 return createNodeObservable(fn, ctx, selector, args);
7408 };
7409};
7410
7411 function isNodeList(el) {
7412 if (root.StaticNodeList) {
7413 // IE8 Specific
7414 // instanceof is slower than Object#toString, but Object#toString will not work as intended in IE8
7415 return el instanceof root.StaticNodeList || el instanceof root.NodeList;
7416 } else {
7417 return Object.prototype.toString.call(el) === '[object NodeList]';
7418 }
7419 }
7420
7421 function ListenDisposable(e, n, fn) {
7422 this._e = e;
7423 this._n = n;
7424 this._fn = fn;
7425 this._e.addEventListener(this._n, this._fn, false);
7426 this.isDisposed = false;
7427 }
7428 ListenDisposable.prototype.dispose = function () {
7429 if (!this.isDisposed) {
7430 this._e.removeEventListener(this._n, this._fn, false);
7431 this.isDisposed = true;
7432 }
7433 };
7434
7435 function createEventListener (el, eventName, handler) {
7436 var disposables = new CompositeDisposable();
7437
7438 // Asume NodeList or HTMLCollection
7439 var elemToString = Object.prototype.toString.call(el);
7440 if (isNodeList(el) || elemToString === '[object HTMLCollection]') {
7441 for (var i = 0, len = el.length; i < len; i++) {
7442 disposables.add(createEventListener(el.item(i), eventName, handler));
7443 }
7444 } else if (el) {
7445 disposables.add(new ListenDisposable(el, eventName, handler));
7446 }
7447
7448 return disposables;
7449 }
7450
7451 /**
7452 * Configuration option to determine whether to use native events only
7453 */
7454 Rx.config.useNativeEvents = false;
7455
7456 var EventObservable = (function(__super__) {
7457 inherits(EventObservable, __super__);
7458 function EventObservable(el, name, fn) {
7459 this._el = el;
7460 this._n = name;
7461 this._fn = fn;
7462 __super__.call(this);
7463 }
7464
7465 function createHandler(o, fn) {
7466 return function handler () {
7467 var results = arguments[0];
7468 if (isFunction(fn)) {
7469 results = tryCatch(fn).apply(null, arguments);
7470 if (results === errorObj) { return o.onError(results.e); }
7471 }
7472 o.onNext(results);
7473 };
7474 }
7475
7476 EventObservable.prototype.subscribeCore = function (o) {
7477 return createEventListener(
7478 this._el,
7479 this._n,
7480 createHandler(o, this._fn));
7481 };
7482
7483 return EventObservable;
7484 }(ObservableBase));
7485
7486 /**
7487 * Creates an observable sequence by adding an event listener to the matching DOMElement or each item in the NodeList.
7488 * @param {Object} element The DOMElement or NodeList to attach a listener.
7489 * @param {String} eventName The event name to attach the observable sequence.
7490 * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
7491 * @returns {Observable} An observable sequence of events from the specified element and the specified event.
7492 */
7493 Observable.fromEvent = function (element, eventName, selector) {
7494 // Node.js specific
7495 if (element.addListener) {
7496 return fromEventPattern(
7497 function (h) { element.addListener(eventName, h); },
7498 function (h) { element.removeListener(eventName, h); },
7499 selector);
7500 }
7501
7502 // Use only if non-native events are allowed
7503 if (!Rx.config.useNativeEvents) {
7504 // Handles jq, Angular.js, Zepto, Marionette, Ember.js
7505 if (typeof element.on === 'function' && typeof element.off === 'function') {
7506 return fromEventPattern(
7507 function (h) { element.on(eventName, h); },
7508 function (h) { element.off(eventName, h); },
7509 selector);
7510 }
7511 }
7512
7513 return new EventObservable(element, eventName, selector).publish().refCount();
7514 };
7515
7516 var EventPatternObservable = (function(__super__) {
7517 inherits(EventPatternObservable, __super__);
7518 function EventPatternObservable(add, del, fn) {
7519 this._add = add;
7520 this._del = del;
7521 this._fn = fn;
7522 __super__.call(this);
7523 }
7524
7525 function createHandler(o, fn) {
7526 return function handler () {
7527 var results = arguments[0];
7528 if (isFunction(fn)) {
7529 results = tryCatch(fn).apply(null, arguments);
7530 if (results === errorObj) { return o.onError(results.e); }
7531 }
7532 o.onNext(results);
7533 };
7534 }
7535
7536 EventPatternObservable.prototype.subscribeCore = function (o) {
7537 var fn = createHandler(o, this._fn);
7538 var returnValue = this._add(fn);
7539 return new EventPatternDisposable(this._del, fn, returnValue);
7540 };
7541
7542 function EventPatternDisposable(del, fn, ret) {
7543 this._del = del;
7544 this._fn = fn;
7545 this._ret = ret;
7546 this.isDisposed = false;
7547 }
7548
7549 EventPatternDisposable.prototype.dispose = function () {
7550 if(!this.isDisposed) {
7551 isFunction(this._del) && this._del(this._fn, this._ret);
7552 }
7553 };
7554
7555 return EventPatternObservable;
7556 }(ObservableBase));
7557
7558 /**
7559 * Creates an observable sequence from an event emitter via an addHandler/removeHandler pair.
7560 * @param {Function} addHandler The function to add a handler to the emitter.
7561 * @param {Function} [removeHandler] The optional function to remove a handler from an emitter.
7562 * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
7563 * @returns {Observable} An observable sequence which wraps an event from an event emitter
7564 */
7565 var fromEventPattern = Observable.fromEventPattern = function (addHandler, removeHandler, selector) {
7566 return new EventPatternObservable(addHandler, removeHandler, selector).publish().refCount();
7567 };
7568
7569 /**
7570 * Invokes the asynchronous function, surfacing the result through an observable sequence.
7571 * @param {Function} functionAsync Asynchronous function which returns a Promise to run.
7572 * @returns {Observable} An observable sequence exposing the function's result value, or an exception.
7573 */
7574 Observable.startAsync = function (functionAsync) {
7575 var promise = tryCatch(functionAsync)();
7576 if (promise === errorObj) { return observableThrow(promise.e); }
7577 return observableFromPromise(promise);
7578 };
7579
7580 var PausableObservable = (function (__super__) {
7581 inherits(PausableObservable, __super__);
7582 function PausableObservable(source, pauser) {
7583 this.source = source;
7584 this.controller = new Subject();
7585
7586 if (pauser && pauser.subscribe) {
7587 this.pauser = this.controller.merge(pauser);
7588 } else {
7589 this.pauser = this.controller;
7590 }
7591
7592 __super__.call(this);
7593 }
7594
7595 PausableObservable.prototype._subscribe = function (o) {
7596 var conn = this.source.publish(),
7597 subscription = conn.subscribe(o),
7598 connection = disposableEmpty;
7599
7600 var pausable = this.pauser.distinctUntilChanged().subscribe(function (b) {
7601 if (b) {
7602 connection = conn.connect();
7603 } else {
7604 connection.dispose();
7605 connection = disposableEmpty;
7606 }
7607 });
7608
7609 return new NAryDisposable([subscription, connection, pausable]);
7610 };
7611
7612 PausableObservable.prototype.pause = function () {
7613 this.controller.onNext(false);
7614 };
7615
7616 PausableObservable.prototype.resume = function () {
7617 this.controller.onNext(true);
7618 };
7619
7620 return PausableObservable;
7621
7622 }(Observable));
7623
7624 /**
7625 * Pauses the underlying observable sequence based upon the observable sequence which yields true/false.
7626 * @example
7627 * var pauser = new Rx.Subject();
7628 * var source = Rx.Observable.interval(100).pausable(pauser);
7629 * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
7630 * @returns {Observable} The observable sequence which is paused based upon the pauser.
7631 */
7632 observableProto.pausable = function (pauser) {
7633 return new PausableObservable(this, pauser);
7634 };
7635
7636 function combineLatestSource(source, subject, resultSelector) {
7637 return new AnonymousObservable(function (o) {
7638 var hasValue = [false, false],
7639 hasValueAll = false,
7640 isDone = false,
7641 values = new Array(2),
7642 err;
7643
7644 function next(x, i) {
7645 values[i] = x;
7646 hasValue[i] = true;
7647 if (hasValueAll || (hasValueAll = hasValue.every(identity))) {
7648 if (err) { return o.onError(err); }
7649 var res = tryCatch(resultSelector).apply(null, values);
7650 if (res === errorObj) { return o.onError(res.e); }
7651 o.onNext(res);
7652 }
7653 isDone && values[1] && o.onCompleted();
7654 }
7655
7656 return new BinaryDisposable(
7657 source.subscribe(
7658 function (x) {
7659 next(x, 0);
7660 },
7661 function (e) {
7662 if (values[1]) {
7663 o.onError(e);
7664 } else {
7665 err = e;
7666 }
7667 },
7668 function () {
7669 isDone = true;
7670 values[1] && o.onCompleted();
7671 }),
7672 subject.subscribe(
7673 function (x) {
7674 next(x, 1);
7675 },
7676 function (e) { o.onError(e); },
7677 function () {
7678 isDone = true;
7679 next(true, 1);
7680 })
7681 );
7682 }, source);
7683 }
7684
7685 var PausableBufferedObservable = (function (__super__) {
7686 inherits(PausableBufferedObservable, __super__);
7687 function PausableBufferedObservable(source, pauser) {
7688 this.source = source;
7689 this.controller = new Subject();
7690
7691 if (pauser && pauser.subscribe) {
7692 this.pauser = this.controller.merge(pauser);
7693 } else {
7694 this.pauser = this.controller;
7695 }
7696
7697 __super__.call(this);
7698 }
7699
7700 PausableBufferedObservable.prototype._subscribe = function (o) {
7701 var q = [], previousShouldFire;
7702
7703 function drainQueue() { while (q.length > 0) { o.onNext(q.shift()); } }
7704
7705 var subscription =
7706 combineLatestSource(
7707 this.source,
7708 this.pauser.startWith(false).distinctUntilChanged(),
7709 function (data, shouldFire) {
7710 return { data: data, shouldFire: shouldFire };
7711 })
7712 .subscribe(
7713 function (results) {
7714 if (previousShouldFire !== undefined && results.shouldFire !== previousShouldFire) {
7715 previousShouldFire = results.shouldFire;
7716 // change in shouldFire
7717 if (results.shouldFire) { drainQueue(); }
7718 } else {
7719 previousShouldFire = results.shouldFire;
7720 // new data
7721 if (results.shouldFire) {
7722 o.onNext(results.data);
7723 } else {
7724 q.push(results.data);
7725 }
7726 }
7727 },
7728 function (err) {
7729 drainQueue();
7730 o.onError(err);
7731 },
7732 function () {
7733 drainQueue();
7734 o.onCompleted();
7735 }
7736 );
7737 return subscription;
7738 };
7739
7740 PausableBufferedObservable.prototype.pause = function () {
7741 this.controller.onNext(false);
7742 };
7743
7744 PausableBufferedObservable.prototype.resume = function () {
7745 this.controller.onNext(true);
7746 };
7747
7748 return PausableBufferedObservable;
7749
7750 }(Observable));
7751
7752 /**
7753 * Pauses the underlying observable sequence based upon the observable sequence which yields true/false,
7754 * and yields the values that were buffered while paused.
7755 * @example
7756 * var pauser = new Rx.Subject();
7757 * var source = Rx.Observable.interval(100).pausableBuffered(pauser);
7758 * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
7759 * @returns {Observable} The observable sequence which is paused based upon the pauser.
7760 */
7761 observableProto.pausableBuffered = function (pauser) {
7762 return new PausableBufferedObservable(this, pauser);
7763 };
7764
7765 var ControlledObservable = (function (__super__) {
7766 inherits(ControlledObservable, __super__);
7767 function ControlledObservable (source, enableQueue, scheduler) {
7768 __super__.call(this);
7769 this.subject = new ControlledSubject(enableQueue, scheduler);
7770 this.source = source.multicast(this.subject).refCount();
7771 }
7772
7773 ControlledObservable.prototype._subscribe = function (o) {
7774 return this.source.subscribe(o);
7775 };
7776
7777 ControlledObservable.prototype.request = function (numberOfItems) {
7778 return this.subject.request(numberOfItems == null ? -1 : numberOfItems);
7779 };
7780
7781 return ControlledObservable;
7782
7783 }(Observable));
7784
7785 var ControlledSubject = (function (__super__) {
7786 inherits(ControlledSubject, __super__);
7787 function ControlledSubject(enableQueue, scheduler) {
7788 enableQueue == null && (enableQueue = true);
7789
7790 __super__.call(this);
7791 this.subject = new Subject();
7792 this.enableQueue = enableQueue;
7793 this.queue = enableQueue ? [] : null;
7794 this.requestedCount = 0;
7795 this.requestedDisposable = null;
7796 this.error = null;
7797 this.hasFailed = false;
7798 this.hasCompleted = false;
7799 this.scheduler = scheduler || currentThreadScheduler;
7800 }
7801
7802 addProperties(ControlledSubject.prototype, Observer, {
7803 _subscribe: function (o) {
7804 return this.subject.subscribe(o);
7805 },
7806 onCompleted: function () {
7807 this.hasCompleted = true;
7808 if (!this.enableQueue || this.queue.length === 0) {
7809 this.subject.onCompleted();
7810 this.disposeCurrentRequest();
7811 } else {
7812 this.queue.push(Notification.createOnCompleted());
7813 }
7814 },
7815 onError: function (error) {
7816 this.hasFailed = true;
7817 this.error = error;
7818 if (!this.enableQueue || this.queue.length === 0) {
7819 this.subject.onError(error);
7820 this.disposeCurrentRequest();
7821 } else {
7822 this.queue.push(Notification.createOnError(error));
7823 }
7824 },
7825 onNext: function (value) {
7826 if (this.requestedCount <= 0) {
7827 this.enableQueue && this.queue.push(Notification.createOnNext(value));
7828 } else {
7829 (this.requestedCount-- === 0) && this.disposeCurrentRequest();
7830 this.subject.onNext(value);
7831 }
7832 },
7833 _processRequest: function (numberOfItems) {
7834 if (this.enableQueue) {
7835 while (this.queue.length > 0 && (numberOfItems > 0 || this.queue[0].kind !== 'N')) {
7836 var first = this.queue.shift();
7837 first.accept(this.subject);
7838 if (first.kind === 'N') {
7839 numberOfItems--;
7840 } else {
7841 this.disposeCurrentRequest();
7842 this.queue = [];
7843 }
7844 }
7845 }
7846
7847 return numberOfItems;
7848 },
7849 request: function (number) {
7850 this.disposeCurrentRequest();
7851 var self = this;
7852
7853 this.requestedDisposable = this.scheduler.schedule(number,
7854 function(s, i) {
7855 var remaining = self._processRequest(i);
7856 var stopped = self.hasCompleted || self.hasFailed;
7857 if (!stopped && remaining > 0) {
7858 self.requestedCount = remaining;
7859
7860 return disposableCreate(function () {
7861 self.requestedCount = 0;
7862 });
7863 // Scheduled item is still in progress. Return a new
7864 // disposable to allow the request to be interrupted
7865 // via dispose.
7866 }
7867 });
7868
7869 return this.requestedDisposable;
7870 },
7871 disposeCurrentRequest: function () {
7872 if (this.requestedDisposable) {
7873 this.requestedDisposable.dispose();
7874 this.requestedDisposable = null;
7875 }
7876 }
7877 });
7878
7879 return ControlledSubject;
7880 }(Observable));
7881
7882 /**
7883 * Attaches a controller to the observable sequence with the ability to queue.
7884 * @example
7885 * var source = Rx.Observable.interval(100).controlled();
7886 * source.request(3); // Reads 3 values
7887 * @param {bool} enableQueue truthy value to determine if values should be queued pending the next request
7888 * @param {Scheduler} scheduler determines how the requests will be scheduled
7889 * @returns {Observable} The observable sequence which only propagates values on request.
7890 */
7891 observableProto.controlled = function (enableQueue, scheduler) {
7892
7893 if (enableQueue && isScheduler(enableQueue)) {
7894 scheduler = enableQueue;
7895 enableQueue = true;
7896 }
7897
7898 if (enableQueue == null) { enableQueue = true; }
7899 return new ControlledObservable(this, enableQueue, scheduler);
7900 };
7901
7902 var StopAndWaitObservable = (function (__super__) {
7903 inherits(StopAndWaitObservable, __super__);
7904 function StopAndWaitObservable (source) {
7905 __super__.call(this);
7906 this.source = source;
7907 }
7908
7909 function scheduleMethod(s, self) {
7910 self.source.request(1);
7911 }
7912
7913 StopAndWaitObservable.prototype._subscribe = function (o) {
7914 this.subscription = this.source.subscribe(new StopAndWaitObserver(o, this, this.subscription));
7915 return new BinaryDisposable(
7916 this.subscription,
7917 defaultScheduler.schedule(this, scheduleMethod)
7918 );
7919 };
7920
7921 var StopAndWaitObserver = (function (__sub__) {
7922 inherits(StopAndWaitObserver, __sub__);
7923 function StopAndWaitObserver (observer, observable, cancel) {
7924 __sub__.call(this);
7925 this.observer = observer;
7926 this.observable = observable;
7927 this.cancel = cancel;
7928 this.scheduleDisposable = null;
7929 }
7930
7931 StopAndWaitObserver.prototype.completed = function () {
7932 this.observer.onCompleted();
7933 this.dispose();
7934 };
7935
7936 StopAndWaitObserver.prototype.error = function (error) {
7937 this.observer.onError(error);
7938 this.dispose();
7939 };
7940
7941 function innerScheduleMethod(s, self) {
7942 self.observable.source.request(1);
7943 }
7944
7945 StopAndWaitObserver.prototype.next = function (value) {
7946 this.observer.onNext(value);
7947 this.scheduleDisposable = defaultScheduler.schedule(this, innerScheduleMethod);
7948 };
7949
7950 StopAndWaitObservable.dispose = function () {
7951 this.observer = null;
7952 if (this.cancel) {
7953 this.cancel.dispose();
7954 this.cancel = null;
7955 }
7956 if (this.scheduleDisposable) {
7957 this.scheduleDisposable.dispose();
7958 this.scheduleDisposable = null;
7959 }
7960 __sub__.prototype.dispose.call(this);
7961 };
7962
7963 return StopAndWaitObserver;
7964 }(AbstractObserver));
7965
7966 return StopAndWaitObservable;
7967 }(Observable));
7968
7969
7970 /**
7971 * Attaches a stop and wait observable to the current observable.
7972 * @returns {Observable} A stop and wait observable.
7973 */
7974 ControlledObservable.prototype.stopAndWait = function () {
7975 return new StopAndWaitObservable(this);
7976 };
7977
7978 var WindowedObservable = (function (__super__) {
7979 inherits(WindowedObservable, __super__);
7980 function WindowedObservable(source, windowSize) {
7981 __super__.call(this);
7982 this.source = source;
7983 this.windowSize = windowSize;
7984 }
7985
7986 function scheduleMethod(s, self) {
7987 self.source.request(self.windowSize);
7988 }
7989
7990 WindowedObservable.prototype._subscribe = function (o) {
7991 this.subscription = this.source.subscribe(new WindowedObserver(o, this, this.subscription));
7992 return new BinaryDisposable(
7993 this.subscription,
7994 defaultScheduler.schedule(this, scheduleMethod)
7995 );
7996 };
7997
7998 var WindowedObserver = (function (__sub__) {
7999 inherits(WindowedObserver, __sub__);
8000 function WindowedObserver(observer, observable, cancel) {
8001 this.observer = observer;
8002 this.observable = observable;
8003 this.cancel = cancel;
8004 this.received = 0;
8005 this.scheduleDisposable = null;
8006 __sub__.call(this);
8007 }
8008
8009 WindowedObserver.prototype.completed = function () {
8010 this.observer.onCompleted();
8011 this.dispose();
8012 };
8013
8014 WindowedObserver.prototype.error = function (error) {
8015 this.observer.onError(error);
8016 this.dispose();
8017 };
8018
8019 function innerScheduleMethod(s, self) {
8020 self.observable.source.request(self.observable.windowSize);
8021 }
8022
8023 WindowedObserver.prototype.next = function (value) {
8024 this.observer.onNext(value);
8025 this.received = ++this.received % this.observable.windowSize;
8026 this.received === 0 && (this.scheduleDisposable = defaultScheduler.schedule(this, innerScheduleMethod));
8027 };
8028
8029 WindowedObserver.prototype.dispose = function () {
8030 this.observer = null;
8031 if (this.cancel) {
8032 this.cancel.dispose();
8033 this.cancel = null;
8034 }
8035 if (this.scheduleDisposable) {
8036 this.scheduleDisposable.dispose();
8037 this.scheduleDisposable = null;
8038 }
8039 __sub__.prototype.dispose.call(this);
8040 };
8041
8042 return WindowedObserver;
8043 }(AbstractObserver));
8044
8045 return WindowedObservable;
8046 }(Observable));
8047
8048 /**
8049 * Creates a sliding windowed observable based upon the window size.
8050 * @param {Number} windowSize The number of items in the window
8051 * @returns {Observable} A windowed observable based upon the window size.
8052 */
8053 ControlledObservable.prototype.windowed = function (windowSize) {
8054 return new WindowedObservable(this, windowSize);
8055 };
8056
8057 /**
8058 * Pipes the existing Observable sequence into a Node.js Stream.
8059 * @param {Stream} dest The destination Node.js stream.
8060 * @returns {Stream} The destination stream.
8061 */
8062 observableProto.pipe = function (dest) {
8063 var source = this.pausableBuffered();
8064
8065 function onDrain() {
8066 source.resume();
8067 }
8068
8069 dest.addListener('drain', onDrain);
8070
8071 source.subscribe(
8072 function (x) {
8073 !dest.write(String(x)) && source.pause();
8074 },
8075 function (err) {
8076 dest.emit('error', err);
8077 },
8078 function () {
8079 // Hack check because STDIO is not closable
8080 !dest._isStdio && dest.end();
8081 dest.removeListener('drain', onDrain);
8082 });
8083
8084 source.resume();
8085
8086 return dest;
8087 };
8088
8089 var MulticastObservable = (function (__super__) {
8090 inherits(MulticastObservable, __super__);
8091 function MulticastObservable(source, fn1, fn2) {
8092 this.source = source;
8093 this._fn1 = fn1;
8094 this._fn2 = fn2;
8095 __super__.call(this);
8096 }
8097
8098 MulticastObservable.prototype.subscribeCore = function (o) {
8099 var connectable = this.source.multicast(this._fn1());
8100 return new BinaryDisposable(this._fn2(connectable).subscribe(o), connectable.connect());
8101 };
8102
8103 return MulticastObservable;
8104 }(ObservableBase));
8105
8106 /**
8107 * Multicasts the source sequence notifications through an instantiated subject into all uses of the sequence within a selector function. Each
8108 * subscription to the resulting sequence causes a separate multicast invocation, exposing the sequence resulting from the selector function's
8109 * invocation. For specializations with fixed subject types, see Publish, PublishLast, and Replay.
8110 *
8111 * @example
8112 * 1 - res = source.multicast(observable);
8113 * 2 - res = source.multicast(function () { return new Subject(); }, function (x) { return x; });
8114 *
8115 * @param {Function|Subject} subjectOrSubjectSelector
8116 * Factory function to create an intermediate subject through which the source sequence's elements will be multicast to the selector function.
8117 * Or:
8118 * Subject to push source elements into.
8119 *
8120 * @param {Function} [selector] Optional selector function which can use the multicasted source sequence subject to the policies enforced by the created subject. Specified only if <paramref name="subjectOrSubjectSelector" is a factory function.
8121 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.
8122 */
8123 observableProto.multicast = function (subjectOrSubjectSelector, selector) {
8124 return isFunction(subjectOrSubjectSelector) ?
8125 new MulticastObservable(this, subjectOrSubjectSelector, selector) :
8126 new ConnectableObservable(this, subjectOrSubjectSelector);
8127 };
8128
8129 /**
8130 * Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence.
8131 * This operator is a specialization of Multicast using a regular Subject.
8132 *
8133 * @example
8134 * var resres = source.publish();
8135 * var res = source.publish(function (x) { return x; });
8136 *
8137 * @param {Function} [selector] Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive all notifications of the source from the time of the subscription on.
8138 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.
8139 */
8140 observableProto.publish = function (selector) {
8141 return selector && isFunction(selector) ?
8142 this.multicast(function () { return new Subject(); }, selector) :
8143 this.multicast(new Subject());
8144 };
8145
8146 /**
8147 * Returns an observable sequence that shares a single subscription to the underlying sequence.
8148 * This operator is a specialization of publish which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed.
8149 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence.
8150 */
8151 observableProto.share = function () {
8152 return this.publish().refCount();
8153 };
8154
8155 /**
8156 * Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence containing only the last notification.
8157 * This operator is a specialization of Multicast using a AsyncSubject.
8158 *
8159 * @example
8160 * var res = source.publishLast();
8161 * var res = source.publishLast(function (x) { return x; });
8162 *
8163 * @param selector [Optional] Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will only receive the last notification of the source.
8164 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.
8165 */
8166 observableProto.publishLast = function (selector) {
8167 return selector && isFunction(selector) ?
8168 this.multicast(function () { return new AsyncSubject(); }, selector) :
8169 this.multicast(new AsyncSubject());
8170 };
8171
8172 /**
8173 * Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence and starts with initialValue.
8174 * This operator is a specialization of Multicast using a BehaviorSubject.
8175 *
8176 * @example
8177 * var res = source.publishValue(42);
8178 * var res = source.publishValue(function (x) { return x.select(function (y) { return y * y; }) }, 42);
8179 *
8180 * @param {Function} [selector] Optional selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive immediately receive the initial value, followed by all notifications of the source from the time of the subscription on.
8181 * @param {Mixed} initialValue Initial value received by observers upon subscription.
8182 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.
8183 */
8184 observableProto.publishValue = function (initialValueOrSelector, initialValue) {
8185 return arguments.length === 2 ?
8186 this.multicast(function () {
8187 return new BehaviorSubject(initialValue);
8188 }, initialValueOrSelector) :
8189 this.multicast(new BehaviorSubject(initialValueOrSelector));
8190 };
8191
8192 /**
8193 * Returns an observable sequence that shares a single subscription to the underlying sequence and starts with an initialValue.
8194 * This operator is a specialization of publishValue which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed.
8195 * @param {Mixed} initialValue Initial value received by observers upon subscription.
8196 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence.
8197 */
8198 observableProto.shareValue = function (initialValue) {
8199 return this.publishValue(initialValue).refCount();
8200 };
8201
8202 /**
8203 * Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer.
8204 * This operator is a specialization of Multicast using a ReplaySubject.
8205 *
8206 * @example
8207 * var res = source.replay(null, 3);
8208 * var res = source.replay(null, 3, 500);
8209 * var res = source.replay(null, 3, 500, scheduler);
8210 * var res = source.replay(function (x) { return x.take(6).repeat(); }, 3, 500, scheduler);
8211 *
8212 * @param selector [Optional] Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive all the notifications of the source subject to the specified replay buffer trimming policy.
8213 * @param bufferSize [Optional] Maximum element count of the replay buffer.
8214 * @param windowSize [Optional] Maximum time length of the replay buffer.
8215 * @param scheduler [Optional] Scheduler where connected observers within the selector function will be invoked on.
8216 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.
8217 */
8218 observableProto.replay = function (selector, bufferSize, windowSize, scheduler) {
8219 return selector && isFunction(selector) ?
8220 this.multicast(function () { return new ReplaySubject(bufferSize, windowSize, scheduler); }, selector) :
8221 this.multicast(new ReplaySubject(bufferSize, windowSize, scheduler));
8222 };
8223
8224 /**
8225 * Returns an observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer.
8226 * This operator is a specialization of replay which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed.
8227 *
8228 * @example
8229 * var res = source.shareReplay(3);
8230 * var res = source.shareReplay(3, 500);
8231 * var res = source.shareReplay(3, 500, scheduler);
8232 *
8233
8234 * @param bufferSize [Optional] Maximum element count of the replay buffer.
8235 * @param window [Optional] Maximum time length of the replay buffer.
8236 * @param scheduler [Optional] Scheduler where connected observers within the selector function will be invoked on.
8237 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence.
8238 */
8239 observableProto.shareReplay = function (bufferSize, windowSize, scheduler) {
8240 return this.replay(null, bufferSize, windowSize, scheduler).refCount();
8241 };
8242
8243 var InnerSubscription = function (s, o) {
8244 this._s = s;
8245 this._o = o;
8246 };
8247
8248 InnerSubscription.prototype.dispose = function () {
8249 if (!this._s.isDisposed && this._o !== null) {
8250 var idx = this._s.observers.indexOf(this._o);
8251 this._s.observers.splice(idx, 1);
8252 this._o = null;
8253 }
8254 };
8255
8256 var RefCountObservable = (function (__super__) {
8257 inherits(RefCountObservable, __super__);
8258 function RefCountObservable(source) {
8259 this.source = source;
8260 this._count = 0;
8261 this._connectableSubscription = null;
8262 __super__.call(this);
8263 }
8264
8265 RefCountObservable.prototype.subscribeCore = function (o) {
8266 var subscription = this.source.subscribe(o);
8267 ++this._count === 1 && (this._connectableSubscription = this.source.connect());
8268 return new RefCountDisposable(this, subscription);
8269 };
8270
8271 function RefCountDisposable(p, s) {
8272 this._p = p;
8273 this._s = s;
8274 this.isDisposed = false;
8275 }
8276
8277 RefCountDisposable.prototype.dispose = function () {
8278 if (!this.isDisposed) {
8279 this.isDisposed = true;
8280 this._s.dispose();
8281 --this._p._count === 0 && this._p._connectableSubscription.dispose();
8282 }
8283 };
8284
8285 return RefCountObservable;
8286 }(ObservableBase));
8287
8288 var ConnectableObservable = Rx.ConnectableObservable = (function (__super__) {
8289 inherits(ConnectableObservable, __super__);
8290 function ConnectableObservable(source, subject) {
8291 this.source = source;
8292 this._connection = null;
8293 this._source = source.asObservable();
8294 this._subject = subject;
8295 __super__.call(this);
8296 }
8297
8298 function ConnectDisposable(parent, subscription) {
8299 this._p = parent;
8300 this._s = subscription;
8301 }
8302
8303 ConnectDisposable.prototype.dispose = function () {
8304 if (this._s) {
8305 this._s.dispose();
8306 this._s = null;
8307 this._p._connection = null;
8308 }
8309 };
8310
8311 ConnectableObservable.prototype.connect = function () {
8312 if (!this._connection) {
8313 var subscription = this._source.subscribe(this._subject);
8314 this._connection = new ConnectDisposable(this, subscription);
8315 }
8316 return this._connection;
8317 };
8318
8319 ConnectableObservable.prototype._subscribe = function (o) {
8320 return this._subject.subscribe(o);
8321 };
8322
8323 ConnectableObservable.prototype.refCount = function () {
8324 return new RefCountObservable(this);
8325 };
8326
8327 return ConnectableObservable;
8328 }(Observable));
8329
8330 /**
8331 * Returns an observable sequence that shares a single subscription to the underlying sequence. This observable sequence
8332 * can be resubscribed to, even if all prior subscriptions have ended. (unlike `.publish().refCount()`)
8333 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source.
8334 */
8335 observableProto.singleInstance = function() {
8336 var source = this, hasObservable = false, observable;
8337
8338 function getObservable() {
8339 if (!hasObservable) {
8340 hasObservable = true;
8341 observable = source['finally'](function() { hasObservable = false; }).publish().refCount();
8342 }
8343 return observable;
8344 }
8345
8346 return new AnonymousObservable(function(o) {
8347 return getObservable().subscribe(o);
8348 });
8349 };
8350
8351 /**
8352 * Correlates the elements of two sequences based on overlapping durations.
8353 *
8354 * @param {Observable} right The right observable sequence to join elements for.
8355 * @param {Function} leftDurationSelector A function to select the duration (expressed as an observable sequence) of each element of the left observable sequence, used to determine overlap.
8356 * @param {Function} rightDurationSelector A function to select the duration (expressed as an observable sequence) of each element of the right observable sequence, used to determine overlap.
8357 * @param {Function} resultSelector A function invoked to compute a result element for any two overlapping elements of the left and right observable sequences. The parameters passed to the function correspond with the elements from the left and right source sequences for which overlap occurs.
8358 * @returns {Observable} An observable sequence that contains result elements computed from source elements that have an overlapping duration.
8359 */
8360 observableProto.join = function (right, leftDurationSelector, rightDurationSelector, resultSelector) {
8361 var left = this;
8362 return new AnonymousObservable(function (o) {
8363 var group = new CompositeDisposable();
8364 var leftDone = false, rightDone = false;
8365 var leftId = 0, rightId = 0;
8366 var leftMap = new Map(), rightMap = new Map();
8367 var handleError = function (e) { o.onError(e); };
8368
8369 group.add(left.subscribe(
8370 function (value) {
8371 var id = leftId++, md = new SingleAssignmentDisposable();
8372
8373 leftMap.set(id, value);
8374 group.add(md);
8375
8376 var duration = tryCatch(leftDurationSelector)(value);
8377 if (duration === errorObj) { return o.onError(duration.e); }
8378
8379 md.setDisposable(duration.take(1).subscribe(
8380 noop,
8381 handleError,
8382 function () {
8383 leftMap['delete'](id) && leftMap.size === 0 && leftDone && o.onCompleted();
8384 group.remove(md);
8385 }));
8386
8387 rightMap.forEach(function (v) {
8388 var result = tryCatch(resultSelector)(value, v);
8389 if (result === errorObj) { return o.onError(result.e); }
8390 o.onNext(result);
8391 });
8392 },
8393 handleError,
8394 function () {
8395 leftDone = true;
8396 (rightDone || leftMap.size === 0) && o.onCompleted();
8397 })
8398 );
8399
8400 group.add(right.subscribe(
8401 function (value) {
8402 var id = rightId++, md = new SingleAssignmentDisposable();
8403
8404 rightMap.set(id, value);
8405 group.add(md);
8406
8407 var duration = tryCatch(rightDurationSelector)(value);
8408 if (duration === errorObj) { return o.onError(duration.e); }
8409
8410 md.setDisposable(duration.take(1).subscribe(
8411 noop,
8412 handleError,
8413 function () {
8414 rightMap['delete'](id) && rightMap.size === 0 && rightDone && o.onCompleted();
8415 group.remove(md);
8416 }));
8417
8418 leftMap.forEach(function (v) {
8419 var result = tryCatch(resultSelector)(v, value);
8420 if (result === errorObj) { return o.onError(result.e); }
8421 o.onNext(result);
8422 });
8423 },
8424 handleError,
8425 function () {
8426 rightDone = true;
8427 (leftDone || rightMap.size === 0) && o.onCompleted();
8428 })
8429 );
8430 return group;
8431 }, left);
8432 };
8433
8434 /**
8435 * Correlates the elements of two sequences based on overlapping durations, and groups the results.
8436 *
8437 * @param {Observable} right The right observable sequence to join elements for.
8438 * @param {Function} leftDurationSelector A function to select the duration (expressed as an observable sequence) of each element of the left observable sequence, used to determine overlap.
8439 * @param {Function} rightDurationSelector A function to select the duration (expressed as an observable sequence) of each element of the right observable sequence, used to determine overlap.
8440 * @param {Function} resultSelector A function invoked to compute a result element for any element of the left sequence with overlapping elements from the right observable sequence. The first parameter passed to the function is an element of the left sequence. The second parameter passed to the function is an observable sequence with elements from the right sequence that overlap with the left sequence's element.
8441 * @returns {Observable} An observable sequence that contains result elements computed from source elements that have an overlapping duration.
8442 */
8443 observableProto.groupJoin = function (right, leftDurationSelector, rightDurationSelector, resultSelector) {
8444 var left = this;
8445 return new AnonymousObservable(function (o) {
8446 var group = new CompositeDisposable();
8447 var r = new RefCountDisposable(group);
8448 var leftMap = new Map(), rightMap = new Map();
8449 var leftId = 0, rightId = 0;
8450 var handleError = function (e) { return function (v) { v.onError(e); }; };
8451
8452 function handleError(e) { };
8453
8454 group.add(left.subscribe(
8455 function (value) {
8456 var s = new Subject();
8457 var id = leftId++;
8458 leftMap.set(id, s);
8459
8460 var result = tryCatch(resultSelector)(value, addRef(s, r));
8461 if (result === errorObj) {
8462 leftMap.forEach(handleError(result.e));
8463 return o.onError(result.e);
8464 }
8465 o.onNext(result);
8466
8467 rightMap.forEach(function (v) { s.onNext(v); });
8468
8469 var md = new SingleAssignmentDisposable();
8470 group.add(md);
8471
8472 var duration = tryCatch(leftDurationSelector)(value);
8473 if (duration === errorObj) {
8474 leftMap.forEach(handleError(duration.e));
8475 return o.onError(duration.e);
8476 }
8477
8478 md.setDisposable(duration.take(1).subscribe(
8479 noop,
8480 function (e) {
8481 leftMap.forEach(handleError(e));
8482 o.onError(e);
8483 },
8484 function () {
8485 leftMap['delete'](id) && s.onCompleted();
8486 group.remove(md);
8487 }));
8488 },
8489 function (e) {
8490 leftMap.forEach(handleError(e));
8491 o.onError(e);
8492 },
8493 function () { o.onCompleted(); })
8494 );
8495
8496 group.add(right.subscribe(
8497 function (value) {
8498 var id = rightId++;
8499 rightMap.set(id, value);
8500
8501 var md = new SingleAssignmentDisposable();
8502 group.add(md);
8503
8504 var duration = tryCatch(rightDurationSelector)(value);
8505 if (duration === errorObj) {
8506 leftMap.forEach(handleError(duration.e));
8507 return o.onError(duration.e);
8508 }
8509
8510 md.setDisposable(duration.take(1).subscribe(
8511 noop,
8512 function (e) {
8513 leftMap.forEach(handleError(e));
8514 o.onError(e);
8515 },
8516 function () {
8517 rightMap['delete'](id);
8518 group.remove(md);
8519 }));
8520
8521 leftMap.forEach(function (v) { v.onNext(value); });
8522 },
8523 function (e) {
8524 leftMap.forEach(handleError(e));
8525 o.onError(e);
8526 })
8527 );
8528
8529 return r;
8530 }, left);
8531 };
8532
8533 function toArray(x) { return x.toArray(); }
8534
8535 /**
8536 * Projects each element of an observable sequence into zero or more buffers.
8537 * @param {Mixed} bufferOpeningsOrClosingSelector Observable sequence whose elements denote the creation of new windows, or, a function invoked to define the boundaries of the produced windows (a new window is started when the previous one is closed, resulting in non-overlapping windows).
8538 * @param {Function} [bufferClosingSelector] A function invoked to define the closing of each produced window. If a closing selector function is specified for the first parameter, this parameter is ignored.
8539 * @returns {Observable} An observable sequence of windows.
8540 */
8541 observableProto.buffer = function () {
8542 return this.window.apply(this, arguments)
8543 .flatMap(toArray);
8544 };
8545
8546 /**
8547 * Projects each element of an observable sequence into zero or more windows.
8548 *
8549 * @param {Mixed} windowOpeningsOrClosingSelector Observable sequence whose elements denote the creation of new windows, or, a function invoked to define the boundaries of the produced windows (a new window is started when the previous one is closed, resulting in non-overlapping windows).
8550 * @param {Function} [windowClosingSelector] A function invoked to define the closing of each produced window. If a closing selector function is specified for the first parameter, this parameter is ignored.
8551 * @returns {Observable} An observable sequence of windows.
8552 */
8553 observableProto.window = function (windowOpeningsOrClosingSelector, windowClosingSelector) {
8554 if (arguments.length === 1 && typeof arguments[0] !== 'function') {
8555 return observableWindowWithBoundaries.call(this, windowOpeningsOrClosingSelector);
8556 }
8557 return typeof windowOpeningsOrClosingSelector === 'function' ?
8558 observableWindowWithClosingSelector.call(this, windowOpeningsOrClosingSelector) :
8559 observableWindowWithOpenings.call(this, windowOpeningsOrClosingSelector, windowClosingSelector);
8560 };
8561
8562 function observableWindowWithOpenings(windowOpenings, windowClosingSelector) {
8563 return windowOpenings.groupJoin(this, windowClosingSelector, observableEmpty, function (_, win) {
8564 return win;
8565 });
8566 }
8567
8568 function observableWindowWithBoundaries(windowBoundaries) {
8569 var source = this;
8570 return new AnonymousObservable(function (observer) {
8571 var win = new Subject(),
8572 d = new CompositeDisposable(),
8573 r = new RefCountDisposable(d);
8574
8575 observer.onNext(addRef(win, r));
8576
8577 d.add(source.subscribe(function (x) {
8578 win.onNext(x);
8579 }, function (err) {
8580 win.onError(err);
8581 observer.onError(err);
8582 }, function () {
8583 win.onCompleted();
8584 observer.onCompleted();
8585 }));
8586
8587 isPromise(windowBoundaries) && (windowBoundaries = observableFromPromise(windowBoundaries));
8588
8589 d.add(windowBoundaries.subscribe(function (w) {
8590 win.onCompleted();
8591 win = new Subject();
8592 observer.onNext(addRef(win, r));
8593 }, function (err) {
8594 win.onError(err);
8595 observer.onError(err);
8596 }, function () {
8597 win.onCompleted();
8598 observer.onCompleted();
8599 }));
8600
8601 return r;
8602 }, source);
8603 }
8604
8605 function observableWindowWithClosingSelector(windowClosingSelector) {
8606 var source = this;
8607 return new AnonymousObservable(function (observer) {
8608 var m = new SerialDisposable(),
8609 d = new CompositeDisposable(m),
8610 r = new RefCountDisposable(d),
8611 win = new Subject();
8612 observer.onNext(addRef(win, r));
8613 d.add(source.subscribe(function (x) {
8614 win.onNext(x);
8615 }, function (err) {
8616 win.onError(err);
8617 observer.onError(err);
8618 }, function () {
8619 win.onCompleted();
8620 observer.onCompleted();
8621 }));
8622
8623 function createWindowClose () {
8624 var windowClose;
8625 try {
8626 windowClose = windowClosingSelector();
8627 } catch (e) {
8628 observer.onError(e);
8629 return;
8630 }
8631
8632 isPromise(windowClose) && (windowClose = observableFromPromise(windowClose));
8633
8634 var m1 = new SingleAssignmentDisposable();
8635 m.setDisposable(m1);
8636 m1.setDisposable(windowClose.take(1).subscribe(noop, function (err) {
8637 win.onError(err);
8638 observer.onError(err);
8639 }, function () {
8640 win.onCompleted();
8641 win = new Subject();
8642 observer.onNext(addRef(win, r));
8643 createWindowClose();
8644 }));
8645 }
8646
8647 createWindowClose();
8648 return r;
8649 }, source);
8650 }
8651
8652 var PairwiseObservable = (function (__super__) {
8653 inherits(PairwiseObservable, __super__);
8654 function PairwiseObservable(source) {
8655 this.source = source;
8656 __super__.call(this);
8657 }
8658
8659 PairwiseObservable.prototype.subscribeCore = function (o) {
8660 return this.source.subscribe(new PairwiseObserver(o));
8661 };
8662
8663 return PairwiseObservable;
8664 }(ObservableBase));
8665
8666 var PairwiseObserver = (function(__super__) {
8667 inherits(PairwiseObserver, __super__);
8668 function PairwiseObserver(o) {
8669 this._o = o;
8670 this._p = null;
8671 this._hp = false;
8672 }
8673
8674 PairwiseObserver.prototype.next = function (x) {
8675 if (this._hp) {
8676 this._o.onNext([this._p, x]);
8677 } else {
8678 this._hp = true;
8679 }
8680 this._p = x;
8681 };
8682 PairwiseObserver.prototype.error = function (err) { this._o.onError(err); };
8683 PairwiseObserver.prototype.completed = function () { this._o.onCompleted(); };
8684
8685 return PairwiseObserver;
8686 }(AbstractObserver));
8687
8688 /**
8689 * Returns a new observable that triggers on the second and subsequent triggerings of the input observable.
8690 * The Nth triggering of the input observable passes the arguments from the N-1th and Nth triggering as a pair.
8691 * The argument passed to the N-1th triggering is held in hidden internal state until the Nth triggering occurs.
8692 * @returns {Observable} An observable that triggers on successive pairs of observations from the input observable as an array.
8693 */
8694 observableProto.pairwise = function () {
8695 return new PairwiseObservable(this);
8696 };
8697
8698 /**
8699 * Returns two observables which partition the observations of the source by the given function.
8700 * The first will trigger observations for those values for which the predicate returns true.
8701 * The second will trigger observations for those values where the predicate returns false.
8702 * The predicate is executed once for each subscribed observer.
8703 * Both also propagate all error observations arising from the source and each completes
8704 * when the source completes.
8705 * @param {Function} predicate
8706 * The function to determine which output Observable will trigger a particular observation.
8707 * @returns {Array}
8708 * An array of observables. The first triggers when the predicate returns true,
8709 * and the second triggers when the predicate returns false.
8710 */
8711 observableProto.partition = function(predicate, thisArg) {
8712 var fn = bindCallback(predicate, thisArg, 3);
8713 return [
8714 this.filter(predicate, thisArg),
8715 this.filter(function (x, i, o) { return !fn(x, i, o); })
8716 ];
8717 };
8718
8719 var WhileEnumerable = (function(__super__) {
8720 inherits(WhileEnumerable, __super__);
8721 function WhileEnumerable(c, s) {
8722 this.c = c;
8723 this.s = s;
8724 }
8725 WhileEnumerable.prototype[$iterator$] = function () {
8726 var self = this;
8727 return {
8728 next: function () {
8729 return self.c() ?
8730 { done: false, value: self.s } :
8731 { done: true, value: void 0 };
8732 }
8733 };
8734 };
8735 return WhileEnumerable;
8736 }(Enumerable));
8737
8738 function enumerableWhile(condition, source) {
8739 return new WhileEnumerable(condition, source);
8740 }
8741
8742 /**
8743 * Returns an observable sequence that is the result of invoking the selector on the source sequence, without sharing subscriptions.
8744 * This operator allows for a fluent style of writing queries that use the same sequence multiple times.
8745 *
8746 * @param {Function} selector Selector function which can use the source sequence as many times as needed, without sharing subscriptions to the source sequence.
8747 * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.
8748 */
8749 observableProto.letBind = observableProto['let'] = function (func) {
8750 return func(this);
8751 };
8752
8753 /**
8754 * Determines whether an observable collection contains values.
8755 *
8756 * @example
8757 * 1 - res = Rx.Observable.if(condition, obs1);
8758 * 2 - res = Rx.Observable.if(condition, obs1, obs2);
8759 * 3 - res = Rx.Observable.if(condition, obs1, scheduler);
8760 * @param {Function} condition The condition which determines if the thenSource or elseSource will be run.
8761 * @param {Observable} thenSource The observable sequence or Promise that will be run if the condition function returns true.
8762 * @param {Observable} [elseSource] The observable sequence or Promise that will be run if the condition function returns false. If this is not provided, it defaults to Rx.Observabe.Empty with the specified scheduler.
8763 * @returns {Observable} An observable sequence which is either the thenSource or elseSource.
8764 */
8765 Observable['if'] = function (condition, thenSource, elseSourceOrScheduler) {
8766 return observableDefer(function () {
8767 elseSourceOrScheduler || (elseSourceOrScheduler = observableEmpty());
8768
8769 isPromise(thenSource) && (thenSource = observableFromPromise(thenSource));
8770 isPromise(elseSourceOrScheduler) && (elseSourceOrScheduler = observableFromPromise(elseSourceOrScheduler));
8771
8772 // Assume a scheduler for empty only
8773 typeof elseSourceOrScheduler.now === 'function' && (elseSourceOrScheduler = observableEmpty(elseSourceOrScheduler));
8774 return condition() ? thenSource : elseSourceOrScheduler;
8775 });
8776 };
8777
8778 /**
8779 * Concatenates the observable sequences obtained by running the specified result selector for each element in source.
8780 * There is an alias for this method called 'forIn' for browsers <IE9
8781 * @param {Array} sources An array of values to turn into an observable sequence.
8782 * @param {Function} resultSelector A function to apply to each item in the sources array to turn it into an observable sequence.
8783 * @returns {Observable} An observable sequence from the concatenated observable sequences.
8784 */
8785 Observable['for'] = Observable.forIn = function (sources, resultSelector, thisArg) {
8786 return enumerableOf(sources, resultSelector, thisArg).concat();
8787 };
8788
8789 /**
8790 * Repeats source as long as condition holds emulating a while loop.
8791 * There is an alias for this method called 'whileDo' for browsers <IE9
8792 *
8793 * @param {Function} condition The condition which determines if the source will be repeated.
8794 * @param {Observable} source The observable sequence that will be run if the condition function returns true.
8795 * @returns {Observable} An observable sequence which is repeated as long as the condition holds.
8796 */
8797 var observableWhileDo = Observable['while'] = Observable.whileDo = function (condition, source) {
8798 isPromise(source) && (source = observableFromPromise(source));
8799 return enumerableWhile(condition, source).concat();
8800 };
8801
8802 /**
8803 * Repeats source as long as condition holds emulating a do while loop.
8804 *
8805 * @param {Function} condition The condition which determines if the source will be repeated.
8806 * @param {Observable} source The observable sequence that will be run if the condition function returns true.
8807 * @returns {Observable} An observable sequence which is repeated as long as the condition holds.
8808 */
8809 observableProto.doWhile = function (condition) {
8810 return observableConcat([this, observableWhileDo(condition, this)]);
8811 };
8812
8813 /**
8814 * Uses selector to determine which source in sources to use.
8815 * @param {Function} selector The function which extracts the value for to test in a case statement.
8816 * @param {Array} sources A object which has keys which correspond to the case statement labels.
8817 * @param {Observable} [elseSource] The observable sequence or Promise that will be run if the sources are not matched. If this is not provided, it defaults to Rx.Observabe.empty with the specified scheduler.
8818 *
8819 * @returns {Observable} An observable sequence which is determined by a case statement.
8820 */
8821 Observable['case'] = function (selector, sources, defaultSourceOrScheduler) {
8822 return observableDefer(function () {
8823 isPromise(defaultSourceOrScheduler) && (defaultSourceOrScheduler = observableFromPromise(defaultSourceOrScheduler));
8824 defaultSourceOrScheduler || (defaultSourceOrScheduler = observableEmpty());
8825
8826 isScheduler(defaultSourceOrScheduler) && (defaultSourceOrScheduler = observableEmpty(defaultSourceOrScheduler));
8827
8828 var result = sources[selector()];
8829 isPromise(result) && (result = observableFromPromise(result));
8830
8831 return result || defaultSourceOrScheduler;
8832 });
8833 };
8834
8835 var ExpandObservable = (function(__super__) {
8836 inherits(ExpandObservable, __super__);
8837 function ExpandObservable(source, fn, scheduler) {
8838 this.source = source;
8839 this._fn = fn;
8840 this._scheduler = scheduler;
8841 __super__.call(this);
8842 }
8843
8844 function scheduleRecursive(args, recurse) {
8845 var state = args[0], self = args[1];
8846 var work;
8847 if (state.q.length > 0) {
8848 work = state.q.shift();
8849 } else {
8850 state.isAcquired = false;
8851 return;
8852 }
8853 var m1 = new SingleAssignmentDisposable();
8854 state.d.add(m1);
8855 m1.setDisposable(work.subscribe(new ExpandObserver(state, self, m1)));
8856 recurse([state, self]);
8857 }
8858
8859 ExpandObservable.prototype._ensureActive = function (state) {
8860 var isOwner = false;
8861 if (state.q.length > 0) {
8862 isOwner = !state.isAcquired;
8863 state.isAcquired = true;
8864 }
8865 isOwner && state.m.setDisposable(this._scheduler.scheduleRecursive([state, this], scheduleRecursive));
8866 };
8867
8868 ExpandObservable.prototype.subscribeCore = function (o) {
8869 var m = new SerialDisposable(),
8870 d = new CompositeDisposable(m),
8871 state = {
8872 q: [],
8873 m: m,
8874 d: d,
8875 activeCount: 0,
8876 isAcquired: false,
8877 o: o
8878 };
8879
8880 state.q.push(this.source);
8881 state.activeCount++;
8882 this._ensureActive(state);
8883 return d;
8884 };
8885
8886 return ExpandObservable;
8887 }(ObservableBase));
8888
8889 var ExpandObserver = (function(__super__) {
8890 inherits(ExpandObserver, __super__);
8891 function ExpandObserver(state, parent, m1) {
8892 this._s = state;
8893 this._p = parent;
8894 this._m1 = m1;
8895 __super__.call(this);
8896 }
8897
8898 ExpandObserver.prototype.next = function (x) {
8899 this._s.o.onNext(x);
8900 var result = tryCatch(this._p._fn)(x);
8901 if (result === errorObj) { return this._s.o.onError(result.e); }
8902 this._s.q.push(result);
8903 this._s.activeCount++;
8904 this._p._ensureActive(this._s);
8905 };
8906
8907 ExpandObserver.prototype.error = function (e) {
8908 this._s.o.onError(e);
8909 };
8910
8911 ExpandObserver.prototype.completed = function () {
8912 this._s.d.remove(this._m1);
8913 this._s.activeCount--;
8914 this._s.activeCount === 0 && this._s.o.onCompleted();
8915 };
8916
8917 return ExpandObserver;
8918 }(AbstractObserver));
8919
8920 /**
8921 * Expands an observable sequence by recursively invoking selector.
8922 *
8923 * @param {Function} selector Selector function to invoke for each produced element, resulting in another sequence to which the selector will be invoked recursively again.
8924 * @param {Scheduler} [scheduler] Scheduler on which to perform the expansion. If not provided, this defaults to the current thread scheduler.
8925 * @returns {Observable} An observable sequence containing all the elements produced by the recursive expansion.
8926 */
8927 observableProto.expand = function (selector, scheduler) {
8928 isScheduler(scheduler) || (scheduler = currentThreadScheduler);
8929 return new ExpandObservable(this, selector, scheduler);
8930 };
8931
8932 function argumentsToArray() {
8933 var len = arguments.length, args = new Array(len);
8934 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
8935 return args;
8936 }
8937
8938 var ForkJoinObservable = (function (__super__) {
8939 inherits(ForkJoinObservable, __super__);
8940 function ForkJoinObservable(sources, cb) {
8941 this._sources = sources;
8942 this._cb = cb;
8943 __super__.call(this);
8944 }
8945
8946 ForkJoinObservable.prototype.subscribeCore = function (o) {
8947 if (this._sources.length === 0) {
8948 o.onCompleted();
8949 return disposableEmpty;
8950 }
8951
8952 var count = this._sources.length;
8953 var state = {
8954 finished: false,
8955 hasResults: new Array(count),
8956 hasCompleted: new Array(count),
8957 results: new Array(count)
8958 };
8959
8960 var subscriptions = new CompositeDisposable();
8961 for (var i = 0, len = this._sources.length; i < len; i++) {
8962 var source = this._sources[i];
8963 isPromise(source) && (source = observableFromPromise(source));
8964 subscriptions.add(source.subscribe(new ForkJoinObserver(o, state, i, this._cb, subscriptions)));
8965 }
8966
8967 return subscriptions;
8968 };
8969
8970 return ForkJoinObservable;
8971 }(ObservableBase));
8972
8973 var ForkJoinObserver = (function(__super__) {
8974 inherits(ForkJoinObserver, __super__);
8975 function ForkJoinObserver(o, s, i, cb, subs) {
8976 this._o = o;
8977 this._s = s;
8978 this._i = i;
8979 this._cb = cb;
8980 this._subs = subs;
8981 __super__.call(this);
8982 }
8983
8984 ForkJoinObserver.prototype.next = function (x) {
8985 if (!this._s.finished) {
8986 this._s.hasResults[this._i] = true;
8987 this._s.results[this._i] = x;
8988 }
8989 };
8990
8991 ForkJoinObserver.prototype.error = function (e) {
8992 this._s.finished = true;
8993 this._o.onError(e);
8994 this._subs.dispose();
8995 };
8996
8997 ForkJoinObserver.prototype.completed = function () {
8998 if (!this._s.finished) {
8999 if (!this._s.hasResults[this._i]) {
9000 return this._o.onCompleted();
9001 }
9002 this._s.hasCompleted[this._i] = true;
9003 for (var i = 0; i < this._s.results.length; i++) {
9004 if (!this._s.hasCompleted[i]) { return; }
9005 }
9006 this._s.finished = true;
9007
9008 var res = tryCatch(this._cb).apply(null, this._s.results);
9009 if (res === errorObj) { return this._o.onError(res.e); }
9010
9011 this._o.onNext(res);
9012 this._o.onCompleted();
9013 }
9014 };
9015
9016 return ForkJoinObserver;
9017 }(AbstractObserver));
9018
9019 /**
9020 * Runs all observable sequences in parallel and collect their last elements.
9021 *
9022 * @example
9023 * 1 - res = Rx.Observable.forkJoin([obs1, obs2]);
9024 * 1 - res = Rx.Observable.forkJoin(obs1, obs2, ...);
9025 * @returns {Observable} An observable sequence with an array collecting the last elements of all the input sequences.
9026 */
9027 Observable.forkJoin = function () {
9028 var len = arguments.length, args = new Array(len);
9029 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
9030 var resultSelector = isFunction(args[len - 1]) ? args.pop() : argumentsToArray;
9031 Array.isArray(args[0]) && (args = args[0]);
9032 return new ForkJoinObservable(args, resultSelector);
9033 };
9034
9035 /**
9036 * Runs two observable sequences in parallel and combines their last elemenets.
9037 * @param {Observable} second Second observable sequence.
9038 * @param {Function} resultSelector Result selector function to invoke with the last elements of both sequences.
9039 * @returns {Observable} An observable sequence with the result of calling the selector function with the last elements of both input sequences.
9040 */
9041 observableProto.forkJoin = function () {
9042 var len = arguments.length, args = new Array(len);
9043 for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
9044 if (Array.isArray(args[0])) {
9045 args[0].unshift(this);
9046 } else {
9047 args.unshift(this);
9048 }
9049 return Observable.forkJoin.apply(null, args);
9050 };
9051
9052 /**
9053 * Comonadic bind operator.
9054 * @param {Function} selector A transform function to apply to each element.
9055 * @param {Object} scheduler Scheduler used to execute the operation. If not specified, defaults to the ImmediateScheduler.
9056 * @returns {Observable} An observable sequence which results from the comonadic bind operation.
9057 */
9058 observableProto.manySelect = observableProto.extend = function (selector, scheduler) {
9059 isScheduler(scheduler) || (scheduler = Rx.Scheduler.immediate);
9060 var source = this;
9061 return observableDefer(function () {
9062 var chain;
9063
9064 return source
9065 .map(function (x) {
9066 var curr = new ChainObservable(x);
9067
9068 chain && chain.onNext(x);
9069 chain = curr;
9070
9071 return curr;
9072 })
9073 .tap(
9074 noop,
9075 function (e) { chain && chain.onError(e); },
9076 function () { chain && chain.onCompleted(); }
9077 )
9078 .observeOn(scheduler)
9079 .map(selector);
9080 }, source);
9081 };
9082
9083 var ChainObservable = (function (__super__) {
9084 inherits(ChainObservable, __super__);
9085 function ChainObservable(head) {
9086 __super__.call(this);
9087 this.head = head;
9088 this.tail = new AsyncSubject();
9089 }
9090
9091 addProperties(ChainObservable.prototype, Observer, {
9092 _subscribe: function (o) {
9093 var g = new CompositeDisposable();
9094 g.add(currentThreadScheduler.schedule(this, function (_, self) {
9095 o.onNext(self.head);
9096 g.add(self.tail.mergeAll().subscribe(o));
9097 }));
9098
9099 return g;
9100 },
9101 onCompleted: function () {
9102 this.onNext(Observable.empty());
9103 },
9104 onError: function (e) {
9105 this.onNext(Observable['throw'](e));
9106 },
9107 onNext: function (v) {
9108 this.tail.onNext(v);
9109 this.tail.onCompleted();
9110 }
9111 });
9112
9113 return ChainObservable;
9114
9115 }(Observable));
9116
9117 var Map = root.Map || (function () {
9118 function Map() {
9119 this.size = 0;
9120 this._values = [];
9121 this._keys = [];
9122 }
9123
9124 Map.prototype['delete'] = function (key) {
9125 var i = this._keys.indexOf(key);
9126 if (i === -1) { return false; }
9127 this._values.splice(i, 1);
9128 this._keys.splice(i, 1);
9129 this.size--;
9130 return true;
9131 };
9132
9133 Map.prototype.get = function (key) {
9134 var i = this._keys.indexOf(key);
9135 return i === -1 ? undefined : this._values[i];
9136 };
9137
9138 Map.prototype.set = function (key, value) {
9139 var i = this._keys.indexOf(key);
9140 if (i === -1) {
9141 this._keys.push(key);
9142 this._values.push(value);
9143 this.size++;
9144 } else {
9145 this._values[i] = value;
9146 }
9147 return this;
9148 };
9149
9150 Map.prototype.forEach = function (cb, thisArg) {
9151 for (var i = 0; i < this.size; i++) {
9152 cb.call(thisArg, this._values[i], this._keys[i]);
9153 }
9154 };
9155
9156 return Map;
9157 }());
9158
9159 /**
9160 * @constructor
9161 * Represents a join pattern over observable sequences.
9162 */
9163 function Pattern(patterns) {
9164 this.patterns = patterns;
9165 }
9166
9167 /**
9168 * Creates a pattern that matches the current plan matches and when the specified observable sequences has an available value.
9169 * @param other Observable sequence to match in addition to the current pattern.
9170 * @return {Pattern} Pattern object that matches when all observable sequences in the pattern have an available value.
9171 */
9172 Pattern.prototype.and = function (other) {
9173 return new Pattern(this.patterns.concat(other));
9174 };
9175
9176 /**
9177 * Matches when all observable sequences in the pattern (specified using a chain of and operators) have an available value and projects the values.
9178 * @param {Function} selector Selector that will be invoked with available values from the source sequences, in the same order of the sequences in the pattern.
9179 * @return {Plan} Plan that produces the projected values, to be fed (with other plans) to the when operator.
9180 */
9181 Pattern.prototype.thenDo = function (selector) {
9182 return new Plan(this, selector);
9183 };
9184
9185 function Plan(expression, selector) {
9186 this.expression = expression;
9187 this.selector = selector;
9188 }
9189
9190 function handleOnError(o) { return function (e) { o.onError(e); }; }
9191 function handleOnNext(self, observer) {
9192 return function onNext () {
9193 var result = tryCatch(self.selector).apply(self, arguments);
9194 if (result === errorObj) { return observer.onError(result.e); }
9195 observer.onNext(result);
9196 };
9197 }
9198
9199 Plan.prototype.activate = function (externalSubscriptions, observer, deactivate) {
9200 var joinObservers = [], errHandler = handleOnError(observer);
9201 for (var i = 0, len = this.expression.patterns.length; i < len; i++) {
9202 joinObservers.push(planCreateObserver(externalSubscriptions, this.expression.patterns[i], errHandler));
9203 }
9204 var activePlan = new ActivePlan(joinObservers, handleOnNext(this, observer), function () {
9205 for (var j = 0, jlen = joinObservers.length; j < jlen; j++) {
9206 joinObservers[j].removeActivePlan(activePlan);
9207 }
9208 deactivate(activePlan);
9209 });
9210 for (i = 0, len = joinObservers.length; i < len; i++) {
9211 joinObservers[i].addActivePlan(activePlan);
9212 }
9213 return activePlan;
9214 };
9215
9216 function planCreateObserver(externalSubscriptions, observable, onError) {
9217 var entry = externalSubscriptions.get(observable);
9218 if (!entry) {
9219 var observer = new JoinObserver(observable, onError);
9220 externalSubscriptions.set(observable, observer);
9221 return observer;
9222 }
9223 return entry;
9224 }
9225
9226 function ActivePlan(joinObserverArray, onNext, onCompleted) {
9227 this.joinObserverArray = joinObserverArray;
9228 this.onNext = onNext;
9229 this.onCompleted = onCompleted;
9230 this.joinObservers = new Map();
9231 for (var i = 0, len = this.joinObserverArray.length; i < len; i++) {
9232 var joinObserver = this.joinObserverArray[i];
9233 this.joinObservers.set(joinObserver, joinObserver);
9234 }
9235 }
9236
9237 ActivePlan.prototype.dequeue = function () {
9238 this.joinObservers.forEach(function (v) { v.queue.shift(); });
9239 };
9240
9241 ActivePlan.prototype.match = function () {
9242 var i, len, hasValues = true;
9243 for (i = 0, len = this.joinObserverArray.length; i < len; i++) {
9244 if (this.joinObserverArray[i].queue.length === 0) {
9245 hasValues = false;
9246 break;
9247 }
9248 }
9249 if (hasValues) {
9250 var firstValues = [],
9251 isCompleted = false;
9252 for (i = 0, len = this.joinObserverArray.length; i < len; i++) {
9253 firstValues.push(this.joinObserverArray[i].queue[0]);
9254 this.joinObserverArray[i].queue[0].kind === 'C' && (isCompleted = true);
9255 }
9256 if (isCompleted) {
9257 this.onCompleted();
9258 } else {
9259 this.dequeue();
9260 var values = [];
9261 for (i = 0, len = firstValues.length; i < firstValues.length; i++) {
9262 values.push(firstValues[i].value);
9263 }
9264 this.onNext.apply(this, values);
9265 }
9266 }
9267 };
9268
9269 var JoinObserver = (function (__super__) {
9270 inherits(JoinObserver, __super__);
9271
9272 function JoinObserver(source, onError) {
9273 __super__.call(this);
9274 this.source = source;
9275 this.onError = onError;
9276 this.queue = [];
9277 this.activePlans = [];
9278 this.subscription = new SingleAssignmentDisposable();
9279 this.isDisposed = false;
9280 }
9281
9282 var JoinObserverPrototype = JoinObserver.prototype;
9283
9284 JoinObserverPrototype.next = function (notification) {
9285 if (!this.isDisposed) {
9286 if (notification.kind === 'E') {
9287 return this.onError(notification.error);
9288 }
9289 this.queue.push(notification);
9290 var activePlans = this.activePlans.slice(0);
9291 for (var i = 0, len = activePlans.length; i < len; i++) {
9292 activePlans[i].match();
9293 }
9294 }
9295 };
9296
9297 JoinObserverPrototype.error = noop;
9298 JoinObserverPrototype.completed = noop;
9299
9300 JoinObserverPrototype.addActivePlan = function (activePlan) {
9301 this.activePlans.push(activePlan);
9302 };
9303
9304 JoinObserverPrototype.subscribe = function () {
9305 this.subscription.setDisposable(this.source.materialize().subscribe(this));
9306 };
9307
9308 JoinObserverPrototype.removeActivePlan = function (activePlan) {
9309 this.activePlans.splice(this.activePlans.indexOf(activePlan), 1);
9310 this.activePlans.length === 0 && this.dispose();
9311 };
9312
9313 JoinObserverPrototype.dispose = function () {
9314 __super__.prototype.dispose.call(this);
9315 if (!this.isDisposed) {
9316 this.isDisposed = true;
9317 this.subscription.dispose();
9318 }
9319 };
9320
9321 return JoinObserver;
9322 } (AbstractObserver));
9323
9324 /**
9325 * Creates a pattern that matches when both observable sequences have an available value.
9326 *
9327 * @param right Observable sequence to match with the current sequence.
9328 * @return {Pattern} Pattern object that matches when both observable sequences have an available value.
9329 */
9330 observableProto.and = function (right) {
9331 return new Pattern([this, right]);
9332 };
9333
9334 /**
9335 * Matches when the observable sequence has an available value and projects the value.
9336 *
9337 * @param {Function} selector Selector that will be invoked for values in the source sequence.
9338 * @returns {Plan} Plan that produces the projected values, to be fed (with other plans) to the when operator.
9339 */
9340 observableProto.thenDo = function (selector) {
9341 return new Pattern([this]).thenDo(selector);
9342 };
9343
9344 /**
9345 * Joins together the results from several patterns.
9346 *
9347 * @param plans A series of plans (specified as an Array of as a series of arguments) created by use of the Then operator on patterns.
9348 * @returns {Observable} Observable sequence with the results form matching several patterns.
9349 */
9350 Observable.when = function () {
9351 var len = arguments.length, plans;
9352 if (Array.isArray(arguments[0])) {
9353 plans = arguments[0];
9354 } else {
9355 plans = new Array(len);
9356 for(var i = 0; i < len; i++) { plans[i] = arguments[i]; }
9357 }
9358 return new AnonymousObservable(function (o) {
9359 var activePlans = [],
9360 externalSubscriptions = new Map();
9361 var outObserver = observerCreate(
9362 function (x) { o.onNext(x); },
9363 function (err) {
9364 externalSubscriptions.forEach(function (v) { v.onError(err); });
9365 o.onError(err);
9366 },
9367 function (x) { o.onCompleted(); }
9368 );
9369 try {
9370 for (var i = 0, len = plans.length; i < len; i++) {
9371 activePlans.push(plans[i].activate(externalSubscriptions, outObserver, function (activePlan) {
9372 var idx = activePlans.indexOf(activePlan);
9373 activePlans.splice(idx, 1);
9374 activePlans.length === 0 && o.onCompleted();
9375 }));
9376 }
9377 } catch (e) {
9378 observableThrow(e).subscribe(o);
9379 }
9380 var group = new CompositeDisposable();
9381 externalSubscriptions.forEach(function (joinObserver) {
9382 joinObserver.subscribe();
9383 group.add(joinObserver);
9384 });
9385
9386 return group;
9387 });
9388 };
9389
9390 var TimerObservable = (function(__super__) {
9391 inherits(TimerObservable, __super__);
9392 function TimerObservable(dt, s) {
9393 this._dt = dt;
9394 this._s = s;
9395 __super__.call(this);
9396 }
9397
9398 TimerObservable.prototype.subscribeCore = function (o) {
9399 return this._s.scheduleFuture(o, this._dt, scheduleMethod);
9400 };
9401
9402 function scheduleMethod(s, o) {
9403 o.onNext(0);
9404 o.onCompleted();
9405 }
9406
9407 return TimerObservable;
9408 }(ObservableBase));
9409
9410 function _observableTimer(dueTime, scheduler) {
9411 return new TimerObservable(dueTime, scheduler);
9412 }
9413
9414 function observableTimerDateAndPeriod(dueTime, period, scheduler) {
9415 return new AnonymousObservable(function (observer) {
9416 var d = dueTime, p = normalizeTime(period);
9417 return scheduler.scheduleRecursiveFuture(0, d, function (count, self) {
9418 if (p > 0) {
9419 var now = scheduler.now();
9420 d = new Date(d.getTime() + p);
9421 d.getTime() <= now && (d = new Date(now + p));
9422 }
9423 observer.onNext(count);
9424 self(count + 1, new Date(d));
9425 });
9426 });
9427 }
9428
9429 function observableTimerTimeSpanAndPeriod(dueTime, period, scheduler) {
9430 return dueTime === period ?
9431 new AnonymousObservable(function (observer) {
9432 return scheduler.schedulePeriodic(0, period, function (count) {
9433 observer.onNext(count);
9434 return count + 1;
9435 });
9436 }) :
9437 observableDefer(function () {
9438 return observableTimerDateAndPeriod(new Date(scheduler.now() + dueTime), period, scheduler);
9439 });
9440 }
9441
9442 /**
9443 * Returns an observable sequence that produces a value after each period.
9444 *
9445 * @example
9446 * 1 - res = Rx.Observable.interval(1000);
9447 * 2 - res = Rx.Observable.interval(1000, Rx.Scheduler.timeout);
9448 *
9449 * @param {Number} period Period for producing the values in the resulting sequence (specified as an integer denoting milliseconds).
9450 * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, Rx.Scheduler.timeout is used.
9451 * @returns {Observable} An observable sequence that produces a value after each period.
9452 */
9453 var observableinterval = Observable.interval = function (period, scheduler) {
9454 return observableTimerTimeSpanAndPeriod(period, period, isScheduler(scheduler) ? scheduler : defaultScheduler);
9455 };
9456
9457 /**
9458 * Returns an observable sequence that produces a value after dueTime has elapsed and then after each period.
9459 * @param {Number} dueTime Absolute (specified as a Date object) or relative time (specified as an integer denoting milliseconds) at which to produce the first value.
9460 * @param {Mixed} [periodOrScheduler] Period to produce subsequent values (specified as an integer denoting milliseconds), or the scheduler to run the timer on. If not specified, the resulting timer is not recurring.
9461 * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, the timeout scheduler is used.
9462 * @returns {Observable} An observable sequence that produces a value after due time has elapsed and then each period.
9463 */
9464 var observableTimer = Observable.timer = function (dueTime, periodOrScheduler, scheduler) {
9465 var period;
9466 isScheduler(scheduler) || (scheduler = defaultScheduler);
9467 if (periodOrScheduler != null && typeof periodOrScheduler === 'number') {
9468 period = periodOrScheduler;
9469 } else if (isScheduler(periodOrScheduler)) {
9470 scheduler = periodOrScheduler;
9471 }
9472 if ((dueTime instanceof Date || typeof dueTime === 'number') && period === undefined) {
9473 return _observableTimer(dueTime, scheduler);
9474 }
9475 if (dueTime instanceof Date && period !== undefined) {
9476 return observableTimerDateAndPeriod(dueTime.getTime(), periodOrScheduler, scheduler);
9477 }
9478 return observableTimerTimeSpanAndPeriod(dueTime, period, scheduler);
9479 };
9480
9481 function observableDelayRelative(source, dueTime, scheduler) {
9482 return new AnonymousObservable(function (o) {
9483 var active = false,
9484 cancelable = new SerialDisposable(),
9485 exception = null,
9486 q = [],
9487 running = false,
9488 subscription;
9489 subscription = source.materialize().timestamp(scheduler).subscribe(function (notification) {
9490 var d, shouldRun;
9491 if (notification.value.kind === 'E') {
9492 q = [];
9493 q.push(notification);
9494 exception = notification.value.error;
9495 shouldRun = !running;
9496 } else {
9497 q.push({ value: notification.value, timestamp: notification.timestamp + dueTime });
9498 shouldRun = !active;
9499 active = true;
9500 }
9501 if (shouldRun) {
9502 if (exception !== null) {
9503 o.onError(exception);
9504 } else {
9505 d = new SingleAssignmentDisposable();
9506 cancelable.setDisposable(d);
9507 d.setDisposable(scheduler.scheduleRecursiveFuture(null, dueTime, function (_, self) {
9508 var e, recurseDueTime, result, shouldRecurse;
9509 if (exception !== null) {
9510 return;
9511 }
9512 running = true;
9513 do {
9514 result = null;
9515 if (q.length > 0 && q[0].timestamp - scheduler.now() <= 0) {
9516 result = q.shift().value;
9517 }
9518 if (result !== null) {
9519 result.accept(o);
9520 }
9521 } while (result !== null);
9522 shouldRecurse = false;
9523 recurseDueTime = 0;
9524 if (q.length > 0) {
9525 shouldRecurse = true;
9526 recurseDueTime = Math.max(0, q[0].timestamp - scheduler.now());
9527 } else {
9528 active = false;
9529 }
9530 e = exception;
9531 running = false;
9532 if (e !== null) {
9533 o.onError(e);
9534 } else if (shouldRecurse) {
9535 self(null, recurseDueTime);
9536 }
9537 }));
9538 }
9539 }
9540 });
9541 return new BinaryDisposable(subscription, cancelable);
9542 }, source);
9543 }
9544
9545 function observableDelayAbsolute(source, dueTime, scheduler) {
9546 return observableDefer(function () {
9547 return observableDelayRelative(source, dueTime - scheduler.now(), scheduler);
9548 });
9549 }
9550
9551 function delayWithSelector(source, subscriptionDelay, delayDurationSelector) {
9552 var subDelay, selector;
9553 if (isFunction(subscriptionDelay)) {
9554 selector = subscriptionDelay;
9555 } else {
9556 subDelay = subscriptionDelay;
9557 selector = delayDurationSelector;
9558 }
9559 return new AnonymousObservable(function (o) {
9560 var delays = new CompositeDisposable(), atEnd = false, subscription = new SerialDisposable();
9561
9562 function start() {
9563 subscription.setDisposable(source.subscribe(
9564 function (x) {
9565 var delay = tryCatch(selector)(x);
9566 if (delay === errorObj) { return o.onError(delay.e); }
9567 var d = new SingleAssignmentDisposable();
9568 delays.add(d);
9569 d.setDisposable(delay.subscribe(
9570 function () {
9571 o.onNext(x);
9572 delays.remove(d);
9573 done();
9574 },
9575 function (e) { o.onError(e); },
9576 function () {
9577 o.onNext(x);
9578 delays.remove(d);
9579 done();
9580 }
9581 ));
9582 },
9583 function (e) { o.onError(e); },
9584 function () {
9585 atEnd = true;
9586 subscription.dispose();
9587 done();
9588 }
9589 ));
9590 }
9591
9592 function done () {
9593 atEnd && delays.length === 0 && o.onCompleted();
9594 }
9595
9596 if (!subDelay) {
9597 start();
9598 } else {
9599 subscription.setDisposable(subDelay.subscribe(start, function (e) { o.onError(e); }, start));
9600 }
9601
9602 return new BinaryDisposable(subscription, delays);
9603 }, this);
9604 }
9605
9606 /**
9607 * Time shifts the observable sequence by dueTime.
9608 * The relative time intervals between the values are preserved.
9609 *
9610 * @param {Number} dueTime Absolute (specified as a Date object) or relative time (specified as an integer denoting milliseconds) by which to shift the observable sequence.
9611 * @param {Scheduler} [scheduler] Scheduler to run the delay timers on. If not specified, the timeout scheduler is used.
9612 * @returns {Observable} Time-shifted sequence.
9613 */
9614 observableProto.delay = function () {
9615 var firstArg = arguments[0];
9616 if (typeof firstArg === 'number' || firstArg instanceof Date) {
9617 var dueTime = firstArg, scheduler = arguments[1];
9618 isScheduler(scheduler) || (scheduler = defaultScheduler);
9619 return dueTime instanceof Date ?
9620 observableDelayAbsolute(this, dueTime, scheduler) :
9621 observableDelayRelative(this, dueTime, scheduler);
9622 } else if (Observable.isObservable(firstArg) || isFunction(firstArg)) {
9623 return delayWithSelector(this, firstArg, arguments[1]);
9624 } else {
9625 throw new Error('Invalid arguments');
9626 }
9627 };
9628
9629 var DebounceObservable = (function (__super__) {
9630 inherits(DebounceObservable, __super__);
9631 function DebounceObservable(source, dt, s) {
9632 isScheduler(s) || (s = defaultScheduler);
9633 this.source = source;
9634 this._dt = dt;
9635 this._s = s;
9636 __super__.call(this);
9637 }
9638
9639 DebounceObservable.prototype.subscribeCore = function (o) {
9640 var cancelable = new SerialDisposable();
9641 return new BinaryDisposable(
9642 this.source.subscribe(new DebounceObserver(o, this.source, this._dt, this._s, cancelable)),
9643 cancelable);
9644 };
9645
9646 return DebounceObservable;
9647 }(ObservableBase));
9648
9649 var DebounceObserver = (function (__super__) {
9650 inherits(DebounceObserver, __super__);
9651 function DebounceObserver(observer, source, dueTime, scheduler, cancelable) {
9652 this._o = observer;
9653 this._s = source;
9654 this._d = dueTime;
9655 this._scheduler = scheduler;
9656 this._c = cancelable;
9657 this._v = null;
9658 this._hv = false;
9659 this._id = 0;
9660 __super__.call(this);
9661 }
9662
9663 DebounceObserver.prototype.next = function (x) {
9664 this._hv = true;
9665 this._v = x;
9666 var currentId = ++this._id, d = new SingleAssignmentDisposable();
9667 this._c.setDisposable(d);
9668 d.setDisposable(this._scheduler.scheduleFuture(this, this._d, function (_, self) {
9669 self._hv && self._id === currentId && self._o.onNext(x);
9670 self._hv = false;
9671 }));
9672 };
9673
9674 DebounceObserver.prototype.error = function (e) {
9675 this._c.dispose();
9676 this._o.onError(e);
9677 this._hv = false;
9678 this._id++;
9679 };
9680
9681 DebounceObserver.prototype.completed = function () {
9682 this._c.dispose();
9683 this._hv && this._o.onNext(this._v);
9684 this._o.onCompleted();
9685 this._hv = false;
9686 this._id++;
9687 };
9688
9689 return DebounceObserver;
9690 }(AbstractObserver));
9691
9692 function debounceWithSelector(source, durationSelector) {
9693 return new AnonymousObservable(function (o) {
9694 var value, hasValue = false, cancelable = new SerialDisposable(), id = 0;
9695 var subscription = source.subscribe(
9696 function (x) {
9697 var throttle = tryCatch(durationSelector)(x);
9698 if (throttle === errorObj) { return o.onError(throttle.e); }
9699
9700 isPromise(throttle) && (throttle = observableFromPromise(throttle));
9701
9702 hasValue = true;
9703 value = x;
9704 id++;
9705 var currentid = id, d = new SingleAssignmentDisposable();
9706 cancelable.setDisposable(d);
9707 d.setDisposable(throttle.subscribe(
9708 function () {
9709 hasValue && id === currentid && o.onNext(value);
9710 hasValue = false;
9711 d.dispose();
9712 },
9713 function (e) { o.onError(e); },
9714 function () {
9715 hasValue && id === currentid && o.onNext(value);
9716 hasValue = false;
9717 d.dispose();
9718 }
9719 ));
9720 },
9721 function (e) {
9722 cancelable.dispose();
9723 o.onError(e);
9724 hasValue = false;
9725 id++;
9726 },
9727 function () {
9728 cancelable.dispose();
9729 hasValue && o.onNext(value);
9730 o.onCompleted();
9731 hasValue = false;
9732 id++;
9733 }
9734 );
9735 return new BinaryDisposable(subscription, cancelable);
9736 }, source);
9737 }
9738
9739 observableProto.debounce = function () {
9740 if (isFunction (arguments[0])) {
9741 return debounceWithSelector(this, arguments[0]);
9742 } else if (typeof arguments[0] === 'number') {
9743 return new DebounceObservable(this, arguments[0], arguments[1]);
9744 } else {
9745 throw new Error('Invalid arguments');
9746 }
9747 };
9748
9749 /**
9750 * Projects each element of an observable sequence into zero or more windows which are produced based on timing information.
9751 * @param {Number} timeSpan Length of each window (specified as an integer denoting milliseconds).
9752 * @param {Mixed} [timeShiftOrScheduler] Interval between creation of consecutive windows (specified as an integer denoting milliseconds), or an optional scheduler parameter. If not specified, the time shift corresponds to the timeSpan parameter, resulting in non-overlapping adjacent windows.
9753 * @param {Scheduler} [scheduler] Scheduler to run windowing timers on. If not specified, the timeout scheduler is used.
9754 * @returns {Observable} An observable sequence of windows.
9755 */
9756 observableProto.windowWithTime = function (timeSpan, timeShiftOrScheduler, scheduler) {
9757 var source = this, timeShift;
9758 timeShiftOrScheduler == null && (timeShift = timeSpan);
9759 isScheduler(scheduler) || (scheduler = defaultScheduler);
9760 if (typeof timeShiftOrScheduler === 'number') {
9761 timeShift = timeShiftOrScheduler;
9762 } else if (isScheduler(timeShiftOrScheduler)) {
9763 timeShift = timeSpan;
9764 scheduler = timeShiftOrScheduler;
9765 }
9766 return new AnonymousObservable(function (observer) {
9767 var groupDisposable,
9768 nextShift = timeShift,
9769 nextSpan = timeSpan,
9770 q = [],
9771 refCountDisposable,
9772 timerD = new SerialDisposable(),
9773 totalTime = 0;
9774 groupDisposable = new CompositeDisposable(timerD),
9775 refCountDisposable = new RefCountDisposable(groupDisposable);
9776
9777 function createTimer () {
9778 var m = new SingleAssignmentDisposable(),
9779 isSpan = false,
9780 isShift = false;
9781 timerD.setDisposable(m);
9782 if (nextSpan === nextShift) {
9783 isSpan = true;
9784 isShift = true;
9785 } else if (nextSpan < nextShift) {
9786 isSpan = true;
9787 } else {
9788 isShift = true;
9789 }
9790 var newTotalTime = isSpan ? nextSpan : nextShift,
9791 ts = newTotalTime - totalTime;
9792 totalTime = newTotalTime;
9793 if (isSpan) {
9794 nextSpan += timeShift;
9795 }
9796 if (isShift) {
9797 nextShift += timeShift;
9798 }
9799 m.setDisposable(scheduler.scheduleFuture(null, ts, function () {
9800 if (isShift) {
9801 var s = new Subject();
9802 q.push(s);
9803 observer.onNext(addRef(s, refCountDisposable));
9804 }
9805 isSpan && q.shift().onCompleted();
9806 createTimer();
9807 }));
9808 };
9809 q.push(new Subject());
9810 observer.onNext(addRef(q[0], refCountDisposable));
9811 createTimer();
9812 groupDisposable.add(source.subscribe(
9813 function (x) {
9814 for (var i = 0, len = q.length; i < len; i++) { q[i].onNext(x); }
9815 },
9816 function (e) {
9817 for (var i = 0, len = q.length; i < len; i++) { q[i].onError(e); }
9818 observer.onError(e);
9819 },
9820 function () {
9821 for (var i = 0, len = q.length; i < len; i++) { q[i].onCompleted(); }
9822 observer.onCompleted();
9823 }
9824 ));
9825 return refCountDisposable;
9826 }, source);
9827 };
9828
9829 /**
9830 * Projects each element of an observable sequence into a window that is completed when either it's full or a given amount of time has elapsed.
9831 * @param {Number} timeSpan Maximum time length of a window.
9832 * @param {Number} count Maximum element count of a window.
9833 * @param {Scheduler} [scheduler] Scheduler to run windowing timers on. If not specified, the timeout scheduler is used.
9834 * @returns {Observable} An observable sequence of windows.
9835 */
9836 observableProto.windowWithTimeOrCount = function (timeSpan, count, scheduler) {
9837 var source = this;
9838 isScheduler(scheduler) || (scheduler = defaultScheduler);
9839 return new AnonymousObservable(function (observer) {
9840 var timerD = new SerialDisposable(),
9841 groupDisposable = new CompositeDisposable(timerD),
9842 refCountDisposable = new RefCountDisposable(groupDisposable),
9843 n = 0,
9844 windowId = 0,
9845 s = new Subject();
9846
9847 function createTimer(id) {
9848 var m = new SingleAssignmentDisposable();
9849 timerD.setDisposable(m);
9850 m.setDisposable(scheduler.scheduleFuture(null, timeSpan, function () {
9851 if (id !== windowId) { return; }
9852 n = 0;
9853 var newId = ++windowId;
9854 s.onCompleted();
9855 s = new Subject();
9856 observer.onNext(addRef(s, refCountDisposable));
9857 createTimer(newId);
9858 }));
9859 }
9860
9861 observer.onNext(addRef(s, refCountDisposable));
9862 createTimer(0);
9863
9864 groupDisposable.add(source.subscribe(
9865 function (x) {
9866 var newId = 0, newWindow = false;
9867 s.onNext(x);
9868 if (++n === count) {
9869 newWindow = true;
9870 n = 0;
9871 newId = ++windowId;
9872 s.onCompleted();
9873 s = new Subject();
9874 observer.onNext(addRef(s, refCountDisposable));
9875 }
9876 newWindow && createTimer(newId);
9877 },
9878 function (e) {
9879 s.onError(e);
9880 observer.onError(e);
9881 }, function () {
9882 s.onCompleted();
9883 observer.onCompleted();
9884 }
9885 ));
9886 return refCountDisposable;
9887 }, source);
9888 };
9889
9890 function toArray(x) { return x.toArray(); }
9891
9892 /**
9893 * Projects each element of an observable sequence into zero or more buffers which are produced based on timing information.
9894 * @param {Number} timeSpan Length of each buffer (specified as an integer denoting milliseconds).
9895 * @param {Mixed} [timeShiftOrScheduler] Interval between creation of consecutive buffers (specified as an integer denoting milliseconds), or an optional scheduler parameter. If not specified, the time shift corresponds to the timeSpan parameter, resulting in non-overlapping adjacent buffers.
9896 * @param {Scheduler} [scheduler] Scheduler to run buffer timers on. If not specified, the timeout scheduler is used.
9897 * @returns {Observable} An observable sequence of buffers.
9898 */
9899 observableProto.bufferWithTime = function (timeSpan, timeShiftOrScheduler, scheduler) {
9900 return this.windowWithTime(timeSpan, timeShiftOrScheduler, scheduler).flatMap(toArray);
9901 };
9902
9903 function toArray(x) { return x.toArray(); }
9904
9905 /**
9906 * Projects each element of an observable sequence into a buffer that is completed when either it's full or a given amount of time has elapsed.
9907 * @param {Number} timeSpan Maximum time length of a buffer.
9908 * @param {Number} count Maximum element count of a buffer.
9909 * @param {Scheduler} [scheduler] Scheduler to run bufferin timers on. If not specified, the timeout scheduler is used.
9910 * @returns {Observable} An observable sequence of buffers.
9911 */
9912 observableProto.bufferWithTimeOrCount = function (timeSpan, count, scheduler) {
9913 return this.windowWithTimeOrCount(timeSpan, count, scheduler).flatMap(toArray);
9914 };
9915
9916 var TimeIntervalObservable = (function (__super__) {
9917 inherits(TimeIntervalObservable, __super__);
9918 function TimeIntervalObservable(source, s) {
9919 this.source = source;
9920 this._s = s;
9921 __super__.call(this);
9922 }
9923
9924 TimeIntervalObservable.prototype.subscribeCore = function (o) {
9925 return this.source.subscribe(new TimeIntervalObserver(o, this._s));
9926 };
9927
9928 return TimeIntervalObservable;
9929 }(ObservableBase));
9930
9931 var TimeIntervalObserver = (function (__super__) {
9932 inherits(TimeIntervalObserver, __super__);
9933
9934 function TimeIntervalObserver(o, s) {
9935 this._o = o;
9936 this._s = s;
9937 this._l = s.now();
9938 __super__.call(this);
9939 }
9940
9941 TimeIntervalObserver.prototype.next = function (x) {
9942 var now = this._s.now(), span = now - this._l;
9943 this._l = now;
9944 this._o.onNext({ value: x, interval: span });
9945 };
9946 TimeIntervalObserver.prototype.error = function (e) { this._o.onError(e); };
9947 TimeIntervalObserver.prototype.completed = function () { this._o.onCompleted(); };
9948
9949 return TimeIntervalObserver;
9950 }(AbstractObserver));
9951
9952 /**
9953 * Records the time interval between consecutive values in an observable sequence.
9954 *
9955 * @example
9956 * 1 - res = source.timeInterval();
9957 * 2 - res = source.timeInterval(Rx.Scheduler.timeout);
9958 *
9959 * @param [scheduler] Scheduler used to compute time intervals. If not specified, the timeout scheduler is used.
9960 * @returns {Observable} An observable sequence with time interval information on values.
9961 */
9962 observableProto.timeInterval = function (scheduler) {
9963 isScheduler(scheduler) || (scheduler = defaultScheduler);
9964 return new TimeIntervalObservable(this, scheduler);
9965 };
9966
9967 var TimestampObservable = (function (__super__) {
9968 inherits(TimestampObservable, __super__);
9969 function TimestampObservable(source, s) {
9970 this.source = source;
9971 this._s = s;
9972 __super__.call(this);
9973 }
9974
9975 TimestampObservable.prototype.subscribeCore = function (o) {
9976 return this.source.subscribe(new TimestampObserver(o, this._s));
9977 };
9978
9979 return TimestampObservable;
9980 }(ObservableBase));
9981
9982 var TimestampObserver = (function (__super__) {
9983 inherits(TimestampObserver, __super__);
9984 function TimestampObserver(o, s) {
9985 this._o = o;
9986 this._s = s;
9987 __super__.call(this);
9988 }
9989
9990 TimestampObserver.prototype.next = function (x) {
9991 this._o.onNext({ value: x, timestamp: this._s.now() });
9992 };
9993
9994 TimestampObserver.prototype.error = function (e) {
9995 this._o.onError(e);
9996 };
9997
9998 TimestampObserver.prototype.completed = function () {
9999 this._o.onCompleted();
10000 };
10001
10002 return TimestampObserver;
10003 }(AbstractObserver));
10004
10005 /**
10006 * Records the timestamp for each value in an observable sequence.
10007 *
10008 * @example
10009 * 1 - res = source.timestamp(); // produces { value: x, timestamp: ts }
10010 * 2 - res = source.timestamp(Rx.Scheduler.default);
10011 *
10012 * @param {Scheduler} [scheduler] Scheduler used to compute timestamps. If not specified, the default scheduler is used.
10013 * @returns {Observable} An observable sequence with timestamp information on values.
10014 */
10015 observableProto.timestamp = function (scheduler) {
10016 isScheduler(scheduler) || (scheduler = defaultScheduler);
10017 return new TimestampObservable(this, scheduler);
10018 };
10019
10020 function sampleObservable(source, sampler) {
10021 return new AnonymousObservable(function (o) {
10022 var atEnd = false, value, hasValue = false;
10023
10024 function sampleSubscribe() {
10025 if (hasValue) {
10026 hasValue = false;
10027 o.onNext(value);
10028 }
10029 atEnd && o.onCompleted();
10030 }
10031
10032 var sourceSubscription = new SingleAssignmentDisposable();
10033 sourceSubscription.setDisposable(source.subscribe(
10034 function (newValue) {
10035 hasValue = true;
10036 value = newValue;
10037 },
10038 function (e) { o.onError(e); },
10039 function () {
10040 atEnd = true;
10041 sourceSubscription.dispose();
10042 }
10043 ));
10044
10045 return new BinaryDisposable(
10046 sourceSubscription,
10047 sampler.subscribe(sampleSubscribe, function (e) { o.onError(e); }, sampleSubscribe)
10048 );
10049 }, source);
10050 }
10051
10052 /**
10053 * Samples the observable sequence at each interval.
10054 *
10055 * @example
10056 * 1 - res = source.sample(sampleObservable); // Sampler tick sequence
10057 * 2 - res = source.sample(5000); // 5 seconds
10058 * 2 - res = source.sample(5000, Rx.Scheduler.timeout); // 5 seconds
10059 *
10060 * @param {Mixed} intervalOrSampler Interval at which to sample (specified as an integer denoting milliseconds) or Sampler Observable.
10061 * @param {Scheduler} [scheduler] Scheduler to run the sampling timer on. If not specified, the timeout scheduler is used.
10062 * @returns {Observable} Sampled observable sequence.
10063 */
10064 observableProto.sample = observableProto.throttleLatest = function (intervalOrSampler, scheduler) {
10065 isScheduler(scheduler) || (scheduler = defaultScheduler);
10066 return typeof intervalOrSampler === 'number' ?
10067 sampleObservable(this, observableinterval(intervalOrSampler, scheduler)) :
10068 sampleObservable(this, intervalOrSampler);
10069 };
10070
10071 var TimeoutError = Rx.TimeoutError = function(message) {
10072 this.message = message || 'Timeout has occurred';
10073 this.name = 'TimeoutError';
10074 Error.call(this);
10075 };
10076 TimeoutError.prototype = Object.create(Error.prototype);
10077
10078 function timeoutWithSelector(source, firstTimeout, timeoutDurationSelector, other) {
10079 if (isFunction(firstTimeout)) {
10080 other = timeoutDurationSelector;
10081 timeoutDurationSelector = firstTimeout;
10082 firstTimeout = observableNever();
10083 }
10084 Observable.isObservable(other) || (other = observableThrow(new TimeoutError()));
10085 return new AnonymousObservable(function (o) {
10086 var subscription = new SerialDisposable(),
10087 timer = new SerialDisposable(),
10088 original = new SingleAssignmentDisposable();
10089
10090 subscription.setDisposable(original);
10091
10092 var id = 0, switched = false;
10093
10094 function setTimer(timeout) {
10095 var myId = id, d = new SingleAssignmentDisposable();
10096
10097 function timerWins() {
10098 switched = (myId === id);
10099 return switched;
10100 }
10101
10102 timer.setDisposable(d);
10103 d.setDisposable(timeout.subscribe(function () {
10104 timerWins() && subscription.setDisposable(other.subscribe(o));
10105 d.dispose();
10106 }, function (e) {
10107 timerWins() && o.onError(e);
10108 }, function () {
10109 timerWins() && subscription.setDisposable(other.subscribe(o));
10110 }));
10111 };
10112
10113 setTimer(firstTimeout);
10114
10115 function oWins() {
10116 var res = !switched;
10117 if (res) { id++; }
10118 return res;
10119 }
10120
10121 original.setDisposable(source.subscribe(function (x) {
10122 if (oWins()) {
10123 o.onNext(x);
10124 var timeout = tryCatch(timeoutDurationSelector)(x);
10125 if (timeout === errorObj) { return o.onError(timeout.e); }
10126 setTimer(isPromise(timeout) ? observableFromPromise(timeout) : timeout);
10127 }
10128 }, function (e) {
10129 oWins() && o.onError(e);
10130 }, function () {
10131 oWins() && o.onCompleted();
10132 }));
10133 return new BinaryDisposable(subscription, timer);
10134 }, source);
10135 }
10136
10137 function timeout(source, dueTime, other, scheduler) {
10138 if (isScheduler(other)) {
10139 scheduler = other;
10140 other = observableThrow(new TimeoutError());
10141 }
10142 if (other instanceof Error) { other = observableThrow(other); }
10143 isScheduler(scheduler) || (scheduler = defaultScheduler);
10144 Observable.isObservable(other) || (other = observableThrow(new TimeoutError()));
10145 return new AnonymousObservable(function (o) {
10146 var id = 0,
10147 original = new SingleAssignmentDisposable(),
10148 subscription = new SerialDisposable(),
10149 switched = false,
10150 timer = new SerialDisposable();
10151
10152 subscription.setDisposable(original);
10153
10154 function createTimer() {
10155 var myId = id;
10156 timer.setDisposable(scheduler.scheduleFuture(null, dueTime, function () {
10157 switched = id === myId;
10158 if (switched) {
10159 isPromise(other) && (other = observableFromPromise(other));
10160 subscription.setDisposable(other.subscribe(o));
10161 }
10162 }));
10163 }
10164
10165 createTimer();
10166
10167 original.setDisposable(source.subscribe(function (x) {
10168 if (!switched) {
10169 id++;
10170 o.onNext(x);
10171 createTimer();
10172 }
10173 }, function (e) {
10174 if (!switched) {
10175 id++;
10176 o.onError(e);
10177 }
10178 }, function () {
10179 if (!switched) {
10180 id++;
10181 o.onCompleted();
10182 }
10183 }));
10184 return new BinaryDisposable(subscription, timer);
10185 }, source);
10186 }
10187
10188 observableProto.timeout = function () {
10189 var firstArg = arguments[0];
10190 if (firstArg instanceof Date || typeof firstArg === 'number') {
10191 return timeout(this, firstArg, arguments[1], arguments[2]);
10192 } else if (Observable.isObservable(firstArg) || isFunction(firstArg)) {
10193 return timeoutWithSelector(this, firstArg, arguments[1], arguments[2]);
10194 } else {
10195 throw new Error('Invalid arguments');
10196 }
10197 };
10198
10199 var GenerateAbsoluteObservable = (function (__super__) {
10200 inherits(GenerateAbsoluteObservable, __super__);
10201 function GenerateAbsoluteObservable(state, cndFn, itrFn, resFn, timeFn, s) {
10202 this._state = state;
10203 this._cndFn = cndFn;
10204 this._itrFn = itrFn;
10205 this._resFn = resFn;
10206 this._timeFn = timeFn;
10207 this._s = s;
10208 this._first = true;
10209 this._hasResult = false;
10210 __super__.call(this);
10211 }
10212
10213 function scheduleRecursive(self, recurse) {
10214 self._hasResult && self._o.onNext(self._state);
10215
10216 if (self._first) {
10217 self._first = false;
10218 } else {
10219 self._state = tryCatch(self._itrFn)(self._state);
10220 if (self._state === errorObj) { return self._o.onError(self._state.e); }
10221 }
10222 self._hasResult = tryCatch(self._cndFn)(self._state);
10223 if (self._hasResult === errorObj) { return self._o.onError(self._hasResult.e); }
10224 if (self._hasResult) {
10225 var result = tryCatch(self._resFn)(self._state);
10226 if (result === errorObj) { return self._o.onError(result.e); }
10227 var time = tryCatch(self._timeFn)(self._state);
10228 if (time === errorObj) { return self._o.onError(time.e); }
10229 recurse(self, time);
10230 } else {
10231 self._o.onCompleted();
10232 }
10233 }
10234
10235 GenerateAbsoluteObservable.prototype.subscribeCore = function (o) {
10236 this._o = o;
10237 return this._s.scheduleRecursiveFuture(this, new Date(this._s.now()), scheduleRecursive);
10238 };
10239
10240 return GenerateAbsoluteObservable;
10241 }(ObservableBase));
10242
10243 /**
10244 * GenerateAbsolutes an observable sequence by iterating a state from an initial state until the condition fails.
10245 *
10246 * @example
10247 * res = source.generateWithAbsoluteTime(0,
10248 * function (x) { return return true; },
10249 * function (x) { return x + 1; },
10250 * function (x) { return x; },
10251 * function (x) { return new Date(); }
10252 * });
10253 *
10254 * @param {Mixed} initialState Initial state.
10255 * @param {Function} condition Condition to terminate generation (upon returning false).
10256 * @param {Function} iterate Iteration step function.
10257 * @param {Function} resultSelector Selector function for results produced in the sequence.
10258 * @param {Function} timeSelector Time selector function to control the speed of values being produced each iteration, returning Date values.
10259 * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not specified, the timeout scheduler is used.
10260 * @returns {Observable} The generated sequence.
10261 */
10262 Observable.generateWithAbsoluteTime = function (initialState, condition, iterate, resultSelector, timeSelector, scheduler) {
10263 isScheduler(scheduler) || (scheduler = defaultScheduler);
10264 return new GenerateAbsoluteObservable(initialState, condition, iterate, resultSelector, timeSelector, scheduler);
10265 };
10266
10267 var GenerateRelativeObservable = (function (__super__) {
10268 inherits(GenerateRelativeObservable, __super__);
10269 function GenerateRelativeObservable(state, cndFn, itrFn, resFn, timeFn, s) {
10270 this._state = state;
10271 this._cndFn = cndFn;
10272 this._itrFn = itrFn;
10273 this._resFn = resFn;
10274 this._timeFn = timeFn;
10275 this._s = s;
10276 this._first = true;
10277 this._hasResult = false;
10278 __super__.call(this);
10279 }
10280
10281 function scheduleRecursive(self, recurse) {
10282 self._hasResult && self._o.onNext(self._state);
10283
10284 if (self._first) {
10285 self._first = false;
10286 } else {
10287 self._state = tryCatch(self._itrFn)(self._state);
10288 if (self._state === errorObj) { return self._o.onError(self._state.e); }
10289 }
10290 self._hasResult = tryCatch(self._cndFn)(self._state);
10291 if (self._hasResult === errorObj) { return self._o.onError(self._hasResult.e); }
10292 if (self._hasResult) {
10293 var result = tryCatch(self._resFn)(self._state);
10294 if (result === errorObj) { return self._o.onError(result.e); }
10295 var time = tryCatch(self._timeFn)(self._state);
10296 if (time === errorObj) { return self._o.onError(time.e); }
10297 recurse(self, time);
10298 } else {
10299 self._o.onCompleted();
10300 }
10301 }
10302
10303 GenerateRelativeObservable.prototype.subscribeCore = function (o) {
10304 this._o = o;
10305 return this._s.scheduleRecursiveFuture(this, 0, scheduleRecursive);
10306 };
10307
10308 return GenerateRelativeObservable;
10309 }(ObservableBase));
10310
10311 /**
10312 * Generates an observable sequence by iterating a state from an initial state until the condition fails.
10313 *
10314 * @example
10315 * res = source.generateWithRelativeTime(0,
10316 * function (x) { return return true; },
10317 * function (x) { return x + 1; },
10318 * function (x) { return x; },
10319 * function (x) { return 500; }
10320 * );
10321 *
10322 * @param {Mixed} initialState Initial state.
10323 * @param {Function} condition Condition to terminate generation (upon returning false).
10324 * @param {Function} iterate Iteration step function.
10325 * @param {Function} resultSelector Selector function for results produced in the sequence.
10326 * @param {Function} timeSelector Time selector function to control the speed of values being produced each iteration, returning integer values denoting milliseconds.
10327 * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not specified, the timeout scheduler is used.
10328 * @returns {Observable} The generated sequence.
10329 */
10330 Observable.generateWithRelativeTime = function (initialState, condition, iterate, resultSelector, timeSelector, scheduler) {
10331 isScheduler(scheduler) || (scheduler = defaultScheduler);
10332 return new GenerateRelativeObservable(initialState, condition, iterate, resultSelector, timeSelector, scheduler);
10333 };
10334
10335 var DelaySubscription = (function(__super__) {
10336 inherits(DelaySubscription, __super__);
10337 function DelaySubscription(source, dt, s) {
10338 this.source = source;
10339 this._dt = dt;
10340 this._s = s;
10341 __super__.call(this);
10342 }
10343
10344 DelaySubscription.prototype.subscribeCore = function (o) {
10345 var d = new SerialDisposable();
10346
10347 d.setDisposable(this._s.scheduleFuture([this.source, o, d], this._dt, scheduleMethod));
10348
10349 return d;
10350 };
10351
10352 function scheduleMethod(s, state) {
10353 var source = state[0], o = state[1], d = state[2];
10354 d.setDisposable(source.subscribe(o));
10355 }
10356
10357 return DelaySubscription;
10358 }(ObservableBase));
10359
10360 /**
10361 * Time shifts the observable sequence by delaying the subscription with the specified relative time duration, using the specified scheduler to run timers.
10362 *
10363 * @example
10364 * 1 - res = source.delaySubscription(5000); // 5s
10365 * 2 - res = source.delaySubscription(5000, Rx.Scheduler.default); // 5 seconds
10366 *
10367 * @param {Number} dueTime Relative or absolute time shift of the subscription.
10368 * @param {Scheduler} [scheduler] Scheduler to run the subscription delay timer on. If not specified, the timeout scheduler is used.
10369 * @returns {Observable} Time-shifted sequence.
10370 */
10371 observableProto.delaySubscription = function (dueTime, scheduler) {
10372 isScheduler(scheduler) || (scheduler = defaultScheduler);
10373 return new DelaySubscription(this, dueTime, scheduler);
10374 };
10375
10376 var SkipLastWithTimeObservable = (function (__super__) {
10377 inherits(SkipLastWithTimeObservable, __super__);
10378 function SkipLastWithTimeObservable(source, d, s) {
10379 this.source = source;
10380 this._d = d;
10381 this._s = s;
10382 __super__.call(this);
10383 }
10384
10385 SkipLastWithTimeObservable.prototype.subscribeCore = function (o) {
10386 return this.source.subscribe(new SkipLastWithTimeObserver(o, this));
10387 };
10388
10389 return SkipLastWithTimeObservable;
10390 }(ObservableBase));
10391
10392 var SkipLastWithTimeObserver = (function (__super__) {
10393 inherits(SkipLastWithTimeObserver, __super__);
10394
10395 function SkipLastWithTimeObserver(o, p) {
10396 this._o = o;
10397 this._s = p._s;
10398 this._d = p._d;
10399 this._q = [];
10400 __super__.call(this);
10401 }
10402
10403 SkipLastWithTimeObserver.prototype.next = function (x) {
10404 var now = this._s.now();
10405 this._q.push({ interval: now, value: x });
10406 while (this._q.length > 0 && now - this._q[0].interval >= this._d) {
10407 this._o.onNext(this._q.shift().value);
10408 }
10409 };
10410 SkipLastWithTimeObserver.prototype.error = function (e) { this._o.onError(e); };
10411 SkipLastWithTimeObserver.prototype.completed = function () {
10412 var now = this._s.now();
10413 while (this._q.length > 0 && now - this._q[0].interval >= this._d) {
10414 this._o.onNext(this._q.shift().value);
10415 }
10416 this._o.onCompleted();
10417 };
10418
10419 return SkipLastWithTimeObserver;
10420 }(AbstractObserver));
10421
10422 /**
10423 * Skips elements for the specified duration from the end of the observable source sequence, using the specified scheduler to run timers.
10424 * @description
10425 * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
10426 * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
10427 * result sequence. This causes elements to be delayed with duration.
10428 * @param {Number} duration Duration for skipping elements from the end of the sequence.
10429 * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout
10430 * @returns {Observable} An observable sequence with the elements skipped during the specified duration from the end of the source sequence.
10431 */
10432 observableProto.skipLastWithTime = function (duration, scheduler) {
10433 isScheduler(scheduler) || (scheduler = defaultScheduler);
10434 return new SkipLastWithTimeObservable(this, duration, scheduler);
10435 };
10436
10437 var TakeLastWithTimeObservable = (function (__super__) {
10438 inherits(TakeLastWithTimeObservable, __super__);
10439 function TakeLastWithTimeObservable(source, d, s) {
10440 this.source = source;
10441 this._d = d;
10442 this._s = s;
10443 __super__.call(this);
10444 }
10445
10446 TakeLastWithTimeObservable.prototype.subscribeCore = function (o) {
10447 return this.source.subscribe(new TakeLastWithTimeObserver(o, this._d, this._s));
10448 };
10449
10450 return TakeLastWithTimeObservable;
10451 }(ObservableBase));
10452
10453 var TakeLastWithTimeObserver = (function (__super__) {
10454 inherits(TakeLastWithTimeObserver, __super__);
10455
10456 function TakeLastWithTimeObserver(o, d, s) {
10457 this._o = o;
10458 this._d = d;
10459 this._s = s;
10460 this._q = [];
10461 __super__.call(this);
10462 }
10463
10464 TakeLastWithTimeObserver.prototype.next = function (x) {
10465 var now = this._s.now();
10466 this._q.push({ interval: now, value: x });
10467 while (this._q.length > 0 && now - this._q[0].interval >= this._d) {
10468 this._q.shift();
10469 }
10470 };
10471 TakeLastWithTimeObserver.prototype.error = function (e) { this._o.onError(e); };
10472 TakeLastWithTimeObserver.prototype.completed = function () {
10473 var now = this._s.now();
10474 while (this._q.length > 0) {
10475 var next = this._q.shift();
10476 if (now - next.interval <= this._d) { this._o.onNext(next.value); }
10477 }
10478 this._o.onCompleted();
10479 };
10480
10481 return TakeLastWithTimeObserver;
10482 }(AbstractObserver));
10483
10484 /**
10485 * Returns elements within the specified duration from the end of the observable source sequence, using the specified schedulers to run timers and to drain the collected elements.
10486 * @description
10487 * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
10488 * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
10489 * result sequence. This causes elements to be delayed with duration.
10490 * @param {Number} duration Duration for taking elements from the end of the sequence.
10491 * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
10492 * @returns {Observable} An observable sequence with the elements taken during the specified duration from the end of the source sequence.
10493 */
10494 observableProto.takeLastWithTime = function (duration, scheduler) {
10495 isScheduler(scheduler) || (scheduler = defaultScheduler);
10496 return new TakeLastWithTimeObservable(this, duration, scheduler);
10497 };
10498
10499 /**
10500 * Returns an array with the elements within the specified duration from the end of the observable source sequence, using the specified scheduler to run timers.
10501 * @description
10502 * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
10503 * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
10504 * result sequence. This causes elements to be delayed with duration.
10505 * @param {Number} duration Duration for taking elements from the end of the sequence.
10506 * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
10507 * @returns {Observable} An observable sequence containing a single array with the elements taken during the specified duration from the end of the source sequence.
10508 */
10509 observableProto.takeLastBufferWithTime = function (duration, scheduler) {
10510 var source = this;
10511 isScheduler(scheduler) || (scheduler = defaultScheduler);
10512 return new AnonymousObservable(function (o) {
10513 var q = [];
10514 return source.subscribe(function (x) {
10515 var now = scheduler.now();
10516 q.push({ interval: now, value: x });
10517 while (q.length > 0 && now - q[0].interval >= duration) {
10518 q.shift();
10519 }
10520 }, function (e) { o.onError(e); }, function () {
10521 var now = scheduler.now(), res = [];
10522 while (q.length > 0) {
10523 var next = q.shift();
10524 now - next.interval <= duration && res.push(next.value);
10525 }
10526 o.onNext(res);
10527 o.onCompleted();
10528 });
10529 }, source);
10530 };
10531
10532 var TakeWithTimeObservable = (function (__super__) {
10533 inherits(TakeWithTimeObservable, __super__);
10534 function TakeWithTimeObservable(source, d, s) {
10535 this.source = source;
10536 this._d = d;
10537 this._s = s;
10538 __super__.call(this);
10539 }
10540
10541 function scheduleMethod(s, o) {
10542 o.onCompleted();
10543 }
10544
10545 TakeWithTimeObservable.prototype.subscribeCore = function (o) {
10546 return new BinaryDisposable(
10547 this._s.scheduleFuture(o, this._d, scheduleMethod),
10548 this.source.subscribe(o)
10549 );
10550 };
10551
10552 return TakeWithTimeObservable;
10553 }(ObservableBase));
10554
10555 /**
10556 * Takes elements for the specified duration from the start of the observable source sequence, using the specified scheduler to run timers.
10557 *
10558 * @example
10559 * 1 - res = source.takeWithTime(5000, [optional scheduler]);
10560 * @description
10561 * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
10562 * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
10563 * result sequence. This causes elements to be delayed with duration.
10564 * @param {Number} duration Duration for taking elements from the start of the sequence.
10565 * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
10566 * @returns {Observable} An observable sequence with the elements taken during the specified duration from the start of the source sequence.
10567 */
10568 observableProto.takeWithTime = function (duration, scheduler) {
10569 isScheduler(scheduler) || (scheduler = defaultScheduler);
10570 return new TakeWithTimeObservable(this, duration, scheduler);
10571 };
10572
10573 var SkipWithTimeObservable = (function (__super__) {
10574 inherits(SkipWithTimeObservable, __super__);
10575 function SkipWithTimeObservable(source, d, s) {
10576 this.source = source;
10577 this._d = d;
10578 this._s = s;
10579 this._open = false;
10580 __super__.call(this);
10581 }
10582
10583 function scheduleMethod(s, self) {
10584 self._open = true;
10585 }
10586
10587 SkipWithTimeObservable.prototype.subscribeCore = function (o) {
10588 return new BinaryDisposable(
10589 this._s.scheduleFuture(this, this._d, scheduleMethod),
10590 this.source.subscribe(new SkipWithTimeObserver(o, this))
10591 );
10592 };
10593
10594 return SkipWithTimeObservable;
10595 }(ObservableBase));
10596
10597 var SkipWithTimeObserver = (function (__super__) {
10598 inherits(SkipWithTimeObserver, __super__);
10599
10600 function SkipWithTimeObserver(o, p) {
10601 this._o = o;
10602 this._p = p;
10603 __super__.call(this);
10604 }
10605
10606 SkipWithTimeObserver.prototype.next = function (x) { this._p._open && this._o.onNext(x); };
10607 SkipWithTimeObserver.prototype.error = function (e) { this._o.onError(e); };
10608 SkipWithTimeObserver.prototype.completed = function () { this._o.onCompleted(); };
10609
10610 return SkipWithTimeObserver;
10611 }(AbstractObserver));
10612
10613 /**
10614 * Skips elements for the specified duration from the start of the observable source sequence, using the specified scheduler to run timers.
10615 * @description
10616 * Specifying a zero value for duration doesn't guarantee no elements will be dropped from the start of the source sequence.
10617 * This is a side-effect of the asynchrony introduced by the scheduler, where the action that causes callbacks from the source sequence to be forwarded
10618 * may not execute immediately, despite the zero due time.
10619 *
10620 * Errors produced by the source sequence are always forwarded to the result sequence, even if the error occurs before the duration.
10621 * @param {Number} duration Duration for skipping elements from the start of the sequence.
10622 * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
10623 * @returns {Observable} An observable sequence with the elements skipped during the specified duration from the start of the source sequence.
10624 */
10625 observableProto.skipWithTime = function (duration, scheduler) {
10626 isScheduler(scheduler) || (scheduler = defaultScheduler);
10627 return new SkipWithTimeObservable(this, duration, scheduler);
10628 };
10629
10630 var SkipUntilWithTimeObservable = (function (__super__) {
10631 inherits(SkipUntilWithTimeObservable, __super__);
10632 function SkipUntilWithTimeObservable(source, startTime, scheduler) {
10633 this.source = source;
10634 this._st = startTime;
10635 this._s = scheduler;
10636 __super__.call(this);
10637 }
10638
10639 function scheduleMethod(s, state) {
10640 state._open = true;
10641 }
10642
10643 SkipUntilWithTimeObservable.prototype.subscribeCore = function (o) {
10644 this._open = false;
10645 return new BinaryDisposable(
10646 this._s.scheduleFuture(this, this._st, scheduleMethod),
10647 this.source.subscribe(new SkipUntilWithTimeObserver(o, this))
10648 );
10649 };
10650
10651 return SkipUntilWithTimeObservable;
10652 }(ObservableBase));
10653
10654 var SkipUntilWithTimeObserver = (function (__super__) {
10655 inherits(SkipUntilWithTimeObserver, __super__);
10656
10657 function SkipUntilWithTimeObserver(o, p) {
10658 this._o = o;
10659 this._p = p;
10660 __super__.call(this);
10661 }
10662
10663 SkipUntilWithTimeObserver.prototype.next = function (x) { this._p._open && this._o.onNext(x); };
10664 SkipUntilWithTimeObserver.prototype.error = function (e) { this._o.onError(e); };
10665 SkipUntilWithTimeObserver.prototype.completed = function () { this._o.onCompleted(); };
10666
10667 return SkipUntilWithTimeObserver;
10668 }(AbstractObserver));
10669
10670
10671 /**
10672 * Skips elements from the observable source sequence until the specified start time, using the specified scheduler to run timers.
10673 * Errors produced by the source sequence are always forwarded to the result sequence, even if the error occurs before the start time.
10674 *
10675 * @examples
10676 * 1 - res = source.skipUntilWithTime(new Date(), [scheduler]);
10677 * 2 - res = source.skipUntilWithTime(5000, [scheduler]);
10678 * @param {Date|Number} startTime Time to start taking elements from the source sequence. If this value is less than or equal to Date(), no elements will be skipped.
10679 * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
10680 * @returns {Observable} An observable sequence with the elements skipped until the specified start time.
10681 */
10682 observableProto.skipUntilWithTime = function (startTime, scheduler) {
10683 isScheduler(scheduler) || (scheduler = defaultScheduler);
10684 return new SkipUntilWithTimeObservable(this, startTime, scheduler);
10685 };
10686
10687 /**
10688 * Takes elements for the specified duration until the specified end time, using the specified scheduler to run timers.
10689 * @param {Number | Date} endTime Time to stop taking elements from the source sequence. If this value is less than or equal to new Date(), the result stream will complete immediately.
10690 * @param {Scheduler} [scheduler] Scheduler to run the timer on.
10691 * @returns {Observable} An observable sequence with the elements taken until the specified end time.
10692 */
10693 observableProto.takeUntilWithTime = function (endTime, scheduler) {
10694 isScheduler(scheduler) || (scheduler = defaultScheduler);
10695 var source = this;
10696 return new AnonymousObservable(function (o) {
10697 return new BinaryDisposable(
10698 scheduler.scheduleFuture(o, endTime, function (_, o) { o.onCompleted(); }),
10699 source.subscribe(o));
10700 }, source);
10701 };
10702
10703 /**
10704 * Returns an Observable that emits only the first item emitted by the source Observable during sequential time windows of a specified duration.
10705 * @param {Number} windowDuration time to wait before emitting another item after emitting the last item
10706 * @param {Scheduler} [scheduler] the Scheduler to use internally to manage the timers that handle timeout for each item. If not provided, defaults to Scheduler.timeout.
10707 * @returns {Observable} An Observable that performs the throttle operation.
10708 */
10709 observableProto.throttle = function (windowDuration, scheduler) {
10710 isScheduler(scheduler) || (scheduler = defaultScheduler);
10711 var duration = +windowDuration || 0;
10712 if (duration <= 0) { throw new RangeError('windowDuration cannot be less or equal zero.'); }
10713 var source = this;
10714 return new AnonymousObservable(function (o) {
10715 var lastOnNext = 0;
10716 return source.subscribe(
10717 function (x) {
10718 var now = scheduler.now();
10719 if (lastOnNext === 0 || now - lastOnNext >= duration) {
10720 lastOnNext = now;
10721 o.onNext(x);
10722 }
10723 },function (e) { o.onError(e); }, function () { o.onCompleted(); }
10724 );
10725 }, source);
10726 };
10727
10728 var TransduceObserver = (function (__super__) {
10729 inherits(TransduceObserver, __super__);
10730 function TransduceObserver(o, xform) {
10731 this._o = o;
10732 this._xform = xform;
10733 __super__.call(this);
10734 }
10735
10736 TransduceObserver.prototype.next = function (x) {
10737 var res = tryCatch(this._xform['@@transducer/step']).call(this._xform, this._o, x);
10738 if (res === errorObj) { this._o.onError(res.e); }
10739 };
10740
10741 TransduceObserver.prototype.error = function (e) { this._o.onError(e); };
10742
10743 TransduceObserver.prototype.completed = function () {
10744 this._xform['@@transducer/result'](this._o);
10745 };
10746
10747 return TransduceObserver;
10748 }(AbstractObserver));
10749
10750 function transformForObserver(o) {
10751 return {
10752 '@@transducer/init': function() {
10753 return o;
10754 },
10755 '@@transducer/step': function(obs, input) {
10756 return obs.onNext(input);
10757 },
10758 '@@transducer/result': function(obs) {
10759 return obs.onCompleted();
10760 }
10761 };
10762 }
10763
10764 /**
10765 * Executes a transducer to transform the observable sequence
10766 * @param {Transducer} transducer A transducer to execute
10767 * @returns {Observable} An Observable sequence containing the results from the transducer.
10768 */
10769 observableProto.transduce = function(transducer) {
10770 var source = this;
10771 return new AnonymousObservable(function(o) {
10772 var xform = transducer(transformForObserver(o));
10773 return source.subscribe(new TransduceObserver(o, xform));
10774 }, source);
10775 };
10776
10777 var SwitchFirstObservable = (function (__super__) {
10778 inherits(SwitchFirstObservable, __super__);
10779 function SwitchFirstObservable(source) {
10780 this.source = source;
10781 __super__.call(this);
10782 }
10783
10784 SwitchFirstObservable.prototype.subscribeCore = function (o) {
10785 var m = new SingleAssignmentDisposable(),
10786 g = new CompositeDisposable(),
10787 state = {
10788 hasCurrent: false,
10789 isStopped: false,
10790 o: o,
10791 g: g
10792 };
10793
10794 g.add(m);
10795 m.setDisposable(this.source.subscribe(new SwitchFirstObserver(state)));
10796 return g;
10797 };
10798
10799 return SwitchFirstObservable;
10800 }(ObservableBase));
10801
10802 var SwitchFirstObserver = (function(__super__) {
10803 inherits(SwitchFirstObserver, __super__);
10804 function SwitchFirstObserver(state) {
10805 this._s = state;
10806 __super__.call(this);
10807 }
10808
10809 SwitchFirstObserver.prototype.next = function (x) {
10810 if (!this._s.hasCurrent) {
10811 this._s.hasCurrent = true;
10812 isPromise(x) && (x = observableFromPromise(x));
10813 var inner = new SingleAssignmentDisposable();
10814 this._s.g.add(inner);
10815 inner.setDisposable(x.subscribe(new InnerObserver(this._s, inner)));
10816 }
10817 };
10818
10819 SwitchFirstObserver.prototype.error = function (e) {
10820 this._s.o.onError(e);
10821 };
10822
10823 SwitchFirstObserver.prototype.completed = function () {
10824 this._s.isStopped = true;
10825 !this._s.hasCurrent && this._s.g.length === 1 && this._s.o.onCompleted();
10826 };
10827
10828 inherits(InnerObserver, __super__);
10829 function InnerObserver(state, inner) {
10830 this._s = state;
10831 this._i = inner;
10832 __super__.call(this);
10833 }
10834
10835 InnerObserver.prototype.next = function (x) { this._s.o.onNext(x); };
10836 InnerObserver.prototype.error = function (e) { this._s.o.onError(e); };
10837 InnerObserver.prototype.completed = function () {
10838 this._s.g.remove(this._i);
10839 this._s.hasCurrent = false;
10840 this._s.isStopped && this._s.g.length === 1 && this._s.o.onCompleted();
10841 };
10842
10843 return SwitchFirstObserver;
10844 }(AbstractObserver));
10845
10846 /**
10847 * Performs a exclusive waiting for the first to finish before subscribing to another observable.
10848 * Observables that come in between subscriptions will be dropped on the floor.
10849 * @returns {Observable} A exclusive observable with only the results that happen when subscribed.
10850 */
10851 observableProto.switchFirst = function () {
10852 return new SwitchFirstObservable(this);
10853 };
10854
10855observableProto.flatMapFirst = observableProto.selectManyFirst = function(selector, resultSelector, thisArg) {
10856 return new FlatMapObservable(this, selector, resultSelector, thisArg).switchFirst();
10857};
10858
10859Rx.Observable.prototype.flatMapWithMaxConcurrent = function(limit, selector, resultSelector, thisArg) {
10860 return new FlatMapObservable(this, selector, resultSelector, thisArg).merge(limit);
10861};
10862 /** Provides a set of extension methods for virtual time scheduling. */
10863 var VirtualTimeScheduler = Rx.VirtualTimeScheduler = (function (__super__) {
10864 inherits(VirtualTimeScheduler, __super__);
10865
10866 /**
10867 * Creates a new virtual time scheduler with the specified initial clock value and absolute time comparer.
10868 *
10869 * @constructor
10870 * @param {Number} initialClock Initial value for the clock.
10871 * @param {Function} comparer Comparer to determine causality of events based on absolute time.
10872 */
10873 function VirtualTimeScheduler(initialClock, comparer) {
10874 this.clock = initialClock;
10875 this.comparer = comparer;
10876 this.isEnabled = false;
10877 this.queue = new PriorityQueue(1024);
10878 __super__.call(this);
10879 }
10880
10881 var VirtualTimeSchedulerPrototype = VirtualTimeScheduler.prototype;
10882
10883 VirtualTimeSchedulerPrototype.now = function () {
10884 return this.toAbsoluteTime(this.clock);
10885 };
10886
10887 VirtualTimeSchedulerPrototype.schedule = function (state, action) {
10888 return this.scheduleAbsolute(state, this.clock, action);
10889 };
10890
10891 VirtualTimeSchedulerPrototype.scheduleFuture = function (state, dueTime, action) {
10892 var dt = dueTime instanceof Date ?
10893 this.toRelativeTime(dueTime - this.now()) :
10894 this.toRelativeTime(dueTime);
10895
10896 return this.scheduleRelative(state, dt, action);
10897 };
10898
10899 /**
10900 * Adds a relative time value to an absolute time value.
10901 * @param {Number} absolute Absolute virtual time value.
10902 * @param {Number} relative Relative virtual time value to add.
10903 * @return {Number} Resulting absolute virtual time sum value.
10904 */
10905 VirtualTimeSchedulerPrototype.add = notImplemented;
10906
10907 /**
10908 * Converts an absolute time to a number
10909 * @param {Any} The absolute time.
10910 * @returns {Number} The absolute time in ms
10911 */
10912 VirtualTimeSchedulerPrototype.toAbsoluteTime = notImplemented;
10913
10914 /**
10915 * Converts the TimeSpan value to a relative virtual time value.
10916 * @param {Number} timeSpan TimeSpan value to convert.
10917 * @return {Number} Corresponding relative virtual time value.
10918 */
10919 VirtualTimeSchedulerPrototype.toRelativeTime = notImplemented;
10920
10921 /**
10922 * Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. The periodic task will be emulated using recursive scheduling.
10923 * @param {Mixed} state Initial state passed to the action upon the first iteration.
10924 * @param {Number} period Period for running the work periodically.
10925 * @param {Function} action Action to be executed, potentially updating the state.
10926 * @returns {Disposable} The disposable object used to cancel the scheduled recurring action (best effort).
10927 */
10928 VirtualTimeSchedulerPrototype.schedulePeriodic = function (state, period, action) {
10929 var s = new SchedulePeriodicRecursive(this, state, period, action);
10930 return s.start();
10931 };
10932
10933 /**
10934 * Schedules an action to be executed after dueTime.
10935 * @param {Mixed} state State passed to the action to be executed.
10936 * @param {Number} dueTime Relative time after which to execute the action.
10937 * @param {Function} action Action to be executed.
10938 * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
10939 */
10940 VirtualTimeSchedulerPrototype.scheduleRelative = function (state, dueTime, action) {
10941 var runAt = this.add(this.clock, dueTime);
10942 return this.scheduleAbsolute(state, runAt, action);
10943 };
10944
10945 /**
10946 * Starts the virtual time scheduler.
10947 */
10948 VirtualTimeSchedulerPrototype.start = function () {
10949 if (!this.isEnabled) {
10950 this.isEnabled = true;
10951 do {
10952 var next = this.getNext();
10953 if (next !== null) {
10954 this.comparer(next.dueTime, this.clock) > 0 && (this.clock = next.dueTime);
10955 next.invoke();
10956 } else {
10957 this.isEnabled = false;
10958 }
10959 } while (this.isEnabled);
10960 }
10961 };
10962
10963 /**
10964 * Stops the virtual time scheduler.
10965 */
10966 VirtualTimeSchedulerPrototype.stop = function () {
10967 this.isEnabled = false;
10968 };
10969
10970 /**
10971 * Advances the scheduler's clock to the specified time, running all work till that point.
10972 * @param {Number} time Absolute time to advance the scheduler's clock to.
10973 */
10974 VirtualTimeSchedulerPrototype.advanceTo = function (time) {
10975 var dueToClock = this.comparer(this.clock, time);
10976 if (this.comparer(this.clock, time) > 0) { throw new ArgumentOutOfRangeError(); }
10977 if (dueToClock === 0) { return; }
10978 if (!this.isEnabled) {
10979 this.isEnabled = true;
10980 do {
10981 var next = this.getNext();
10982 if (next !== null && this.comparer(next.dueTime, time) <= 0) {
10983 this.comparer(next.dueTime, this.clock) > 0 && (this.clock = next.dueTime);
10984 next.invoke();
10985 } else {
10986 this.isEnabled = false;
10987 }
10988 } while (this.isEnabled);
10989 this.clock = time;
10990 }
10991 };
10992
10993 /**
10994 * Advances the scheduler's clock by the specified relative time, running all work scheduled for that timespan.
10995 * @param {Number} time Relative time to advance the scheduler's clock by.
10996 */
10997 VirtualTimeSchedulerPrototype.advanceBy = function (time) {
10998 var dt = this.add(this.clock, time),
10999 dueToClock = this.comparer(this.clock, dt);
11000 if (dueToClock > 0) { throw new ArgumentOutOfRangeError(); }
11001 if (dueToClock === 0) { return; }
11002
11003 this.advanceTo(dt);
11004 };
11005
11006 /**
11007 * Advances the scheduler's clock by the specified relative time.
11008 * @param {Number} time Relative time to advance the scheduler's clock by.
11009 */
11010 VirtualTimeSchedulerPrototype.sleep = function (time) {
11011 var dt = this.add(this.clock, time);
11012 if (this.comparer(this.clock, dt) >= 0) { throw new ArgumentOutOfRangeError(); }
11013
11014 this.clock = dt;
11015 };
11016
11017 /**
11018 * Gets the next scheduled item to be executed.
11019 * @returns {ScheduledItem} The next scheduled item.
11020 */
11021 VirtualTimeSchedulerPrototype.getNext = function () {
11022 while (this.queue.length > 0) {
11023 var next = this.queue.peek();
11024 if (next.isCancelled()) {
11025 this.queue.dequeue();
11026 } else {
11027 return next;
11028 }
11029 }
11030 return null;
11031 };
11032
11033 /**
11034 * Schedules an action to be executed at dueTime.
11035 * @param {Mixed} state State passed to the action to be executed.
11036 * @param {Number} dueTime Absolute time at which to execute the action.
11037 * @param {Function} action Action to be executed.
11038 * @returns {Disposable} The disposable object used to cancel the scheduled action (best effort).
11039 */
11040 VirtualTimeSchedulerPrototype.scheduleAbsolute = function (state, dueTime, action) {
11041 var self = this;
11042
11043 function run(scheduler, state1) {
11044 self.queue.remove(si);
11045 return action(scheduler, state1);
11046 }
11047
11048 var si = new ScheduledItem(this, state, run, dueTime, this.comparer);
11049 this.queue.enqueue(si);
11050
11051 return si.disposable;
11052 };
11053
11054 return VirtualTimeScheduler;
11055 }(Scheduler));
11056
11057 /** Provides a virtual time scheduler that uses Date for absolute time and number for relative time. */
11058 Rx.HistoricalScheduler = (function (__super__) {
11059 inherits(HistoricalScheduler, __super__);
11060
11061 /**
11062 * Creates a new historical scheduler with the specified initial clock value.
11063 * @constructor
11064 * @param {Number} initialClock Initial value for the clock.
11065 * @param {Function} comparer Comparer to determine causality of events based on absolute time.
11066 */
11067 function HistoricalScheduler(initialClock, comparer) {
11068 var clock = initialClock == null ? 0 : initialClock;
11069 var cmp = comparer || defaultSubComparer;
11070 __super__.call(this, clock, cmp);
11071 }
11072
11073 var HistoricalSchedulerProto = HistoricalScheduler.prototype;
11074
11075 /**
11076 * Adds a relative time value to an absolute time value.
11077 * @param {Number} absolute Absolute virtual time value.
11078 * @param {Number} relative Relative virtual time value to add.
11079 * @return {Number} Resulting absolute virtual time sum value.
11080 */
11081 HistoricalSchedulerProto.add = function (absolute, relative) {
11082 return absolute + relative;
11083 };
11084
11085 HistoricalSchedulerProto.toAbsoluteTime = function (absolute) {
11086 return new Date(absolute).getTime();
11087 };
11088
11089 /**
11090 * Converts the TimeSpan value to a relative virtual time value.
11091 * @memberOf HistoricalScheduler
11092 * @param {Number} timeSpan TimeSpan value to convert.
11093 * @return {Number} Corresponding relative virtual time value.
11094 */
11095 HistoricalSchedulerProto.toRelativeTime = function (timeSpan) {
11096 return timeSpan;
11097 };
11098
11099 return HistoricalScheduler;
11100 }(Rx.VirtualTimeScheduler));
11101
11102function OnNextPredicate(predicate) {
11103 this.predicate = predicate;
11104}
11105
11106OnNextPredicate.prototype.equals = function (other) {
11107 if (other === this) { return true; }
11108 if (other == null) { return false; }
11109 if (other.kind !== 'N') { return false; }
11110 return this.predicate(other.value);
11111};
11112
11113function OnErrorPredicate(predicate) {
11114 this.predicate = predicate;
11115}
11116
11117OnErrorPredicate.prototype.equals = function (other) {
11118 if (other === this) { return true; }
11119 if (other == null) { return false; }
11120 if (other.kind !== 'E') { return false; }
11121 return this.predicate(other.error);
11122};
11123
11124var ReactiveTest = Rx.ReactiveTest = {
11125 /** Default virtual time used for creation of observable sequences in unit tests. */
11126 created: 100,
11127 /** Default virtual time used to subscribe to observable sequences in unit tests. */
11128 subscribed: 200,
11129 /** Default virtual time used to dispose subscriptions in unit tests. */
11130 disposed: 1000,
11131
11132 /**
11133 * Factory method for an OnNext notification record at a given time with a given value or a predicate function.
11134 *
11135 * 1 - ReactiveTest.onNext(200, 42);
11136 * 2 - ReactiveTest.onNext(200, function (x) { return x.length == 2; });
11137 *
11138 * @param ticks Recorded virtual time the OnNext notification occurs.
11139 * @param value Recorded value stored in the OnNext notification or a predicate.
11140 * @return Recorded OnNext notification.
11141 */
11142 onNext: function (ticks, value) {
11143 return typeof value === 'function' ?
11144 new Recorded(ticks, new OnNextPredicate(value)) :
11145 new Recorded(ticks, Notification.createOnNext(value));
11146 },
11147 /**
11148 * Factory method for an OnError notification record at a given time with a given error.
11149 *
11150 * 1 - ReactiveTest.onNext(200, new Error('error'));
11151 * 2 - ReactiveTest.onNext(200, function (e) { return e.message === 'error'; });
11152 *
11153 * @param ticks Recorded virtual time the OnError notification occurs.
11154 * @param exception Recorded exception stored in the OnError notification.
11155 * @return Recorded OnError notification.
11156 */
11157 onError: function (ticks, error) {
11158 return typeof error === 'function' ?
11159 new Recorded(ticks, new OnErrorPredicate(error)) :
11160 new Recorded(ticks, Notification.createOnError(error));
11161 },
11162 /**
11163 * Factory method for an OnCompleted notification record at a given time.
11164 *
11165 * @param ticks Recorded virtual time the OnCompleted notification occurs.
11166 * @return Recorded OnCompleted notification.
11167 */
11168 onCompleted: function (ticks) {
11169 return new Recorded(ticks, Notification.createOnCompleted());
11170 },
11171 /**
11172 * Factory method for a subscription record based on a given subscription and disposal time.
11173 *
11174 * @param start Virtual time indicating when the subscription was created.
11175 * @param end Virtual time indicating when the subscription was disposed.
11176 * @return Subscription object.
11177 */
11178 subscribe: function (start, end) {
11179 return new Subscription(start, end);
11180 }
11181};
11182
11183 /**
11184 * Creates a new object recording the production of the specified value at the given virtual time.
11185 *
11186 * @constructor
11187 * @param {Number} time Virtual time the value was produced on.
11188 * @param {Mixed} value Value that was produced.
11189 * @param {Function} comparer An optional comparer.
11190 */
11191 var Recorded = Rx.Recorded = function (time, value, comparer) {
11192 this.time = time;
11193 this.value = value;
11194 this.comparer = comparer || defaultComparer;
11195 };
11196
11197 /**
11198 * Checks whether the given recorded object is equal to the current instance.
11199 *
11200 * @param {Recorded} other Recorded object to check for equality.
11201 * @returns {Boolean} true if both objects are equal; false otherwise.
11202 */
11203 Recorded.prototype.equals = function (other) {
11204 return this.time === other.time && this.comparer(this.value, other.value);
11205 };
11206
11207 /**
11208 * Returns a string representation of the current Recorded value.
11209 *
11210 * @returns {String} String representation of the current Recorded value.
11211 */
11212 Recorded.prototype.toString = function () {
11213 return this.value.toString() + '@' + this.time;
11214 };
11215
11216 /**
11217 * Creates a new subscription object with the given virtual subscription and unsubscription time.
11218 *
11219 * @constructor
11220 * @param {Number} subscribe Virtual time at which the subscription occurred.
11221 * @param {Number} unsubscribe Virtual time at which the unsubscription occurred.
11222 */
11223 var Subscription = Rx.Subscription = function (start, end) {
11224 this.subscribe = start;
11225 this.unsubscribe = end || Number.MAX_VALUE;
11226 };
11227
11228 /**
11229 * Checks whether the given subscription is equal to the current instance.
11230 * @param other Subscription object to check for equality.
11231 * @returns {Boolean} true if both objects are equal; false otherwise.
11232 */
11233 Subscription.prototype.equals = function (other) {
11234 return this.subscribe === other.subscribe && this.unsubscribe === other.unsubscribe;
11235 };
11236
11237 /**
11238 * Returns a string representation of the current Subscription value.
11239 * @returns {String} String representation of the current Subscription value.
11240 */
11241 Subscription.prototype.toString = function () {
11242 return '(' + this.subscribe + ', ' + (this.unsubscribe === Number.MAX_VALUE ? 'Infinite' : this.unsubscribe) + ')';
11243 };
11244
11245 var MockDisposable = Rx.MockDisposable = function (scheduler) {
11246 this.scheduler = scheduler;
11247 this.disposes = [];
11248 this.disposes.push(this.scheduler.clock);
11249 };
11250
11251 MockDisposable.prototype.dispose = function () {
11252 this.disposes.push(this.scheduler.clock);
11253 };
11254
11255 var MockObserver = (function (__super__) {
11256 inherits(MockObserver, __super__);
11257
11258 function MockObserver(scheduler) {
11259 __super__.call(this);
11260 this.scheduler = scheduler;
11261 this.messages = [];
11262 }
11263
11264 var MockObserverPrototype = MockObserver.prototype;
11265
11266 MockObserverPrototype.onNext = function (value) {
11267 this.messages.push(new Recorded(this.scheduler.clock, Notification.createOnNext(value)));
11268 };
11269
11270 MockObserverPrototype.onError = function (e) {
11271 this.messages.push(new Recorded(this.scheduler.clock, Notification.createOnError(e)));
11272 };
11273
11274 MockObserverPrototype.onCompleted = function () {
11275 this.messages.push(new Recorded(this.scheduler.clock, Notification.createOnCompleted()));
11276 };
11277
11278 return MockObserver;
11279 })(Observer);
11280
11281 function MockPromise(scheduler, messages) {
11282 var self = this;
11283 this.scheduler = scheduler;
11284 this.messages = messages;
11285 this.subscriptions = [];
11286 this.observers = [];
11287 for (var i = 0, len = this.messages.length; i < len; i++) {
11288 var message = this.messages[i],
11289 notification = message.value;
11290 (function (innerNotification) {
11291 scheduler.scheduleAbsolute(null, message.time, function () {
11292 var obs = self.observers.slice(0);
11293
11294 for (var j = 0, jLen = obs.length; j < jLen; j++) {
11295 innerNotification.accept(obs[j]);
11296 }
11297 return disposableEmpty;
11298 });
11299 })(notification);
11300 }
11301 }
11302
11303 MockPromise.prototype.then = function (onResolved, onRejected) {
11304 var self = this;
11305
11306 this.subscriptions.push(new Subscription(this.scheduler.clock));
11307 var index = this.subscriptions.length - 1;
11308
11309 var newPromise;
11310
11311 var observer = Rx.Observer.create(
11312 function (x) {
11313 var retValue = onResolved(x);
11314 if (retValue && typeof retValue.then === 'function') {
11315 newPromise = retValue;
11316 } else {
11317 var ticks = self.scheduler.clock;
11318 newPromise = new MockPromise(self.scheduler, [Rx.ReactiveTest.onNext(ticks, undefined), Rx.ReactiveTest.onCompleted(ticks)]);
11319 }
11320 var idx = self.observers.indexOf(observer);
11321 self.observers.splice(idx, 1);
11322 self.subscriptions[index] = new Subscription(self.subscriptions[index].subscribe, self.scheduler.clock);
11323 },
11324 function (err) {
11325 onRejected(err);
11326 var idx = self.observers.indexOf(observer);
11327 self.observers.splice(idx, 1);
11328 self.subscriptions[index] = new Subscription(self.subscriptions[index].subscribe, self.scheduler.clock);
11329 }
11330 );
11331 this.observers.push(observer);
11332
11333 return newPromise || new MockPromise(this.scheduler, this.messages);
11334 };
11335
11336 var HotObservable = (function (__super__) {
11337 inherits(HotObservable, __super__);
11338
11339 function HotObservable(scheduler, messages) {
11340 __super__.call(this);
11341 var message, notification, observable = this;
11342 this.scheduler = scheduler;
11343 this.messages = messages;
11344 this.subscriptions = [];
11345 this.observers = [];
11346 for (var i = 0, len = this.messages.length; i < len; i++) {
11347 message = this.messages[i];
11348 notification = message.value;
11349 (function (innerNotification) {
11350 scheduler.scheduleAbsolute(null, message.time, function () {
11351 var obs = observable.observers.slice(0);
11352
11353 for (var j = 0, jLen = obs.length; j < jLen; j++) {
11354 innerNotification.accept(obs[j]);
11355 }
11356 return disposableEmpty;
11357 });
11358 })(notification);
11359 }
11360 }
11361
11362 HotObservable.prototype._subscribe = function (o) {
11363 var observable = this;
11364 this.observers.push(o);
11365 this.subscriptions.push(new Subscription(this.scheduler.clock));
11366 var index = this.subscriptions.length - 1;
11367 return disposableCreate(function () {
11368 var idx = observable.observers.indexOf(o);
11369 observable.observers.splice(idx, 1);
11370 observable.subscriptions[index] = new Subscription(observable.subscriptions[index].subscribe, observable.scheduler.clock);
11371 });
11372 };
11373
11374 return HotObservable;
11375 })(Observable);
11376
11377 var ColdObservable = (function (__super__) {
11378 inherits(ColdObservable, __super__);
11379
11380 function ColdObservable(scheduler, messages) {
11381 __super__.call(this);
11382 this.scheduler = scheduler;
11383 this.messages = messages;
11384 this.subscriptions = [];
11385 }
11386
11387 ColdObservable.prototype._subscribe = function (o) {
11388 var message, notification, observable = this;
11389 this.subscriptions.push(new Subscription(this.scheduler.clock));
11390 var index = this.subscriptions.length - 1;
11391 var d = new CompositeDisposable();
11392 for (var i = 0, len = this.messages.length; i < len; i++) {
11393 message = this.messages[i];
11394 notification = message.value;
11395 (function (innerNotification) {
11396 d.add(observable.scheduler.scheduleRelative(null, message.time, function () {
11397 innerNotification.accept(o);
11398 return disposableEmpty;
11399 }));
11400 })(notification);
11401 }
11402 return disposableCreate(function () {
11403 observable.subscriptions[index] = new Subscription(observable.subscriptions[index].subscribe, observable.scheduler.clock);
11404 d.dispose();
11405 });
11406 };
11407
11408 return ColdObservable;
11409 })(Observable);
11410
11411 /** Virtual time scheduler used for testing applications and libraries built using Reactive Extensions. */
11412 Rx.TestScheduler = (function (__super__) {
11413 inherits(TestScheduler, __super__);
11414
11415 function baseComparer(x, y) {
11416 return x > y ? 1 : (x < y ? -1 : 0);
11417 }
11418
11419 function TestScheduler() {
11420 __super__.call(this, 0, baseComparer);
11421 }
11422
11423 /**
11424 * Schedules an action to be executed at the specified virtual time.
11425 *
11426 * @param state State passed to the action to be executed.
11427 * @param dueTime Absolute virtual time at which to execute the action.
11428 * @param action Action to be executed.
11429 * @return Disposable object used to cancel the scheduled action (best effort).
11430 */
11431 TestScheduler.prototype.scheduleAbsolute = function (state, dueTime, action) {
11432 dueTime <= this.clock && (dueTime = this.clock + 1);
11433 return __super__.prototype.scheduleAbsolute.call(this, state, dueTime, action);
11434 };
11435 /**
11436 * Adds a relative virtual time to an absolute virtual time value.
11437 *
11438 * @param absolute Absolute virtual time value.
11439 * @param relative Relative virtual time value to add.
11440 * @return Resulting absolute virtual time sum value.
11441 */
11442 TestScheduler.prototype.add = function (absolute, relative) {
11443 return absolute + relative;
11444 };
11445 /**
11446 * Converts the absolute virtual time value to a DateTimeOffset value.
11447 *
11448 * @param absolute Absolute virtual time value to convert.
11449 * @return Corresponding DateTimeOffset value.
11450 */
11451 TestScheduler.prototype.toAbsoluteTime = function (absolute) {
11452 return new Date(absolute).getTime();
11453 };
11454 /**
11455 * Converts the TimeSpan value to a relative virtual time value.
11456 *
11457 * @param timeSpan TimeSpan value to convert.
11458 * @return Corresponding relative virtual time value.
11459 */
11460 TestScheduler.prototype.toRelativeTime = function (timeSpan) {
11461 return timeSpan;
11462 };
11463 /**
11464 * Starts the test scheduler and uses the specified virtual times to invoke the factory function, subscribe to the resulting sequence, and dispose the subscription.
11465 *
11466 * @param create Factory method to create an observable sequence.
11467 * @param created Virtual time at which to invoke the factory to create an observable sequence.
11468 * @param subscribed Virtual time at which to subscribe to the created observable sequence.
11469 * @param disposed Virtual time at which to dispose the subscription.
11470 * @return Observer with timestamped recordings of notification messages that were received during the virtual time window when the subscription to the source sequence was active.
11471 */
11472 TestScheduler.prototype.startScheduler = function (createFn, settings) {
11473 settings || (settings = {});
11474 settings.created == null && (settings.created = ReactiveTest.created);
11475 settings.subscribed == null && (settings.subscribed = ReactiveTest.subscribed);
11476 settings.disposed == null && (settings.disposed = ReactiveTest.disposed);
11477
11478 var observer = this.createObserver(), source, subscription;
11479
11480 this.scheduleAbsolute(null, settings.created, function () {
11481 source = createFn();
11482 return disposableEmpty;
11483 });
11484
11485 this.scheduleAbsolute(null, settings.subscribed, function () {
11486 subscription = source.subscribe(observer);
11487 return disposableEmpty;
11488 });
11489
11490 this.scheduleAbsolute(null, settings.disposed, function () {
11491 subscription.dispose();
11492 return disposableEmpty;
11493 });
11494
11495 this.start();
11496
11497 return observer;
11498 };
11499
11500 /**
11501 * Creates a hot observable using the specified timestamped notification messages either as an array or arguments.
11502 * @param messages Notifications to surface through the created sequence at their specified absolute virtual times.
11503 * @return Hot observable sequence that can be used to assert the timing of subscriptions and notifications.
11504 */
11505 TestScheduler.prototype.createHotObservable = function () {
11506 var len = arguments.length, args;
11507 if (Array.isArray(arguments[0])) {
11508 args = arguments[0];
11509 } else {
11510 args = new Array(len);
11511 for (var i = 0; i < len; i++) { args[i] = arguments[i]; }
11512 }
11513 return new HotObservable(this, args);
11514 };
11515
11516 /**
11517 * Creates a cold observable using the specified timestamped notification messages either as an array or arguments.
11518 * @param messages Notifications to surface through the created sequence at their specified virtual time offsets from the sequence subscription time.
11519 * @return Cold observable sequence that can be used to assert the timing of subscriptions and notifications.
11520 */
11521 TestScheduler.prototype.createColdObservable = function () {
11522 var len = arguments.length, args;
11523 if (Array.isArray(arguments[0])) {
11524 args = arguments[0];
11525 } else {
11526 args = new Array(len);
11527 for (var i = 0; i < len; i++) { args[i] = arguments[i]; }
11528 }
11529 return new ColdObservable(this, args);
11530 };
11531
11532 /**
11533 * Creates a resolved promise with the given value and ticks
11534 * @param {Number} ticks The absolute time of the resolution.
11535 * @param {Any} value The value to yield at the given tick.
11536 * @returns {MockPromise} A mock Promise which fulfills with the given value.
11537 */
11538 TestScheduler.prototype.createResolvedPromise = function (ticks, value) {
11539 return new MockPromise(this, [Rx.ReactiveTest.onNext(ticks, value), Rx.ReactiveTest.onCompleted(ticks)]);
11540 };
11541
11542 /**
11543 * Creates a rejected promise with the given reason and ticks
11544 * @param {Number} ticks The absolute time of the resolution.
11545 * @param {Any} reason The reason for rejection to yield at the given tick.
11546 * @returns {MockPromise} A mock Promise which rejects with the given reason.
11547 */
11548 TestScheduler.prototype.createRejectedPromise = function (ticks, reason) {
11549 return new MockPromise(this, [Rx.ReactiveTest.onError(ticks, reason)]);
11550 };
11551
11552 /**
11553 * Creates an observer that records received notification messages and timestamps those.
11554 * @return Observer that can be used to assert the timing of received notifications.
11555 */
11556 TestScheduler.prototype.createObserver = function () {
11557 return new MockObserver(this);
11558 };
11559
11560 return TestScheduler;
11561 })(VirtualTimeScheduler);
11562
11563 var AnonymousObservable = Rx.AnonymousObservable = (function (__super__) {
11564 inherits(AnonymousObservable, __super__);
11565
11566 // Fix subscriber to check for undefined or function returned to decorate as Disposable
11567 function fixSubscriber(subscriber) {
11568 return subscriber && isFunction(subscriber.dispose) ? subscriber :
11569 isFunction(subscriber) ? disposableCreate(subscriber) : disposableEmpty;
11570 }
11571
11572 function setDisposable(s, state) {
11573 var ado = state[0], self = state[1];
11574 var sub = tryCatch(self.__subscribe).call(self, ado);
11575 if (sub === errorObj && !ado.fail(errorObj.e)) { thrower(errorObj.e); }
11576 ado.setDisposable(fixSubscriber(sub));
11577 }
11578
11579 function AnonymousObservable(subscribe, parent) {
11580 this.source = parent;
11581 this.__subscribe = subscribe;
11582 __super__.call(this);
11583 }
11584
11585 AnonymousObservable.prototype._subscribe = function (o) {
11586 var ado = new AutoDetachObserver(o), state = [ado, this];
11587
11588 if (currentThreadScheduler.scheduleRequired()) {
11589 currentThreadScheduler.schedule(state, setDisposable);
11590 } else {
11591 setDisposable(null, state);
11592 }
11593 return ado;
11594 };
11595
11596 return AnonymousObservable;
11597
11598 }(Observable));
11599
11600 var AutoDetachObserver = (function (__super__) {
11601 inherits(AutoDetachObserver, __super__);
11602
11603 function AutoDetachObserver(observer) {
11604 __super__.call(this);
11605 this.observer = observer;
11606 this.m = new SingleAssignmentDisposable();
11607 }
11608
11609 var AutoDetachObserverPrototype = AutoDetachObserver.prototype;
11610
11611 AutoDetachObserverPrototype.next = function (value) {
11612 var result = tryCatch(this.observer.onNext).call(this.observer, value);
11613 if (result === errorObj) {
11614 this.dispose();
11615 thrower(result.e);
11616 }
11617 };
11618
11619 AutoDetachObserverPrototype.error = function (err) {
11620 var result = tryCatch(this.observer.onError).call(this.observer, err);
11621 this.dispose();
11622 result === errorObj && thrower(result.e);
11623 };
11624
11625 AutoDetachObserverPrototype.completed = function () {
11626 var result = tryCatch(this.observer.onCompleted).call(this.observer);
11627 this.dispose();
11628 result === errorObj && thrower(result.e);
11629 };
11630
11631 AutoDetachObserverPrototype.setDisposable = function (value) { this.m.setDisposable(value); };
11632 AutoDetachObserverPrototype.getDisposable = function () { return this.m.getDisposable(); };
11633
11634 AutoDetachObserverPrototype.dispose = function () {
11635 __super__.prototype.dispose.call(this);
11636 this.m.dispose();
11637 };
11638
11639 return AutoDetachObserver;
11640 }(AbstractObserver));
11641
11642 var UnderlyingObservable = (function (__super__) {
11643 inherits(UnderlyingObservable, __super__);
11644 function UnderlyingObservable(m, u) {
11645 this._m = m;
11646 this._u = u;
11647 __super__.call(this);
11648 }
11649
11650 UnderlyingObservable.prototype.subscribeCore = function (o) {
11651 return new BinaryDisposable(this._m.getDisposable(), this._u.subscribe(o));
11652 };
11653
11654 return UnderlyingObservable;
11655 }(ObservableBase));
11656
11657 var GroupedObservable = (function (__super__) {
11658 inherits(GroupedObservable, __super__);
11659 function GroupedObservable(key, underlyingObservable, mergedDisposable) {
11660 __super__.call(this);
11661 this.key = key;
11662 this.underlyingObservable = !mergedDisposable ?
11663 underlyingObservable :
11664 new UnderlyingObservable(mergedDisposable, underlyingObservable);
11665 }
11666
11667 GroupedObservable.prototype._subscribe = function (o) {
11668 return this.underlyingObservable.subscribe(o);
11669 };
11670
11671 return GroupedObservable;
11672 }(Observable));
11673
11674 /**
11675 * Represents an object that is both an observable sequence as well as an observer.
11676 * Each notification is broadcasted to all subscribed observers.
11677 */
11678 var Subject = Rx.Subject = (function (__super__) {
11679 inherits(Subject, __super__);
11680 function Subject() {
11681 __super__.call(this);
11682 this.isDisposed = false;
11683 this.isStopped = false;
11684 this.observers = [];
11685 this.hasError = false;
11686 }
11687
11688 addProperties(Subject.prototype, Observer.prototype, {
11689 _subscribe: function (o) {
11690 checkDisposed(this);
11691 if (!this.isStopped) {
11692 this.observers.push(o);
11693 return new InnerSubscription(this, o);
11694 }
11695 if (this.hasError) {
11696 o.onError(this.error);
11697 return disposableEmpty;
11698 }
11699 o.onCompleted();
11700 return disposableEmpty;
11701 },
11702 /**
11703 * Indicates whether the subject has observers subscribed to it.
11704 * @returns {Boolean} Indicates whether the subject has observers subscribed to it.
11705 */
11706 hasObservers: function () { return this.observers.length > 0; },
11707 /**
11708 * Notifies all subscribed observers about the end of the sequence.
11709 */
11710 onCompleted: function () {
11711 checkDisposed(this);
11712 if (!this.isStopped) {
11713 this.isStopped = true;
11714 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
11715 os[i].onCompleted();
11716 }
11717
11718 this.observers.length = 0;
11719 }
11720 },
11721 /**
11722 * Notifies all subscribed observers about the exception.
11723 * @param {Mixed} error The exception to send to all observers.
11724 */
11725 onError: function (error) {
11726 checkDisposed(this);
11727 if (!this.isStopped) {
11728 this.isStopped = true;
11729 this.error = error;
11730 this.hasError = true;
11731 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
11732 os[i].onError(error);
11733 }
11734
11735 this.observers.length = 0;
11736 }
11737 },
11738 /**
11739 * Notifies all subscribed observers about the arrival of the specified element in the sequence.
11740 * @param {Mixed} value The value to send to all observers.
11741 */
11742 onNext: function (value) {
11743 checkDisposed(this);
11744 if (!this.isStopped) {
11745 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
11746 os[i].onNext(value);
11747 }
11748 }
11749 },
11750 /**
11751 * Unsubscribe all observers and release resources.
11752 */
11753 dispose: function () {
11754 this.isDisposed = true;
11755 this.observers = null;
11756 }
11757 });
11758
11759 /**
11760 * Creates a subject from the specified observer and observable.
11761 * @param {Observer} observer The observer used to send messages to the subject.
11762 * @param {Observable} observable The observable used to subscribe to messages sent from the subject.
11763 * @returns {Subject} Subject implemented using the given observer and observable.
11764 */
11765 Subject.create = function (observer, observable) {
11766 return new AnonymousSubject(observer, observable);
11767 };
11768
11769 return Subject;
11770 }(Observable));
11771
11772 /**
11773 * Represents the result of an asynchronous operation.
11774 * The last value before the OnCompleted notification, or the error received through OnError, is sent to all subscribed observers.
11775 */
11776 var AsyncSubject = Rx.AsyncSubject = (function (__super__) {
11777 inherits(AsyncSubject, __super__);
11778
11779 /**
11780 * Creates a subject that can only receive one value and that value is cached for all future observations.
11781 * @constructor
11782 */
11783 function AsyncSubject() {
11784 __super__.call(this);
11785 this.isDisposed = false;
11786 this.isStopped = false;
11787 this.hasValue = false;
11788 this.observers = [];
11789 this.hasError = false;
11790 }
11791
11792 addProperties(AsyncSubject.prototype, Observer.prototype, {
11793 _subscribe: function (o) {
11794 checkDisposed(this);
11795
11796 if (!this.isStopped) {
11797 this.observers.push(o);
11798 return new InnerSubscription(this, o);
11799 }
11800
11801 if (this.hasError) {
11802 o.onError(this.error);
11803 } else if (this.hasValue) {
11804 o.onNext(this.value);
11805 o.onCompleted();
11806 } else {
11807 o.onCompleted();
11808 }
11809
11810 return disposableEmpty;
11811 },
11812 /**
11813 * Indicates whether the subject has observers subscribed to it.
11814 * @returns {Boolean} Indicates whether the subject has observers subscribed to it.
11815 */
11816 hasObservers: function () {
11817 checkDisposed(this);
11818 return this.observers.length > 0;
11819 },
11820 /**
11821 * Notifies all subscribed observers about the end of the sequence, also causing the last received value to be sent out (if any).
11822 */
11823 onCompleted: function () {
11824 var i, len;
11825 checkDisposed(this);
11826 if (!this.isStopped) {
11827 this.isStopped = true;
11828 var os = cloneArray(this.observers), len = os.length;
11829
11830 if (this.hasValue) {
11831 for (i = 0; i < len; i++) {
11832 var o = os[i];
11833 o.onNext(this.value);
11834 o.onCompleted();
11835 }
11836 } else {
11837 for (i = 0; i < len; i++) {
11838 os[i].onCompleted();
11839 }
11840 }
11841
11842 this.observers.length = 0;
11843 }
11844 },
11845 /**
11846 * Notifies all subscribed observers about the error.
11847 * @param {Mixed} error The Error to send to all observers.
11848 */
11849 onError: function (error) {
11850 checkDisposed(this);
11851 if (!this.isStopped) {
11852 this.isStopped = true;
11853 this.hasError = true;
11854 this.error = error;
11855
11856 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
11857 os[i].onError(error);
11858 }
11859
11860 this.observers.length = 0;
11861 }
11862 },
11863 /**
11864 * Sends a value to the subject. The last value received before successful termination will be sent to all subscribed and future observers.
11865 * @param {Mixed} value The value to store in the subject.
11866 */
11867 onNext: function (value) {
11868 checkDisposed(this);
11869 if (this.isStopped) { return; }
11870 this.value = value;
11871 this.hasValue = true;
11872 },
11873 /**
11874 * Unsubscribe all observers and release resources.
11875 */
11876 dispose: function () {
11877 this.isDisposed = true;
11878 this.observers = null;
11879 this.error = null;
11880 this.value = null;
11881 }
11882 });
11883
11884 return AsyncSubject;
11885 }(Observable));
11886
11887 /**
11888 * Represents a value that changes over time.
11889 * Observers can subscribe to the subject to receive the last (or initial) value and all subsequent notifications.
11890 */
11891 var BehaviorSubject = Rx.BehaviorSubject = (function (__super__) {
11892 inherits(BehaviorSubject, __super__);
11893 function BehaviorSubject(value) {
11894 __super__.call(this);
11895 this.value = value;
11896 this.observers = [];
11897 this.isDisposed = false;
11898 this.isStopped = false;
11899 this.hasError = false;
11900 }
11901
11902 addProperties(BehaviorSubject.prototype, Observer.prototype, {
11903 _subscribe: function (o) {
11904 checkDisposed(this);
11905 if (!this.isStopped) {
11906 this.observers.push(o);
11907 o.onNext(this.value);
11908 return new InnerSubscription(this, o);
11909 }
11910 if (this.hasError) {
11911 o.onError(this.error);
11912 } else {
11913 o.onCompleted();
11914 }
11915 return disposableEmpty;
11916 },
11917 /**
11918 * Gets the current value or throws an exception.
11919 * Value is frozen after onCompleted is called.
11920 * After onError is called always throws the specified exception.
11921 * An exception is always thrown after dispose is called.
11922 * @returns {Mixed} The initial value passed to the constructor until onNext is called; after which, the last value passed to onNext.
11923 */
11924 getValue: function () {
11925 checkDisposed(this);
11926 if (this.hasError) { thrower(this.error); }
11927 return this.value;
11928 },
11929 /**
11930 * Indicates whether the subject has observers subscribed to it.
11931 * @returns {Boolean} Indicates whether the subject has observers subscribed to it.
11932 */
11933 hasObservers: function () { return this.observers.length > 0; },
11934 /**
11935 * Notifies all subscribed observers about the end of the sequence.
11936 */
11937 onCompleted: function () {
11938 checkDisposed(this);
11939 if (this.isStopped) { return; }
11940 this.isStopped = true;
11941 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
11942 os[i].onCompleted();
11943 }
11944
11945 this.observers.length = 0;
11946 },
11947 /**
11948 * Notifies all subscribed observers about the exception.
11949 * @param {Mixed} error The exception to send to all observers.
11950 */
11951 onError: function (error) {
11952 checkDisposed(this);
11953 if (this.isStopped) { return; }
11954 this.isStopped = true;
11955 this.hasError = true;
11956 this.error = error;
11957
11958 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
11959 os[i].onError(error);
11960 }
11961
11962 this.observers.length = 0;
11963 },
11964 /**
11965 * Notifies all subscribed observers about the arrival of the specified element in the sequence.
11966 * @param {Mixed} value The value to send to all observers.
11967 */
11968 onNext: function (value) {
11969 checkDisposed(this);
11970 if (this.isStopped) { return; }
11971 this.value = value;
11972 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
11973 os[i].onNext(value);
11974 }
11975 },
11976 /**
11977 * Unsubscribe all observers and release resources.
11978 */
11979 dispose: function () {
11980 this.isDisposed = true;
11981 this.observers = null;
11982 this.value = null;
11983 this.error = null;
11984 }
11985 });
11986
11987 return BehaviorSubject;
11988 }(Observable));
11989
11990 /**
11991 * Represents an object that is both an observable sequence as well as an observer.
11992 * Each notification is broadcasted to all subscribed and future observers, subject to buffer trimming policies.
11993 */
11994 var ReplaySubject = Rx.ReplaySubject = (function (__super__) {
11995
11996 var maxSafeInteger = Math.pow(2, 53) - 1;
11997
11998 function createRemovableDisposable(subject, observer) {
11999 return disposableCreate(function () {
12000 observer.dispose();
12001 !subject.isDisposed && subject.observers.splice(subject.observers.indexOf(observer), 1);
12002 });
12003 }
12004
12005 inherits(ReplaySubject, __super__);
12006
12007 /**
12008 * Initializes a new instance of the ReplaySubject class with the specified buffer size, window size and scheduler.
12009 * @param {Number} [bufferSize] Maximum element count of the replay buffer.
12010 * @param {Number} [windowSize] Maximum time length of the replay buffer.
12011 * @param {Scheduler} [scheduler] Scheduler the observers are invoked on.
12012 */
12013 function ReplaySubject(bufferSize, windowSize, scheduler) {
12014 this.bufferSize = bufferSize == null ? maxSafeInteger : bufferSize;
12015 this.windowSize = windowSize == null ? maxSafeInteger : windowSize;
12016 this.scheduler = scheduler || currentThreadScheduler;
12017 this.q = [];
12018 this.observers = [];
12019 this.isStopped = false;
12020 this.isDisposed = false;
12021 this.hasError = false;
12022 this.error = null;
12023 __super__.call(this);
12024 }
12025
12026 addProperties(ReplaySubject.prototype, Observer.prototype, {
12027 _subscribe: function (o) {
12028 checkDisposed(this);
12029 var so = new ScheduledObserver(this.scheduler, o), subscription = createRemovableDisposable(this, so);
12030
12031 this._trim(this.scheduler.now());
12032 this.observers.push(so);
12033
12034 for (var i = 0, len = this.q.length; i < len; i++) {
12035 so.onNext(this.q[i].value);
12036 }
12037
12038 if (this.hasError) {
12039 so.onError(this.error);
12040 } else if (this.isStopped) {
12041 so.onCompleted();
12042 }
12043
12044 so.ensureActive();
12045 return subscription;
12046 },
12047 /**
12048 * Indicates whether the subject has observers subscribed to it.
12049 * @returns {Boolean} Indicates whether the subject has observers subscribed to it.
12050 */
12051 hasObservers: function () {
12052 return this.observers.length > 0;
12053 },
12054 _trim: function (now) {
12055 while (this.q.length > this.bufferSize) {
12056 this.q.shift();
12057 }
12058 while (this.q.length > 0 && (now - this.q[0].interval) > this.windowSize) {
12059 this.q.shift();
12060 }
12061 },
12062 /**
12063 * Notifies all subscribed observers about the arrival of the specified element in the sequence.
12064 * @param {Mixed} value The value to send to all observers.
12065 */
12066 onNext: function (value) {
12067 checkDisposed(this);
12068 if (this.isStopped) { return; }
12069 var now = this.scheduler.now();
12070 this.q.push({ interval: now, value: value });
12071 this._trim(now);
12072
12073 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
12074 var observer = os[i];
12075 observer.onNext(value);
12076 observer.ensureActive();
12077 }
12078 },
12079 /**
12080 * Notifies all subscribed observers about the exception.
12081 * @param {Mixed} error The exception to send to all observers.
12082 */
12083 onError: function (error) {
12084 checkDisposed(this);
12085 if (this.isStopped) { return; }
12086 this.isStopped = true;
12087 this.error = error;
12088 this.hasError = true;
12089 var now = this.scheduler.now();
12090 this._trim(now);
12091 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
12092 var observer = os[i];
12093 observer.onError(error);
12094 observer.ensureActive();
12095 }
12096 this.observers.length = 0;
12097 },
12098 /**
12099 * Notifies all subscribed observers about the end of the sequence.
12100 */
12101 onCompleted: function () {
12102 checkDisposed(this);
12103 if (this.isStopped) { return; }
12104 this.isStopped = true;
12105 var now = this.scheduler.now();
12106 this._trim(now);
12107 for (var i = 0, os = cloneArray(this.observers), len = os.length; i < len; i++) {
12108 var observer = os[i];
12109 observer.onCompleted();
12110 observer.ensureActive();
12111 }
12112 this.observers.length = 0;
12113 },
12114 /**
12115 * Unsubscribe all observers and release resources.
12116 */
12117 dispose: function () {
12118 this.isDisposed = true;
12119 this.observers = null;
12120 }
12121 });
12122
12123 return ReplaySubject;
12124 }(Observable));
12125
12126 var AnonymousSubject = Rx.AnonymousSubject = (function (__super__) {
12127 inherits(AnonymousSubject, __super__);
12128 function AnonymousSubject(observer, observable) {
12129 this.observer = observer;
12130 this.observable = observable;
12131 __super__.call(this);
12132 }
12133
12134 addProperties(AnonymousSubject.prototype, Observer.prototype, {
12135 _subscribe: function (o) {
12136 return this.observable.subscribe(o);
12137 },
12138 onCompleted: function () {
12139 this.observer.onCompleted();
12140 },
12141 onError: function (error) {
12142 this.observer.onError(error);
12143 },
12144 onNext: function (value) {
12145 this.observer.onNext(value);
12146 }
12147 });
12148
12149 return AnonymousSubject;
12150 }(Observable));
12151
12152 /**
12153 * Used to pause and resume streams.
12154 */
12155 Rx.Pauser = (function (__super__) {
12156 inherits(Pauser, __super__);
12157 function Pauser() {
12158 __super__.call(this);
12159 }
12160
12161 /**
12162 * Pauses the underlying sequence.
12163 */
12164 Pauser.prototype.pause = function () { this.onNext(false); };
12165
12166 /**
12167 * Resumes the underlying sequence.
12168 */
12169 Pauser.prototype.resume = function () { this.onNext(true); };
12170
12171 return Pauser;
12172 }(Subject));
12173
12174 if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
12175 root.Rx = Rx;
12176
12177 define(function() {
12178 return Rx;
12179 });
12180 } else if (freeExports && freeModule) {
12181 // in Node.js or RingoJS
12182 if (moduleExports) {
12183 (freeModule.exports = Rx).Rx = Rx;
12184 } else {
12185 freeExports.Rx = Rx;
12186 }
12187 } else {
12188 // in a browser or Rhino
12189 root.Rx = Rx;
12190 }
12191
12192 // All code before this point will be filtered from stack traces.
12193 var rEndingLine = captureLine();
12194
12195}.call(this));