123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365 |
- // Copyright (c) Microsoft, All rights reserved. See License.txt in the project root for license information.
- ;(function (factory) {
- var objectTypes = {
- 'function': true,
- 'object': true
- };
- function checkGlobal(value) {
- return (value && value.Object === Object) ? value : null;
- }
- var freeExports = (objectTypes[typeof exports] && exports && !exports.nodeType) ? exports : null;
- var freeModule = (objectTypes[typeof module] && module && !module.nodeType) ? module : null;
- var freeGlobal = checkGlobal(freeExports && freeModule && typeof global === 'object' && global);
- var freeSelf = checkGlobal(objectTypes[typeof self] && self);
- var freeWindow = checkGlobal(objectTypes[typeof window] && window);
- var moduleExports = (freeModule && freeModule.exports === freeExports) ? freeExports : null;
- var thisGlobal = checkGlobal(objectTypes[typeof this] && this);
- var root = freeGlobal || ((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) || freeSelf || thisGlobal || Function('return this')();
- // Because of build optimizers
- if (typeof define === 'function' && define.amd) {
- define(['./rx'], function (Rx, exports) {
- return factory(root, exports, Rx);
- });
- } else if (typeof module === 'object' && module && module.exports === freeExports) {
- module.exports = factory(root, module.exports, require('./rx'));
- } else {
- root.Rx = factory(root, {}, root.Rx);
- }
- }.call(this, function (root, exp, Rx, undefined) {
- // References
- var Observable = Rx.Observable,
- observableProto = Observable.prototype,
- BinaryDisposable = Rx.BinaryDisposable,
- AnonymousObservable = Rx.AnonymousObservable,
- AbstractObserver = Rx.internals.AbstractObserver,
- disposableEmpty = Rx.Disposable.empty,
- helpers = Rx.helpers,
- defaultComparer = helpers.defaultComparer,
- identity = helpers.identity,
- defaultSubComparer = helpers.defaultSubComparer,
- isFunction = helpers.isFunction,
- isPromise = helpers.isPromise,
- isArrayLike = helpers.isArrayLike,
- isIterable = helpers.isIterable,
- inherits = Rx.internals.inherits,
- observableFromPromise = Observable.fromPromise,
- observableFrom = Observable.from,
- bindCallback = Rx.internals.bindCallback,
- EmptyError = Rx.EmptyError,
- ObservableBase = Rx.ObservableBase,
- ArgumentOutOfRangeError = Rx.ArgumentOutOfRangeError;
- var errorObj = {e: {}};
-
- function tryCatcherGen(tryCatchTarget) {
- return function tryCatcher() {
- try {
- return tryCatchTarget.apply(this, arguments);
- } catch (e) {
- errorObj.e = e;
- return errorObj;
- }
- };
- }
- var tryCatch = Rx.internals.tryCatch = function tryCatch(fn) {
- if (!isFunction(fn)) { throw new TypeError('fn must be a function'); }
- return tryCatcherGen(fn);
- };
- function thrower(e) {
- throw e;
- }
- var ExtremaByObservable = (function (__super__) {
- inherits(ExtremaByObservable, __super__);
- function ExtremaByObservable(source, k, c) {
- this.source = source;
- this._k = k;
- this._c = c;
- __super__.call(this);
- }
- ExtremaByObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new ExtremaByObserver(o, this._k, this._c));
- };
- return ExtremaByObservable;
- }(ObservableBase));
- var ExtremaByObserver = (function (__super__) {
- inherits(ExtremaByObserver, __super__);
- function ExtremaByObserver(o, k, c) {
- this._o = o;
- this._k = k;
- this._c = c;
- this._v = null;
- this._hv = false;
- this._l = [];
- __super__.call(this);
- }
- ExtremaByObserver.prototype.next = function (x) {
- var key = tryCatch(this._k)(x);
- if (key === errorObj) { return this._o.onError(key.e); }
- var comparison = 0;
- if (!this._hv) {
- this._hv = true;
- this._v = key;
- } else {
- comparison = tryCatch(this._c)(key, this._v);
- if (comparison === errorObj) { return this._o.onError(comparison.e); }
- }
- if (comparison > 0) {
- this._v = key;
- this._l = [];
- }
- if (comparison >= 0) { this._l.push(x); }
- };
- ExtremaByObserver.prototype.error = function (e) {
- this._o.onError(e);
- };
- ExtremaByObserver.prototype.completed = function () {
- this._o.onNext(this._l);
- this._o.onCompleted();
- };
- return ExtremaByObserver;
- }(AbstractObserver));
- function firstOnly(x) {
- if (x.length === 0) { throw new EmptyError(); }
- return x[0];
- }
- var ReduceObservable = (function(__super__) {
- inherits(ReduceObservable, __super__);
- function ReduceObservable(source, accumulator, hasSeed, seed) {
- this.source = source;
- this.accumulator = accumulator;
- this.hasSeed = hasSeed;
- this.seed = seed;
- __super__.call(this);
- }
- ReduceObservable.prototype.subscribeCore = function(observer) {
- return this.source.subscribe(new ReduceObserver(observer,this));
- };
- return ReduceObservable;
- }(ObservableBase));
- var ReduceObserver = (function (__super__) {
- inherits(ReduceObserver, __super__);
- function ReduceObserver(o, parent) {
- this._o = o;
- this._p = parent;
- this._fn = parent.accumulator;
- this._hs = parent.hasSeed;
- this._s = parent.seed;
- this._ha = false;
- this._a = null;
- this._hv = false;
- this._i = 0;
- __super__.call(this);
- }
- ReduceObserver.prototype.next = function (x) {
- !this._hv && (this._hv = true);
- if (this._ha) {
- this._a = tryCatch(this._fn)(this._a, x, this._i, this._p);
- } else {
- this._a = this._hs ? tryCatch(this._fn)(this._s, x, this._i, this._p) : x;
- this._ha = true;
- }
- if (this._a === errorObj) { return this._o.onError(this._a.e); }
- this._i++;
- };
- ReduceObserver.prototype.error = function (e) {
- this._o.onError(e);
- };
- ReduceObserver.prototype.completed = function () {
- this._hv && this._o.onNext(this._a);
- !this._hv && this._hs && this._o.onNext(this._s);
- !this._hv && !this._hs && this._o.onError(new EmptyError());
- this._o.onCompleted();
- };
- return ReduceObserver;
- }(AbstractObserver));
- /**
- * 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.
- * For aggregation behavior with incremental intermediate results, see Observable.scan.
- * @param {Function} accumulator An accumulator function to be invoked on each element.
- * @param {Any} [seed] The initial accumulator value.
- * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
- */
- observableProto.reduce = function () {
- var hasSeed = false, seed, accumulator = arguments[0];
- if (arguments.length === 2) {
- hasSeed = true;
- seed = arguments[1];
- }
- return new ReduceObservable(this, accumulator, hasSeed, seed);
- };
- var SomeObservable = (function (__super__) {
- inherits(SomeObservable, __super__);
- function SomeObservable(source, fn) {
- this.source = source;
- this._fn = fn;
- __super__.call(this);
- }
- SomeObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new SomeObserver(o, this._fn, this.source));
- };
- return SomeObservable;
- }(ObservableBase));
- var SomeObserver = (function (__super__) {
- inherits(SomeObserver, __super__);
- function SomeObserver(o, fn, s) {
- this._o = o;
- this._fn = fn;
- this._s = s;
- this._i = 0;
- __super__.call(this);
- }
- SomeObserver.prototype.next = function (x) {
- var result = tryCatch(this._fn)(x, this._i++, this._s);
- if (result === errorObj) { return this._o.onError(result.e); }
- if (Boolean(result)) {
- this._o.onNext(true);
- this._o.onCompleted();
- }
- };
- SomeObserver.prototype.error = function (e) { this._o.onError(e); };
- SomeObserver.prototype.completed = function () {
- this._o.onNext(false);
- this._o.onCompleted();
- };
- return SomeObserver;
- }(AbstractObserver));
- /**
- * Determines whether any element of an observable sequence satisfies a condition if present, else if any items are in the sequence.
- * @param {Function} [predicate] A function to test each element for a condition.
- * @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.
- */
- observableProto.some = function (predicate, thisArg) {
- var fn = bindCallback(predicate, thisArg, 3);
- return new SomeObservable(this, fn);
- };
- var IsEmptyObservable = (function (__super__) {
- inherits(IsEmptyObservable, __super__);
- function IsEmptyObservable(source) {
- this.source = source;
- __super__.call(this);
- }
- IsEmptyObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new IsEmptyObserver(o));
- };
- return IsEmptyObservable;
- }(ObservableBase));
- var IsEmptyObserver = (function(__super__) {
- inherits(IsEmptyObserver, __super__);
- function IsEmptyObserver(o) {
- this._o = o;
- __super__.call(this);
- }
- IsEmptyObserver.prototype.next = function () {
- this._o.onNext(false);
- this._o.onCompleted();
- };
- IsEmptyObserver.prototype.error = function (e) { this._o.onError(e); };
- IsEmptyObserver.prototype.completed = function () {
- this._o.onNext(true);
- this._o.onCompleted();
- };
- return IsEmptyObserver;
- }(AbstractObserver));
- /**
- * Determines whether an observable sequence is empty.
- * @returns {Observable} An observable sequence containing a single element determining whether the source sequence is empty.
- */
- observableProto.isEmpty = function () {
- return new IsEmptyObservable(this);
- };
- var EveryObservable = (function (__super__) {
- inherits(EveryObservable, __super__);
- function EveryObservable(source, fn) {
- this.source = source;
- this._fn = fn;
- __super__.call(this);
- }
- EveryObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new EveryObserver(o, this._fn, this.source));
- };
- return EveryObservable;
- }(ObservableBase));
- var EveryObserver = (function (__super__) {
- inherits(EveryObserver, __super__);
- function EveryObserver(o, fn, s) {
- this._o = o;
- this._fn = fn;
- this._s = s;
- this._i = 0;
- __super__.call(this);
- }
- EveryObserver.prototype.next = function (x) {
- var result = tryCatch(this._fn)(x, this._i++, this._s);
- if (result === errorObj) { return this._o.onError(result.e); }
- if (!Boolean(result)) {
- this._o.onNext(false);
- this._o.onCompleted();
- }
- };
- EveryObserver.prototype.error = function (e) { this._o.onError(e); };
- EveryObserver.prototype.completed = function () {
- this._o.onNext(true);
- this._o.onCompleted();
- };
- return EveryObserver;
- }(AbstractObserver));
- /**
- * Determines whether all elements of an observable sequence satisfy a condition.
- * @param {Function} [predicate] A function to test each element for a condition.
- * @param {Any} [thisArg] Object to use as this when executing callback.
- * @returns {Observable} An observable sequence containing a single element determining whether all elements in the source sequence pass the test in the specified predicate.
- */
- observableProto.every = function (predicate, thisArg) {
- var fn = bindCallback(predicate, thisArg, 3);
- return new EveryObservable(this, fn);
- };
- var IncludesObservable = (function (__super__) {
- inherits(IncludesObservable, __super__);
- function IncludesObservable(source, elem, idx) {
- var n = +idx || 0;
- Math.abs(n) === Infinity && (n = 0);
- this.source = source;
- this._elem = elem;
- this._n = n;
- __super__.call(this);
- }
- IncludesObservable.prototype.subscribeCore = function (o) {
- if (this._n < 0) {
- o.onNext(false);
- o.onCompleted();
- return disposableEmpty;
- }
- return this.source.subscribe(new IncludesObserver(o, this._elem, this._n));
- };
- return IncludesObservable;
- }(ObservableBase));
- var IncludesObserver = (function (__super__) {
- inherits(IncludesObserver, __super__);
- function IncludesObserver(o, elem, n) {
- this._o = o;
- this._elem = elem;
- this._n = n;
- this._i = 0;
- __super__.call(this);
- }
- function comparer(a, b) {
- return (a === 0 && b === 0) || (a === b || (isNaN(a) && isNaN(b)));
- }
- IncludesObserver.prototype.next = function (x) {
- if (this._i++ >= this._n && comparer(x, this._elem)) {
- this._o.onNext(true);
- this._o.onCompleted();
- }
- };
- IncludesObserver.prototype.error = function (e) { this._o.onError(e); };
- IncludesObserver.prototype.completed = function () { this._o.onNext(false); this._o.onCompleted(); };
- return IncludesObserver;
- }(AbstractObserver));
- /**
- * Determines whether an observable sequence includes a specified element with an optional equality comparer.
- * @param searchElement The value to locate in the source sequence.
- * @param {Number} [fromIndex] An equality comparer to compare elements.
- * @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.
- */
- observableProto.includes = function (searchElement, fromIndex) {
- return new IncludesObservable(this, searchElement, fromIndex);
- };
- var CountObservable = (function (__super__) {
- inherits(CountObservable, __super__);
- function CountObservable(source, fn) {
- this.source = source;
- this._fn = fn;
- __super__.call(this);
- }
- CountObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new CountObserver(o, this._fn, this.source));
- };
- return CountObservable;
- }(ObservableBase));
- var CountObserver = (function (__super__) {
- inherits(CountObserver, __super__);
- function CountObserver(o, fn, s) {
- this._o = o;
- this._fn = fn;
- this._s = s;
- this._i = 0;
- this._c = 0;
- __super__.call(this);
- }
- CountObserver.prototype.next = function (x) {
- if (this._fn) {
- var result = tryCatch(this._fn)(x, this._i++, this._s);
- if (result === errorObj) { return this._o.onError(result.e); }
- Boolean(result) && (this._c++);
- } else {
- this._c++;
- }
- };
- CountObserver.prototype.error = function (e) { this._o.onError(e); };
- CountObserver.prototype.completed = function () {
- this._o.onNext(this._c);
- this._o.onCompleted();
- };
- return CountObserver;
- }(AbstractObserver));
- /**
- * 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.
- * @example
- * res = source.count();
- * res = source.count(function (x) { return x > 3; });
- * @param {Function} [predicate]A function to test each element for a condition.
- * @param {Any} [thisArg] Object to use as this when executing callback.
- * @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.
- */
- observableProto.count = function (predicate, thisArg) {
- var fn = bindCallback(predicate, thisArg, 3);
- return new CountObservable(this, fn);
- };
- var IndexOfObservable = (function (__super__) {
- inherits(IndexOfObservable, __super__);
- function IndexOfObservable(source, e, n) {
- this.source = source;
- this._e = e;
- this._n = n;
- __super__.call(this);
- }
- IndexOfObservable.prototype.subscribeCore = function (o) {
- if (this._n < 0) {
- o.onNext(-1);
- o.onCompleted();
- return disposableEmpty;
- }
- return this.source.subscribe(new IndexOfObserver(o, this._e, this._n));
- };
- return IndexOfObservable;
- }(ObservableBase));
- var IndexOfObserver = (function (__super__) {
- inherits(IndexOfObserver, __super__);
- function IndexOfObserver(o, e, n) {
- this._o = o;
- this._e = e;
- this._n = n;
- this._i = 0;
- __super__.call(this);
- }
- IndexOfObserver.prototype.next = function (x) {
- if (this._i >= this._n && x === this._e) {
- this._o.onNext(this._i);
- this._o.onCompleted();
- }
- this._i++;
- };
- IndexOfObserver.prototype.error = function (e) { this._o.onError(e); };
- IndexOfObserver.prototype.completed = function () { this._o.onNext(-1); this._o.onCompleted(); };
- return IndexOfObserver;
- }(AbstractObserver));
- /**
- * Returns the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
- * @param {Any} searchElement Element to locate in the array.
- * @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
- * @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.
- */
- observableProto.indexOf = function(searchElement, fromIndex) {
- var n = +fromIndex || 0;
- Math.abs(n) === Infinity && (n = 0);
- return new IndexOfObservable(this, searchElement, n);
- };
- var SumObservable = (function (__super__) {
- inherits(SumObservable, __super__);
- function SumObservable(source, fn) {
- this.source = source;
- this._fn = fn;
- __super__.call(this);
- }
- SumObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new SumObserver(o, this._fn, this.source));
- };
- return SumObservable;
- }(ObservableBase));
- var SumObserver = (function (__super__) {
- inherits(SumObserver, __super__);
- function SumObserver(o, fn, s) {
- this._o = o;
- this._fn = fn;
- this._s = s;
- this._i = 0;
- this._c = 0;
- __super__.call(this);
- }
- SumObserver.prototype.next = function (x) {
- if (this._fn) {
- var result = tryCatch(this._fn)(x, this._i++, this._s);
- if (result === errorObj) { return this._o.onError(result.e); }
- this._c += result;
- } else {
- this._c += x;
- }
- };
- SumObserver.prototype.error = function (e) { this._o.onError(e); };
- SumObserver.prototype.completed = function () {
- this._o.onNext(this._c);
- this._o.onCompleted();
- };
- return SumObserver;
- }(AbstractObserver));
- /**
- * 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.
- * @param {Function} [selector] A transform function to apply to each element.
- * @param {Any} [thisArg] Object to use as this when executing callback.
- * @returns {Observable} An observable sequence containing a single element with the sum of the values in the source sequence.
- */
- observableProto.sum = function (keySelector, thisArg) {
- var fn = bindCallback(keySelector, thisArg, 3);
- return new SumObservable(this, fn);
- };
- /**
- * Returns the elements in an observable sequence with the minimum key value according to the specified comparer.
- * @example
- * var res = source.minBy(function (x) { return x.value; });
- * var res = source.minBy(function (x) { return x.value; }, function (x, y) { return x - y; });
- * @param {Function} keySelector Key selector function.
- * @param {Function} [comparer] Comparer used to compare key values.
- * @returns {Observable} An observable sequence containing a list of zero or more elements that have a minimum key value.
- */
- observableProto.minBy = function (keySelector, comparer) {
- comparer || (comparer = defaultSubComparer);
- return new ExtremaByObservable(this, keySelector, function (x, y) { return comparer(x, y) * -1; });
- };
- /**
- * Returns the minimum element in an observable sequence according to the optional comparer else a default greater than less than check.
- * @example
- * var res = source.min();
- * var res = source.min(function (x, y) { return x.value - y.value; });
- * @param {Function} [comparer] Comparer used to compare elements.
- * @returns {Observable} An observable sequence containing a single element with the minimum element in the source sequence.
- */
- observableProto.min = function (comparer) {
- return this.minBy(identity, comparer).map(firstOnly);
- };
- /**
- * Returns the elements in an observable sequence with the maximum key value according to the specified comparer.
- * @example
- * var res = source.maxBy(function (x) { return x.value; });
- * var res = source.maxBy(function (x) { return x.value; }, function (x, y) { return x - y;; });
- * @param {Function} keySelector Key selector function.
- * @param {Function} [comparer] Comparer used to compare key values.
- * @returns {Observable} An observable sequence containing a list of zero or more elements that have a maximum key value.
- */
- observableProto.maxBy = function (keySelector, comparer) {
- comparer || (comparer = defaultSubComparer);
- return new ExtremaByObservable(this, keySelector, comparer);
- };
- /**
- * Returns the maximum value in an observable sequence according to the specified comparer.
- * @example
- * var res = source.max();
- * var res = source.max(function (x, y) { return x.value - y.value; });
- * @param {Function} [comparer] Comparer used to compare elements.
- * @returns {Observable} An observable sequence containing a single element with the maximum element in the source sequence.
- */
- observableProto.max = function (comparer) {
- return this.maxBy(identity, comparer).map(firstOnly);
- };
- var AverageObservable = (function (__super__) {
- inherits(AverageObservable, __super__);
- function AverageObservable(source, fn) {
- this.source = source;
- this._fn = fn;
- __super__.call(this);
- }
- AverageObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new AverageObserver(o, this._fn, this.source));
- };
- return AverageObservable;
- }(ObservableBase));
- var AverageObserver = (function(__super__) {
- inherits(AverageObserver, __super__);
- function AverageObserver(o, fn, s) {
- this._o = o;
- this._fn = fn;
- this._s = s;
- this._c = 0;
- this._t = 0;
- __super__.call(this);
- }
- AverageObserver.prototype.next = function (x) {
- if(this._fn) {
- var r = tryCatch(this._fn)(x, this._c++, this._s);
- if (r === errorObj) { return this._o.onError(r.e); }
- this._t += r;
- } else {
- this._c++;
- this._t += x;
- }
- };
- AverageObserver.prototype.error = function (e) { this._o.onError(e); };
- AverageObserver.prototype.completed = function () {
- if (this._c === 0) { return this._o.onError(new EmptyError()); }
- this._o.onNext(this._t / this._c);
- this._o.onCompleted();
- };
- return AverageObserver;
- }(AbstractObserver));
- /**
- * 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.
- * @param {Function} [selector] A transform function to apply to each element.
- * @param {Any} [thisArg] Object to use as this when executing callback.
- * @returns {Observable} An observable sequence containing a single element with the average of the sequence of values.
- */
- observableProto.average = function (keySelector, thisArg) {
- var source = this, fn;
- if (isFunction(keySelector)) {
- fn = bindCallback(keySelector, thisArg, 3);
- }
- return new AverageObservable(source, fn);
- };
- /**
- * Determines whether two sequences are equal by comparing the elements pairwise using a specified equality comparer.
- *
- * @example
- * var res = res = source.sequenceEqual([1,2,3]);
- * var res = res = source.sequenceEqual([{ value: 42 }], function (x, y) { return x.value === y.value; });
- * 3 - res = source.sequenceEqual(Rx.Observable.returnValue(42));
- * 4 - res = source.sequenceEqual(Rx.Observable.returnValue({ value: 42 }), function (x, y) { return x.value === y.value; });
- * @param {Observable} second Second observable sequence or array to compare.
- * @param {Function} [comparer] Comparer used to compare elements of both sequences.
- * @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.
- */
- observableProto.sequenceEqual = function (second, comparer) {
- var first = this;
- comparer || (comparer = defaultComparer);
- return new AnonymousObservable(function (o) {
- var donel = false, doner = false, ql = [], qr = [];
- var subscription1 = first.subscribe(function (x) {
- if (qr.length > 0) {
- var v = qr.shift();
- var equal = tryCatch(comparer)(v, x);
- if (equal === errorObj) { return o.onError(equal.e); }
- if (!equal) {
- o.onNext(false);
- o.onCompleted();
- }
- } else if (doner) {
- o.onNext(false);
- o.onCompleted();
- } else {
- ql.push(x);
- }
- }, function(e) { o.onError(e); }, function () {
- donel = true;
- if (ql.length === 0) {
- if (qr.length > 0) {
- o.onNext(false);
- o.onCompleted();
- } else if (doner) {
- o.onNext(true);
- o.onCompleted();
- }
- }
- });
- (isArrayLike(second) || isIterable(second)) && (second = observableFrom(second));
- isPromise(second) && (second = observableFromPromise(second));
- var subscription2 = second.subscribe(function (x) {
- if (ql.length > 0) {
- var v = ql.shift();
- var equal = tryCatch(comparer)(v, x);
- if (equal === errorObj) { return o.onError(equal.e); }
- if (!equal) {
- o.onNext(false);
- o.onCompleted();
- }
- } else if (donel) {
- o.onNext(false);
- o.onCompleted();
- } else {
- qr.push(x);
- }
- }, function(e) { o.onError(e); }, function () {
- doner = true;
- if (qr.length === 0) {
- if (ql.length > 0) {
- o.onNext(false);
- o.onCompleted();
- } else if (donel) {
- o.onNext(true);
- o.onCompleted();
- }
- }
- });
- return new BinaryDisposable(subscription1, subscription2);
- }, first);
- };
- var ElementAtObservable = (function (__super__) {
- inherits(ElementAtObservable, __super__);
- function ElementAtObservable(source, i, d) {
- this.source = source;
- this._i = i;
- this._d = d;
- __super__.call(this);
- }
- ElementAtObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new ElementAtObserver(o, this._i, this._d));
- };
- return ElementAtObservable;
- }(ObservableBase));
- var ElementAtObserver = (function (__super__) {
- inherits(ElementAtObserver, __super__);
- function ElementAtObserver(o, i, d) {
- this._o = o;
- this._i = i;
- this._d = d;
- __super__.call(this);
- }
- ElementAtObserver.prototype.next = function (x) {
- if (this._i-- === 0) {
- this._o.onNext(x);
- this._o.onCompleted();
- }
- };
- ElementAtObserver.prototype.error = function (e) { this._o.onError(e); };
- ElementAtObserver.prototype.completed = function () {
- if (this._d === undefined) {
- this._o.onError(new ArgumentOutOfRangeError());
- } else {
- this._o.onNext(this._d);
- this._o.onCompleted();
- }
- };
- return ElementAtObserver;
- }(AbstractObserver));
- /**
- * Returns the element at a specified index in a sequence or default value if not found.
- * @param {Number} index The zero-based index of the element to retrieve.
- * @param {Any} [defaultValue] The default value to use if elementAt does not find a value.
- * @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence.
- */
- observableProto.elementAt = function (index, defaultValue) {
- if (index < 0) { throw new ArgumentOutOfRangeError(); }
- return new ElementAtObservable(this, index, defaultValue);
- };
- var SingleObserver = (function(__super__) {
- inherits(SingleObserver, __super__);
- function SingleObserver(o, obj, s) {
- this._o = o;
- this._obj = obj;
- this._s = s;
- this._i = 0;
- this._hv = false;
- this._v = null;
- __super__.call(this);
- }
- SingleObserver.prototype.next = function (x) {
- var shouldYield = false;
- if (this._obj.predicate) {
- var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
- if (res === errorObj) { return this._o.onError(res.e); }
- Boolean(res) && (shouldYield = true);
- } else if (!this._obj.predicate) {
- shouldYield = true;
- }
- if (shouldYield) {
- if (this._hv) {
- return this._o.onError(new Error('Sequence contains more than one matching element'));
- }
- this._hv = true;
- this._v = x;
- }
- };
- SingleObserver.prototype.error = function (e) { this._o.onError(e); };
- SingleObserver.prototype.completed = function () {
- if (this._hv) {
- this._o.onNext(this._v);
- this._o.onCompleted();
- }
- else if (this._obj.defaultValue === undefined) {
- this._o.onError(new EmptyError());
- } else {
- this._o.onNext(this._obj.defaultValue);
- this._o.onCompleted();
- }
- };
- return SingleObserver;
- }(AbstractObserver));
- /**
- * 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.
- * @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.
- */
- observableProto.single = function (predicate, thisArg) {
- var obj = {}, source = this;
- if (typeof arguments[0] === 'object') {
- obj = arguments[0];
- } else {
- obj = {
- predicate: arguments[0],
- thisArg: arguments[1],
- defaultValue: arguments[2]
- };
- }
- if (isFunction (obj.predicate)) {
- var fn = obj.predicate;
- obj.predicate = bindCallback(fn, obj.thisArg, 3);
- }
- return new AnonymousObservable(function (o) {
- return source.subscribe(new SingleObserver(o, obj, source));
- }, source);
- };
- var FirstObservable = (function (__super__) {
- inherits(FirstObservable, __super__);
- function FirstObservable(source, obj) {
- this.source = source;
- this._obj = obj;
- __super__.call(this);
- }
- FirstObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new FirstObserver(o, this._obj, this.source));
- };
- return FirstObservable;
- }(ObservableBase));
- var FirstObserver = (function(__super__) {
- inherits(FirstObserver, __super__);
- function FirstObserver(o, obj, s) {
- this._o = o;
- this._obj = obj;
- this._s = s;
- this._i = 0;
- __super__.call(this);
- }
- FirstObserver.prototype.next = function (x) {
- if (this._obj.predicate) {
- var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
- if (res === errorObj) { return this._o.onError(res.e); }
- if (Boolean(res)) {
- this._o.onNext(x);
- this._o.onCompleted();
- }
- } else if (!this._obj.predicate) {
- this._o.onNext(x);
- this._o.onCompleted();
- }
- };
- FirstObserver.prototype.error = function (e) { this._o.onError(e); };
- FirstObserver.prototype.completed = function () {
- if (this._obj.defaultValue === undefined) {
- this._o.onError(new EmptyError());
- } else {
- this._o.onNext(this._obj.defaultValue);
- this._o.onCompleted();
- }
- };
- return FirstObserver;
- }(AbstractObserver));
- /**
- * Returns the first element of an observable sequence that satisfies the condition in the predicate if present else the first item in the sequence.
- * @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.
- */
- observableProto.first = function () {
- var obj = {}, source = this;
- if (typeof arguments[0] === 'object') {
- obj = arguments[0];
- } else {
- obj = {
- predicate: arguments[0],
- thisArg: arguments[1],
- defaultValue: arguments[2]
- };
- }
- if (isFunction (obj.predicate)) {
- var fn = obj.predicate;
- obj.predicate = bindCallback(fn, obj.thisArg, 3);
- }
- return new FirstObservable(this, obj);
- };
- var LastObservable = (function (__super__) {
- inherits(LastObservable, __super__);
- function LastObservable(source, obj) {
- this.source = source;
- this._obj = obj;
- __super__.call(this);
- }
- LastObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new LastObserver(o, this._obj, this.source));
- };
- return LastObservable;
- }(ObservableBase));
- var LastObserver = (function(__super__) {
- inherits(LastObserver, __super__);
- function LastObserver(o, obj, s) {
- this._o = o;
- this._obj = obj;
- this._s = s;
- this._i = 0;
- this._hv = false;
- this._v = null;
- __super__.call(this);
- }
- LastObserver.prototype.next = function (x) {
- var shouldYield = false;
- if (this._obj.predicate) {
- var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
- if (res === errorObj) { return this._o.onError(res.e); }
- Boolean(res) && (shouldYield = true);
- } else if (!this._obj.predicate) {
- shouldYield = true;
- }
- if (shouldYield) {
- this._hv = true;
- this._v = x;
- }
- };
- LastObserver.prototype.error = function (e) { this._o.onError(e); };
- LastObserver.prototype.completed = function () {
- if (this._hv) {
- this._o.onNext(this._v);
- this._o.onCompleted();
- }
- else if (this._obj.defaultValue === undefined) {
- this._o.onError(new EmptyError());
- } else {
- this._o.onNext(this._obj.defaultValue);
- this._o.onCompleted();
- }
- };
- return LastObserver;
- }(AbstractObserver));
- /**
- * Returns the last element of an observable sequence that satisfies the condition in the predicate if specified, else the last element.
- * @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate.
- */
- observableProto.last = function () {
- var obj = {}, source = this;
- if (typeof arguments[0] === 'object') {
- obj = arguments[0];
- } else {
- obj = {
- predicate: arguments[0],
- thisArg: arguments[1],
- defaultValue: arguments[2]
- };
- }
- if (isFunction (obj.predicate)) {
- var fn = obj.predicate;
- obj.predicate = bindCallback(fn, obj.thisArg, 3);
- }
- return new LastObservable(this, obj);
- };
- var FindValueObserver = (function(__super__) {
- inherits(FindValueObserver, __super__);
- function FindValueObserver(observer, source, callback, yieldIndex) {
- this._o = observer;
- this._s = source;
- this._cb = callback;
- this._y = yieldIndex;
- this._i = 0;
- __super__.call(this);
- }
- FindValueObserver.prototype.next = function (x) {
- var shouldRun = tryCatch(this._cb)(x, this._i, this._s);
- if (shouldRun === errorObj) { return this._o.onError(shouldRun.e); }
- if (shouldRun) {
- this._o.onNext(this._y ? this._i : x);
- this._o.onCompleted();
- } else {
- this._i++;
- }
- };
- FindValueObserver.prototype.error = function (e) {
- this._o.onError(e);
- };
- FindValueObserver.prototype.completed = function () {
- this._y && this._o.onNext(-1);
- this._o.onCompleted();
- };
- return FindValueObserver;
- }(AbstractObserver));
- function findValue (source, predicate, thisArg, yieldIndex) {
- var callback = bindCallback(predicate, thisArg, 3);
- return new AnonymousObservable(function (o) {
- return source.subscribe(new FindValueObserver(o, source, callback, yieldIndex));
- }, source);
- }
- /**
- * Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Observable sequence.
- * @param {Function} predicate The predicate that defines the conditions of the element to search for.
- * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
- * @returns {Observable} An Observable sequence with the first element that matches the conditions defined by the specified predicate, if found; otherwise, undefined.
- */
- observableProto.find = function (predicate, thisArg) {
- return findValue(this, predicate, thisArg, false);
- };
- /**
- * Searches for an element that matches the conditions defined by the specified predicate, and returns
- * an Observable sequence with the zero-based index of the first occurrence within the entire Observable sequence.
- * @param {Function} predicate The predicate that defines the conditions of the element to search for.
- * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
- * @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.
- */
- observableProto.findIndex = function (predicate, thisArg) {
- return findValue(this, predicate, thisArg, true);
- };
- var ToSetObservable = (function (__super__) {
- inherits(ToSetObservable, __super__);
- function ToSetObservable(source) {
- this.source = source;
- __super__.call(this);
- }
- ToSetObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new ToSetObserver(o));
- };
- return ToSetObservable;
- }(ObservableBase));
- var ToSetObserver = (function (__super__) {
- inherits(ToSetObserver, __super__);
- function ToSetObserver(o) {
- this._o = o;
- this._s = new root.Set();
- __super__.call(this);
- }
- ToSetObserver.prototype.next = function (x) {
- this._s.add(x);
- };
- ToSetObserver.prototype.error = function (e) {
- this._o.onError(e);
- };
- ToSetObserver.prototype.completed = function () {
- this._o.onNext(this._s);
- this._o.onCompleted();
- };
- return ToSetObserver;
- }(AbstractObserver));
- /**
- * Converts the observable sequence to a Set if it exists.
- * @returns {Observable} An observable sequence with a single value of a Set containing the values from the observable sequence.
- */
- observableProto.toSet = function () {
- if (typeof root.Set === 'undefined') { throw new TypeError(); }
- return new ToSetObservable(this);
- };
- var ToMapObservable = (function (__super__) {
- inherits(ToMapObservable, __super__);
- function ToMapObservable(source, k, e) {
- this.source = source;
- this._k = k;
- this._e = e;
- __super__.call(this);
- }
- ToMapObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new ToMapObserver(o, this._k, this._e));
- };
- return ToMapObservable;
- }(ObservableBase));
- var ToMapObserver = (function (__super__) {
- inherits(ToMapObserver, __super__);
- function ToMapObserver(o, k, e) {
- this._o = o;
- this._k = k;
- this._e = e;
- this._m = new root.Map();
- __super__.call(this);
- }
- ToMapObserver.prototype.next = function (x) {
- var key = tryCatch(this._k)(x);
- if (key === errorObj) { return this._o.onError(key.e); }
- var elem = x;
- if (this._e) {
- elem = tryCatch(this._e)(x);
- if (elem === errorObj) { return this._o.onError(elem.e); }
- }
- this._m.set(key, elem);
- };
- ToMapObserver.prototype.error = function (e) {
- this._o.onError(e);
- };
- ToMapObserver.prototype.completed = function () {
- this._o.onNext(this._m);
- this._o.onCompleted();
- };
- return ToMapObserver;
- }(AbstractObserver));
- /**
- * Converts the observable sequence to a Map if it exists.
- * @param {Function} keySelector A function which produces the key for the Map.
- * @param {Function} [elementSelector] An optional function which produces the element for the Map. If not present, defaults to the value from the observable sequence.
- * @returns {Observable} An observable sequence with a single value of a Map containing the values from the observable sequence.
- */
- observableProto.toMap = function (keySelector, elementSelector) {
- if (typeof root.Map === 'undefined') { throw new TypeError(); }
- return new ToMapObservable(this, keySelector, elementSelector);
- };
- var SliceObservable = (function (__super__) {
- inherits(SliceObservable, __super__);
- function SliceObservable(source, b, e) {
- this.source = source;
- this._b = b;
- this._e = e;
- __super__.call(this);
- }
- SliceObservable.prototype.subscribeCore = function (o) {
- return this.source.subscribe(new SliceObserver(o, this._b, this._e));
- };
- return SliceObservable;
- }(ObservableBase));
- var SliceObserver = (function (__super__) {
- inherits(SliceObserver, __super__);
- function SliceObserver(o, b, e) {
- this._o = o;
- this._b = b;
- this._e = e;
- this._i = 0;
- __super__.call(this);
- }
- SliceObserver.prototype.next = function (x) {
- if (this._i >= this._b) {
- if (this._e === this._i) {
- this._o.onCompleted();
- } else {
- this._o.onNext(x);
- }
- }
- this._i++;
- };
- SliceObserver.prototype.error = function (e) { this._o.onError(e); };
- SliceObserver.prototype.completed = function () { this._o.onCompleted(); };
- return SliceObserver;
- }(AbstractObserver));
- /*
- * The slice() method returns a shallow copy of a portion of an Observable into a new Observable object.
- * Unlike the array version, this does not support negative numbers for being or end.
- * @param {Number} [begin] Zero-based index at which to begin extraction. If omitted, this will default to zero.
- * @param {Number} [end] Zero-based index at which to end extraction. slice extracts up to but not including end.
- * If omitted, this will emit the rest of the Observable object.
- * @returns {Observable} A shallow copy of a portion of an Observable into a new Observable object.
- */
- observableProto.slice = function (begin, end) {
- var start = begin || 0;
- if (start < 0) { throw new Rx.ArgumentOutOfRangeError(); }
- if (typeof end === 'number' && end < start) {
- throw new Rx.ArgumentOutOfRangeError();
- }
- return new SliceObservable(this, start, end);
- };
- var LastIndexOfObservable = (function (__super__) {
- inherits(LastIndexOfObservable, __super__);
- function LastIndexOfObservable(source, e, n) {
- this.source = source;
- this._e = e;
- this._n = n;
- __super__.call(this);
- }
- LastIndexOfObservable.prototype.subscribeCore = function (o) {
- if (this._n < 0) {
- o.onNext(-1);
- o.onCompleted();
- return disposableEmpty;
- }
- return this.source.subscribe(new LastIndexOfObserver(o, this._e, this._n));
- };
- return LastIndexOfObservable;
- }(ObservableBase));
- var LastIndexOfObserver = (function (__super__) {
- inherits(LastIndexOfObserver, __super__);
- function LastIndexOfObserver(o, e, n) {
- this._o = o;
- this._e = e;
- this._n = n;
- this._v = 0;
- this._hv = false;
- this._i = 0;
- __super__.call(this);
- }
- LastIndexOfObserver.prototype.next = function (x) {
- if (this._i >= this._n && x === this._e) {
- this._hv = true;
- this._v = this._i;
- }
- this._i++;
- };
- LastIndexOfObserver.prototype.error = function (e) { this._o.onError(e); };
- LastIndexOfObserver.prototype.completed = function () {
- if (this._hv) {
- this._o.onNext(this._v);
- } else {
- this._o.onNext(-1);
- }
- this._o.onCompleted();
- };
- return LastIndexOfObserver;
- }(AbstractObserver));
- /**
- * Returns the last index at which a given element can be found in the observable sequence, or -1 if it is not present.
- * @param {Any} searchElement Element to locate in the array.
- * @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
- * @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.
- */
- observableProto.lastIndexOf = function(searchElement, fromIndex) {
- var n = +fromIndex || 0;
- Math.abs(n) === Infinity && (n = 0);
- return new LastIndexOfObservable(this, searchElement, n);
- };
- return Rx;
- }));
|