rxjs-rails 2.5.2 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rxjs/rails/version.rb +1 -1
  3. data/vendor/assets/javascripts/rx.aggregates.js +367 -284
  4. data/vendor/assets/javascripts/rx.aggregates.map +1 -1
  5. data/vendor/assets/javascripts/rx.aggregates.min.js +1 -1
  6. data/vendor/assets/javascripts/rx.all.compat.js +2405 -2438
  7. data/vendor/assets/javascripts/rx.all.compat.map +1 -1
  8. data/vendor/assets/javascripts/rx.all.compat.min.js +4 -4
  9. data/vendor/assets/javascripts/rx.all.js +2380 -2395
  10. data/vendor/assets/javascripts/rx.all.map +1 -1
  11. data/vendor/assets/javascripts/rx.all.min.js +4 -4
  12. data/vendor/assets/javascripts/rx.async.compat.js +311 -333
  13. data/vendor/assets/javascripts/rx.async.compat.map +1 -1
  14. data/vendor/assets/javascripts/rx.async.compat.min.js +1 -1
  15. data/vendor/assets/javascripts/rx.async.js +280 -327
  16. data/vendor/assets/javascripts/rx.async.map +1 -1
  17. data/vendor/assets/javascripts/rx.async.min.js +1 -1
  18. data/vendor/assets/javascripts/rx.backpressure.js +208 -176
  19. data/vendor/assets/javascripts/rx.backpressure.map +1 -1
  20. data/vendor/assets/javascripts/rx.backpressure.min.js +1 -1
  21. data/vendor/assets/javascripts/rx.binding.js +44 -27
  22. data/vendor/assets/javascripts/rx.binding.map +1 -1
  23. data/vendor/assets/javascripts/rx.binding.min.js +1 -1
  24. data/vendor/assets/javascripts/rx.coincidence.js +220 -453
  25. data/vendor/assets/javascripts/rx.coincidence.map +1 -1
  26. data/vendor/assets/javascripts/rx.coincidence.min.js +1 -1
  27. data/vendor/assets/javascripts/rx.compat.js +1366 -1173
  28. data/vendor/assets/javascripts/rx.compat.map +1 -1
  29. data/vendor/assets/javascripts/rx.compat.min.js +3 -2
  30. data/vendor/assets/javascripts/rx.core.binding.js +823 -0
  31. data/vendor/assets/javascripts/rx.core.binding.map +1 -0
  32. data/vendor/assets/javascripts/rx.core.binding.min.js +3 -0
  33. data/vendor/assets/javascripts/rx.core.js +1281 -0
  34. data/vendor/assets/javascripts/rx.core.map +1 -0
  35. data/vendor/assets/javascripts/rx.core.min.js +3 -0
  36. data/vendor/assets/javascripts/rx.core.testing.js +1242 -0
  37. data/vendor/assets/javascripts/rx.core.testing.map +1 -0
  38. data/vendor/assets/javascripts/rx.core.testing.min.js +3 -0
  39. data/vendor/assets/javascripts/rx.experimental.js +85 -123
  40. data/vendor/assets/javascripts/rx.experimental.map +1 -1
  41. data/vendor/assets/javascripts/rx.experimental.min.js +1 -1
  42. data/vendor/assets/javascripts/rx.joinpatterns.js +48 -37
  43. data/vendor/assets/javascripts/rx.joinpatterns.map +1 -1
  44. data/vendor/assets/javascripts/rx.joinpatterns.min.js +1 -1
  45. data/vendor/assets/javascripts/rx.js +1367 -1174
  46. data/vendor/assets/javascripts/rx.lite.compat.js +1621 -1403
  47. data/vendor/assets/javascripts/rx.lite.compat.map +1 -1
  48. data/vendor/assets/javascripts/rx.lite.compat.min.js +3 -3
  49. data/vendor/assets/javascripts/rx.lite.extras.compat.js +119 -89
  50. data/vendor/assets/javascripts/rx.lite.extras.compat.map +1 -1
  51. data/vendor/assets/javascripts/rx.lite.extras.compat.min.js +1 -1
  52. data/vendor/assets/javascripts/rx.lite.extras.js +119 -89
  53. data/vendor/assets/javascripts/rx.lite.extras.map +1 -1
  54. data/vendor/assets/javascripts/rx.lite.extras.min.js +1 -1
  55. data/vendor/assets/javascripts/rx.lite.js +1583 -1390
  56. data/vendor/assets/javascripts/rx.lite.map +1 -1
  57. data/vendor/assets/javascripts/rx.lite.min.js +3 -3
  58. data/vendor/assets/javascripts/rx.map +1 -1
  59. data/vendor/assets/javascripts/rx.min.js +3 -2
  60. data/vendor/assets/javascripts/rx.sorting.js +23 -27
  61. data/vendor/assets/javascripts/rx.sorting.map +1 -1
  62. data/vendor/assets/javascripts/rx.sorting.min.js +1 -1
  63. data/vendor/assets/javascripts/rx.testing.js +11 -15
  64. data/vendor/assets/javascripts/rx.testing.map +1 -1
  65. data/vendor/assets/javascripts/rx.testing.min.js +1 -1
  66. data/vendor/assets/javascripts/rx.time.js +117 -133
  67. data/vendor/assets/javascripts/rx.time.map +1 -1
  68. data/vendor/assets/javascripts/rx.time.min.js +1 -1
  69. data/vendor/assets/javascripts/rx.virtualtime.js +120 -39
  70. data/vendor/assets/javascripts/rx.virtualtime.map +1 -1
  71. data/vendor/assets/javascripts/rx.virtualtime.min.js +1 -1
  72. metadata +12 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d08d45804c21b4feb931f66f70e3586b21c142a
4
- data.tar.gz: 995172c50cb0292a6585558de83ba45068ff22b4
3
+ metadata.gz: 0b111e27294bc8f2e0549f90f60085314400604e
4
+ data.tar.gz: 66f28417a4225e75e3f9e7782ed833cdbb8534e2
5
5
  SHA512:
6
- metadata.gz: 68b4fbaf99f844f0ec6be9bfbf0572d7010dd142927cecf881d27e3c1e3e6ed5a5734daf18eacccbcc7030c2295279ada28364197e81593b7558d33788b97190
7
- data.tar.gz: a6c37bb753be3e51aaf9c71215e80414771ac1e4e188c27515efe6cba5b57c593285f0fc81c95387043496a451af2db1c31634e5a20e8b6951e4f020d739ef40
6
+ metadata.gz: 853a384991ee228166ead4d55335f8cac8d99760e4e12a99e7a1074f713c76a26e6d1a0cd20c6988b2f253dd6a97b5f3cf7c4b22f571e8c6ad21876bd4bdd48f
7
+ data.tar.gz: afacd60d4886105cd7fdab1f5d6bbd1e660b83515f65d3807e2174e3860bbd9047992407b29482381e27945757ac5e20026e4acf249f87f76e10fad43d165723
@@ -1,6 +1,6 @@
1
1
  module Rxjs
2
2
  module Rails
3
- VERSION = "2.5.2"
3
+ VERSION = "3.1.0"
4
4
  end
5
5
  end
6
6
 
@@ -1,35 +1,31 @@
1
1
  // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
2
 
3
3
  ;(function (factory) {
4
- var objectTypes = {
5
- 'boolean': false,
6
- 'function': true,
7
- 'object': true,
8
- 'number': false,
9
- 'string': false,
10
- 'undefined': false
11
- };
12
-
13
- var root = (objectTypes[typeof window] && window) || this,
14
- freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports,
15
- freeModule = objectTypes[typeof module] && module && !module.nodeType && module,
16
- moduleExports = freeModule && freeModule.exports === freeExports && freeExports,
17
- freeGlobal = objectTypes[typeof global] && global;
18
-
19
- if (freeGlobal && (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal)) {
20
- root = freeGlobal;
21
- }
4
+ var objectTypes = {
5
+ 'function': true,
6
+ 'object': true
7
+ };
22
8
 
23
- // Because of build optimizers
24
- if (typeof define === 'function' && define.amd) {
25
- define(['rx'], function (Rx, exports) {
26
- return factory(root, exports, Rx);
27
- });
28
- } else if (typeof module === 'object' && module && module.exports === freeExports) {
29
- module.exports = factory(root, module.exports, require('./rx'));
30
- } else {
31
- root.Rx = factory(root, {}, root.Rx);
32
- }
9
+ var
10
+ freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports,
11
+ freeSelf = objectTypes[typeof self] && self.Object && self,
12
+ freeWindow = objectTypes[typeof window] && window && window.Object && window,
13
+ freeModule = objectTypes[typeof module] && module && !module.nodeType && module,
14
+ moduleExports = freeModule && freeModule.exports === freeExports && freeExports,
15
+ freeGlobal = freeExports && freeModule && typeof global == 'object' && global && global.Object && global;
16
+
17
+ var root = root = freeGlobal || ((freeWindow !== (this && this.window)) && freeWindow) || freeSelf || this;
18
+
19
+ // Because of build optimizers
20
+ if (typeof define === 'function' && define.amd) {
21
+ define(['./rx'], function (Rx, exports) {
22
+ return factory(root, exports, Rx);
23
+ });
24
+ } else if (typeof module === 'object' && module && module.exports === freeExports) {
25
+ module.exports = factory(root, module.exports, require('./rx'));
26
+ } else {
27
+ root.Rx = factory(root, {}, root.Rx);
28
+ }
33
29
  }.call(this, function (root, exp, Rx, undefined) {
34
30
 
35
31
  // References
@@ -37,6 +33,7 @@
37
33
  observableProto = Observable.prototype,
38
34
  CompositeDisposable = Rx.CompositeDisposable,
39
35
  AnonymousObservable = Rx.AnonymousObservable,
36
+ AbstractObserver = Rx.internals.AbstractObserver,
40
37
  disposableEmpty = Rx.Disposable.empty,
41
38
  isEqual = Rx.internals.isEqual,
42
39
  helpers = Rx.helpers,
@@ -48,12 +45,33 @@
48
45
  isPromise = helpers.isPromise,
49
46
  isArrayLike = helpers.isArrayLike,
50
47
  isIterable = helpers.isIterable,
48
+ inherits = Rx.internals.inherits,
51
49
  observableFromPromise = Observable.fromPromise,
52
50
  observableFrom = Observable.from,
53
51
  bindCallback = Rx.internals.bindCallback,
54
52
  EmptyError = Rx.EmptyError,
53
+ ObservableBase = Rx.ObservableBase,
55
54
  ArgumentOutOfRangeError = Rx.ArgumentOutOfRangeError;
56
55
 
56
+ var errorObj = {e: {}};
57
+ var tryCatchTarget;
58
+ function tryCatcher() {
59
+ try {
60
+ return tryCatchTarget.apply(this, arguments);
61
+ } catch (e) {
62
+ errorObj.e = e;
63
+ return errorObj;
64
+ }
65
+ }
66
+ function tryCatch(fn) {
67
+ if (!isFunction(fn)) { throw new TypeError('fn must be a function'); }
68
+ tryCatchTarget = fn;
69
+ return tryCatcher;
70
+ }
71
+ function thrower(e) {
72
+ throw e;
73
+ }
74
+
57
75
  function extremaBy(source, keySelector, comparer) {
58
76
  return new AnonymousObservable(function (o) {
59
77
  var hasValue = false, lastKey = null, list = [];
@@ -94,124 +112,181 @@
94
112
  return x[0];
95
113
  }
96
114
 
97
- /**
98
- * 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.
99
- * For aggregation behavior with incremental intermediate results, see Observable.scan.
100
- * @deprecated Use #reduce instead
101
- * @param {Mixed} [seed] The initial accumulator value.
102
- * @param {Function} accumulator An accumulator function to be invoked on each element.
103
- * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
104
- */
105
- observableProto.aggregate = function () {
106
- var hasSeed = false, accumulator, seed, source = this;
107
- if (arguments.length === 2) {
108
- hasSeed = true;
109
- seed = arguments[0];
110
- accumulator = arguments[1];
111
- } else {
112
- accumulator = arguments[0];
115
+ var ReduceObservable = (function(__super__) {
116
+ inherits(ReduceObservable, __super__);
117
+ function ReduceObservable(source, acc, hasSeed, seed) {
118
+ this.source = source;
119
+ this.acc = acc;
120
+ this.hasSeed = hasSeed;
121
+ this.seed = seed;
122
+ __super__.call(this);
113
123
  }
114
- return new AnonymousObservable(function (o) {
115
- var hasAccumulation, accumulation, hasValue;
116
- return source.subscribe (
117
- function (x) {
118
- !hasValue && (hasValue = true);
119
- try {
120
- if (hasAccumulation) {
121
- accumulation = accumulator(accumulation, x);
122
- } else {
123
- accumulation = hasSeed ? accumulator(seed, x) : x;
124
- hasAccumulation = true;
125
- }
126
- } catch (e) {
127
- return o.onError(e);
128
- }
129
- },
130
- function (e) { o.onError(e); },
131
- function () {
132
- hasValue && o.onNext(accumulation);
133
- !hasValue && hasSeed && o.onNext(seed);
134
- !hasValue && !hasSeed && o.onError(new EmptyError());
135
- o.onCompleted();
136
- }
137
- );
138
- }, source);
139
- };
124
+
125
+ ReduceObservable.prototype.subscribeCore = function(observer) {
126
+ return this.source.subscribe(new InnerObserver(observer,this));
127
+ };
128
+
129
+ function InnerObserver(o, parent) {
130
+ this.o = o;
131
+ this.acc = parent.acc;
132
+ this.hasSeed = parent.hasSeed;
133
+ this.seed = parent.seed;
134
+ this.hasAccumulation = false;
135
+ this.result = null;
136
+ this.hasValue = false;
137
+ this.isStopped = false;
138
+ }
139
+ InnerObserver.prototype.onNext = function (x) {
140
+ if (this.isStopped) { return; }
141
+ !this.hasValue && (this.hasValue = true);
142
+ if (this.hasAccumulation) {
143
+ this.result = tryCatch(this.acc)(this.result, x);
144
+ } else {
145
+ this.result = this.hasSeed ? tryCatch(this.acc)(this.seed, x) : x;
146
+ this.hasAccumulation = true;
147
+ }
148
+ if (this.result === errorObj) { this.o.onError(this.result.e); }
149
+ };
150
+ InnerObserver.prototype.onError = function (e) {
151
+ if (!this.isStopped) { this.isStopped = true; this.o.onError(e); }
152
+ };
153
+ InnerObserver.prototype.onCompleted = function () {
154
+ if (!this.isStopped) {
155
+ this.isStopped = true;
156
+ this.hasValue && this.o.onNext(this.result);
157
+ !this.hasValue && this.hasSeed && this.o.onNext(this.seed);
158
+ !this.hasValue && !this.hasSeed && this.o.onError(new EmptyError());
159
+ this.o.onCompleted();
160
+ }
161
+ };
162
+ InnerObserver.prototype.dispose = function () { this.isStopped = true; };
163
+ InnerObserver.prototype.fail = function(e) {
164
+ if (!this.isStopped) {
165
+ this.isStopped = true;
166
+ this.o.onError(e);
167
+ return true;
168
+ }
169
+ return false;
170
+ };
171
+
172
+ return ReduceObservable;
173
+ }(ObservableBase));
140
174
 
141
175
  /**
142
- * 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.
143
- * For aggregation behavior with incremental intermediate results, see Observable.scan.
144
- * @param {Function} accumulator An accumulator function to be invoked on each element.
145
- * @param {Any} [seed] The initial accumulator value.
146
- * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
147
- */
176
+ * 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.
177
+ * For aggregation behavior with incremental intermediate results, see Observable.scan.
178
+ * @param {Function} accumulator An accumulator function to be invoked on each element.
179
+ * @param {Any} [seed] The initial accumulator value.
180
+ * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
181
+ */
148
182
  observableProto.reduce = function (accumulator) {
149
- var hasSeed = false, seed, source = this;
183
+ var hasSeed = false;
150
184
  if (arguments.length === 2) {
151
185
  hasSeed = true;
152
- seed = arguments[1];
186
+ var seed = arguments[1];
153
187
  }
154
- return new AnonymousObservable(function (o) {
155
- var hasAccumulation, accumulation, hasValue;
156
- return source.subscribe (
157
- function (x) {
158
- !hasValue && (hasValue = true);
159
- try {
160
- if (hasAccumulation) {
161
- accumulation = accumulator(accumulation, x);
162
- } else {
163
- accumulation = hasSeed ? accumulator(seed, x) : x;
164
- hasAccumulation = true;
165
- }
166
- } catch (e) {
167
- return o.onError(e);
168
- }
169
- },
170
- function (e) { o.onError(e); },
171
- function () {
172
- hasValue && o.onNext(accumulation);
173
- !hasValue && hasSeed && o.onNext(seed);
174
- !hasValue && !hasSeed && o.onError(new EmptyError());
175
- o.onCompleted();
176
- }
177
- );
178
- }, source);
188
+ return new ReduceObservable(this, accumulator, hasSeed, seed);
179
189
  };
180
190
 
191
+ var SomeObserver = (function (__super__) {
192
+ inherits(SomeObserver, __super__);
193
+
194
+ function SomeObserver(o, fn, s) {
195
+ this._o = o;
196
+ this._fn = fn;
197
+ this._s = s;
198
+ this._i = 0;
199
+ __super__.call(this);
200
+ }
201
+
202
+ SomeObserver.prototype.next = function (x) {
203
+ var result = tryCatch(this._fn)(x, this._i++, this._s);
204
+ if (result === errorObj) { return this._o.onError(result.e); }
205
+ if (Boolean(result)) {
206
+ this._o.onNext(true);
207
+ this._o.onCompleted();
208
+ }
209
+ };
210
+ SomeObserver.prototype.error = function (e) { this._o.onError(e); };
211
+ SomeObserver.prototype.completed = function () {
212
+ this._o.onNext(false);
213
+ this._o.onCompleted();
214
+ };
215
+
216
+ return SomeObserver;
217
+ }(AbstractObserver));
218
+
181
219
  /**
182
220
  * Determines whether any element of an observable sequence satisfies a condition if present, else if any items are in the sequence.
183
221
  * @param {Function} [predicate] A function to test each element for a condition.
184
222
  * @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.
185
223
  */
186
224
  observableProto.some = function (predicate, thisArg) {
187
- var source = this;
188
- return predicate ?
189
- source.filter(predicate, thisArg).some() :
190
- new AnonymousObservable(function (observer) {
191
- return source.subscribe(function () {
192
- observer.onNext(true);
193
- observer.onCompleted();
194
- }, function (e) { observer.onError(e); }, function () {
195
- observer.onNext(false);
196
- observer.onCompleted();
197
- });
198
- }, source);
225
+ var source = this, fn = bindCallback(predicate, thisArg, 3);
226
+ return new AnonymousObservable(function (o) {
227
+ return source.subscribe(new SomeObserver(o, fn, source));
228
+ });
199
229
  };
200
230
 
201
- /** @deprecated use #some instead */
202
- observableProto.any = function () {
203
- //deprecate('any', 'some');
204
- return this.some.apply(this, arguments);
205
- };
231
+ var IsEmptyObserver = (function(__super__) {
232
+ inherits(IsEmptyObserver, __super__);
233
+ function IsEmptyObserver(o) {
234
+ this._o = o;
235
+ __super__.call(this);
236
+ }
237
+
238
+ IsEmptyObserver.prototype.next = function () {
239
+ this._o.onNext(false);
240
+ this._o.onCompleted();
241
+ };
242
+ IsEmptyObserver.prototype.error = function (e) { this._o.onError(e); };
243
+ IsEmptyObserver.prototype.completed = function () {
244
+ this._o.onNext(true);
245
+ this._o.onCompleted();
246
+ };
247
+
248
+ return IsEmptyObserver;
249
+ }(AbstractObserver));
206
250
 
207
251
  /**
208
252
  * Determines whether an observable sequence is empty.
209
253
  * @returns {Observable} An observable sequence containing a single element determining whether the source sequence is empty.
210
254
  */
211
255
  observableProto.isEmpty = function () {
212
- return this.any().map(not);
256
+ var source = this;
257
+ return new AnonymousObservable(function (o) {
258
+ return source.subscribe(new IsEmptyObserver(o));
259
+ }, source);
213
260
  };
214
261
 
262
+ var EveryObserver = (function (__super__) {
263
+ inherits(EveryObserver, __super__);
264
+
265
+ function EveryObserver(o, fn, s) {
266
+ this._o = o;
267
+ this._fn = fn;
268
+ this._s = s;
269
+ this._i = 0;
270
+ __super__.call(this);
271
+ }
272
+
273
+ EveryObserver.prototype.next = function (x) {
274
+ var result = tryCatch(this._fn)(x, this._i++, this._s);
275
+ if (result === errorObj) { return this._o.onError(result.e); }
276
+ if (!Boolean(result)) {
277
+ this._o.onNext(false);
278
+ this._o.onCompleted();
279
+ }
280
+ };
281
+ EveryObserver.prototype.error = function (e) { this._o.onError(e); };
282
+ EveryObserver.prototype.completed = function () {
283
+ this._o.onNext(true);
284
+ this._o.onCompleted();
285
+ };
286
+
287
+ return EveryObserver;
288
+ }(AbstractObserver));
289
+
215
290
  /**
216
291
  * Determines whether all elements of an observable sequence satisfy a condition.
217
292
  * @param {Function} [predicate] A function to test each element for a condition.
@@ -219,13 +294,10 @@
219
294
  * @returns {Observable} An observable sequence containing a single element determining whether all elements in the source sequence pass the test in the specified predicate.
220
295
  */
221
296
  observableProto.every = function (predicate, thisArg) {
222
- return this.filter(function (v) { return !predicate(v); }, thisArg).some().map(not);
223
- };
224
-
225
- /** @deprecated use #every instead */
226
- observableProto.all = function () {
227
- //deprecate('all', 'every');
228
- return this.every.apply(this, arguments);
297
+ var source = this, fn = bindCallback(predicate, thisArg, 3);
298
+ return new AnonymousObservable(function (o) {
299
+ return source.subscribe(new EveryObserver(o, fn, source));
300
+ }, this);
229
301
  };
230
302
 
231
303
  /**
@@ -381,6 +453,37 @@
381
453
  return this.maxBy(identity, comparer).map(function (x) { return firstOnly(x); });
382
454
  };
383
455
 
456
+ var AverageObserver = (function(__super__) {
457
+ inherits(AverageObserver, __super__);
458
+ function AverageObserver(o, fn, s) {
459
+ this._o = o;
460
+ this._fn = fn;
461
+ this._s = s;
462
+ this._c = 0;
463
+ this._t = 0;
464
+ __super__.call(this);
465
+ }
466
+
467
+ AverageObserver.prototype.next = function (x) {
468
+ if(this._fn) {
469
+ var r = tryCatch(this._fn)(x, this._c++, this._s);
470
+ if (r === errorObj) { return this._o.onError(r.e); }
471
+ this._t += r;
472
+ } else {
473
+ this._c++;
474
+ this._t += x;
475
+ }
476
+ };
477
+ AverageObserver.prototype.error = function (e) { this._o.onError(e); };
478
+ AverageObserver.prototype.completed = function () {
479
+ if (this._c === 0) { return this._o.onError(new EmptyError()); }
480
+ this._o.onNext(this._t / this._c);
481
+ this._o.onCompleted();
482
+ };
483
+
484
+ return AverageObserver;
485
+ }(AbstractObserver));
486
+
384
487
  /**
385
488
  * 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.
386
489
  * @param {Function} [selector] A transform function to apply to each element.
@@ -388,17 +491,13 @@
388
491
  * @returns {Observable} An observable sequence containing a single element with the average of the sequence of values.
389
492
  */
390
493
  observableProto.average = function (keySelector, thisArg) {
391
- return keySelector && isFunction(keySelector) ?
392
- this.map(keySelector, thisArg).average() :
393
- this.reduce(function (prev, cur) {
394
- return {
395
- sum: prev.sum + cur,
396
- count: prev.count + 1
397
- };
398
- }, {sum: 0, count: 0 }).map(function (s) {
399
- if (s.count === 0) { throw new EmptyError(); }
400
- return s.sum / s.count;
401
- });
494
+ var source = this, fn;
495
+ if (isFunction(keySelector)) {
496
+ fn = bindCallback(keySelector, thisArg, 3);
497
+ }
498
+ return new AnonymousObservable(function (o) {
499
+ return source.subscribe(new AverageObserver(o, fn, source));
500
+ }, source);
402
501
  };
403
502
 
404
503
  /**
@@ -489,53 +588,47 @@
489
588
  }, first);
490
589
  };
491
590
 
492
- function elementAtOrDefault(source, index, hasDefault, defaultValue) {
591
+ /**
592
+ * Returns the element at a specified index in a sequence or default value if not found.
593
+ * @param {Number} index The zero-based index of the element to retrieve.
594
+ * @param {Any} [defaultValue] The default value to use if elementAt does not find a value.
595
+ * @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence.
596
+ */
597
+ observableProto.elementAt = function (index, defaultValue) {
493
598
  if (index < 0) { throw new ArgumentOutOfRangeError(); }
599
+ var source = this;
494
600
  return new AnonymousObservable(function (o) {
495
601
  var i = index;
496
- return source.subscribe(function (x) {
497
- if (i-- === 0) {
498
- o.onNext(x);
499
- o.onCompleted();
500
- }
501
- }, function (e) { o.onError(e); }, function () {
502
- if (!hasDefault) {
503
- o.onError(new ArgumentOutOfRangeError());
504
- } else {
505
- o.onNext(defaultValue);
506
- o.onCompleted();
507
- }
602
+ return source.subscribe(
603
+ function (x) {
604
+ if (i-- === 0) {
605
+ o.onNext(x);
606
+ o.onCompleted();
607
+ }
608
+ },
609
+ function (e) { o.onError(e); },
610
+ function () {
611
+ if (defaultValue === undefined) {
612
+ o.onError(new ArgumentOutOfRangeError());
613
+ } else {
614
+ o.onNext(defaultValue);
615
+ o.onCompleted();
616
+ }
508
617
  });
509
618
  }, source);
510
- }
511
-
512
- /**
513
- * Returns the element at a specified index in a sequence.
514
- * @example
515
- * var res = source.elementAt(5);
516
- * @param {Number} index The zero-based index of the element to retrieve.
517
- * @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence.
518
- */
519
- observableProto.elementAt = function (index) {
520
- return elementAtOrDefault(this, index, false);
521
619
  };
522
620
 
523
621
  /**
524
- * Returns the element at a specified index in a sequence or a default value if the index is out of range.
525
- * @example
526
- * var res = source.elementAtOrDefault(5);
527
- * var res = source.elementAtOrDefault(5, 0);
528
- * @param {Number} index The zero-based index of the element to retrieve.
529
- * @param [defaultValue] The default value if the index is outside the bounds of the source sequence.
530
- * @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence, or a default value if the index is outside the bounds of the source sequence.
622
+ * 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.
623
+ * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
624
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
625
+ * @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.
531
626
  */
532
- observableProto.elementAtOrDefault = function (index, defaultValue) {
533
- return elementAtOrDefault(this, index, true, defaultValue);
534
- };
535
-
536
- function singleOrDefaultAsync(source, hasDefault, defaultValue) {
627
+ observableProto.single = function (predicate, thisArg) {
628
+ if (isFunction(predicate)) { return this.filter(predicate, thisArg).single(); }
629
+ var source = this;
537
630
  return new AnonymousObservable(function (o) {
538
- var value = defaultValue, seenValue = false;
631
+ var value, seenValue = false;
539
632
  return source.subscribe(function (x) {
540
633
  if (seenValue) {
541
634
  o.onError(new Error('Sequence contains more than one element'));
@@ -544,131 +637,121 @@
544
637
  seenValue = true;
545
638
  }
546
639
  }, function (e) { o.onError(e); }, function () {
547
- if (!seenValue && !hasDefault) {
548
- o.onError(new EmptyError());
549
- } else {
550
- o.onNext(value);
551
- o.onCompleted();
552
- }
640
+ o.onNext(value);
641
+ o.onCompleted();
553
642
  });
554
643
  }, source);
555
- }
556
-
557
- /**
558
- * 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.
559
- * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
560
- * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
561
- * @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.
562
- */
563
- observableProto.single = function (predicate, thisArg) {
564
- return predicate && isFunction(predicate) ?
565
- this.where(predicate, thisArg).single() :
566
- singleOrDefaultAsync(this, false);
567
644
  };
568
645
 
569
- /**
570
- * Returns the only element of an observable sequence that matches the predicate, or a default value if no such element exists; this method reports an exception if there is more than one element in the observable sequence.
571
- * @example
572
- * var res = res = source.singleOrDefault();
573
- * var res = res = source.singleOrDefault(function (x) { return x === 42; });
574
- * res = source.singleOrDefault(function (x) { return x === 42; }, 0);
575
- * res = source.singleOrDefault(null, 0);
576
- * @memberOf Observable#
577
- * @param {Function} predicate A predicate function to evaluate for elements in the source sequence.
578
- * @param [defaultValue] The default value if the index is outside the bounds of the source sequence.
579
- * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
580
- * @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
581
- */
582
- observableProto.singleOrDefault = function (predicate, defaultValue, thisArg) {
583
- return predicate && isFunction(predicate) ?
584
- this.filter(predicate, thisArg).singleOrDefault(null, defaultValue) :
585
- singleOrDefaultAsync(this, true, defaultValue);
586
- };
646
+ var FirstObserver = (function(__super__) {
647
+ inherits(FirstObserver, __super__);
648
+ function FirstObserver(o, obj, s) {
649
+ this._o = o;
650
+ this._obj = obj;
651
+ this._s = s;
652
+ this._i = 0;
653
+ __super__.call(this);
654
+ }
587
655
 
588
- function firstOrDefaultAsync(source, hasDefault, defaultValue) {
589
- return new AnonymousObservable(function (o) {
590
- return source.subscribe(function (x) {
591
- o.onNext(x);
592
- o.onCompleted();
593
- }, function (e) { o.onError(e); }, function () {
594
- if (!hasDefault) {
595
- o.onError(new EmptyError());
596
- } else {
597
- o.onNext(defaultValue);
598
- o.onCompleted();
656
+ FirstObserver.prototype.next = function (x) {
657
+ if (this._obj.predicate) {
658
+ var res = tryCatch(this._obj.predicate)(x, this._i++, this._s);
659
+ if (res === errorObj) { return this._o.onError(res.e); }
660
+ if (Boolean(res)) {
661
+ this._o.onNext(x);
662
+ this._o.onCompleted();
599
663
  }
600
- });
601
- }, source);
602
- }
664
+ } else if (!this._obj.predicate) {
665
+ this._o.onNext(x);
666
+ this._o.onCompleted();
667
+ }
668
+ };
669
+ FirstObserver.prototype.error = function (e) { this._o.onError(e); };
670
+ FirstObserver.prototype.completed = function () {
671
+ if (this._obj.defaultValue === undefined) {
672
+ this._o.onError(new EmptyError());
673
+ } else {
674
+ this._o.onNext(this._obj.defaultValue);
675
+ this._o.onCompleted();
676
+ }
677
+ };
678
+
679
+ return FirstObserver;
680
+ }(AbstractObserver));
603
681
 
604
682
  /**
605
683
  * Returns the first element of an observable sequence that satisfies the condition in the predicate if present else the first item in the sequence.
606
- * @example
607
- * var res = res = source.first();
608
- * var res = res = source.first(function (x) { return x > 3; });
609
- * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
610
- * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
611
684
  * @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.
612
685
  */
613
- observableProto.first = function (predicate, thisArg) {
614
- return predicate ?
615
- this.where(predicate, thisArg).first() :
616
- firstOrDefaultAsync(this, false);
617
- };
618
-
619
- /**
620
- * Returns the first element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
621
- * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
622
- * @param {Any} [defaultValue] The default value if no such element exists. If not specified, defaults to null.
623
- * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
624
- * @returns {Observable} Sequence containing the first element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
625
- */
626
- observableProto.firstOrDefault = function (predicate, defaultValue, thisArg) {
627
- return predicate ?
628
- this.where(predicate).firstOrDefault(null, defaultValue) :
629
- firstOrDefaultAsync(this, true, defaultValue);
630
- };
631
-
632
- function lastOrDefaultAsync(source, hasDefault, defaultValue) {
686
+ observableProto.first = function () {
687
+ var obj = {}, source = this;
688
+ if (typeof arguments[0] === 'object') {
689
+ obj = arguments[0];
690
+ } else {
691
+ obj = {
692
+ predicate: arguments[0],
693
+ thisArg: arguments[1],
694
+ defaultValue: arguments[2]
695
+ };
696
+ }
697
+ if (isFunction (obj.predicate)) {
698
+ var fn = obj.predicate;
699
+ obj.predicate = bindCallback(fn, obj.thisArg, 3);
700
+ }
633
701
  return new AnonymousObservable(function (o) {
634
- var value = defaultValue, seenValue = false;
635
- return source.subscribe(function (x) {
636
- value = x;
637
- seenValue = true;
638
- }, function (e) { o.onError(e); }, function () {
639
- if (!seenValue && !hasDefault) {
640
- o.onError(new EmptyError());
641
- } else {
642
- o.onNext(value);
643
- o.onCompleted();
644
- }
645
- });
702
+ return source.subscribe(new FirstObserver(o, obj, source));
646
703
  }, source);
647
- }
704
+ };
648
705
 
649
706
  /**
650
707
  * Returns the last element of an observable sequence that satisfies the condition in the predicate if specified, else the last element.
651
- * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
652
- * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
653
708
  * @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate.
654
709
  */
655
- observableProto.last = function (predicate, thisArg) {
656
- return predicate ?
657
- this.where(predicate, thisArg).last() :
658
- lastOrDefaultAsync(this, false);
659
- };
660
-
661
- /**
662
- * Returns the last element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
663
- * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
664
- * @param [defaultValue] The default value if no such element exists. If not specified, defaults to null.
665
- * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
666
- * @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
667
- */
668
- observableProto.lastOrDefault = function (predicate, defaultValue, thisArg) {
669
- return predicate ?
670
- this.where(predicate, thisArg).lastOrDefault(null, defaultValue) :
671
- lastOrDefaultAsync(this, true, defaultValue);
710
+ observableProto.last = function () {
711
+ var obj = {}, source = this;
712
+ if (typeof arguments[0] === 'object') {
713
+ obj = arguments[0];
714
+ } else {
715
+ obj = {
716
+ predicate: arguments[0],
717
+ thisArg: arguments[1],
718
+ defaultValue: arguments[2]
719
+ };
720
+ }
721
+ if (isFunction (obj.predicate)) {
722
+ var fn = obj.predicate;
723
+ obj.predicate = bindCallback(fn, obj.thisArg, 3);
724
+ }
725
+ return new AnonymousObservable(function (o) {
726
+ var value, seenValue = false, i = 0;
727
+ return source.subscribe(
728
+ function (x) {
729
+ if (obj.predicate) {
730
+ var res = tryCatch(obj.predicate)(x, i++, source);
731
+ if (res === errorObj) { return o.onError(res.e); }
732
+ if (res) {
733
+ seenValue = true;
734
+ value = x;
735
+ }
736
+ } else if (!obj.predicate) {
737
+ seenValue = true;
738
+ value = x;
739
+ }
740
+ },
741
+ function (e) { o.onError(e); },
742
+ function () {
743
+ if (seenValue) {
744
+ o.onNext(value);
745
+ o.onCompleted();
746
+ }
747
+ else if (obj.defaultValue === undefined) {
748
+ o.onError(new EmptyError());
749
+ } else {
750
+ o.onNext(obj.defaultValue);
751
+ o.onCompleted();
752
+ }
753
+ });
754
+ }, source);
672
755
  };
673
756
 
674
757
  function findValue (source, predicate, thisArg, yieldIndex) {
@@ -777,5 +860,5 @@
777
860
  }, source);
778
861
  };
779
862
 
780
- return Rx;
863
+ return Rx;
781
864
  }));