rxjs-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +19 -0
  4. data/README.md +14 -0
  5. data/lib/rxjs/rails/engine.rb +7 -0
  6. data/lib/rxjs/rails/railtie.rb +7 -0
  7. data/lib/rxjs/rails/version.rb +7 -0
  8. data/lib/rxjs/rails.rb +8 -0
  9. data/lib/rxjs.rb +1 -0
  10. data/rxjs-rails.gemspec +22 -0
  11. data/vendor/assets/javascripts/rx.aggregates.js +687 -0
  12. data/vendor/assets/javascripts/rx.aggregates.min.js +1 -0
  13. data/vendor/assets/javascripts/rx.async.compat.js +376 -0
  14. data/vendor/assets/javascripts/rx.async.compat.min.js +1 -0
  15. data/vendor/assets/javascripts/rx.async.js +306 -0
  16. data/vendor/assets/javascripts/rx.async.min.js +1 -0
  17. data/vendor/assets/javascripts/rx.binding.js +561 -0
  18. data/vendor/assets/javascripts/rx.binding.min.js +1 -0
  19. data/vendor/assets/javascripts/rx.coincidence.js +691 -0
  20. data/vendor/assets/javascripts/rx.coincidence.min.js +1 -0
  21. data/vendor/assets/javascripts/rx.compat.js +4351 -0
  22. data/vendor/assets/javascripts/rx.compat.min.js +2 -0
  23. data/vendor/assets/javascripts/rx.experimental.js +453 -0
  24. data/vendor/assets/javascripts/rx.experimental.min.js +1 -0
  25. data/vendor/assets/javascripts/rx.joinpatterns.js +418 -0
  26. data/vendor/assets/javascripts/rx.joinpatterns.min.js +1 -0
  27. data/vendor/assets/javascripts/rx.lite.compat.js +5188 -0
  28. data/vendor/assets/javascripts/rx.lite.compat.min.js +2 -0
  29. data/vendor/assets/javascripts/rx.lite.js +5000 -0
  30. data/vendor/assets/javascripts/rx.lite.min.js +2 -0
  31. data/vendor/assets/javascripts/rx.min.js +2 -0
  32. data/vendor/assets/javascripts/rx.node.js +143 -0
  33. data/vendor/assets/javascripts/rx.testing.js +503 -0
  34. data/vendor/assets/javascripts/rx.testing.min.js +1 -0
  35. data/vendor/assets/javascripts/rx.time.js +1166 -0
  36. data/vendor/assets/javascripts/rx.time.min.js +1 -0
  37. data/vendor/assets/javascripts/rx.virtualtime.js +336 -0
  38. data/vendor/assets/javascripts/rx.virtualtime.min.js +1 -0
  39. metadata +101 -0
@@ -0,0 +1,687 @@
1
+ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
+
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
+ }
22
+
23
+ // Because of build optimizers
24
+ if (typeof define === 'function' && define.amd) {
25
+ define(['rx', 'exports'], function (Rx, exports) {
26
+ root.Rx = factory(root, exports, Rx);
27
+ return root.Rx;
28
+ });
29
+ } else if (typeof module === 'object' && module && module.exports === freeExports) {
30
+ module.exports = factory(root, module.exports, require('./rx'));
31
+ } else {
32
+ root.Rx = factory(root, {}, root.Rx);
33
+ }
34
+ }.call(this, function (root, exp, Rx, undefined) {
35
+
36
+ // References
37
+ var Observable = Rx.Observable,
38
+ observableProto = Observable.prototype,
39
+ CompositeDisposable = Rx.CompositeDisposable,
40
+ AnonymousObservable = Rx.Internals.AnonymousObservable,
41
+ isEqual = Rx.Internals.isEqual;
42
+
43
+ // Defaults
44
+ var argumentOutOfRange = 'Argument out of range';
45
+ var sequenceContainsNoElements = "Sequence contains no elements.";
46
+ function defaultComparer(x, y) { return isEqual(x, y); }
47
+ function identity(x) { return x; }
48
+ function subComparer(x, y) {
49
+ if (x > y) {
50
+ return 1;
51
+ }
52
+ if (x < y) {
53
+ return -1
54
+ }
55
+ return 0;
56
+ }
57
+
58
+ function extremaBy(source, keySelector, comparer) {
59
+ return new AnonymousObservable(function (observer) {
60
+ var hasValue = false, lastKey = null, list = [];
61
+ return source.subscribe(function (x) {
62
+ var comparison, key;
63
+ try {
64
+ key = keySelector(x);
65
+ } catch (ex) {
66
+ observer.onError(ex);
67
+ return;
68
+ }
69
+ comparison = 0;
70
+ if (!hasValue) {
71
+ hasValue = true;
72
+ lastKey = key;
73
+ } else {
74
+ try {
75
+ comparison = comparer(key, lastKey);
76
+ } catch (ex1) {
77
+ observer.onError(ex1);
78
+ return;
79
+ }
80
+ }
81
+ if (comparison > 0) {
82
+ lastKey = key;
83
+ list = [];
84
+ }
85
+ if (comparison >= 0) {
86
+ list.push(x);
87
+ }
88
+ }, observer.onError.bind(observer), function () {
89
+ observer.onNext(list);
90
+ observer.onCompleted();
91
+ });
92
+ });
93
+ }
94
+
95
+ function firstOnly(x) {
96
+ if (x.length === 0) {
97
+ throw new Error(sequenceContainsNoElements);
98
+ }
99
+ return x[0];
100
+ }
101
+
102
+ /**
103
+ * 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.
104
+ * For aggregation behavior with incremental intermediate results, see Observable.scan.
105
+ * @example
106
+ * 1 - res = source.aggregate(function (acc, x) { return acc + x; });
107
+ * 2 - res = source.aggregate(0, function (acc, x) { return acc + x; });
108
+ * @param {Mixed} [seed] The initial accumulator value.
109
+ * @param {Function} accumulator An accumulator function to be invoked on each element.
110
+ * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
111
+ */
112
+ observableProto.aggregate = function () {
113
+ var seed, hasSeed, accumulator;
114
+ if (arguments.length === 2) {
115
+ seed = arguments[0];
116
+ hasSeed = true;
117
+ accumulator = arguments[1];
118
+ } else {
119
+ accumulator = arguments[0];
120
+ }
121
+ return hasSeed ? this.scan(seed, accumulator).startWith(seed).finalValue() : this.scan(accumulator).finalValue();
122
+ };
123
+
124
+ /**
125
+ * 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.
126
+ * For aggregation behavior with incremental intermediate results, see Observable.scan.
127
+ * @example
128
+ * 1 - res = source.reduce(function (acc, x) { return acc + x; });
129
+ * 2 - res = source.reduce(function (acc, x) { return acc + x; }, 0);
130
+ * @param {Function} accumulator An accumulator function to be invoked on each element.
131
+ * @param {Any} [seed] The initial accumulator value.
132
+ * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
133
+ */
134
+ observableProto.reduce = function (accumulator) {
135
+ var seed, hasSeed;
136
+ if (arguments.length === 2) {
137
+ hasSeed = true;
138
+ seed = arguments[1];
139
+ }
140
+ return hasSeed ? this.scan(seed, accumulator).startWith(seed).finalValue() : this.scan(accumulator).finalValue();
141
+ };
142
+
143
+ /**
144
+ * Determines whether any element of an observable sequence satisfies a condition if present, else if any items are in the sequence.
145
+ * @example
146
+ * var result = source.any();
147
+ * var result = source.any(function (x) { return x > 3; });
148
+ * @param {Function} [predicate] A function to test each element for a condition.
149
+ * @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.
150
+ */
151
+ observableProto.some = observableProto.any = function (predicate, thisArg) {
152
+ var source = this;
153
+ return predicate ?
154
+ source.where(predicate, thisArg).any() :
155
+ new AnonymousObservable(function (observer) {
156
+ return source.subscribe(function () {
157
+ observer.onNext(true);
158
+ observer.onCompleted();
159
+ }, observer.onError.bind(observer), function () {
160
+ observer.onNext(false);
161
+ observer.onCompleted();
162
+ });
163
+ });
164
+ };
165
+
166
+ /**
167
+ * Determines whether an observable sequence is empty.
168
+ *
169
+ * @memberOf Observable#
170
+ * @returns {Observable} An observable sequence containing a single element determining whether the source sequence is empty.
171
+ */
172
+ observableProto.isEmpty = function () {
173
+ return this.any().select(function (b) { return !b; });
174
+ };
175
+
176
+ /**
177
+ * Determines whether all elements of an observable sequence satisfy a condition.
178
+ *
179
+ * 1 - res = source.all(function (value) { return value.length > 3; });
180
+ * @memberOf Observable#
181
+ * @param {Function} [predicate] A function to test each element for a condition.
182
+ * @param {Any} [thisArg] Object to use as this when executing callback.
183
+ * @returns {Observable} An observable sequence containing a single element determining whether all elements in the source sequence pass the test in the specified predicate.
184
+ */
185
+ observableProto.every = observableProto.all = function (predicate, thisArg) {
186
+ return this.where(function (v) {
187
+ return !predicate(v);
188
+ }, thisArg).any().select(function (b) {
189
+ return !b;
190
+ });
191
+ };
192
+
193
+ /**
194
+ * Determines whether an observable sequence contains a specified element with an optional equality comparer.
195
+ * @example
196
+ * 1 - res = source.contains(42);
197
+ * 2 - res = source.contains({ value: 42 }, function (x, y) { return x.value === y.value; });
198
+ * @param value The value to locate in the source sequence.
199
+ * @param {Function} [comparer] An equality comparer to compare elements.
200
+ * @returns {Observable} An observable sequence containing a single element determining whether the source sequence contains an element that has the specified value.
201
+ */
202
+ observableProto.contains = function (value, comparer) {
203
+ comparer || (comparer = defaultComparer);
204
+ return this.where(function (v) {
205
+ return comparer(v, value);
206
+ }).any();
207
+ };
208
+
209
+ /**
210
+ * 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.
211
+ * @example
212
+ * res = source.count();
213
+ * res = source.count(function (x) { return x > 3; });
214
+ * @param {Function} [predicate]A function to test each element for a condition.
215
+ * @param {Any} [thisArg] Object to use as this when executing callback.
216
+ * @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.
217
+ */
218
+ observableProto.count = function (predicate, thisArg) {
219
+ return predicate ?
220
+ this.where(predicate, thisArg).count() :
221
+ this.aggregate(0, function (count) {
222
+ return count + 1;
223
+ });
224
+ };
225
+
226
+ /**
227
+ * 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.
228
+ * @example
229
+ * var res = source.sum();
230
+ * var res = source.sum(function (x) { return x.value; });
231
+ * @param {Function} [selector] A transform function to apply to each element.
232
+ * @param {Any} [thisArg] Object to use as this when executing callback.
233
+ * @returns {Observable} An observable sequence containing a single element with the sum of the values in the source sequence.
234
+ */
235
+ observableProto.sum = function (keySelector, thisArg) {
236
+ return keySelector ?
237
+ this.select(keySelector, thisArg).sum() :
238
+ this.aggregate(0, function (prev, curr) {
239
+ return prev + curr;
240
+ });
241
+ };
242
+
243
+ /**
244
+ * Returns the elements in an observable sequence with the minimum key value according to the specified comparer.
245
+ * @example
246
+ * var res = source.minBy(function (x) { return x.value; });
247
+ * var res = source.minBy(function (x) { return x.value; }, function (x, y) { return x - y; });
248
+ * @param {Function} keySelector Key selector function.
249
+ * @param {Function} [comparer] Comparer used to compare key values.
250
+ * @returns {Observable} An observable sequence containing a list of zero or more elements that have a minimum key value.
251
+ */
252
+ observableProto.minBy = function (keySelector, comparer) {
253
+ comparer || (comparer = subComparer);
254
+ return extremaBy(this, keySelector, function (x, y) {
255
+ return comparer(x, y) * -1;
256
+ });
257
+ };
258
+
259
+ /**
260
+ * Returns the minimum element in an observable sequence according to the optional comparer else a default greater than less than check.
261
+ * @example
262
+ * var res = source.min();
263
+ * var res = source.min(function (x, y) { return x.value - y.value; });
264
+ * @param {Function} [comparer] Comparer used to compare elements.
265
+ * @returns {Observable} An observable sequence containing a single element with the minimum element in the source sequence.
266
+ */
267
+ observableProto.min = function (comparer) {
268
+ return this.minBy(identity, comparer).select(function (x) {
269
+ return firstOnly(x);
270
+ });
271
+ };
272
+
273
+ /**
274
+ * Returns the elements in an observable sequence with the maximum key value according to the specified comparer.
275
+ * @example
276
+ * var res = source.maxBy(function (x) { return x.value; });
277
+ * var res = source.maxBy(function (x) { return x.value; }, function (x, y) { return x - y;; });
278
+ * @param {Function} keySelector Key selector function.
279
+ * @param {Function} [comparer] Comparer used to compare key values.
280
+ * @returns {Observable} An observable sequence containing a list of zero or more elements that have a maximum key value.
281
+ */
282
+ observableProto.maxBy = function (keySelector, comparer) {
283
+ comparer || (comparer = subComparer);
284
+ return extremaBy(this, keySelector, comparer);
285
+ };
286
+
287
+ /**
288
+ * Returns the maximum value in an observable sequence according to the specified comparer.
289
+ * @example
290
+ * var res = source.max();
291
+ * var res = source.max(function (x, y) { return x.value - y.value; });
292
+ * @param {Function} [comparer] Comparer used to compare elements.
293
+ * @returns {Observable} An observable sequence containing a single element with the maximum element in the source sequence.
294
+ */
295
+ observableProto.max = function (comparer) {
296
+ return this.maxBy(identity, comparer).select(function (x) {
297
+ return firstOnly(x);
298
+ });
299
+ };
300
+
301
+ /**
302
+ * 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.
303
+ * @example
304
+ * var res = res = source.average();
305
+ * var res = res = source.average(function (x) { return x.value; });
306
+ * @param {Function} [selector] A transform function to apply to each element.
307
+ * @param {Any} [thisArg] Object to use as this when executing callback.
308
+ * @returns {Observable} An observable sequence containing a single element with the average of the sequence of values.
309
+ */
310
+ observableProto.average = function (keySelector, thisArg) {
311
+ return keySelector ?
312
+ this.select(keySelector, thisArg).average() :
313
+ this.scan({
314
+ sum: 0,
315
+ count: 0
316
+ }, function (prev, cur) {
317
+ return {
318
+ sum: prev.sum + cur,
319
+ count: prev.count + 1
320
+ };
321
+ }).finalValue().select(function (s) {
322
+ if (s.count === 0) {
323
+ throw new Error('The input sequence was empty');
324
+ }
325
+ return s.sum / s.count;
326
+ });
327
+ };
328
+
329
+ function sequenceEqualArray(first, second, comparer) {
330
+ return new AnonymousObservable(function (observer) {
331
+ var count = 0, len = second.length;
332
+ return first.subscribe(function (value) {
333
+ var equal = false;
334
+ try {
335
+ if (count < len) {
336
+ equal = comparer(value, second[count++]);
337
+ }
338
+ } catch (e) {
339
+ observer.onError(e);
340
+ return;
341
+ }
342
+ if (!equal) {
343
+ observer.onNext(false);
344
+ observer.onCompleted();
345
+ }
346
+ }, observer.onError.bind(observer), function () {
347
+ observer.onNext(count === len);
348
+ observer.onCompleted();
349
+ });
350
+ });
351
+ }
352
+
353
+ /**
354
+ * Determines whether two sequences are equal by comparing the elements pairwise using a specified equality comparer.
355
+ *
356
+ * @example
357
+ * var res = res = source.sequenceEqual([1,2,3]);
358
+ * var res = res = source.sequenceEqual([{ value: 42 }], function (x, y) { return x.value === y.value; });
359
+ * 3 - res = source.sequenceEqual(Rx.Observable.returnValue(42));
360
+ * 4 - res = source.sequenceEqual(Rx.Observable.returnValue({ value: 42 }), function (x, y) { return x.value === y.value; });
361
+ * @param {Observable} second Second observable sequence or array to compare.
362
+ * @param {Function} [comparer] Comparer used to compare elements of both sequences.
363
+ * @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.
364
+ */
365
+ observableProto.sequenceEqual = function (second, comparer) {
366
+ var first = this;
367
+ comparer || (comparer = defaultComparer);
368
+ if (Array.isArray(second)) {
369
+ return sequenceEqualArray(first, second, comparer);
370
+ }
371
+ return new AnonymousObservable(function (observer) {
372
+ var donel = false, doner = false, ql = [], qr = [];
373
+ var subscription1 = first.subscribe(function (x) {
374
+ var equal, v;
375
+ if (qr.length > 0) {
376
+ v = qr.shift();
377
+ try {
378
+ equal = comparer(v, x);
379
+ } catch (e) {
380
+ observer.onError(e);
381
+ return;
382
+ }
383
+ if (!equal) {
384
+ observer.onNext(false);
385
+ observer.onCompleted();
386
+ }
387
+ } else if (doner) {
388
+ observer.onNext(false);
389
+ observer.onCompleted();
390
+ } else {
391
+ ql.push(x);
392
+ }
393
+ }, observer.onError.bind(observer), function () {
394
+ donel = true;
395
+ if (ql.length === 0) {
396
+ if (qr.length > 0) {
397
+ observer.onNext(false);
398
+ observer.onCompleted();
399
+ } else if (doner) {
400
+ observer.onNext(true);
401
+ observer.onCompleted();
402
+ }
403
+ }
404
+ });
405
+ var subscription2 = second.subscribe(function (x) {
406
+ var equal, v;
407
+ if (ql.length > 0) {
408
+ v = ql.shift();
409
+ try {
410
+ equal = comparer(v, x);
411
+ } catch (exception) {
412
+ observer.onError(exception);
413
+ return;
414
+ }
415
+ if (!equal) {
416
+ observer.onNext(false);
417
+ observer.onCompleted();
418
+ }
419
+ } else if (donel) {
420
+ observer.onNext(false);
421
+ observer.onCompleted();
422
+ } else {
423
+ qr.push(x);
424
+ }
425
+ }, observer.onError.bind(observer), function () {
426
+ doner = true;
427
+ if (qr.length === 0) {
428
+ if (ql.length > 0) {
429
+ observer.onNext(false);
430
+ observer.onCompleted();
431
+ } else if (donel) {
432
+ observer.onNext(true);
433
+ observer.onCompleted();
434
+ }
435
+ }
436
+ });
437
+ return new CompositeDisposable(subscription1, subscription2);
438
+ });
439
+ };
440
+
441
+ function elementAtOrDefault(source, index, hasDefault, defaultValue) {
442
+ if (index < 0) {
443
+ throw new Error(argumentOutOfRange);
444
+ }
445
+ return new AnonymousObservable(function (observer) {
446
+ var i = index;
447
+ return source.subscribe(function (x) {
448
+ if (i === 0) {
449
+ observer.onNext(x);
450
+ observer.onCompleted();
451
+ }
452
+ i--;
453
+ }, observer.onError.bind(observer), function () {
454
+ if (!hasDefault) {
455
+ observer.onError(new Error(argumentOutOfRange));
456
+ } else {
457
+ observer.onNext(defaultValue);
458
+ observer.onCompleted();
459
+ }
460
+ });
461
+ });
462
+ }
463
+
464
+ /**
465
+ * Returns the element at a specified index in a sequence.
466
+ * @example
467
+ * var res = source.elementAt(5);
468
+ * @param {Number} index The zero-based index of the element to retrieve.
469
+ * @returns {Observable} An observable sequence that produces the element at the specified position in the source sequence.
470
+ */
471
+ observableProto.elementAt = function (index) {
472
+ return elementAtOrDefault(this, index, false);
473
+ };
474
+
475
+ /**
476
+ * Returns the element at a specified index in a sequence or a default value if the index is out of range.
477
+ * @example
478
+ * var res = source.elementAtOrDefault(5);
479
+ * var res = source.elementAtOrDefault(5, 0);
480
+ * @param {Number} index The zero-based index of the element to retrieve.
481
+ * @param [defaultValue] The default value if the index is outside the bounds of the source sequence.
482
+ * @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.
483
+ */
484
+ observableProto.elementAtOrDefault = function (index, defaultValue) {
485
+ return elementAtOrDefault(this, index, true, defaultValue);
486
+ };
487
+
488
+ function singleOrDefaultAsync(source, hasDefault, defaultValue) {
489
+ return new AnonymousObservable(function (observer) {
490
+ var value = defaultValue, seenValue = false;
491
+ return source.subscribe(function (x) {
492
+ if (seenValue) {
493
+ observer.onError(new Error('Sequence contains more than one element'));
494
+ } else {
495
+ value = x;
496
+ seenValue = true;
497
+ }
498
+ }, observer.onError.bind(observer), function () {
499
+ if (!seenValue && !hasDefault) {
500
+ observer.onError(new Error(sequenceContainsNoElements));
501
+ } else {
502
+ observer.onNext(value);
503
+ observer.onCompleted();
504
+ }
505
+ });
506
+ });
507
+ }
508
+
509
+ /**
510
+ * 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.
511
+ * @example
512
+ * var res = res = source.single();
513
+ * var res = res = source.single(function (x) { return x === 42; });
514
+ * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
515
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
516
+ * @returns {Observable} Sequence containing the single element in the observable sequence that satisfies the condition in the predicate.
517
+ */
518
+ observableProto.single = function (predicate, thisArg) {
519
+ return predicate ?
520
+ this.where(predicate, thisArg).single() :
521
+ singleOrDefaultAsync(this, false);
522
+ };
523
+
524
+ /**
525
+ * 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.
526
+ * @example
527
+ * var res = res = source.singleOrDefault();
528
+ * var res = res = source.singleOrDefault(function (x) { return x === 42; });
529
+ * res = source.singleOrDefault(function (x) { return x === 42; }, 0);
530
+ * res = source.singleOrDefault(null, 0);
531
+ * @memberOf Observable#
532
+ * @param {Function} predicate A predicate function to evaluate for elements in the source sequence.
533
+ * @param [defaultValue] The default value if the index is outside the bounds of the source sequence.
534
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
535
+ * @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.
536
+ */
537
+ observableProto.singleOrDefault = function (predicate, defaultValue, thisArg) {
538
+ return predicate?
539
+ this.where(predicate, thisArg).singleOrDefault(null, defaultValue) :
540
+ singleOrDefaultAsync(this, true, defaultValue)
541
+ };
542
+ function firstOrDefaultAsync(source, hasDefault, defaultValue) {
543
+ return new AnonymousObservable(function (observer) {
544
+ return source.subscribe(function (x) {
545
+ observer.onNext(x);
546
+ observer.onCompleted();
547
+ }, observer.onError.bind(observer), function () {
548
+ if (!hasDefault) {
549
+ observer.onError(new Error(sequenceContainsNoElements));
550
+ } else {
551
+ observer.onNext(defaultValue);
552
+ observer.onCompleted();
553
+ }
554
+ });
555
+ });
556
+ }
557
+
558
+ /**
559
+ * Returns the first element of an observable sequence that satisfies the condition in the predicate if present else the first item in the sequence.
560
+ * @example
561
+ * var res = res = source.first();
562
+ * var res = res = source.first(function (x) { return x > 3; });
563
+ * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
564
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
565
+ * @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.
566
+ */
567
+ observableProto.first = function (predicate, thisArg) {
568
+ return predicate ?
569
+ this.where(predicate, thisArg).first() :
570
+ firstOrDefaultAsync(this, false);
571
+ };
572
+
573
+ /**
574
+ * Returns the first element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
575
+ * @example
576
+ * var res = res = source.firstOrDefault();
577
+ * var res = res = source.firstOrDefault(function (x) { return x > 3; });
578
+ * var res = source.firstOrDefault(function (x) { return x > 3; }, 0);
579
+ * var res = source.firstOrDefault(null, 0);
580
+ * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
581
+ * @param {Any} [defaultValue] The default value if no such element exists. If not specified, defaults to null.
582
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
583
+ * @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.
584
+ */
585
+ observableProto.firstOrDefault = function (predicate, defaultValue, thisArg) {
586
+ return predicate ?
587
+ this.where(predicate).firstOrDefault(null, defaultValue) :
588
+ firstOrDefaultAsync(this, true, defaultValue);
589
+ };
590
+
591
+ function lastOrDefaultAsync(source, hasDefault, defaultValue) {
592
+ return new AnonymousObservable(function (observer) {
593
+ var value = defaultValue, seenValue = false;
594
+ return source.subscribe(function (x) {
595
+ value = x;
596
+ seenValue = true;
597
+ }, observer.onError.bind(observer), function () {
598
+ if (!seenValue && !hasDefault) {
599
+ observer.onError(new Error(sequenceContainsNoElements));
600
+ } else {
601
+ observer.onNext(value);
602
+ observer.onCompleted();
603
+ }
604
+ });
605
+ });
606
+ }
607
+
608
+ /**
609
+ * Returns the last element of an observable sequence that satisfies the condition in the predicate if specified, else the last element.
610
+ * @example
611
+ * var res = source.last();
612
+ * var res = source.last(function (x) { return x > 3; });
613
+ * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
614
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
615
+ * @returns {Observable} Sequence containing the last element in the observable sequence that satisfies the condition in the predicate.
616
+ */
617
+ observableProto.last = function (predicate, thisArg) {
618
+ return predicate ?
619
+ this.where(predicate, thisArg).last() :
620
+ lastOrDefaultAsync(this, false);
621
+ };
622
+
623
+ /**
624
+ * Returns the last element of an observable sequence that satisfies the condition in the predicate, or a default value if no such element exists.
625
+ * @example
626
+ * var res = source.lastOrDefault();
627
+ * var res = source.lastOrDefault(function (x) { return x > 3; });
628
+ * var res = source.lastOrDefault(function (x) { return x > 3; }, 0);
629
+ * var res = source.lastOrDefault(null, 0);
630
+ * @param {Function} [predicate] A predicate function to evaluate for elements in the source sequence.
631
+ * @param [defaultValue] The default value if no such element exists. If not specified, defaults to null.
632
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
633
+ * @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.
634
+ */
635
+ observableProto.lastOrDefault = function (predicate, defaultValue, thisArg) {
636
+ return predicate ?
637
+ this.where(predicate, thisArg).lastOrDefault(null, defaultValue) :
638
+ lastOrDefaultAsync(this, true, defaultValue);
639
+ };
640
+
641
+ function findValue (source, predicate, thisArg, yieldIndex) {
642
+ return new AnonymousObservable(function (observer) {
643
+ var i = 0;
644
+ return source.subscribe(function (x) {
645
+ var shouldRun;
646
+ try {
647
+ shouldRun = predicate.call(thisArg, x, i, source);
648
+ } catch(e) {
649
+ observer.onError(e);
650
+ return;
651
+ }
652
+ if (shouldRun) {
653
+ observer.onNext(yieldIndex ? i : x);
654
+ observer.onCompleted();
655
+ } else {
656
+ i++;
657
+ }
658
+ }, observer.onError.bind(observer), function () {
659
+ observer.onNext(yieldIndex ? -1 : undefined);
660
+ observer.onCompleted();
661
+ });
662
+ });
663
+ }
664
+
665
+ /**
666
+ * Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Observable sequence.
667
+ * @param {Function} predicate The predicate that defines the conditions of the element to search for.
668
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
669
+ * @returns {Observable} An Observable sequence with the first element that matches the conditions defined by the specified predicate, if found; otherwise, undefined.
670
+ */
671
+ observableProto.find = function (predicate, thisArg) {
672
+ return findValue(this, predicate, thisArg, false);
673
+ };
674
+
675
+ /**
676
+ * Searches for an element that matches the conditions defined by the specified predicate, and returns
677
+ * an Observable sequence with the zero-based index of the first occurrence within the entire Observable sequence.
678
+ * @param {Function} predicate The predicate that defines the conditions of the element to search for.
679
+ * @param {Any} [thisArg] Object to use as `this` when executing the predicate.
680
+ * @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.
681
+ */
682
+ observableProto.findIndex = function (predicate, thisArg) {
683
+ return findValue(this, predicate, thisArg, true);
684
+ };
685
+
686
+ return Rx;
687
+ }));