rx.lite.aggregates.js 45 KB

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