rxjs-rails 2.2.27 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +0 -3
  3. data/lib/rxjs/rails/version.rb +1 -1
  4. data/vendor/assets/javascripts/rx.aggregates.min.js +1 -1
  5. data/vendor/assets/javascripts/rx.all.compat.js +1545 -1441
  6. data/vendor/assets/javascripts/rx.all.compat.min.js +3 -3
  7. data/vendor/assets/javascripts/rx.all.js +1545 -1441
  8. data/vendor/assets/javascripts/rx.all.min.js +3 -3
  9. data/vendor/assets/javascripts/rx.async.compat.js +149 -152
  10. data/vendor/assets/javascripts/rx.async.compat.min.js +1 -1
  11. data/vendor/assets/javascripts/rx.async.js +140 -143
  12. data/vendor/assets/javascripts/rx.async.min.js +1 -1
  13. data/vendor/assets/javascripts/rx.backpressure.min.js +1 -1
  14. data/vendor/assets/javascripts/rx.binding.js +18 -18
  15. data/vendor/assets/javascripts/rx.binding.min.js +1 -1
  16. data/vendor/assets/javascripts/rx.coincidence.min.js +1 -1
  17. data/vendor/assets/javascripts/rx.compat.js +490 -339
  18. data/vendor/assets/javascripts/rx.compat.min.js +2 -2
  19. data/vendor/assets/javascripts/rx.core.compat.js +109 -141
  20. data/vendor/assets/javascripts/rx.core.compat.min.js +1 -1
  21. data/vendor/assets/javascripts/rx.core.js +109 -141
  22. data/vendor/assets/javascripts/rx.core.min.js +1 -1
  23. data/vendor/assets/javascripts/rx.experimental.js +129 -136
  24. data/vendor/assets/javascripts/rx.experimental.min.js +1 -1
  25. data/vendor/assets/javascripts/rx.joinpatterns.js +59 -59
  26. data/vendor/assets/javascripts/rx.joinpatterns.min.js +1 -1
  27. data/vendor/assets/javascripts/rx.js +490 -339
  28. data/vendor/assets/javascripts/rx.lite.compat.js +1099 -954
  29. data/vendor/assets/javascripts/rx.lite.compat.min.js +2 -2
  30. data/vendor/assets/javascripts/rx.lite.extras.min.js +1 -1
  31. data/vendor/assets/javascripts/rx.lite.js +1099 -954
  32. data/vendor/assets/javascripts/rx.lite.min.js +2 -2
  33. data/vendor/assets/javascripts/rx.min.js +2 -2
  34. data/vendor/assets/javascripts/rx.testing.min.js +1 -1
  35. data/vendor/assets/javascripts/rx.time.js +715 -747
  36. data/vendor/assets/javascripts/rx.time.min.js +1 -1
  37. data/vendor/assets/javascripts/rx.virtualtime.min.js +1 -1
  38. metadata +11 -10
@@ -31,6 +31,8 @@
31
31
 
32
32
  // Defaults
33
33
  var noop = Rx.helpers.noop = function () { },
34
+ notDefined = Rx.helpers.notDefined = function (x) { return typeof x === 'undefined'; },
35
+ isScheduler = Rx.helpers.isScheduler = function (x) { return x instanceof Rx.Scheduler; },
34
36
  identity = Rx.helpers.identity = function (x) { return x; },
35
37
  pluck = Rx.helpers.pluck = function (property) { return function (x) { return x[property]; }; },
36
38
  just = Rx.helpers.just = function (value) { return function () { return value; }; },
@@ -50,14 +52,13 @@
50
52
  function checkDisposed() { if (this.isDisposed) { throw new Error(objectDisposed); } }
51
53
 
52
54
  // Shim in iterator support
53
- var $iterator$ = (typeof Symbol === 'object' && Symbol.iterator) ||
55
+ var $iterator$ = (typeof Symbol === 'function' && Symbol.iterator) ||
54
56
  '_es6shim_iterator_';
55
- // Firefox ships a partial implementation using the name @@iterator.
56
- // https://bugzilla.mozilla.org/show_bug.cgi?id=907077#c14
57
- // So use that name if we detect it.
57
+ // Bug for mozilla version
58
58
  if (root.Set && typeof new root.Set()['@@iterator'] === 'function') {
59
59
  $iterator$ = '@@iterator';
60
60
  }
61
+
61
62
  var doneEnumerator = { done: true, value: undefined };
62
63
 
63
64
  /** `Object#toString` result shortcuts */
@@ -1512,140 +1513,111 @@
1512
1513
  return CatchScheduler;
1513
1514
  }(Scheduler));
1514
1515
 
1515
- /**
1516
- * Represents a notification to an observer.
1517
- */
1518
- var Notification = Rx.Notification = (function () {
1519
- function Notification(kind, hasValue) {
1520
- this.hasValue = hasValue == null ? false : hasValue;
1521
- this.kind = kind;
1522
- }
1523
-
1524
- var NotificationPrototype = Notification.prototype;
1525
-
1526
- /**
1527
- * Invokes the delegate corresponding to the notification or the observer's method corresponding to the notification and returns the produced result.
1528
- *
1529
- * @memberOf Notification
1530
- * @param {Any} observerOrOnNext Delegate to invoke for an OnNext notification or Observer to invoke the notification on..
1531
- * @param {Function} onError Delegate to invoke for an OnError notification.
1532
- * @param {Function} onCompleted Delegate to invoke for an OnCompleted notification.
1533
- * @returns {Any} Result produced by the observation.
1534
- */
1535
- NotificationPrototype.accept = function (observerOrOnNext, onError, onCompleted) {
1536
- if (arguments.length === 1 && typeof observerOrOnNext === 'object') {
1537
- return this._acceptObservable(observerOrOnNext);
1538
- }
1539
- return this._accept(observerOrOnNext, onError, onCompleted);
1540
- };
1541
-
1542
- /**
1543
- * Returns an observable sequence with a single notification.
1544
- *
1545
- * @memberOf Notification
1546
- * @param {Scheduler} [scheduler] Scheduler to send out the notification calls on.
1547
- * @returns {Observable} The observable sequence that surfaces the behavior of the notification upon subscription.
1548
- */
1549
- NotificationPrototype.toObservable = function (scheduler) {
1550
- var notification = this;
1551
- scheduler || (scheduler = immediateScheduler);
1552
- return new AnonymousObservable(function (observer) {
1553
- return scheduler.schedule(function () {
1554
- notification._acceptObservable(observer);
1555
- if (notification.kind === 'N') {
1556
- observer.onCompleted();
1557
- }
1558
- });
1559
- });
1560
- };
1561
-
1562
- return Notification;
1563
- })();
1516
+ /**
1517
+ * Represents a notification to an observer.
1518
+ */
1519
+ var Notification = Rx.Notification = (function () {
1520
+ function Notification(kind, hasValue) {
1521
+ this.hasValue = hasValue == null ? false : hasValue;
1522
+ this.kind = kind;
1523
+ }
1564
1524
 
1565
1525
  /**
1566
- * Creates an object that represents an OnNext notification to an observer.
1567
- * @param {Any} value The value contained in the notification.
1568
- * @returns {Notification} The OnNext notification containing the value.
1526
+ * Invokes the delegate corresponding to the notification or the observer's method corresponding to the notification and returns the produced result.
1527
+ *
1528
+ * @memberOf Notification
1529
+ * @param {Any} observerOrOnNext Delegate to invoke for an OnNext notification or Observer to invoke the notification on..
1530
+ * @param {Function} onError Delegate to invoke for an OnError notification.
1531
+ * @param {Function} onCompleted Delegate to invoke for an OnCompleted notification.
1532
+ * @returns {Any} Result produced by the observation.
1569
1533
  */
1570
- var notificationCreateOnNext = Notification.createOnNext = (function () {
1571
-
1572
- function _accept (onNext) {
1573
- return onNext(this.value);
1574
- }
1575
-
1576
- function _acceptObservable(observer) {
1577
- return observer.onNext(this.value);
1578
- }
1579
-
1580
- function toString () {
1581
- return 'OnNext(' + this.value + ')';
1582
- }
1583
-
1584
- return function (value) {
1585
- var notification = new Notification('N', true);
1586
- notification.value = value;
1587
- notification._accept = _accept;
1588
- notification._acceptObservable = _acceptObservable;
1589
- notification.toString = toString;
1590
- return notification;
1591
- };
1592
- }());
1534
+ Notification.prototype.accept = function (observerOrOnNext, onError, onCompleted) {
1535
+ return observerOrOnNext && typeof observerOrOnNext === 'object' ?
1536
+ this._acceptObservable(observerOrOnNext) :
1537
+ this._accept(observerOrOnNext, onError, onCompleted);
1538
+ };
1593
1539
 
1594
1540
  /**
1595
- * Creates an object that represents an OnError notification to an observer.
1596
- * @param {Any} error The exception contained in the notification.
1597
- * @returns {Notification} The OnError notification containing the exception.
1541
+ * Returns an observable sequence with a single notification.
1542
+ *
1543
+ * @memberOf Notifications
1544
+ * @param {Scheduler} [scheduler] Scheduler to send out the notification calls on.
1545
+ * @returns {Observable} The observable sequence that surfaces the behavior of the notification upon subscription.
1598
1546
  */
1599
- var notificationCreateOnError = Notification.createOnError = (function () {
1600
-
1601
- function _accept (onNext, onError) {
1602
- return onError(this.exception);
1603
- }
1604
-
1605
- function _acceptObservable(observer) {
1606
- return observer.onError(this.exception);
1607
- }
1608
-
1609
- function toString () {
1610
- return 'OnError(' + this.exception + ')';
1611
- }
1547
+ Notification.prototype.toObservable = function (scheduler) {
1548
+ var notification = this;
1549
+ isScheduler(scheduler) || (scheduler = immediateScheduler);
1550
+ return new AnonymousObservable(function (observer) {
1551
+ return scheduler.schedule(function () {
1552
+ notification._acceptObservable(observer);
1553
+ notification.kind === 'N' && observer.onCompleted();
1554
+ });
1555
+ });
1556
+ };
1612
1557
 
1613
- return function (exception) {
1614
- var notification = new Notification('E');
1615
- notification.exception = exception;
1616
- notification._accept = _accept;
1617
- notification._acceptObservable = _acceptObservable;
1618
- notification.toString = toString;
1619
- return notification;
1620
- };
1621
- }());
1558
+ return Notification;
1559
+ })();
1622
1560
 
1623
- /**
1624
- * Creates an object that represents an OnCompleted notification to an observer.
1625
- * @returns {Notification} The OnCompleted notification.
1626
- */
1627
- var notificationCreateOnCompleted = Notification.createOnCompleted = (function () {
1561
+ /**
1562
+ * Creates an object that represents an OnNext notification to an observer.
1563
+ * @param {Any} value The value contained in the notification.
1564
+ * @returns {Notification} The OnNext notification containing the value.
1565
+ */
1566
+ var notificationCreateOnNext = Notification.createOnNext = (function () {
1567
+
1568
+ function _accept (onNext) { return onNext(this.value); }
1569
+ function _acceptObservable(observer) { return observer.onNext(this.value); }
1570
+ function toString () { return 'OnNext(' + this.value + ')'; }
1571
+
1572
+ return function (value) {
1573
+ var notification = new Notification('N', true);
1574
+ notification.value = value;
1575
+ notification._accept = _accept;
1576
+ notification._acceptObservable = _acceptObservable;
1577
+ notification.toString = toString;
1578
+ return notification;
1579
+ };
1580
+ }());
1628
1581
 
1629
- function _accept (onNext, onError, onCompleted) {
1630
- return onCompleted();
1631
- }
1582
+ /**
1583
+ * Creates an object that represents an OnError notification to an observer.
1584
+ * @param {Any} error The exception contained in the notification.
1585
+ * @returns {Notification} The OnError notification containing the exception.
1586
+ */
1587
+ var notificationCreateOnError = Notification.createOnError = (function () {
1588
+
1589
+ function _accept (onNext, onError) { return onError(this.exception); }
1590
+ function _acceptObservable(observer) { return observer.onError(this.exception); }
1591
+ function toString () { return 'OnError(' + this.exception + ')'; }
1592
+
1593
+ return function (exception) {
1594
+ var notification = new Notification('E');
1595
+ notification.exception = exception;
1596
+ notification._accept = _accept;
1597
+ notification._acceptObservable = _acceptObservable;
1598
+ notification.toString = toString;
1599
+ return notification;
1600
+ };
1601
+ }());
1632
1602
 
1633
- function _acceptObservable(observer) {
1634
- return observer.onCompleted();
1635
- }
1603
+ /**
1604
+ * Creates an object that represents an OnCompleted notification to an observer.
1605
+ * @returns {Notification} The OnCompleted notification.
1606
+ */
1607
+ var notificationCreateOnCompleted = Notification.createOnCompleted = (function () {
1636
1608
 
1637
- function toString () {
1638
- return 'OnCompleted()';
1639
- }
1609
+ function _accept (onNext, onError, onCompleted) { return onCompleted(); }
1610
+ function _acceptObservable(observer) { return observer.onCompleted(); }
1611
+ function toString () { return 'OnCompleted()'; }
1640
1612
 
1641
- return function () {
1642
- var notification = new Notification('C');
1643
- notification._accept = _accept;
1644
- notification._acceptObservable = _acceptObservable;
1645
- notification.toString = toString;
1646
- return notification;
1647
- };
1648
- }());
1613
+ return function () {
1614
+ var notification = new Notification('C');
1615
+ notification._accept = _accept;
1616
+ notification._acceptObservable = _acceptObservable;
1617
+ notification.toString = toString;
1618
+ return notification;
1619
+ };
1620
+ }());
1649
1621
 
1650
1622
  var Enumerator = Rx.internals.Enumerator = function (next) {
1651
1623
  this._next = next;
@@ -2325,129 +2297,204 @@
2325
2297
  });
2326
2298
  };
2327
2299
 
2328
- /**
2329
- * Returns an empty observable sequence, using the specified scheduler to send out the single OnCompleted message.
2330
- *
2331
- * @example
2332
- * var res = Rx.Observable.empty();
2333
- * var res = Rx.Observable.empty(Rx.Scheduler.timeout);
2334
- * @param {Scheduler} [scheduler] Scheduler to send the termination call on.
2335
- * @returns {Observable} An observable sequence with no elements.
2336
- */
2337
- var observableEmpty = Observable.empty = function (scheduler) {
2338
- scheduler || (scheduler = immediateScheduler);
2339
- return new AnonymousObservable(function (observer) {
2340
- return scheduler.schedule(function () {
2341
- observer.onCompleted();
2342
- });
2343
- });
2344
- };
2300
+ /**
2301
+ * Returns an empty observable sequence, using the specified scheduler to send out the single OnCompleted message.
2302
+ *
2303
+ * @example
2304
+ * var res = Rx.Observable.empty();
2305
+ * var res = Rx.Observable.empty(Rx.Scheduler.timeout);
2306
+ * @param {Scheduler} [scheduler] Scheduler to send the termination call on.
2307
+ * @returns {Observable} An observable sequence with no elements.
2308
+ */
2309
+ var observableEmpty = Observable.empty = function (scheduler) {
2310
+ isScheduler(scheduler) || (scheduler = immediateScheduler);
2311
+ return new AnonymousObservable(function (observer) {
2312
+ return scheduler.schedule(function () {
2313
+ observer.onCompleted();
2314
+ });
2315
+ });
2316
+ };
2345
2317
 
2346
- /**
2347
- * Converts an array to an observable sequence, using an optional scheduler to enumerate the array.
2348
- *
2349
- * @example
2350
- * var res = Rx.Observable.fromArray([1,2,3]);
2351
- * var res = Rx.Observable.fromArray([1,2,3], Rx.Scheduler.timeout);
2352
- * @param {Scheduler} [scheduler] Scheduler to run the enumeration of the input sequence on.
2353
- * @returns {Observable} The observable sequence whose elements are pulled from the given enumerable sequence.
2354
- */
2355
- var observableFromArray = Observable.fromArray = function (array, scheduler) {
2356
- scheduler || (scheduler = currentThreadScheduler);
2357
- return new AnonymousObservable(function (observer) {
2358
- var count = 0;
2359
- return scheduler.scheduleRecursive(function (self) {
2360
- if (count < array.length) {
2361
- observer.onNext(array[count++]);
2362
- self();
2363
- } else {
2364
- observer.onCompleted();
2365
- }
2366
- });
2367
- });
2368
- };
2318
+ var maxSafeInteger = Math.pow(2, 53) - 1;
2319
+
2320
+ function numberIsFinite(value) {
2321
+ return typeof value === 'number' && root.isFinite(value);
2322
+ }
2323
+
2324
+ function isNan(n) {
2325
+ return n !== n;
2326
+ }
2327
+
2328
+ function isIterable(o) {
2329
+ return o[$iterator$] !== undefined;
2330
+ }
2331
+
2332
+ function sign(value) {
2333
+ var number = +value;
2334
+ if (number === 0) { return number; }
2335
+ if (isNaN(number)) { return number; }
2336
+ return number < 0 ? -1 : 1;
2337
+ }
2338
+
2339
+ function toLength(o) {
2340
+ var len = +o.length;
2341
+ if (isNaN(len)) { return 0; }
2342
+ if (len === 0 || !numberIsFinite(len)) { return len; }
2343
+ len = sign(len) * Math.floor(Math.abs(len));
2344
+ if (len <= 0) { return 0; }
2345
+ if (len > maxSafeInteger) { return maxSafeInteger; }
2346
+ return len;
2347
+ }
2348
+
2349
+ function isCallable(f) {
2350
+ return Object.prototype.toString.call(f) === '[object Function]' && typeof f === 'function';
2351
+ }
2352
+
2353
+ /**
2354
+ * This method creates a new Observable sequence from an array-like or iterable object.
2355
+ * @param {Any} arrayLike An array-like or iterable object to convert to an Observable sequence.
2356
+ * @param {Function} [mapFn] Map function to call on every element of the array.
2357
+ * @param {Any} [thisArg] The context to use calling the mapFn if provided.
2358
+ * @param {Scheduler} [scheduler] Optional scheduler to use for scheduling. If not provided, defaults to Scheduler.currentThread.
2359
+ */
2360
+ Observable.from = function (iterable, mapFn, thisArg, scheduler) {
2361
+ if (iterable == null) {
2362
+ throw new Error('iterable cannot be null.')
2363
+ }
2364
+ if (mapFn && !isCallable(mapFn)) {
2365
+ throw new Error('mapFn when provided must be a function');
2366
+ }
2367
+ isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2368
+ return new AnonymousObservable(function (observer) {
2369
+ var list = Object(iterable),
2370
+ objIsIterable = isIterable(list),
2371
+ len = objIsIterable ? 0 : toLength(list),
2372
+ it = objIsIterable ? list[$iterator$]() : null,
2373
+ i = 0;
2374
+ return scheduler.scheduleRecursive(function (self) {
2375
+ if (i < len || objIsIterable) {
2376
+ var result;
2377
+ if (objIsIterable) {
2378
+ var next = it.next();
2379
+ if (next.done) {
2380
+ observer.onCompleted();
2381
+ return;
2382
+ }
2383
+
2384
+ result = next.value;
2385
+ } else {
2386
+ result = list[i];
2387
+ }
2388
+
2389
+ if (mapFn && isCallable(mapFn)) {
2390
+ try {
2391
+ result = thisArg ? mapFn.call(thisArg, result, i) : mapFn(result, i);
2392
+ } catch (e) {
2393
+ observer.onError(e);
2394
+ return;
2395
+ }
2396
+ }
2369
2397
 
2398
+ observer.onNext(result);
2399
+ i++;
2400
+ self();
2401
+ } else {
2402
+ observer.onCompleted();
2403
+ }
2404
+ });
2405
+ });
2406
+ };
2370
2407
  /**
2371
- * Converts an iterable into an Observable sequence
2408
+ * Converts an array to an observable sequence, using an optional scheduler to enumerate the array.
2372
2409
  *
2373
2410
  * @example
2374
- * var res = Rx.Observable.fromIterable(new Map());
2375
- * var res = Rx.Observable.fromIterable(new Set(), Rx.Scheduler.timeout);
2411
+ * var res = Rx.Observable.fromArray([1,2,3]);
2412
+ * var res = Rx.Observable.fromArray([1,2,3], Rx.Scheduler.timeout);
2376
2413
  * @param {Scheduler} [scheduler] Scheduler to run the enumeration of the input sequence on.
2377
- * @returns {Observable} The observable sequence whose elements are pulled from the given generator sequence.
2414
+ * @returns {Observable} The observable sequence whose elements are pulled from the given enumerable sequence.
2378
2415
  */
2379
- Observable.fromIterable = function (iterable, scheduler) {
2380
- scheduler || (scheduler = currentThreadScheduler);
2416
+ var observableFromArray = Observable.fromArray = function (array, scheduler) {
2417
+ isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2381
2418
  return new AnonymousObservable(function (observer) {
2382
- var iterator;
2383
- try {
2384
- iterator = iterable[$iterator$]();
2385
- } catch (e) {
2386
- observer.onError(e);
2387
- return;
2388
- }
2419
+ var count = 0, len = array.length;
2420
+ return scheduler.scheduleRecursive(function (self) {
2421
+ if (count < len) {
2422
+ observer.onNext(array[count++]);
2423
+ self();
2424
+ } else {
2425
+ observer.onCompleted();
2426
+ }
2427
+ });
2428
+ });
2429
+ };
2389
2430
 
2431
+ /**
2432
+ * Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler to send out observer messages.
2433
+ *
2434
+ * @example
2435
+ * var res = Rx.Observable.generate(0, function (x) { return x < 10; }, function (x) { return x + 1; }, function (x) { return x; });
2436
+ * var res = Rx.Observable.generate(0, function (x) { return x < 10; }, function (x) { return x + 1; }, function (x) { return x; }, Rx.Scheduler.timeout);
2437
+ * @param {Mixed} initialState Initial state.
2438
+ * @param {Function} condition Condition to terminate generation (upon returning false).
2439
+ * @param {Function} iterate Iteration step function.
2440
+ * @param {Function} resultSelector Selector function for results produced in the sequence.
2441
+ * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not provided, defaults to Scheduler.currentThread.
2442
+ * @returns {Observable} The generated sequence.
2443
+ */
2444
+ Observable.generate = function (initialState, condition, iterate, resultSelector, scheduler) {
2445
+ isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2446
+ return new AnonymousObservable(function (observer) {
2447
+ var first = true, state = initialState;
2390
2448
  return scheduler.scheduleRecursive(function (self) {
2391
- var next;
2449
+ var hasResult, result;
2392
2450
  try {
2393
- next = iterator.next();
2394
- } catch (err) {
2395
- observer.onError(err);
2451
+ if (first) {
2452
+ first = false;
2453
+ } else {
2454
+ state = iterate(state);
2455
+ }
2456
+ hasResult = condition(state);
2457
+ if (hasResult) {
2458
+ result = resultSelector(state);
2459
+ }
2460
+ } catch (exception) {
2461
+ observer.onError(exception);
2396
2462
  return;
2397
2463
  }
2398
-
2399
- if (next.done) {
2400
- observer.onCompleted();
2401
- } else {
2402
- observer.onNext(next.value);
2464
+ if (hasResult) {
2465
+ observer.onNext(result);
2403
2466
  self();
2467
+ } else {
2468
+ observer.onCompleted();
2404
2469
  }
2405
2470
  });
2406
2471
  });
2407
2472
  };
2408
2473
 
2409
- /**
2410
- * Generates an observable sequence by running a state-driven loop producing the sequence's elements, using the specified scheduler to send out observer messages.
2411
- *
2412
- * @example
2413
- * var res = Rx.Observable.generate(0, function (x) { return x < 10; }, function (x) { return x + 1; }, function (x) { return x; });
2414
- * var res = Rx.Observable.generate(0, function (x) { return x < 10; }, function (x) { return x + 1; }, function (x) { return x; }, Rx.Scheduler.timeout);
2415
- * @param {Mixed} initialState Initial state.
2416
- * @param {Function} condition Condition to terminate generation (upon returning false).
2417
- * @param {Function} iterate Iteration step function.
2418
- * @param {Function} resultSelector Selector function for results produced in the sequence.
2419
- * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not provided, defaults to Scheduler.currentThread.
2420
- * @returns {Observable} The generated sequence.
2421
- */
2422
- Observable.generate = function (initialState, condition, iterate, resultSelector, scheduler) {
2423
- scheduler || (scheduler = currentThreadScheduler);
2424
- return new AnonymousObservable(function (observer) {
2425
- var first = true, state = initialState;
2426
- return scheduler.scheduleRecursive(function (self) {
2427
- var hasResult, result;
2428
- try {
2429
- if (first) {
2430
- first = false;
2431
- } else {
2432
- state = iterate(state);
2433
- }
2434
- hasResult = condition(state);
2435
- if (hasResult) {
2436
- result = resultSelector(state);
2437
- }
2438
- } catch (exception) {
2439
- observer.onError(exception);
2440
- return;
2441
- }
2442
- if (hasResult) {
2443
- observer.onNext(result);
2444
- self();
2445
- } else {
2446
- observer.onCompleted();
2447
- }
2448
- });
2449
- });
2450
- };
2474
+ /**
2475
+ * This method creates a new Observable instance with a variable number of arguments, regardless of number or type of the arguments.
2476
+ * @example
2477
+ * var res = Rx.Observable.of(1,2,3);
2478
+ * @returns {Observable} The observable sequence whose elements are pulled from the given arguments.
2479
+ */
2480
+ Observable.of = function () {
2481
+ var len = arguments.length, args = new Array(len);
2482
+ for(var i = 0; i < len; i++) { args[i] = arguments[i]; }
2483
+ return observableFromArray(args);
2484
+ };
2485
+
2486
+ /**
2487
+ * This method creates a new Observable instance with a variable number of arguments, regardless of number or type of the arguments.
2488
+ * @example
2489
+ * var res = Rx.Observable.of(1,2,3);
2490
+ * @param {Scheduler} scheduler A scheduler to use for scheduling the arguments.
2491
+ * @returns {Observable} The observable sequence whose elements are pulled from the given arguments.
2492
+ */
2493
+ var observableOf = Observable.ofWithScheduler = function (scheduler) {
2494
+ var len = arguments.length - 1, args = new Array(len);
2495
+ for(var i = 0; i < len; i++) { args[i] = arguments[i + 1]; }
2496
+ return observableFromArray(args, scheduler);
2497
+ };
2451
2498
 
2452
2499
  /**
2453
2500
  * Returns a non-terminating observable sequence, which can be used to denote an infinite duration (e.g. when using reactive joins).
@@ -2459,115 +2506,112 @@
2459
2506
  });
2460
2507
  };
2461
2508
 
2462
- /**
2463
- * Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to send out observer messages.
2464
- *
2465
- * @example
2466
- * var res = Rx.Observable.range(0, 10);
2467
- * var res = Rx.Observable.range(0, 10, Rx.Scheduler.timeout);
2468
- * @param {Number} start The value of the first integer in the sequence.
2469
- * @param {Number} count The number of sequential integers to generate.
2470
- * @param {Scheduler} [scheduler] Scheduler to run the generator loop on. If not specified, defaults to Scheduler.currentThread.
2471
- * @returns {Observable} An observable sequence that contains a range of sequential integral numbers.
2472
- */
2473
- Observable.range = function (start, count, scheduler) {
2474
- scheduler || (scheduler = currentThreadScheduler);
2475
- return new AnonymousObservable(function (observer) {
2476
- return scheduler.scheduleRecursiveWithState(0, function (i, self) {
2477
- if (i < count) {
2478
- observer.onNext(start + i);
2479
- self(i + 1);
2480
- } else {
2481
- observer.onCompleted();
2482
- }
2483
- });
2484
- });
2485
- };
2486
-
2487
- /**
2488
- * Generates an observable sequence that repeats the given element the specified number of times, using the specified scheduler to send out observer messages.
2489
- *
2490
- * @example
2491
- * var res = Rx.Observable.repeat(42);
2492
- * var res = Rx.Observable.repeat(42, 4);
2493
- * 3 - res = Rx.Observable.repeat(42, 4, Rx.Scheduler.timeout);
2494
- * 4 - res = Rx.Observable.repeat(42, null, Rx.Scheduler.timeout);
2495
- * @param {Mixed} value Element to repeat.
2496
- * @param {Number} repeatCount [Optiona] Number of times to repeat the element. If not specified, repeats indefinitely.
2497
- * @param {Scheduler} scheduler Scheduler to run the producer loop on. If not specified, defaults to Scheduler.immediate.
2498
- * @returns {Observable} An observable sequence that repeats the given element the specified number of times.
2499
- */
2500
- Observable.repeat = function (value, repeatCount, scheduler) {
2501
- scheduler || (scheduler = currentThreadScheduler);
2502
- if (repeatCount == null) {
2503
- repeatCount = -1;
2504
- }
2505
- return observableReturn(value, scheduler).repeat(repeatCount);
2506
- };
2507
-
2508
- /**
2509
- * Returns an observable sequence that contains a single element, using the specified scheduler to send out observer messages.
2510
- * There is an alias called 'returnValue' for browsers <IE9.
2511
- *
2512
- * @example
2513
- * var res = Rx.Observable.return(42);
2514
- * var res = Rx.Observable.return(42, Rx.Scheduler.timeout);
2515
- * @param {Mixed} value Single element in the resulting observable sequence.
2516
- * @param {Scheduler} scheduler Scheduler to send the single element on. If not specified, defaults to Scheduler.immediate.
2517
- * @returns {Observable} An observable sequence containing the single specified element.
2518
- */
2519
- var observableReturn = Observable['return'] = Observable.returnValue = function (value, scheduler) {
2520
- scheduler || (scheduler = immediateScheduler);
2521
- return new AnonymousObservable(function (observer) {
2522
- return scheduler.schedule(function () {
2523
- observer.onNext(value);
2524
- observer.onCompleted();
2525
- });
2526
- });
2527
- };
2528
-
2529
- /**
2530
- * Returns an observable sequence that terminates with an exception, using the specified scheduler to send out the single onError message.
2531
- * There is an alias to this method called 'throwException' for browsers <IE9.
2532
- *
2533
- * @example
2534
- * var res = Rx.Observable.throwException(new Error('Error'));
2535
- * var res = Rx.Observable.throwException(new Error('Error'), Rx.Scheduler.timeout);
2536
- * @param {Mixed} exception An object used for the sequence's termination.
2537
- * @param {Scheduler} scheduler Scheduler to send the exceptional termination call on. If not specified, defaults to Scheduler.immediate.
2538
- * @returns {Observable} The observable sequence that terminates exceptionally with the specified exception object.
2539
- */
2540
- var observableThrow = Observable['throw'] = Observable.throwException = function (exception, scheduler) {
2541
- scheduler || (scheduler = immediateScheduler);
2542
- return new AnonymousObservable(function (observer) {
2543
- return scheduler.schedule(function () {
2544
- observer.onError(exception);
2545
- });
2546
- });
2547
- };
2548
-
2549
2509
  /**
2550
- * Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime.
2510
+ * Generates an observable sequence of integral numbers within a specified range, using the specified scheduler to send out observer messages.
2551
2511
  *
2552
2512
  * @example
2553
- * var res = Rx.Observable.using(function () { return new AsyncSubject(); }, function (s) { return s; });
2554
- * @param {Function} resourceFactory Factory function to obtain a resource object.
2555
- * @param {Function} observableFactory Factory function to obtain an observable sequence that depends on the obtained resource.
2556
- * @returns {Observable} An observable sequence whose lifetime controls the lifetime of the dependent resource object.
2513
+ * var res = Rx.Observable.range(0, 10);
2514
+ * var res = Rx.Observable.range(0, 10, Rx.Scheduler.timeout);
2515
+ * @param {Number} start The value of the first integer in the sequence.
2516
+ * @param {Number} count The number of sequential integers to generate.
2517
+ * @param {Scheduler} [scheduler] Scheduler to run the generator loop on. If not specified, defaults to Scheduler.currentThread.
2518
+ * @returns {Observable} An observable sequence that contains a range of sequential integral numbers.
2557
2519
  */
2558
- Observable.using = function (resourceFactory, observableFactory) {
2520
+ Observable.range = function (start, count, scheduler) {
2521
+ isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2559
2522
  return new AnonymousObservable(function (observer) {
2560
- var disposable = disposableEmpty, resource, source;
2561
- try {
2562
- resource = resourceFactory();
2563
- if (resource) {
2564
- disposable = resource;
2523
+ return scheduler.scheduleRecursiveWithState(0, function (i, self) {
2524
+ if (i < count) {
2525
+ observer.onNext(start + i);
2526
+ self(i + 1);
2527
+ } else {
2528
+ observer.onCompleted();
2565
2529
  }
2566
- source = observableFactory(resource);
2567
- } catch (exception) {
2568
- return new CompositeDisposable(observableThrow(exception).subscribe(observer), disposable);
2569
- }
2570
- return new CompositeDisposable(source.subscribe(observer), disposable);
2530
+ });
2531
+ });
2532
+ };
2533
+
2534
+ /**
2535
+ * Generates an observable sequence that repeats the given element the specified number of times, using the specified scheduler to send out observer messages.
2536
+ *
2537
+ * @example
2538
+ * var res = Rx.Observable.repeat(42);
2539
+ * var res = Rx.Observable.repeat(42, 4);
2540
+ * 3 - res = Rx.Observable.repeat(42, 4, Rx.Scheduler.timeout);
2541
+ * 4 - res = Rx.Observable.repeat(42, null, Rx.Scheduler.timeout);
2542
+ * @param {Mixed} value Element to repeat.
2543
+ * @param {Number} repeatCount [Optiona] Number of times to repeat the element. If not specified, repeats indefinitely.
2544
+ * @param {Scheduler} scheduler Scheduler to run the producer loop on. If not specified, defaults to Scheduler.immediate.
2545
+ * @returns {Observable} An observable sequence that repeats the given element the specified number of times.
2546
+ */
2547
+ Observable.repeat = function (value, repeatCount, scheduler) {
2548
+ isScheduler(scheduler) || (scheduler = currentThreadScheduler);
2549
+ return observableReturn(value, scheduler).repeat(repeatCount == null ? -1 : repeatCount);
2550
+ };
2551
+
2552
+ /**
2553
+ * Returns an observable sequence that contains a single element, using the specified scheduler to send out observer messages.
2554
+ * There is an alias called 'just', and 'returnValue' for browsers <IE9.
2555
+ *
2556
+ * @example
2557
+ * var res = Rx.Observable.return(42);
2558
+ * var res = Rx.Observable.return(42, Rx.Scheduler.timeout);
2559
+ * @param {Mixed} value Single element in the resulting observable sequence.
2560
+ * @param {Scheduler} scheduler Scheduler to send the single element on. If not specified, defaults to Scheduler.immediate.
2561
+ * @returns {Observable} An observable sequence containing the single specified element.
2562
+ */
2563
+ var observableReturn = Observable['return'] = Observable.returnValue = Observable.just = function (value, scheduler) {
2564
+ isScheduler(scheduler) || (scheduler = immediateScheduler);
2565
+ return new AnonymousObservable(function (observer) {
2566
+ return scheduler.schedule(function () {
2567
+ observer.onNext(value);
2568
+ observer.onCompleted();
2569
+ });
2570
+ });
2571
+ };
2572
+
2573
+ /**
2574
+ * Returns an observable sequence that terminates with an exception, using the specified scheduler to send out the single onError message.
2575
+ * There is an alias to this method called 'throwException' for browsers <IE9.
2576
+ *
2577
+ * @example
2578
+ * var res = Rx.Observable.throw(new Error('Error'));
2579
+ * var res = Rx.Observable.throw(new Error('Error'), Rx.Scheduler.timeout);
2580
+ * @param {Mixed} exception An object used for the sequence's termination.
2581
+ * @param {Scheduler} scheduler Scheduler to send the exceptional termination call on. If not specified, defaults to Scheduler.immediate.
2582
+ * @returns {Observable} The observable sequence that terminates exceptionally with the specified exception object.
2583
+ */
2584
+ var observableThrow = Observable['throw'] = Observable.throwException = function (exception, scheduler) {
2585
+ isScheduler(scheduler) || (scheduler = immediateScheduler);
2586
+ return new AnonymousObservable(function (observer) {
2587
+ return scheduler.schedule(function () {
2588
+ observer.onError(exception);
2589
+ });
2590
+ });
2591
+ };
2592
+
2593
+ /**
2594
+ * Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime.
2595
+ *
2596
+ * @example
2597
+ * var res = Rx.Observable.using(function () { return new AsyncSubject(); }, function (s) { return s; });
2598
+ * @param {Function} resourceFactory Factory function to obtain a resource object.
2599
+ * @param {Function} observableFactory Factory function to obtain an observable sequence that depends on the obtained resource.
2600
+ * @returns {Observable} An observable sequence whose lifetime controls the lifetime of the dependent resource object.
2601
+ */
2602
+ Observable.using = function (resourceFactory, observableFactory) {
2603
+ return new AnonymousObservable(function (observer) {
2604
+ var disposable = disposableEmpty, resource, source;
2605
+ try {
2606
+ resource = resourceFactory();
2607
+ if (resource) {
2608
+ disposable = resource;
2609
+ }
2610
+ source = observableFactory(resource);
2611
+ } catch (exception) {
2612
+ return new CompositeDisposable(observableThrow(exception).subscribe(observer), disposable);
2613
+ }
2614
+ return new CompositeDisposable(source.subscribe(observer), disposable);
2571
2615
  });
2572
2616
  };
2573
2617
 
@@ -3449,18 +3493,19 @@
3449
3493
  return enumerableRepeat(this, repeatCount).concat();
3450
3494
  };
3451
3495
 
3452
- /**
3453
- * Repeats the source observable sequence the specified number of times or until it successfully terminates. If the retry count is not specified, it retries indefinitely.
3454
- *
3455
- * @example
3456
- * var res = retried = retry.repeat();
3457
- * var res = retried = retry.repeat(42);
3458
- * @param {Number} [retryCount] Number of times to retry the sequence. If not provided, retry the sequence indefinitely.
3459
- * @returns {Observable} An observable sequence producing the elements of the given sequence repeatedly until it terminates successfully.
3460
- */
3461
- observableProto.retry = function (retryCount) {
3462
- return enumerableRepeat(this, retryCount).catchException();
3463
- };
3496
+ /**
3497
+ * Repeats the source observable sequence the specified number of times or until it successfully terminates. If the retry count is not specified, it retries indefinitely.
3498
+ * Note if you encounter an error and want it to retry once, then you must use .retry(2);
3499
+ *
3500
+ * @example
3501
+ * var res = retried = retry.repeat();
3502
+ * var res = retried = retry.repeat(2);
3503
+ * @param {Number} [retryCount] Number of times to retry the sequence. If not provided, retry the sequence indefinitely.
3504
+ * @returns {Observable} An observable sequence producing the elements of the given sequence repeatedly until it terminates successfully.
3505
+ */
3506
+ observableProto.retry = function (retryCount) {
3507
+ return enumerableRepeat(this, retryCount).catchException();
3508
+ };
3464
3509
 
3465
3510
  /**
3466
3511
  * Applies an accumulator function over an observable sequence and returns each intermediate result. The optional seed value is used as the initial accumulator value.
@@ -4025,7 +4070,7 @@
4025
4070
  * @returns {Observable} An observable sequence whose elements are the result of invoking the transform function on each element of source producing an Observable of Observable sequences
4026
4071
  * and that at any point in time produces the elements of the most recent inner observable sequence that has been received.
4027
4072
  */
4028
- observableProto.selectSwitch = observableProto.flatMapLatest = function (selector, thisArg) {
4073
+ observableProto.selectSwitch = observableProto.flatMapLatest = observableProto.switchMap = function (selector, thisArg) {
4029
4074
  return this.select(selector, thisArg).switchLatest();
4030
4075
  };
4031
4076
 
@@ -4822,151 +4867,147 @@
4822
4867
  return findValue(this, predicate, thisArg, true);
4823
4868
  };
4824
4869
 
4825
- /**
4826
- * Invokes the specified function asynchronously on the specified scheduler, surfacing the result through an observable sequence.
4827
- *
4828
- * @example
4829
- * var res = Rx.Observable.start(function () { console.log('hello'); });
4830
- * var res = Rx.Observable.start(function () { console.log('hello'); }, Rx.Scheduler.timeout);
4831
- * var res = Rx.Observable.start(function () { this.log('hello'); }, Rx.Scheduler.timeout, console);
4832
- *
4833
- * @param {Function} func Function to run asynchronously.
4834
- * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
4835
- * @param [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
4836
- * @returns {Observable} An observable sequence exposing the function's result value, or an exception.
4837
- *
4838
- * Remarks
4839
- * * The function is called immediately, not during the subscription of the resulting sequence.
4840
- * * Multiple subscriptions to the resulting sequence can observe the function's result.
4841
- */
4842
- Observable.start = function (func, scheduler, context) {
4843
- return observableToAsync(func, scheduler, context)();
4844
- };
4845
-
4846
- /**
4847
- * Converts the function into an asynchronous function. Each invocation of the resulting asynchronous function causes an invocation of the original synchronous function on the specified scheduler.
4848
- *
4849
- * @example
4850
- * var res = Rx.Observable.toAsync(function (x, y) { return x + y; })(4, 3);
4851
- * var res = Rx.Observable.toAsync(function (x, y) { return x + y; }, Rx.Scheduler.timeout)(4, 3);
4852
- * var res = Rx.Observable.toAsync(function (x) { this.log(x); }, Rx.Scheduler.timeout, console)('hello');
4853
- *
4854
- * @param {Function} function Function to convert to an asynchronous function.
4855
- * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
4856
- * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
4857
- * @returns {Function} Asynchronous function.
4858
- */
4859
- var observableToAsync = Observable.toAsync = function (func, scheduler, context) {
4860
- scheduler || (scheduler = timeoutScheduler);
4861
- return function () {
4862
- var args = arguments,
4863
- subject = new AsyncSubject();
4870
+ /**
4871
+ * Invokes the specified function asynchronously on the specified scheduler, surfacing the result through an observable sequence.
4872
+ *
4873
+ * @example
4874
+ * var res = Rx.Observable.start(function () { console.log('hello'); });
4875
+ * var res = Rx.Observable.start(function () { console.log('hello'); }, Rx.Scheduler.timeout);
4876
+ * var res = Rx.Observable.start(function () { this.log('hello'); }, Rx.Scheduler.timeout, console);
4877
+ *
4878
+ * @param {Function} func Function to run asynchronously.
4879
+ * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
4880
+ * @param [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
4881
+ * @returns {Observable} An observable sequence exposing the function's result value, or an exception.
4882
+ *
4883
+ * Remarks
4884
+ * * The function is called immediately, not during the subscription of the resulting sequence.
4885
+ * * Multiple subscriptions to the resulting sequence can observe the function's result.
4886
+ */
4887
+ Observable.start = function (func, context, scheduler) {
4888
+ return observableToAsync(func, context, scheduler)();
4889
+ };
4864
4890
 
4865
- scheduler.schedule(function () {
4866
- var result;
4867
- try {
4868
- result = func.apply(context, args);
4869
- } catch (e) {
4870
- subject.onError(e);
4871
- return;
4872
- }
4873
- subject.onNext(result);
4874
- subject.onCompleted();
4875
- });
4876
- return subject.asObservable();
4877
- };
4891
+ /**
4892
+ * Converts the function into an asynchronous function. Each invocation of the resulting asynchronous function causes an invocation of the original synchronous function on the specified scheduler.
4893
+ *
4894
+ * @example
4895
+ * var res = Rx.Observable.toAsync(function (x, y) { return x + y; })(4, 3);
4896
+ * var res = Rx.Observable.toAsync(function (x, y) { return x + y; }, Rx.Scheduler.timeout)(4, 3);
4897
+ * var res = Rx.Observable.toAsync(function (x) { this.log(x); }, Rx.Scheduler.timeout, console)('hello');
4898
+ *
4899
+ * @param {Function} function Function to convert to an asynchronous function.
4900
+ * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
4901
+ * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
4902
+ * @returns {Function} Asynchronous function.
4903
+ */
4904
+ var observableToAsync = Observable.toAsync = function (func, context, scheduler) {
4905
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
4906
+ return function () {
4907
+ var args = arguments,
4908
+ subject = new AsyncSubject();
4909
+
4910
+ scheduler.schedule(function () {
4911
+ var result;
4912
+ try {
4913
+ result = func.apply(context, args);
4914
+ } catch (e) {
4915
+ subject.onError(e);
4916
+ return;
4917
+ }
4918
+ subject.onNext(result);
4919
+ subject.onCompleted();
4920
+ });
4921
+ return subject.asObservable();
4878
4922
  };
4923
+ };
4879
4924
 
4880
- /**
4881
- * Converts a callback function to an observable sequence.
4882
- *
4883
- * @param {Function} function Function with a callback as the last parameter to convert to an Observable sequence.
4884
- * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
4885
- * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
4886
- * @param {Function} [selector] A selector which takes the arguments from the callback to produce a single item to yield on next.
4887
- * @returns {Function} A function, when executed with the required parameters minus the callback, produces an Observable sequence with a single value of the arguments to the callback as an array.
4888
- */
4889
- Observable.fromCallback = function (func, scheduler, context, selector) {
4890
- scheduler || (scheduler = immediateScheduler);
4891
- return function () {
4892
- var args = slice.call(arguments, 0);
4925
+ /**
4926
+ * Converts a callback function to an observable sequence.
4927
+ *
4928
+ * @param {Function} function Function with a callback as the last parameter to convert to an Observable sequence.
4929
+ * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
4930
+ * @param {Function} [selector] A selector which takes the arguments from the callback to produce a single item to yield on next.
4931
+ * @returns {Function} A function, when executed with the required parameters minus the callback, produces an Observable sequence with a single value of the arguments to the callback as an array.
4932
+ */
4933
+ Observable.fromCallback = function (func, context, selector) {
4934
+ return function () {
4935
+ var args = slice.call(arguments, 0);
4893
4936
 
4894
- return new AnonymousObservable(function (observer) {
4895
- return scheduler.schedule(function () {
4896
- function handler(e) {
4897
- var results = e;
4898
-
4899
- if (selector) {
4900
- try {
4901
- results = selector(arguments);
4902
- } catch (err) {
4903
- observer.onError(err);
4904
- return;
4905
- }
4906
- } else {
4907
- if (results.length === 1) {
4908
- results = results[0];
4909
- }
4910
- }
4937
+ return new AnonymousObservable(function (observer) {
4938
+ function handler(e) {
4939
+ var results = e;
4940
+
4941
+ if (selector) {
4942
+ try {
4943
+ results = selector(arguments);
4944
+ } catch (err) {
4945
+ observer.onError(err);
4946
+ return;
4947
+ }
4911
4948
 
4912
- observer.onNext(results);
4913
- observer.onCompleted();
4914
- }
4949
+ observer.onNext(results);
4950
+ } else {
4951
+ if (results.length <= 1) {
4952
+ observer.onNext.apply(observer, results);
4953
+ } else {
4954
+ observer.onNext(results);
4955
+ }
4956
+ }
4957
+
4958
+ observer.onCompleted();
4959
+ }
4915
4960
 
4916
- args.push(handler);
4917
- func.apply(context, args);
4918
- });
4919
- });
4920
- };
4961
+ args.push(handler);
4962
+ func.apply(context, args);
4963
+ });
4921
4964
  };
4965
+ };
4922
4966
 
4923
- /**
4924
- * Converts a Node.js callback style function to an observable sequence. This must be in function (err, ...) format.
4925
- * @param {Function} func The function to call
4926
- * @param {Scheduler} [scheduler] Scheduler to run the function on. If not specified, defaults to Scheduler.timeout.
4927
- * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
4928
- * @param {Function} [selector] A selector which takes the arguments from the callback minus the error to produce a single item to yield on next.
4929
- * @returns {Function} An async function which when applied, returns an observable sequence with the callback arguments as an array.
4930
- */
4931
- Observable.fromNodeCallback = function (func, scheduler, context, selector) {
4932
- scheduler || (scheduler = immediateScheduler);
4933
- return function () {
4934
- var args = slice.call(arguments, 0);
4967
+ /**
4968
+ * Converts a Node.js callback style function to an observable sequence. This must be in function (err, ...) format.
4969
+ * @param {Function} func The function to call
4970
+ * @param {Mixed} [context] The context for the func parameter to be executed. If not specified, defaults to undefined.
4971
+ * @param {Function} [selector] A selector which takes the arguments from the callback minus the error to produce a single item to yield on next.
4972
+ * @returns {Function} An async function which when applied, returns an observable sequence with the callback arguments as an array.
4973
+ */
4974
+ Observable.fromNodeCallback = function (func, context, selector) {
4975
+ return function () {
4976
+ var args = slice.call(arguments, 0);
4935
4977
 
4936
- return new AnonymousObservable(function (observer) {
4937
- return scheduler.schedule(function () {
4938
-
4939
- function handler(err) {
4940
- if (err) {
4941
- observer.onError(err);
4942
- return;
4943
- }
4978
+ return new AnonymousObservable(function (observer) {
4979
+ function handler(err) {
4980
+ if (err) {
4981
+ observer.onError(err);
4982
+ return;
4983
+ }
4944
4984
 
4945
- var results = slice.call(arguments, 1);
4946
-
4947
- if (selector) {
4948
- try {
4949
- results = selector(results);
4950
- } catch (e) {
4951
- observer.onError(e);
4952
- return;
4953
- }
4954
- } else {
4955
- if (results.length === 1) {
4956
- results = results[0];
4957
- }
4958
- }
4985
+ var results = slice.call(arguments, 1);
4986
+
4987
+ if (selector) {
4988
+ try {
4989
+ results = selector(results);
4990
+ } catch (e) {
4991
+ observer.onError(e);
4992
+ return;
4993
+ }
4994
+ observer.onNext(results);
4995
+ } else {
4996
+ if (results.length <= 1) {
4997
+ observer.onNext.apply(observer, results);
4998
+ } else {
4999
+ observer.onNext(results);
5000
+ }
5001
+ }
4959
5002
 
4960
- observer.onNext(results);
4961
- observer.onCompleted();
4962
- }
5003
+ observer.onCompleted();
5004
+ }
4963
5005
 
4964
- args.push(handler);
4965
- func.apply(context, args);
4966
- });
4967
- });
4968
- };
5006
+ args.push(handler);
5007
+ func.apply(context, args);
5008
+ });
4969
5009
  };
5010
+ };
4970
5011
 
4971
5012
  function fixEvent(event) {
4972
5013
  var stopPropagation = function () {
@@ -5662,24 +5703,24 @@
5662
5703
  }, selector);
5663
5704
  };
5664
5705
 
5665
- /**
5666
- * Returns an observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer.
5667
- * This operator is a specialization of replay which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed.
5668
- *
5669
- * @example
5670
- * var res = source.replayWhileObserved(3);
5671
- * var res = source.replayWhileObserved(3, 500);
5672
- * var res = source.replayWhileObserved(3, 500, scheduler);
5673
- *
5706
+ /**
5707
+ * Returns an observable sequence that shares a single subscription to the underlying sequence replaying notifications subject to a maximum time length for the replay buffer.
5708
+ * This operator is a specialization of replay which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed.
5709
+ *
5710
+ * @example
5711
+ * var res = source.shareReplay(3);
5712
+ * var res = source.shareReplay(3, 500);
5713
+ * var res = source.shareReplay(3, 500, scheduler);
5714
+ *
5674
5715
 
5675
- * @param bufferSize [Optional] Maximum element count of the replay buffer.
5676
- * @param window [Optional] Maximum time length of the replay buffer.
5677
- * @param scheduler [Optional] Scheduler where connected observers within the selector function will be invoked on.
5678
- * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence.
5679
- */
5680
- observableProto.replayWhileObserved = function (bufferSize, window, scheduler) {
5681
- return this.replay(null, bufferSize, window, scheduler).refCount();
5682
- };
5716
+ * @param bufferSize [Optional] Maximum element count of the replay buffer.
5717
+ * @param window [Optional] Maximum time length of the replay buffer.
5718
+ * @param scheduler [Optional] Scheduler where connected observers within the selector function will be invoked on.
5719
+ * @returns {Observable} An observable sequence that contains the elements of a sequence produced by multicasting the source sequence.
5720
+ */
5721
+ observableProto.shareReplay = function (bufferSize, window, scheduler) {
5722
+ return this.replay(null, bufferSize, window, scheduler).refCount();
5723
+ };
5683
5724
 
5684
5725
  /** @private */
5685
5726
  var InnerSubscription = function (subject, observer) {
@@ -6807,6 +6848,7 @@
6807
6848
  */
6808
6849
  Observable['case'] = Observable.switchCase = function (selector, sources, defaultSourceOrScheduler) {
6809
6850
  return observableDefer(function () {
6851
+ isPromise(defaultSourceOrScheduler) && (defaultSourceOrScheduler = observableFromPromise(defaultSourceOrScheduler));
6810
6852
  defaultSourceOrScheduler || (defaultSourceOrScheduler = observableEmpty());
6811
6853
 
6812
6854
  typeof defaultSourceOrScheduler.now === 'function' && (defaultSourceOrScheduler = observableEmpty(defaultSourceOrScheduler));
@@ -6818,69 +6860,69 @@
6818
6860
  });
6819
6861
  };
6820
6862
 
6821
- /**
6822
- * Expands an observable sequence by recursively invoking selector.
6823
- *
6824
- * @param {Function} selector Selector function to invoke for each produced element, resulting in another sequence to which the selector will be invoked recursively again.
6825
- * @param {Scheduler} [scheduler] Scheduler on which to perform the expansion. If not provided, this defaults to the current thread scheduler.
6826
- * @returns {Observable} An observable sequence containing all the elements produced by the recursive expansion.
6827
- */
6828
- observableProto.expand = function (selector, scheduler) {
6829
- scheduler || (scheduler = immediateScheduler);
6830
- var source = this;
6831
- return new AnonymousObservable(function (observer) {
6832
- var q = [],
6833
- m = new SerialDisposable(),
6834
- d = new CompositeDisposable(m),
6835
- activeCount = 0,
6836
- isAcquired = false;
6837
-
6838
- var ensureActive = function () {
6839
- var isOwner = false;
6840
- if (q.length > 0) {
6841
- isOwner = !isAcquired;
6842
- isAcquired = true;
6843
- }
6844
- if (isOwner) {
6845
- m.setDisposable(scheduler.scheduleRecursive(function (self) {
6846
- var work;
6847
- if (q.length > 0) {
6848
- work = q.shift();
6849
- } else {
6850
- isAcquired = false;
6851
- return;
6852
- }
6853
- var m1 = new SingleAssignmentDisposable();
6854
- d.add(m1);
6855
- m1.setDisposable(work.subscribe(function (x) {
6856
- observer.onNext(x);
6857
- var result = null;
6858
- try {
6859
- result = selector(x);
6860
- } catch (e) {
6861
- observer.onError(e);
6862
- }
6863
- q.push(result);
6864
- activeCount++;
6865
- ensureActive();
6866
- }, observer.onError.bind(observer), function () {
6867
- d.remove(m1);
6868
- activeCount--;
6869
- if (activeCount === 0) {
6870
- observer.onCompleted();
6871
- }
6872
- }));
6873
- self();
6874
- }));
6875
- }
6876
- };
6863
+ /**
6864
+ * Expands an observable sequence by recursively invoking selector.
6865
+ *
6866
+ * @param {Function} selector Selector function to invoke for each produced element, resulting in another sequence to which the selector will be invoked recursively again.
6867
+ * @param {Scheduler} [scheduler] Scheduler on which to perform the expansion. If not provided, this defaults to the current thread scheduler.
6868
+ * @returns {Observable} An observable sequence containing all the elements produced by the recursive expansion.
6869
+ */
6870
+ observableProto.expand = function (selector, scheduler) {
6871
+ isScheduler(scheduler) || (scheduler = immediateScheduler);
6872
+ var source = this;
6873
+ return new AnonymousObservable(function (observer) {
6874
+ var q = [],
6875
+ m = new SerialDisposable(),
6876
+ d = new CompositeDisposable(m),
6877
+ activeCount = 0,
6878
+ isAcquired = false;
6879
+
6880
+ var ensureActive = function () {
6881
+ var isOwner = false;
6882
+ if (q.length > 0) {
6883
+ isOwner = !isAcquired;
6884
+ isAcquired = true;
6885
+ }
6886
+ if (isOwner) {
6887
+ m.setDisposable(scheduler.scheduleRecursive(function (self) {
6888
+ var work;
6889
+ if (q.length > 0) {
6890
+ work = q.shift();
6891
+ } else {
6892
+ isAcquired = false;
6893
+ return;
6894
+ }
6895
+ var m1 = new SingleAssignmentDisposable();
6896
+ d.add(m1);
6897
+ m1.setDisposable(work.subscribe(function (x) {
6898
+ observer.onNext(x);
6899
+ var result = null;
6900
+ try {
6901
+ result = selector(x);
6902
+ } catch (e) {
6903
+ observer.onError(e);
6904
+ }
6905
+ q.push(result);
6906
+ activeCount++;
6907
+ ensureActive();
6908
+ }, observer.onError.bind(observer), function () {
6909
+ d.remove(m1);
6910
+ activeCount--;
6911
+ if (activeCount === 0) {
6912
+ observer.onCompleted();
6913
+ }
6914
+ }));
6915
+ self();
6916
+ }));
6917
+ }
6918
+ };
6877
6919
 
6878
- q.push(source);
6879
- activeCount++;
6880
- ensureActive();
6881
- return d;
6882
- });
6883
- };
6920
+ q.push(source);
6921
+ activeCount++;
6922
+ ensureActive();
6923
+ return d;
6924
+ });
6925
+ };
6884
6926
 
6885
6927
  /**
6886
6928
  * Runs all observable sequences in parallel and collect their last elements.
@@ -7023,82 +7065,73 @@
7023
7065
  });
7024
7066
  };
7025
7067
 
7026
- /**
7027
- * Comonadic bind operator.
7028
- * @param {Function} selector A transform function to apply to each element.
7029
- * @param {Object} scheduler Scheduler used to execute the operation. If not specified, defaults to the ImmediateScheduler.
7030
- * @returns {Observable} An observable sequence which results from the comonadic bind operation.
7031
- */
7032
- observableProto.manySelect = function (selector, scheduler) {
7033
- scheduler || (scheduler = immediateScheduler);
7034
- var source = this;
7035
- return observableDefer(function () {
7036
- var chain;
7037
-
7038
- return source
7039
- .select(
7040
- function (x) {
7041
- var curr = new ChainObservable(x);
7042
- if (chain) {
7043
- chain.onNext(x);
7044
- }
7045
- chain = curr;
7046
-
7047
- return curr;
7048
- })
7049
- .doAction(
7050
- noop,
7051
- function (e) {
7052
- if (chain) {
7053
- chain.onError(e);
7054
- }
7055
- },
7056
- function () {
7057
- if (chain) {
7058
- chain.onCompleted();
7059
- }
7060
- })
7061
- .observeOn(scheduler)
7062
- .select(function (x, i, o) { return selector(x, i, o); });
7063
- });
7064
- };
7068
+ /**
7069
+ * Comonadic bind operator.
7070
+ * @param {Function} selector A transform function to apply to each element.
7071
+ * @param {Object} scheduler Scheduler used to execute the operation. If not specified, defaults to the ImmediateScheduler.
7072
+ * @returns {Observable} An observable sequence which results from the comonadic bind operation.
7073
+ */
7074
+ observableProto.manySelect = function (selector, scheduler) {
7075
+ isScheduler(scheduler) || (scheduler = immediateScheduler);
7076
+ var source = this;
7077
+ return observableDefer(function () {
7078
+ var chain;
7065
7079
 
7066
- var ChainObservable = (function (_super) {
7080
+ return source
7081
+ .map(function (x) {
7082
+ var curr = new ChainObservable(x);
7067
7083
 
7068
- function subscribe (observer) {
7069
- var self = this, g = new CompositeDisposable();
7070
- g.add(currentThreadScheduler.schedule(function () {
7071
- observer.onNext(self.head);
7072
- g.add(self.tail.mergeObservable().subscribe(observer));
7073
- }));
7084
+ chain && chain.onNext(x);
7085
+ chain = curr;
7074
7086
 
7075
- return g;
7076
- }
7087
+ return curr;
7088
+ })
7089
+ .doAction(
7090
+ noop,
7091
+ function (e) { chain && chain.onError(e); },
7092
+ function () { chain && chain.onCompleted(); }
7093
+ )
7094
+ .observeOn(scheduler)
7095
+ .map(selector);
7096
+ });
7097
+ };
7077
7098
 
7078
- inherits(ChainObservable, _super);
7099
+ var ChainObservable = (function (__super__) {
7079
7100
 
7080
- function ChainObservable(head) {
7081
- _super.call(this, subscribe);
7082
- this.head = head;
7083
- this.tail = new AsyncSubject();
7084
- }
7101
+ function subscribe (observer) {
7102
+ var self = this, g = new CompositeDisposable();
7103
+ g.add(currentThreadScheduler.schedule(function () {
7104
+ observer.onNext(self.head);
7105
+ g.add(self.tail.mergeObservable().subscribe(observer));
7106
+ }));
7085
7107
 
7086
- addProperties(ChainObservable.prototype, Observer, {
7087
- onCompleted: function () {
7088
- this.onNext(Observable.empty());
7089
- },
7090
- onError: function (e) {
7091
- this.onNext(Observable.throwException(e));
7092
- },
7093
- onNext: function (v) {
7094
- this.tail.onNext(v);
7095
- this.tail.onCompleted();
7096
- }
7097
- });
7108
+ return g;
7109
+ }
7098
7110
 
7099
- return ChainObservable;
7111
+ inherits(ChainObservable, __super__);
7100
7112
 
7101
- }(Observable));
7113
+ function ChainObservable(head) {
7114
+ __super__.call(this, subscribe);
7115
+ this.head = head;
7116
+ this.tail = new AsyncSubject();
7117
+ }
7118
+
7119
+ addProperties(ChainObservable.prototype, Observer, {
7120
+ onCompleted: function () {
7121
+ this.onNext(Observable.empty());
7122
+ },
7123
+ onError: function (e) {
7124
+ this.onNext(Observable.throwException(e));
7125
+ },
7126
+ onNext: function (v) {
7127
+ this.tail.onNext(v);
7128
+ this.tail.onCompleted();
7129
+ }
7130
+ });
7131
+
7132
+ return ChainObservable;
7133
+
7134
+ }(Observable));
7102
7135
 
7103
7136
  /** @private */
7104
7137
  var Map = (function () {
@@ -7201,51 +7234,51 @@
7201
7234
  * @param selector Selector that will be invoked with available values from the source sequences, in the same order of the sequences in the pattern.
7202
7235
  * @return Plan that produces the projected values, to be fed (with other plans) to the when operator.
7203
7236
  */
7204
- Pattern.prototype.then = function (selector) {
7237
+ Pattern.prototype.thenDo = function (selector) {
7205
7238
  return new Plan(this, selector);
7206
7239
  };
7207
7240
 
7208
- function Plan(expression, selector) {
7209
- this.expression = expression;
7210
- this.selector = selector;
7241
+ function Plan(expression, selector) {
7242
+ this.expression = expression;
7243
+ this.selector = selector;
7244
+ }
7245
+
7246
+ Plan.prototype.activate = function (externalSubscriptions, observer, deactivate) {
7247
+ var self = this;
7248
+ var joinObservers = [];
7249
+ for (var i = 0, len = this.expression.patterns.length; i < len; i++) {
7250
+ joinObservers.push(planCreateObserver(externalSubscriptions, this.expression.patterns[i], observer.onError.bind(observer)));
7211
7251
  }
7212
-
7213
- Plan.prototype.activate = function (externalSubscriptions, observer, deactivate) {
7214
- var self = this;
7215
- var joinObservers = [];
7216
- for (var i = 0, len = this.expression.patterns.length; i < len; i++) {
7217
- joinObservers.push(planCreateObserver(externalSubscriptions, this.expression.patterns[i], observer.onError.bind(observer)));
7218
- }
7219
- var activePlan = new ActivePlan(joinObservers, function () {
7220
- var result;
7221
- try {
7222
- result = self.selector.apply(self, arguments);
7223
- } catch (exception) {
7224
- observer.onError(exception);
7225
- return;
7226
- }
7227
- observer.onNext(result);
7228
- }, function () {
7229
- for (var j = 0, jlen = joinObservers.length; j < jlen; j++) {
7230
- joinObservers[j].removeActivePlan(activePlan);
7231
- }
7232
- deactivate(activePlan);
7233
- });
7234
- for (i = 0, len = joinObservers.length; i < len; i++) {
7235
- joinObservers[i].addActivePlan(activePlan);
7236
- }
7237
- return activePlan;
7238
- };
7252
+ var activePlan = new ActivePlan(joinObservers, function () {
7253
+ var result;
7254
+ try {
7255
+ result = self.selector.apply(self, arguments);
7256
+ } catch (exception) {
7257
+ observer.onError(exception);
7258
+ return;
7259
+ }
7260
+ observer.onNext(result);
7261
+ }, function () {
7262
+ for (var j = 0, jlen = joinObservers.length; j < jlen; j++) {
7263
+ joinObservers[j].removeActivePlan(activePlan);
7264
+ }
7265
+ deactivate(activePlan);
7266
+ });
7267
+ for (i = 0, len = joinObservers.length; i < len; i++) {
7268
+ joinObservers[i].addActivePlan(activePlan);
7269
+ }
7270
+ return activePlan;
7271
+ };
7239
7272
 
7240
- function planCreateObserver(externalSubscriptions, observable, onError) {
7241
- var entry = externalSubscriptions.get(observable);
7242
- if (!entry) {
7243
- var observer = new JoinObserver(observable, onError);
7244
- externalSubscriptions.set(observable, observer);
7245
- return observer;
7246
- }
7247
- return entry;
7273
+ function planCreateObserver(externalSubscriptions, observable, onError) {
7274
+ var entry = externalSubscriptions.get(observable);
7275
+ if (!entry) {
7276
+ var observer = new JoinObserver(observable, onError);
7277
+ externalSubscriptions.set(observable, observer);
7278
+ return observer;
7248
7279
  }
7280
+ return entry;
7281
+ }
7249
7282
 
7250
7283
  // Active Plan
7251
7284
  function ActivePlan(joinObserverArray, onNext, onCompleted) {
@@ -7390,25 +7423,25 @@
7390
7423
  return JoinObserver;
7391
7424
  } (AbstractObserver));
7392
7425
 
7393
- /**
7394
- * Creates a pattern that matches when both observable sequences have an available value.
7395
- *
7396
- * @param right Observable sequence to match with the current sequence.
7397
- * @return {Pattern} Pattern object that matches when both observable sequences have an available value.
7398
- */
7399
- observableProto.and = function (right) {
7400
- return new Pattern([this, right]);
7401
- };
7426
+ /**
7427
+ * Creates a pattern that matches when both observable sequences have an available value.
7428
+ *
7429
+ * @param right Observable sequence to match with the current sequence.
7430
+ * @return {Pattern} Pattern object that matches when both observable sequences have an available value.
7431
+ */
7432
+ observableProto.and = function (right) {
7433
+ return new Pattern([this, right]);
7434
+ };
7402
7435
 
7403
- /**
7404
- * Matches when the observable sequence has an available value and projects the value.
7405
- *
7406
- * @param selector Selector that will be invoked for values in the source sequence.
7407
- * @returns {Plan} Plan that produces the projected values, to be fed (with other plans) to the when operator.
7408
- */
7409
- observableProto.then = function (selector) {
7410
- return new Pattern([this]).then(selector);
7411
- };
7436
+ /**
7437
+ * Matches when the observable sequence has an available value and projects the value.
7438
+ *
7439
+ * @param selector Selector that will be invoked for values in the source sequence.
7440
+ * @returns {Plan} Plan that produces the projected values, to be fed (with other plans) to the when operator.
7441
+ */
7442
+ observableProto.thenDo = function (selector) {
7443
+ return new Pattern([this]).thenDo(selector);
7444
+ };
7412
7445
 
7413
7446
  /**
7414
7447
  * Joins together the results from several patterns.
@@ -7466,24 +7499,24 @@
7466
7499
  });
7467
7500
  }
7468
7501
 
7469
- function observableTimerDateAndPeriod(dueTime, period, scheduler) {
7470
- var p = normalizeTime(period);
7471
- return new AnonymousObservable(function (observer) {
7472
- var count = 0, d = dueTime;
7473
- return scheduler.scheduleRecursiveWithAbsolute(d, function (self) {
7474
- var now;
7475
- if (p > 0) {
7476
- now = scheduler.now();
7477
- d = d + p;
7478
- if (d <= now) {
7479
- d = now + p;
7480
- }
7481
- }
7482
- observer.onNext(count++);
7483
- self(d);
7484
- });
7485
- });
7486
- }
7502
+ function observableTimerDateAndPeriod(dueTime, period, scheduler) {
7503
+ var p = normalizeTime(period);
7504
+ return new AnonymousObservable(function (observer) {
7505
+ var count = 0, d = dueTime;
7506
+ return scheduler.scheduleRecursiveWithAbsolute(d, function (self) {
7507
+ var now;
7508
+ if (p > 0) {
7509
+ now = scheduler.now();
7510
+ d = d + p;
7511
+ if (d <= now) {
7512
+ d = now + p;
7513
+ }
7514
+ }
7515
+ observer.onNext(count++);
7516
+ self(d);
7517
+ });
7518
+ });
7519
+ }
7487
7520
 
7488
7521
  function observableTimerTimeSpan(dueTime, scheduler) {
7489
7522
  var d = normalizeTime(dueTime);
@@ -7495,357 +7528,343 @@
7495
7528
  });
7496
7529
  }
7497
7530
 
7498
- function observableTimerTimeSpanAndPeriod(dueTime, period, scheduler) {
7499
- if (dueTime === period) {
7500
- return new AnonymousObservable(function (observer) {
7501
- return scheduler.schedulePeriodicWithState(0, period, function (count) {
7502
- observer.onNext(count);
7503
- return count + 1;
7504
- });
7505
- });
7506
- }
7507
- return observableDefer(function () {
7508
- return observableTimerDateAndPeriod(scheduler.now() + dueTime, period, scheduler);
7531
+ function observableTimerTimeSpanAndPeriod(dueTime, period, scheduler) {
7532
+ if (dueTime === period) {
7533
+ return new AnonymousObservable(function (observer) {
7534
+ return scheduler.schedulePeriodicWithState(0, period, function (count) {
7535
+ observer.onNext(count);
7536
+ return count + 1;
7509
7537
  });
7538
+ });
7510
7539
  }
7540
+ return observableDefer(function () {
7541
+ return observableTimerDateAndPeriod(scheduler.now() + dueTime, period, scheduler);
7542
+ });
7543
+ }
7511
7544
 
7512
- /**
7513
- * Returns an observable sequence that produces a value after each period.
7514
- *
7515
- * @example
7516
- * 1 - res = Rx.Observable.interval(1000);
7517
- * 2 - res = Rx.Observable.interval(1000, Rx.Scheduler.timeout);
7518
- *
7519
- * @param {Number} period Period for producing the values in the resulting sequence (specified as an integer denoting milliseconds).
7520
- * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, Rx.Scheduler.timeout is used.
7521
- * @returns {Observable} An observable sequence that produces a value after each period.
7522
- */
7523
- var observableinterval = Observable.interval = function (period, scheduler) {
7524
- scheduler || (scheduler = timeoutScheduler);
7525
- return observableTimerTimeSpanAndPeriod(period, period, scheduler);
7526
- };
7545
+ /**
7546
+ * Returns an observable sequence that produces a value after each period.
7547
+ *
7548
+ * @example
7549
+ * 1 - res = Rx.Observable.interval(1000);
7550
+ * 2 - res = Rx.Observable.interval(1000, Rx.Scheduler.timeout);
7551
+ *
7552
+ * @param {Number} period Period for producing the values in the resulting sequence (specified as an integer denoting milliseconds).
7553
+ * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, Rx.Scheduler.timeout is used.
7554
+ * @returns {Observable} An observable sequence that produces a value after each period.
7555
+ */
7556
+ var observableinterval = Observable.interval = function (period, scheduler) {
7557
+ return observableTimerTimeSpanAndPeriod(period, period, isScheduler(scheduler) ? scheduler : timeoutScheduler);
7558
+ };
7527
7559
 
7528
- /**
7529
- * Returns an observable sequence that produces a value after dueTime has elapsed and then after each period.
7530
- *
7531
- * @example
7532
- * 1 - res = Rx.Observable.timer(new Date());
7533
- * 2 - res = Rx.Observable.timer(new Date(), 1000);
7534
- * 3 - res = Rx.Observable.timer(new Date(), Rx.Scheduler.timeout);
7535
- * 4 - res = Rx.Observable.timer(new Date(), 1000, Rx.Scheduler.timeout);
7536
- *
7537
- * 5 - res = Rx.Observable.timer(5000);
7538
- * 6 - res = Rx.Observable.timer(5000, 1000);
7539
- * 7 - res = Rx.Observable.timer(5000, Rx.Scheduler.timeout);
7540
- * 8 - res = Rx.Observable.timer(5000, 1000, Rx.Scheduler.timeout);
7541
- *
7542
- * @param {Number} dueTime Absolute (specified as a Date object) or relative time (specified as an integer denoting milliseconds) at which to produce the first value.
7543
- * @param {Mixed} [periodOrScheduler] Period to produce subsequent values (specified as an integer denoting milliseconds), or the scheduler to run the timer on. If not specified, the resulting timer is not recurring.
7544
- * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, the timeout scheduler is used.
7545
- * @returns {Observable} An observable sequence that produces a value after due time has elapsed and then each period.
7546
- */
7547
- var observableTimer = Observable.timer = function (dueTime, periodOrScheduler, scheduler) {
7548
- var period;
7549
- scheduler || (scheduler = timeoutScheduler);
7550
- if (periodOrScheduler !== undefined && typeof periodOrScheduler === 'number') {
7551
- period = periodOrScheduler;
7552
- } else if (periodOrScheduler !== undefined && typeof periodOrScheduler === 'object') {
7553
- scheduler = periodOrScheduler;
7554
- }
7555
- if (dueTime instanceof Date && period === undefined) {
7556
- return observableTimerDate(dueTime.getTime(), scheduler);
7557
- }
7558
- if (dueTime instanceof Date && period !== undefined) {
7559
- period = periodOrScheduler;
7560
- return observableTimerDateAndPeriod(dueTime.getTime(), period, scheduler);
7560
+ /**
7561
+ * Returns an observable sequence that produces a value after dueTime has elapsed and then after each period.
7562
+ *
7563
+ * @example
7564
+ * 1 - res = Rx.Observable.timer(new Date());
7565
+ * 2 - res = Rx.Observable.timer(new Date(), 1000);
7566
+ * 3 - res = Rx.Observable.timer(new Date(), Rx.Scheduler.timeout);
7567
+ * 4 - res = Rx.Observable.timer(new Date(), 1000, Rx.Scheduler.timeout);
7568
+ *
7569
+ * 5 - res = Rx.Observable.timer(5000);
7570
+ * 6 - res = Rx.Observable.timer(5000, 1000);
7571
+ * 7 - res = Rx.Observable.timer(5000, Rx.Scheduler.timeout);
7572
+ * 8 - res = Rx.Observable.timer(5000, 1000, Rx.Scheduler.timeout);
7573
+ *
7574
+ * @param {Number} dueTime Absolute (specified as a Date object) or relative time (specified as an integer denoting milliseconds) at which to produce the first value.
7575
+ * @param {Mixed} [periodOrScheduler] Period to produce subsequent values (specified as an integer denoting milliseconds), or the scheduler to run the timer on. If not specified, the resulting timer is not recurring.
7576
+ * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, the timeout scheduler is used.
7577
+ * @returns {Observable} An observable sequence that produces a value after due time has elapsed and then each period.
7578
+ */
7579
+ var observableTimer = Observable.timer = function (dueTime, periodOrScheduler, scheduler) {
7580
+ var period;
7581
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7582
+ if (periodOrScheduler !== undefined && typeof periodOrScheduler === 'number') {
7583
+ period = periodOrScheduler;
7584
+ } else if (periodOrScheduler !== undefined && typeof periodOrScheduler === 'object') {
7585
+ scheduler = periodOrScheduler;
7586
+ }
7587
+ if (dueTime instanceof Date && notDefined(period)) {
7588
+ return observableTimerDate(dueTime.getTime(), scheduler);
7589
+ }
7590
+ if (dueTime instanceof Date && period !== undefined) {
7591
+ period = periodOrScheduler;
7592
+ return observableTimerDateAndPeriod(dueTime.getTime(), period, scheduler);
7593
+ }
7594
+ return notDefined(period) ?
7595
+ observableTimerTimeSpan(dueTime, scheduler) :
7596
+ observableTimerTimeSpanAndPeriod(dueTime, period, scheduler);
7597
+ };
7598
+
7599
+ function observableDelayTimeSpan(source, dueTime, scheduler) {
7600
+ return new AnonymousObservable(function (observer) {
7601
+ var active = false,
7602
+ cancelable = new SerialDisposable(),
7603
+ exception = null,
7604
+ q = [],
7605
+ running = false,
7606
+ subscription;
7607
+ subscription = source.materialize().timestamp(scheduler).subscribe(function (notification) {
7608
+ var d, shouldRun;
7609
+ if (notification.value.kind === 'E') {
7610
+ q = [];
7611
+ q.push(notification);
7612
+ exception = notification.value.exception;
7613
+ shouldRun = !running;
7614
+ } else {
7615
+ q.push({ value: notification.value, timestamp: notification.timestamp + dueTime });
7616
+ shouldRun = !active;
7617
+ active = true;
7561
7618
  }
7562
- if (period === undefined) {
7563
- return observableTimerTimeSpan(dueTime, scheduler);
7619
+ if (shouldRun) {
7620
+ if (exception !== null) {
7621
+ observer.onError(exception);
7622
+ } else {
7623
+ d = new SingleAssignmentDisposable();
7624
+ cancelable.setDisposable(d);
7625
+ d.setDisposable(scheduler.scheduleRecursiveWithRelative(dueTime, function (self) {
7626
+ var e, recurseDueTime, result, shouldRecurse;
7627
+ if (exception !== null) {
7628
+ return;
7629
+ }
7630
+ running = true;
7631
+ do {
7632
+ result = null;
7633
+ if (q.length > 0 && q[0].timestamp - scheduler.now() <= 0) {
7634
+ result = q.shift().value;
7635
+ }
7636
+ if (result !== null) {
7637
+ result.accept(observer);
7638
+ }
7639
+ } while (result !== null);
7640
+ shouldRecurse = false;
7641
+ recurseDueTime = 0;
7642
+ if (q.length > 0) {
7643
+ shouldRecurse = true;
7644
+ recurseDueTime = Math.max(0, q[0].timestamp - scheduler.now());
7645
+ } else {
7646
+ active = false;
7647
+ }
7648
+ e = exception;
7649
+ running = false;
7650
+ if (e !== null) {
7651
+ observer.onError(e);
7652
+ } else if (shouldRecurse) {
7653
+ self(recurseDueTime);
7654
+ }
7655
+ }));
7656
+ }
7564
7657
  }
7565
- return observableTimerTimeSpanAndPeriod(dueTime, period, scheduler);
7566
- };
7567
-
7568
- function observableDelayTimeSpan(dueTime, scheduler) {
7569
- var source = this;
7570
- return new AnonymousObservable(function (observer) {
7571
- var active = false,
7572
- cancelable = new SerialDisposable(),
7573
- exception = null,
7574
- q = [],
7575
- running = false,
7576
- subscription;
7577
- subscription = source.materialize().timestamp(scheduler).subscribe(function (notification) {
7578
- var d, shouldRun;
7579
- if (notification.value.kind === 'E') {
7580
- q = [];
7581
- q.push(notification);
7582
- exception = notification.value.exception;
7583
- shouldRun = !running;
7584
- } else {
7585
- q.push({ value: notification.value, timestamp: notification.timestamp + dueTime });
7586
- shouldRun = !active;
7587
- active = true;
7588
- }
7589
- if (shouldRun) {
7590
- if (exception !== null) {
7591
- observer.onError(exception);
7592
- } else {
7593
- d = new SingleAssignmentDisposable();
7594
- cancelable.setDisposable(d);
7595
- d.setDisposable(scheduler.scheduleRecursiveWithRelative(dueTime, function (self) {
7596
- var e, recurseDueTime, result, shouldRecurse;
7597
- if (exception !== null) {
7598
- return;
7599
- }
7600
- running = true;
7601
- do {
7602
- result = null;
7603
- if (q.length > 0 && q[0].timestamp - scheduler.now() <= 0) {
7604
- result = q.shift().value;
7605
- }
7606
- if (result !== null) {
7607
- result.accept(observer);
7608
- }
7609
- } while (result !== null);
7610
- shouldRecurse = false;
7611
- recurseDueTime = 0;
7612
- if (q.length > 0) {
7613
- shouldRecurse = true;
7614
- recurseDueTime = Math.max(0, q[0].timestamp - scheduler.now());
7615
- } else {
7616
- active = false;
7617
- }
7618
- e = exception;
7619
- running = false;
7620
- if (e !== null) {
7621
- observer.onError(e);
7622
- } else if (shouldRecurse) {
7623
- self(recurseDueTime);
7624
- }
7625
- }));
7626
- }
7627
- }
7628
- });
7629
- return new CompositeDisposable(subscription, cancelable);
7630
- });
7631
- }
7658
+ });
7659
+ return new CompositeDisposable(subscription, cancelable);
7660
+ });
7661
+ }
7632
7662
 
7633
- function observableDelayDate(dueTime, scheduler) {
7634
- var self = this;
7635
- return observableDefer(function () {
7636
- var timeSpan = dueTime - scheduler.now();
7637
- return observableDelayTimeSpan.call(self, timeSpan, scheduler);
7638
- });
7639
- }
7663
+ function observableDelayDate(source, dueTime, scheduler) {
7664
+ return observableDefer(function () {
7665
+ return observableDelayTimeSpan(source, dueTime - scheduler.now(), scheduler);
7666
+ });
7667
+ }
7640
7668
 
7641
- /**
7642
- * Time shifts the observable sequence by dueTime. The relative time intervals between the values are preserved.
7643
- *
7644
- * @example
7645
- * 1 - res = Rx.Observable.delay(new Date());
7646
- * 2 - res = Rx.Observable.delay(new Date(), Rx.Scheduler.timeout);
7647
- *
7648
- * 3 - res = Rx.Observable.delay(5000);
7649
- * 4 - res = Rx.Observable.delay(5000, 1000, Rx.Scheduler.timeout);
7650
- * @memberOf Observable#
7651
- * @param {Number} dueTime Absolute (specified as a Date object) or relative time (specified as an integer denoting milliseconds) by which to shift the observable sequence.
7652
- * @param {Scheduler} [scheduler] Scheduler to run the delay timers on. If not specified, the timeout scheduler is used.
7653
- * @returns {Observable} Time-shifted sequence.
7654
- */
7655
- observableProto.delay = function (dueTime, scheduler) {
7656
- scheduler || (scheduler = timeoutScheduler);
7657
- return dueTime instanceof Date ?
7658
- observableDelayDate.call(this, dueTime.getTime(), scheduler) :
7659
- observableDelayTimeSpan.call(this, dueTime, scheduler);
7660
- };
7669
+ /**
7670
+ * Time shifts the observable sequence by dueTime. The relative time intervals between the values are preserved.
7671
+ *
7672
+ * @example
7673
+ * 1 - res = Rx.Observable.delay(new Date());
7674
+ * 2 - res = Rx.Observable.delay(new Date(), Rx.Scheduler.timeout);
7675
+ *
7676
+ * 3 - res = Rx.Observable.delay(5000);
7677
+ * 4 - res = Rx.Observable.delay(5000, 1000, Rx.Scheduler.timeout);
7678
+ * @memberOf Observable#
7679
+ * @param {Number} dueTime Absolute (specified as a Date object) or relative time (specified as an integer denoting milliseconds) by which to shift the observable sequence.
7680
+ * @param {Scheduler} [scheduler] Scheduler to run the delay timers on. If not specified, the timeout scheduler is used.
7681
+ * @returns {Observable} Time-shifted sequence.
7682
+ */
7683
+ observableProto.delay = function (dueTime, scheduler) {
7684
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7685
+ return dueTime instanceof Date ?
7686
+ observableDelayDate(this, dueTime.getTime(), scheduler) :
7687
+ observableDelayTimeSpan(this, dueTime, scheduler);
7688
+ };
7661
7689
 
7662
- /**
7663
- * Ignores values from an observable sequence which are followed by another value before dueTime.
7664
- *
7665
- * @example
7666
- * 1 - res = source.throttle(5000); // 5 seconds
7667
- * 2 - res = source.throttle(5000, scheduler);
7668
- *
7669
- * @param {Number} dueTime Duration of the throttle period for each value (specified as an integer denoting milliseconds).
7670
- * @param {Scheduler} [scheduler] Scheduler to run the throttle timers on. If not specified, the timeout scheduler is used.
7671
- * @returns {Observable} The throttled sequence.
7672
- */
7673
- observableProto.throttle = function (dueTime, scheduler) {
7674
- scheduler || (scheduler = timeoutScheduler);
7675
- var source = this;
7676
- return this.throttleWithSelector(function () { return observableTimer(dueTime, scheduler); })
7677
- };
7690
+ /**
7691
+ * Ignores values from an observable sequence which are followed by another value before dueTime.
7692
+ *
7693
+ * @example
7694
+ * 1 - res = source.throttle(5000); // 5 seconds
7695
+ * 2 - res = source.throttle(5000, scheduler);
7696
+ *
7697
+ * @param {Number} dueTime Duration of the throttle period for each value (specified as an integer denoting milliseconds).
7698
+ * @param {Scheduler} [scheduler] Scheduler to run the throttle timers on. If not specified, the timeout scheduler is used.
7699
+ * @returns {Observable} The throttled sequence.
7700
+ */
7701
+ observableProto.throttle = function (dueTime, scheduler) {
7702
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7703
+ return this.throttleWithSelector(function () { return observableTimer(dueTime, scheduler); })
7704
+ };
7678
7705
 
7679
- /**
7680
- * Projects each element of an observable sequence into zero or more windows which are produced based on timing information.
7681
- *
7682
- * @example
7683
- * 1 - res = xs.windowWithTime(1000, scheduler); // non-overlapping segments of 1 second
7684
- * 2 - res = xs.windowWithTime(1000, 500 , scheduler); // segments of 1 second with time shift 0.5 seconds
7685
- *
7686
- * @param {Number} timeSpan Length of each window (specified as an integer denoting milliseconds).
7687
- * @param {Mixed} [timeShiftOrScheduler] Interval between creation of consecutive windows (specified as an integer denoting milliseconds), or an optional scheduler parameter. If not specified, the time shift corresponds to the timeSpan parameter, resulting in non-overlapping adjacent windows.
7688
- * @param {Scheduler} [scheduler] Scheduler to run windowing timers on. If not specified, the timeout scheduler is used.
7689
- * @returns {Observable} An observable sequence of windows.
7690
- */
7691
- observableProto.windowWithTime = function (timeSpan, timeShiftOrScheduler, scheduler) {
7692
- var source = this, timeShift;
7693
- if (timeShiftOrScheduler === undefined) {
7694
- timeShift = timeSpan;
7706
+ /**
7707
+ * Projects each element of an observable sequence into zero or more windows which are produced based on timing information.
7708
+ *
7709
+ * @example
7710
+ * 1 - res = xs.windowWithTime(1000, scheduler); // non-overlapping segments of 1 second
7711
+ * 2 - res = xs.windowWithTime(1000, 500 , scheduler); // segments of 1 second with time shift 0.5 seconds
7712
+ *
7713
+ * @param {Number} timeSpan Length of each window (specified as an integer denoting milliseconds).
7714
+ * @param {Mixed} [timeShiftOrScheduler] Interval between creation of consecutive windows (specified as an integer denoting milliseconds), or an optional scheduler parameter. If not specified, the time shift corresponds to the timeSpan parameter, resulting in non-overlapping adjacent windows.
7715
+ * @param {Scheduler} [scheduler] Scheduler to run windowing timers on. If not specified, the timeout scheduler is used.
7716
+ * @returns {Observable} An observable sequence of windows.
7717
+ */
7718
+ observableProto.windowWithTime = function (timeSpan, timeShiftOrScheduler, scheduler) {
7719
+ var source = this, timeShift;
7720
+
7721
+ notDefined(timeShiftOrScheduler) && (timeShift = timeSpan);
7722
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7723
+ if (typeof timeShiftOrScheduler === 'number') {
7724
+ timeShift = timeShiftOrScheduler;
7725
+ } else if (typeof timeShiftOrScheduler === 'object') {
7726
+ timeShift = timeSpan;
7727
+ scheduler = timeShiftOrScheduler;
7728
+ }
7729
+ return new AnonymousObservable(function (observer) {
7730
+ var groupDisposable,
7731
+ nextShift = timeShift,
7732
+ nextSpan = timeSpan,
7733
+ q = [],
7734
+ refCountDisposable,
7735
+ timerD = new SerialDisposable(),
7736
+ totalTime = 0;
7737
+ groupDisposable = new CompositeDisposable(timerD),
7738
+ refCountDisposable = new RefCountDisposable(groupDisposable);
7739
+
7740
+
7741
+ q.push(new Subject());
7742
+ observer.onNext(addRef(q[0], refCountDisposable));
7743
+ createTimer();
7744
+ groupDisposable.add(source.subscribe(function (x) {
7745
+ var i, len;
7746
+ for (i = 0, len = q.length; i < len; i++) {
7747
+ q[i].onNext(x);
7695
7748
  }
7696
- if (scheduler === undefined) {
7697
- scheduler = timeoutScheduler;
7749
+ }, function (e) {
7750
+ var i, len;
7751
+ for (i = 0, len = q.length; i < len; i++) {
7752
+ q[i].onError(e);
7698
7753
  }
7699
- if (typeof timeShiftOrScheduler === 'number') {
7700
- timeShift = timeShiftOrScheduler;
7701
- } else if (typeof timeShiftOrScheduler === 'object') {
7702
- timeShift = timeSpan;
7703
- scheduler = timeShiftOrScheduler;
7754
+ observer.onError(e);
7755
+ }, function () {
7756
+ var i, len;
7757
+ for (i = 0, len = q.length; i < len; i++) {
7758
+ q[i].onCompleted();
7704
7759
  }
7705
- return new AnonymousObservable(function (observer) {
7706
- var groupDisposable,
7707
- nextShift = timeShift,
7708
- nextSpan = timeSpan,
7709
- q = [],
7710
- refCountDisposable,
7711
- timerD = new SerialDisposable(),
7712
- totalTime = 0;
7713
- groupDisposable = new CompositeDisposable(timerD),
7714
- refCountDisposable = new RefCountDisposable(groupDisposable);
7760
+ observer.onCompleted();
7761
+ }));
7715
7762
 
7716
- function createTimer () {
7717
- var m = new SingleAssignmentDisposable(),
7718
- isSpan = false,
7719
- isShift = false;
7720
- timerD.setDisposable(m);
7721
- if (nextSpan === nextShift) {
7722
- isSpan = true;
7723
- isShift = true;
7724
- } else if (nextSpan < nextShift) {
7725
- isSpan = true;
7726
- } else {
7727
- isShift = true;
7728
- }
7729
- var newTotalTime = isSpan ? nextSpan : nextShift,
7730
- ts = newTotalTime - totalTime;
7731
- totalTime = newTotalTime;
7732
- if (isSpan) {
7733
- nextSpan += timeShift;
7734
- }
7735
- if (isShift) {
7736
- nextShift += timeShift;
7737
- }
7738
- m.setDisposable(scheduler.scheduleWithRelative(ts, function () {
7739
- var s;
7740
- if (isShift) {
7741
- s = new Subject();
7742
- q.push(s);
7743
- observer.onNext(addRef(s, refCountDisposable));
7744
- }
7745
- if (isSpan) {
7746
- s = q.shift();
7747
- s.onCompleted();
7748
- }
7749
- createTimer();
7750
- }));
7751
- };
7752
- q.push(new Subject());
7753
- observer.onNext(addRef(q[0], refCountDisposable));
7754
- createTimer();
7755
- groupDisposable.add(source.subscribe(function (x) {
7756
- var i, s;
7757
- for (i = 0; i < q.length; i++) {
7758
- s = q[i];
7759
- s.onNext(x);
7760
- }
7761
- }, function (e) {
7762
- var i, s;
7763
- for (i = 0; i < q.length; i++) {
7764
- s = q[i];
7765
- s.onError(e);
7766
- }
7767
- observer.onError(e);
7768
- }, function () {
7769
- var i, s;
7770
- for (i = 0; i < q.length; i++) {
7771
- s = q[i];
7772
- s.onCompleted();
7773
- }
7774
- observer.onCompleted();
7775
- }));
7776
- return refCountDisposable;
7777
- });
7778
- };
7763
+ return refCountDisposable;
7779
7764
 
7780
- /**
7781
- * Projects each element of an observable sequence into a window that is completed when either it's full or a given amount of time has elapsed.
7782
- * @example
7783
- * 1 - res = source.windowWithTimeOrCount(5000, 50); // 5s or 50 items
7784
- * 2 - res = source.windowWithTimeOrCount(5000, 50, scheduler); //5s or 50 items
7785
- *
7786
- * @memberOf Observable#
7787
- * @param {Number} timeSpan Maximum time length of a window.
7788
- * @param {Number} count Maximum element count of a window.
7789
- * @param {Scheduler} [scheduler] Scheduler to run windowing timers on. If not specified, the timeout scheduler is used.
7790
- * @returns {Observable} An observable sequence of windows.
7791
- */
7792
- observableProto.windowWithTimeOrCount = function (timeSpan, count, scheduler) {
7793
- var source = this;
7794
- scheduler || (scheduler = timeoutScheduler);
7795
- return new AnonymousObservable(function (observer) {
7796
- var createTimer,
7797
- groupDisposable,
7798
- n = 0,
7799
- refCountDisposable,
7800
- s,
7801
- timerD = new SerialDisposable(),
7802
- windowId = 0;
7803
- groupDisposable = new CompositeDisposable(timerD);
7804
- refCountDisposable = new RefCountDisposable(groupDisposable);
7805
- createTimer = function (id) {
7806
- var m = new SingleAssignmentDisposable();
7807
- timerD.setDisposable(m);
7808
- m.setDisposable(scheduler.scheduleWithRelative(timeSpan, function () {
7809
- var newId;
7810
- if (id !== windowId) {
7811
- return;
7812
- }
7813
- n = 0;
7814
- newId = ++windowId;
7815
- s.onCompleted();
7816
- s = new Subject();
7817
- observer.onNext(addRef(s, refCountDisposable));
7818
- createTimer(newId);
7819
- }));
7820
- };
7765
+ function createTimer () {
7766
+ var m = new SingleAssignmentDisposable(), isSpan = false, isShift = false;
7767
+ timerD.setDisposable(m);
7768
+
7769
+ if (nextSpan === nextShift) {
7770
+ isSpan = true;
7771
+ isShift = true;
7772
+ } else if (nextSpan < nextShift) {
7773
+ isSpan = true;
7774
+ } else {
7775
+ isShift = true;
7776
+ }
7777
+
7778
+ var newTotalTime = isSpan ? nextSpan : nextShift,
7779
+ ts = newTotalTime - totalTime;
7780
+ totalTime = newTotalTime;
7781
+ isSpan && (nextSpan += timeShift);
7782
+ isShift && (nextShift += timeShift);
7783
+
7784
+ m.setDisposable(scheduler.scheduleWithRelative(ts, function () {
7785
+ var s;
7786
+ if (isShift) {
7821
7787
  s = new Subject();
7788
+ q.push(s);
7822
7789
  observer.onNext(addRef(s, refCountDisposable));
7823
- createTimer(0);
7824
- groupDisposable.add(source.subscribe(function (x) {
7825
- var newId = 0, newWindow = false;
7826
- s.onNext(x);
7827
- n++;
7828
- if (n === count) {
7829
- newWindow = true;
7830
- n = 0;
7831
- newId = ++windowId;
7832
- s.onCompleted();
7833
- s = new Subject();
7834
- observer.onNext(addRef(s, refCountDisposable));
7835
- }
7836
- if (newWindow) {
7837
- createTimer(newId);
7838
- }
7839
- }, function (e) {
7840
- s.onError(e);
7841
- observer.onError(e);
7842
- }, function () {
7843
- s.onCompleted();
7844
- observer.onCompleted();
7845
- }));
7846
- return refCountDisposable;
7847
- });
7848
- };
7790
+ }
7791
+ if (isSpan) {
7792
+ s = q.shift();
7793
+ s.onCompleted();
7794
+ }
7795
+ createTimer();
7796
+ }));
7797
+ }
7798
+ });
7799
+ };
7800
+
7801
+ /**
7802
+ * Projects each element of an observable sequence into a window that is completed when either it's full or a given amount of time has elapsed.
7803
+ * @example
7804
+ * 1 - res = source.windowWithTimeOrCount(5000, 50); // 5s or 50 items
7805
+ * 2 - res = source.windowWithTimeOrCount(5000, 50, scheduler); //5s or 50 items
7806
+ *
7807
+ * @memberOf Observable#
7808
+ * @param {Number} timeSpan Maximum time length of a window.
7809
+ * @param {Number} count Maximum element count of a window.
7810
+ * @param {Scheduler} [scheduler] Scheduler to run windowing timers on. If not specified, the timeout scheduler is used.
7811
+ * @returns {Observable} An observable sequence of windows.
7812
+ */
7813
+ observableProto.windowWithTimeOrCount = function (timeSpan, count, scheduler) {
7814
+ var source = this;
7815
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7816
+ return new AnonymousObservable(function (observer) {
7817
+ var createTimer,
7818
+ groupDisposable,
7819
+ n = 0,
7820
+ refCountDisposable,
7821
+ s = new Subject()
7822
+ timerD = new SerialDisposable(),
7823
+ windowId = 0;
7824
+ groupDisposable = new CompositeDisposable(timerD);
7825
+ refCountDisposable = new RefCountDisposable(groupDisposable);
7826
+
7827
+ observer.onNext(addRef(s, refCountDisposable));
7828
+ createTimer(0);
7829
+ groupDisposable.add(source.subscribe(function (x) {
7830
+ var newId = 0, newWindow = false;
7831
+ s.onNext(x);
7832
+ n++;
7833
+ if (n === count) {
7834
+ newWindow = true;
7835
+ n = 0;
7836
+ newId = ++windowId;
7837
+ s.onCompleted();
7838
+ s = new Subject();
7839
+ observer.onNext(addRef(s, refCountDisposable));
7840
+ }
7841
+ newWindow && createTimer(newId);
7842
+ }, function (e) {
7843
+ s.onError(e);
7844
+ observer.onError(e);
7845
+ }, function () {
7846
+ s.onCompleted();
7847
+ observer.onCompleted();
7848
+ }));
7849
+
7850
+ return refCountDisposable;
7851
+
7852
+ function createTimer(id) {
7853
+ var m = new SingleAssignmentDisposable();
7854
+ timerD.setDisposable(m);
7855
+ m.setDisposable(scheduler.scheduleWithRelative(timeSpan, function () {
7856
+ var newId;
7857
+ if (id !== windowId) { return; }
7858
+ n = 0;
7859
+ newId = ++windowId;
7860
+ s.onCompleted();
7861
+ s = new Subject();
7862
+ observer.onNext(addRef(s, refCountDisposable));
7863
+ createTimer(newId);
7864
+ }));
7865
+ }
7866
+ });
7867
+ };
7849
7868
 
7850
7869
  /**
7851
7870
  * Projects each element of an observable sequence into zero or more buffers which are produced based on timing information.
@@ -7881,98 +7900,89 @@
7881
7900
  });
7882
7901
  };
7883
7902
 
7884
- /**
7885
- * Records the time interval between consecutive values in an observable sequence.
7886
- *
7887
- * @example
7888
- * 1 - res = source.timeInterval();
7889
- * 2 - res = source.timeInterval(Rx.Scheduler.timeout);
7890
- *
7891
- * @param [scheduler] Scheduler used to compute time intervals. If not specified, the timeout scheduler is used.
7892
- * @returns {Observable} An observable sequence with time interval information on values.
7893
- */
7894
- observableProto.timeInterval = function (scheduler) {
7895
- var source = this;
7896
- scheduler || (scheduler = timeoutScheduler);
7897
- return observableDefer(function () {
7898
- var last = scheduler.now();
7899
- return source.select(function (x) {
7900
- var now = scheduler.now(), span = now - last;
7901
- last = now;
7902
- return {
7903
- value: x,
7904
- interval: span
7905
- };
7906
- });
7907
- });
7908
- };
7903
+ /**
7904
+ * Records the time interval between consecutive values in an observable sequence.
7905
+ *
7906
+ * @example
7907
+ * 1 - res = source.timeInterval();
7908
+ * 2 - res = source.timeInterval(Rx.Scheduler.timeout);
7909
+ *
7910
+ * @param [scheduler] Scheduler used to compute time intervals. If not specified, the timeout scheduler is used.
7911
+ * @returns {Observable} An observable sequence with time interval information on values.
7912
+ */
7913
+ observableProto.timeInterval = function (scheduler) {
7914
+ var source = this;
7915
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7916
+ return observableDefer(function () {
7917
+ var last = scheduler.now();
7918
+ return source.map(function (x) {
7919
+ var now = scheduler.now(), span = now - last;
7920
+ last = now;
7921
+ return { value: x, interval: span };
7922
+ });
7923
+ });
7924
+ };
7909
7925
 
7910
- /**
7911
- * Records the timestamp for each value in an observable sequence.
7912
- *
7913
- * @example
7914
- * 1 - res = source.timestamp(); // produces { value: x, timestamp: ts }
7915
- * 2 - res = source.timestamp(Rx.Scheduler.timeout);
7916
- *
7917
- * @param {Scheduler} [scheduler] Scheduler used to compute timestamps. If not specified, the timeout scheduler is used.
7918
- * @returns {Observable} An observable sequence with timestamp information on values.
7919
- */
7920
- observableProto.timestamp = function (scheduler) {
7921
- scheduler || (scheduler = timeoutScheduler);
7922
- return this.select(function (x) {
7923
- return {
7924
- value: x,
7925
- timestamp: scheduler.now()
7926
- };
7927
- });
7928
- };
7926
+ /**
7927
+ * Records the timestamp for each value in an observable sequence.
7928
+ *
7929
+ * @example
7930
+ * 1 - res = source.timestamp(); // produces { value: x, timestamp: ts }
7931
+ * 2 - res = source.timestamp(Rx.Scheduler.timeout);
7932
+ *
7933
+ * @param {Scheduler} [scheduler] Scheduler used to compute timestamps. If not specified, the timeout scheduler is used.
7934
+ * @returns {Observable} An observable sequence with timestamp information on values.
7935
+ */
7936
+ observableProto.timestamp = function (scheduler) {
7937
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7938
+ return this.map(function (x) {
7939
+ return { value: x, timestamp: scheduler.now() };
7940
+ });
7941
+ };
7929
7942
 
7930
- function sampleObservable(source, sampler) {
7931
-
7932
- return new AnonymousObservable(function (observer) {
7933
- var atEnd, value, hasValue;
7943
+ function sampleObservable(source, sampler) {
7944
+
7945
+ return new AnonymousObservable(function (observer) {
7946
+ var atEnd, value, hasValue;
7934
7947
 
7935
- function sampleSubscribe() {
7936
- if (hasValue) {
7937
- hasValue = false;
7938
- observer.onNext(value);
7939
- }
7940
- if (atEnd) {
7941
- observer.onCompleted();
7942
- }
7943
- }
7948
+ function sampleSubscribe() {
7949
+ if (hasValue) {
7950
+ hasValue = false;
7951
+ observer.onNext(value);
7952
+ }
7953
+ atEnd && observer.onCompleted();
7954
+ }
7944
7955
 
7945
- return new CompositeDisposable(
7946
- source.subscribe(function (newValue) {
7947
- hasValue = true;
7948
- value = newValue;
7949
- }, observer.onError.bind(observer), function () {
7950
- atEnd = true;
7951
- }),
7952
- sampler.subscribe(sampleSubscribe, observer.onError.bind(observer), sampleSubscribe)
7953
- );
7954
- });
7955
- }
7956
+ return new CompositeDisposable(
7957
+ source.subscribe(function (newValue) {
7958
+ hasValue = true;
7959
+ value = newValue;
7960
+ }, observer.onError.bind(observer), function () {
7961
+ atEnd = true;
7962
+ }),
7963
+ sampler.subscribe(sampleSubscribe, observer.onError.bind(observer), sampleSubscribe)
7964
+ );
7965
+ });
7966
+ }
7956
7967
 
7957
- /**
7958
- * Samples the observable sequence at each interval.
7959
- *
7960
- * @example
7961
- * 1 - res = source.sample(sampleObservable); // Sampler tick sequence
7962
- * 2 - res = source.sample(5000); // 5 seconds
7963
- * 2 - res = source.sample(5000, Rx.Scheduler.timeout); // 5 seconds
7964
- *
7965
- * @param {Mixed} intervalOrSampler Interval at which to sample (specified as an integer denoting milliseconds) or Sampler Observable.
7966
- * @param {Scheduler} [scheduler] Scheduler to run the sampling timer on. If not specified, the timeout scheduler is used.
7967
- * @returns {Observable} Sampled observable sequence.
7968
- */
7969
- observableProto.sample = function (intervalOrSampler, scheduler) {
7970
- scheduler || (scheduler = timeoutScheduler);
7971
- if (typeof intervalOrSampler === 'number') {
7972
- return sampleObservable(this, observableinterval(intervalOrSampler, scheduler));
7973
- }
7974
- return sampleObservable(this, intervalOrSampler);
7975
- };
7968
+ /**
7969
+ * Samples the observable sequence at each interval.
7970
+ *
7971
+ * @example
7972
+ * 1 - res = source.sample(sampleObservable); // Sampler tick sequence
7973
+ * 2 - res = source.sample(5000); // 5 seconds
7974
+ * 2 - res = source.sample(5000, Rx.Scheduler.timeout); // 5 seconds
7975
+ *
7976
+ * @param {Mixed} intervalOrSampler Interval at which to sample (specified as an integer denoting milliseconds) or Sampler Observable.
7977
+ * @param {Scheduler} [scheduler] Scheduler to run the sampling timer on. If not specified, the timeout scheduler is used.
7978
+ * @returns {Observable} Sampled observable sequence.
7979
+ */
7980
+ observableProto.sample = function (intervalOrSampler, scheduler) {
7981
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7982
+ return typeof intervalOrSampler === 'number' ?
7983
+ sampleObservable(this, observableinterval(intervalOrSampler, scheduler)) :
7984
+ sampleObservable(this, intervalOrSampler);
7985
+ };
7976
7986
 
7977
7987
  /**
7978
7988
  * Returns the source observable sequence or the other observable sequence if dueTime elapses.
@@ -7992,7 +8002,7 @@
7992
8002
  */
7993
8003
  observableProto.timeout = function (dueTime, other, scheduler) {
7994
8004
  other || (other = observableThrow(new Error('Timeout')));
7995
- scheduler || (scheduler = timeoutScheduler);
8005
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
7996
8006
 
7997
8007
  var source = this, schedulerMethod = dueTime instanceof Date ?
7998
8008
  'scheduleWithAbsolute' :
@@ -8040,131 +8050,128 @@
8040
8050
  });
8041
8051
  };
8042
8052
 
8043
- /**
8044
- * Generates an observable sequence by iterating a state from an initial state until the condition fails.
8045
- *
8046
- * @example
8047
- * res = source.generateWithAbsoluteTime(0,
8048
- * function (x) { return return true; },
8049
- * function (x) { return x + 1; },
8050
- * function (x) { return x; },
8051
- * function (x) { return new Date(); }
8052
- * });
8053
- *
8054
- * @param {Mixed} initialState Initial state.
8055
- * @param {Function} condition Condition to terminate generation (upon returning false).
8056
- * @param {Function} iterate Iteration step function.
8057
- * @param {Function} resultSelector Selector function for results produced in the sequence.
8058
- * @param {Function} timeSelector Time selector function to control the speed of values being produced each iteration, returning Date values.
8059
- * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not specified, the timeout scheduler is used.
8060
- * @returns {Observable} The generated sequence.
8061
- */
8062
- Observable.generateWithAbsoluteTime = function (initialState, condition, iterate, resultSelector, timeSelector, scheduler) {
8063
- scheduler || (scheduler = timeoutScheduler);
8064
- return new AnonymousObservable(function (observer) {
8065
- var first = true,
8066
- hasResult = false,
8067
- result,
8068
- state = initialState,
8069
- time;
8070
- return scheduler.scheduleRecursiveWithAbsolute(scheduler.now(), function (self) {
8071
- if (hasResult) {
8072
- observer.onNext(result);
8073
- }
8074
- try {
8075
- if (first) {
8076
- first = false;
8077
- } else {
8078
- state = iterate(state);
8079
- }
8080
- hasResult = condition(state);
8081
- if (hasResult) {
8082
- result = resultSelector(state);
8083
- time = timeSelector(state);
8084
- }
8085
- } catch (e) {
8086
- observer.onError(e);
8087
- return;
8088
- }
8089
- if (hasResult) {
8090
- self(time);
8091
- } else {
8092
- observer.onCompleted();
8093
- }
8094
- });
8095
- });
8096
- };
8053
+ /**
8054
+ * Generates an observable sequence by iterating a state from an initial state until the condition fails.
8055
+ *
8056
+ * @example
8057
+ * res = source.generateWithAbsoluteTime(0,
8058
+ * function (x) { return return true; },
8059
+ * function (x) { return x + 1; },
8060
+ * function (x) { return x; },
8061
+ * function (x) { return new Date(); }
8062
+ * });
8063
+ *
8064
+ * @param {Mixed} initialState Initial state.
8065
+ * @param {Function} condition Condition to terminate generation (upon returning false).
8066
+ * @param {Function} iterate Iteration step function.
8067
+ * @param {Function} resultSelector Selector function for results produced in the sequence.
8068
+ * @param {Function} timeSelector Time selector function to control the speed of values being produced each iteration, returning Date values.
8069
+ * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not specified, the timeout scheduler is used.
8070
+ * @returns {Observable} The generated sequence.
8071
+ */
8072
+ Observable.generateWithAbsoluteTime = function (initialState, condition, iterate, resultSelector, timeSelector, scheduler) {
8073
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
8074
+ return new AnonymousObservable(function (observer) {
8075
+ var first = true,
8076
+ hasResult = false,
8077
+ result,
8078
+ state = initialState,
8079
+ time;
8080
+ return scheduler.scheduleRecursiveWithAbsolute(scheduler.now(), function (self) {
8081
+ hasResult && observer.onNext(result);
8082
+
8083
+ try {
8084
+ if (first) {
8085
+ first = false;
8086
+ } else {
8087
+ state = iterate(state);
8088
+ }
8089
+ hasResult = condition(state);
8090
+ if (hasResult) {
8091
+ result = resultSelector(state);
8092
+ time = timeSelector(state);
8093
+ }
8094
+ } catch (e) {
8095
+ observer.onError(e);
8096
+ return;
8097
+ }
8098
+ if (hasResult) {
8099
+ self(time);
8100
+ } else {
8101
+ observer.onCompleted();
8102
+ }
8103
+ });
8104
+ });
8105
+ };
8097
8106
 
8098
- /**
8099
- * Generates an observable sequence by iterating a state from an initial state until the condition fails.
8100
- *
8101
- * @example
8102
- * res = source.generateWithRelativeTime(0,
8103
- * function (x) { return return true; },
8104
- * function (x) { return x + 1; },
8105
- * function (x) { return x; },
8106
- * function (x) { return 500; }
8107
- * );
8108
- *
8109
- * @param {Mixed} initialState Initial state.
8110
- * @param {Function} condition Condition to terminate generation (upon returning false).
8111
- * @param {Function} iterate Iteration step function.
8112
- * @param {Function} resultSelector Selector function for results produced in the sequence.
8113
- * @param {Function} timeSelector Time selector function to control the speed of values being produced each iteration, returning integer values denoting milliseconds.
8114
- * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not specified, the timeout scheduler is used.
8115
- * @returns {Observable} The generated sequence.
8116
- */
8117
- Observable.generateWithRelativeTime = function (initialState, condition, iterate, resultSelector, timeSelector, scheduler) {
8118
- scheduler || (scheduler = timeoutScheduler);
8119
- return new AnonymousObservable(function (observer) {
8120
- var first = true,
8121
- hasResult = false,
8122
- result,
8123
- state = initialState,
8124
- time;
8125
- return scheduler.scheduleRecursiveWithRelative(0, function (self) {
8126
- if (hasResult) {
8127
- observer.onNext(result);
8128
- }
8129
- try {
8130
- if (first) {
8131
- first = false;
8132
- } else {
8133
- state = iterate(state);
8134
- }
8135
- hasResult = condition(state);
8136
- if (hasResult) {
8137
- result = resultSelector(state);
8138
- time = timeSelector(state);
8139
- }
8140
- } catch (e) {
8141
- observer.onError(e);
8142
- return;
8143
- }
8144
- if (hasResult) {
8145
- self(time);
8146
- } else {
8147
- observer.onCompleted();
8148
- }
8149
- });
8150
- });
8151
- };
8107
+ /**
8108
+ * Generates an observable sequence by iterating a state from an initial state until the condition fails.
8109
+ *
8110
+ * @example
8111
+ * res = source.generateWithRelativeTime(0,
8112
+ * function (x) { return return true; },
8113
+ * function (x) { return x + 1; },
8114
+ * function (x) { return x; },
8115
+ * function (x) { return 500; }
8116
+ * );
8117
+ *
8118
+ * @param {Mixed} initialState Initial state.
8119
+ * @param {Function} condition Condition to terminate generation (upon returning false).
8120
+ * @param {Function} iterate Iteration step function.
8121
+ * @param {Function} resultSelector Selector function for results produced in the sequence.
8122
+ * @param {Function} timeSelector Time selector function to control the speed of values being produced each iteration, returning integer values denoting milliseconds.
8123
+ * @param {Scheduler} [scheduler] Scheduler on which to run the generator loop. If not specified, the timeout scheduler is used.
8124
+ * @returns {Observable} The generated sequence.
8125
+ */
8126
+ Observable.generateWithRelativeTime = function (initialState, condition, iterate, resultSelector, timeSelector, scheduler) {
8127
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
8128
+ return new AnonymousObservable(function (observer) {
8129
+ var first = true,
8130
+ hasResult = false,
8131
+ result,
8132
+ state = initialState,
8133
+ time;
8134
+ return scheduler.scheduleRecursiveWithRelative(0, function (self) {
8135
+ hasResult && observer.onNext(result);
8136
+
8137
+ try {
8138
+ if (first) {
8139
+ first = false;
8140
+ } else {
8141
+ state = iterate(state);
8142
+ }
8143
+ hasResult = condition(state);
8144
+ if (hasResult) {
8145
+ result = resultSelector(state);
8146
+ time = timeSelector(state);
8147
+ }
8148
+ } catch (e) {
8149
+ observer.onError(e);
8150
+ return;
8151
+ }
8152
+ if (hasResult) {
8153
+ self(time);
8154
+ } else {
8155
+ observer.onCompleted();
8156
+ }
8157
+ });
8158
+ });
8159
+ };
8152
8160
 
8153
- /**
8154
- * Time shifts the observable sequence by delaying the subscription.
8155
- *
8156
- * @example
8157
- * 1 - res = source.delaySubscription(5000); // 5s
8158
- * 2 - res = source.delaySubscription(5000, Rx.Scheduler.timeout); // 5 seconds
8159
- *
8160
- * @param {Number} dueTime Absolute or relative time to perform the subscription at.
8161
- * @param {Scheduler} [scheduler] Scheduler to run the subscription delay timer on. If not specified, the timeout scheduler is used.
8162
- * @returns {Observable} Time-shifted sequence.
8163
- */
8164
- observableProto.delaySubscription = function (dueTime, scheduler) {
8165
- scheduler || (scheduler = timeoutScheduler);
8166
- return this.delayWithSelector(observableTimer(dueTime, scheduler), function () { return observableEmpty(); });
8167
- };
8161
+ /**
8162
+ * Time shifts the observable sequence by delaying the subscription.
8163
+ *
8164
+ * @example
8165
+ * 1 - res = source.delaySubscription(5000); // 5s
8166
+ * 2 - res = source.delaySubscription(5000, Rx.Scheduler.timeout); // 5 seconds
8167
+ *
8168
+ * @param {Number} dueTime Absolute or relative time to perform the subscription at.
8169
+ * @param {Scheduler} [scheduler] Scheduler to run the subscription delay timer on. If not specified, the timeout scheduler is used.
8170
+ * @returns {Observable} Time-shifted sequence.
8171
+ */
8172
+ observableProto.delaySubscription = function (dueTime, scheduler) {
8173
+ return this.delayWithSelector(observableTimer(dueTime, isScheduler(scheduler) ? scheduler : timeoutScheduler), observableEmpty);
8174
+ };
8168
8175
 
8169
8176
  /**
8170
8177
  * Time shifts the observable sequence based on a subscription delay and a delay selector function for each element.
@@ -8310,96 +8317,96 @@
8310
8317
  });
8311
8318
  };
8312
8319
 
8313
- /**
8314
- * Ignores values from an observable sequence which are followed by another value within a computed throttle duration.
8315
- *
8316
- * @example
8317
- * 1 - res = source.delayWithSelector(function (x) { return Rx.Scheduler.timer(x + x); });
8318
- *
8319
- * @param {Function} throttleDurationSelector Selector function to retrieve a sequence indicating the throttle duration for each given element.
8320
- * @returns {Observable} The throttled sequence.
8321
- */
8322
- observableProto.throttleWithSelector = function (throttleDurationSelector) {
8323
- var source = this;
8324
- return new AnonymousObservable(function (observer) {
8325
- var value, hasValue = false, cancelable = new SerialDisposable(), id = 0, subscription = source.subscribe(function (x) {
8326
- var throttle;
8327
- try {
8328
- throttle = throttleDurationSelector(x);
8329
- } catch (e) {
8330
- observer.onError(e);
8331
- return;
8332
- }
8333
- hasValue = true;
8334
- value = x;
8335
- id++;
8336
- var currentid = id, d = new SingleAssignmentDisposable();
8337
- cancelable.setDisposable(d);
8338
- d.setDisposable(throttle.subscribe(function () {
8339
- if (hasValue && id === currentid) {
8340
- observer.onNext(value);
8341
- }
8342
- hasValue = false;
8343
- d.dispose();
8344
- }, observer.onError.bind(observer), function () {
8345
- if (hasValue && id === currentid) {
8346
- observer.onNext(value);
8347
- }
8348
- hasValue = false;
8349
- d.dispose();
8350
- }));
8351
- }, function (e) {
8352
- cancelable.dispose();
8353
- observer.onError(e);
8354
- hasValue = false;
8355
- id++;
8356
- }, function () {
8357
- cancelable.dispose();
8358
- if (hasValue) {
8359
- observer.onNext(value);
8360
- }
8361
- observer.onCompleted();
8362
- hasValue = false;
8363
- id++;
8364
- });
8365
- return new CompositeDisposable(subscription, cancelable);
8366
- });
8367
- };
8320
+ /**
8321
+ * Ignores values from an observable sequence which are followed by another value within a computed throttle duration.
8322
+ *
8323
+ * @example
8324
+ * 1 - res = source.delayWithSelector(function (x) { return Rx.Scheduler.timer(x + x); });
8325
+ *
8326
+ * @param {Function} throttleDurationSelector Selector function to retrieve a sequence indicating the throttle duration for each given element.
8327
+ * @returns {Observable} The throttled sequence.
8328
+ */
8329
+ observableProto.throttleWithSelector = function (throttleDurationSelector) {
8330
+ var source = this;
8331
+ return new AnonymousObservable(function (observer) {
8332
+ var value, hasValue = false, cancelable = new SerialDisposable(), id = 0, subscription = source.subscribe(function (x) {
8333
+ var throttle;
8334
+ try {
8335
+ throttle = throttleDurationSelector(x);
8336
+ } catch (e) {
8337
+ observer.onError(e);
8338
+ return;
8339
+ }
8340
+ hasValue = true;
8341
+ value = x;
8342
+ id++;
8343
+ var currentid = id, d = new SingleAssignmentDisposable();
8344
+ cancelable.setDisposable(d);
8345
+ d.setDisposable(throttle.subscribe(function () {
8346
+ if (hasValue && id === currentid) {
8347
+ observer.onNext(value);
8348
+ }
8349
+ hasValue = false;
8350
+ d.dispose();
8351
+ }, observer.onError.bind(observer), function () {
8352
+ if (hasValue && id === currentid) {
8353
+ observer.onNext(value);
8354
+ }
8355
+ hasValue = false;
8356
+ d.dispose();
8357
+ }));
8358
+ }, function (e) {
8359
+ cancelable.dispose();
8360
+ observer.onError(e);
8361
+ hasValue = false;
8362
+ id++;
8363
+ }, function () {
8364
+ cancelable.dispose();
8365
+ if (hasValue) {
8366
+ observer.onNext(value);
8367
+ }
8368
+ observer.onCompleted();
8369
+ hasValue = false;
8370
+ id++;
8371
+ });
8372
+ return new CompositeDisposable(subscription, cancelable);
8373
+ });
8374
+ };
8368
8375
 
8369
- /**
8370
- * Skips elements for the specified duration from the end of the observable source sequence, using the specified scheduler to run timers.
8371
- *
8372
- * 1 - res = source.skipLastWithTime(5000);
8373
- * 2 - res = source.skipLastWithTime(5000, scheduler);
8374
- *
8375
- * @description
8376
- * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
8377
- * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
8378
- * result sequence. This causes elements to be delayed with duration.
8379
- * @param {Number} duration Duration for skipping elements from the end of the sequence.
8380
- * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout
8381
- * @returns {Observable} An observable sequence with the elements skipped during the specified duration from the end of the source sequence.
8382
- */
8383
- observableProto.skipLastWithTime = function (duration, scheduler) {
8384
- scheduler || (scheduler = timeoutScheduler);
8385
- var source = this;
8386
- return new AnonymousObservable(function (observer) {
8387
- var q = [];
8388
- return source.subscribe(function (x) {
8389
- var now = scheduler.now();
8390
- q.push({ interval: now, value: x });
8391
- while (q.length > 0 && now - q[0].interval >= duration) {
8392
- observer.onNext(q.shift().value);
8393
- }
8394
- }, observer.onError.bind(observer), function () {
8395
- var now = scheduler.now();
8396
- while (q.length > 0 && now - q[0].interval >= duration) {
8397
- observer.onNext(q.shift().value);
8398
- }
8399
- observer.onCompleted();
8400
- });
8401
- });
8402
- };
8376
+ /**
8377
+ * Skips elements for the specified duration from the end of the observable source sequence, using the specified scheduler to run timers.
8378
+ *
8379
+ * 1 - res = source.skipLastWithTime(5000);
8380
+ * 2 - res = source.skipLastWithTime(5000, scheduler);
8381
+ *
8382
+ * @description
8383
+ * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
8384
+ * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
8385
+ * result sequence. This causes elements to be delayed with duration.
8386
+ * @param {Number} duration Duration for skipping elements from the end of the sequence.
8387
+ * @param {Scheduler} [scheduler] Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout
8388
+ * @returns {Observable} An observable sequence with the elements skipped during the specified duration from the end of the source sequence.
8389
+ */
8390
+ observableProto.skipLastWithTime = function (duration, scheduler) {
8391
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
8392
+ var source = this;
8393
+ return new AnonymousObservable(function (observer) {
8394
+ var q = [];
8395
+ return source.subscribe(function (x) {
8396
+ var now = scheduler.now();
8397
+ q.push({ interval: now, value: x });
8398
+ while (q.length > 0 && now - q[0].interval >= duration) {
8399
+ observer.onNext(q.shift().value);
8400
+ }
8401
+ }, observer.onError.bind(observer), function () {
8402
+ var now = scheduler.now();
8403
+ while (q.length > 0 && now - q[0].interval >= duration) {
8404
+ observer.onNext(q.shift().value);
8405
+ }
8406
+ observer.onCompleted();
8407
+ });
8408
+ });
8409
+ };
8403
8410
 
8404
8411
  /**
8405
8412
  * Returns elements within the specified duration from the end of the observable source sequence, using the specified schedulers to run timers and to drain the collected elements.
@@ -8419,102 +8426,93 @@
8419
8426
  return this.takeLastBufferWithTime(duration, timerScheduler).selectMany(function (xs) { return observableFromArray(xs, loopScheduler); });
8420
8427
  };
8421
8428
 
8422
- /**
8423
- * Returns an array with the elements within the specified duration from the end of the observable source sequence, using the specified scheduler to run timers.
8424
- *
8425
- * @example
8426
- * 1 - res = source.takeLastBufferWithTime(5000, [optional scheduler]);
8427
- * @description
8428
- * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
8429
- * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
8430
- * result sequence. This causes elements to be delayed with duration.
8431
- * @param {Number} duration Duration for taking elements from the end of the sequence.
8432
- * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
8433
- * @returns {Observable} An observable sequence containing a single array with the elements taken during the specified duration from the end of the source sequence.
8434
- */
8435
- observableProto.takeLastBufferWithTime = function (duration, scheduler) {
8436
- var source = this;
8437
- scheduler || (scheduler = timeoutScheduler);
8438
- return new AnonymousObservable(function (observer) {
8439
- var q = [];
8440
-
8441
- return source.subscribe(function (x) {
8442
- var now = scheduler.now();
8443
- q.push({ interval: now, value: x });
8444
- while (q.length > 0 && now - q[0].interval >= duration) {
8445
- q.shift();
8446
- }
8447
- }, observer.onError.bind(observer), function () {
8448
- var now = scheduler.now(), res = [];
8449
- while (q.length > 0) {
8450
- var next = q.shift();
8451
- if (now - next.interval <= duration) {
8452
- res.push(next.value);
8453
- }
8454
- }
8455
-
8456
- observer.onNext(res);
8457
- observer.onCompleted();
8458
- });
8459
- });
8460
- };
8429
+ /**
8430
+ * Returns an array with the elements within the specified duration from the end of the observable source sequence, using the specified scheduler to run timers.
8431
+ *
8432
+ * @example
8433
+ * 1 - res = source.takeLastBufferWithTime(5000, [optional scheduler]);
8434
+ * @description
8435
+ * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
8436
+ * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
8437
+ * result sequence. This causes elements to be delayed with duration.
8438
+ * @param {Number} duration Duration for taking elements from the end of the sequence.
8439
+ * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
8440
+ * @returns {Observable} An observable sequence containing a single array with the elements taken during the specified duration from the end of the source sequence.
8441
+ */
8442
+ observableProto.takeLastBufferWithTime = function (duration, scheduler) {
8443
+ var source = this;
8444
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
8445
+ return new AnonymousObservable(function (observer) {
8446
+ var q = [];
8461
8447
 
8462
- /**
8463
- * Takes elements for the specified duration from the start of the observable source sequence, using the specified scheduler to run timers.
8464
- *
8465
- * @example
8466
- * 1 - res = source.takeWithTime(5000, [optional scheduler]);
8467
- * @description
8468
- * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
8469
- * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
8470
- * result sequence. This causes elements to be delayed with duration.
8471
- * @param {Number} duration Duration for taking elements from the start of the sequence.
8472
- * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
8473
- * @returns {Observable} An observable sequence with the elements taken during the specified duration from the start of the source sequence.
8474
- */
8475
- observableProto.takeWithTime = function (duration, scheduler) {
8476
- var source = this;
8477
- scheduler || (scheduler = timeoutScheduler);
8478
- return new AnonymousObservable(function (observer) {
8479
- var t = scheduler.scheduleWithRelative(duration, function () {
8480
- observer.onCompleted();
8481
- });
8448
+ return source.subscribe(function (x) {
8449
+ var now = scheduler.now();
8450
+ q.push({ interval: now, value: x });
8451
+ while (q.length > 0 && now - q[0].interval >= duration) {
8452
+ q.shift();
8453
+ }
8454
+ }, observer.onError.bind(observer), function () {
8455
+ var now = scheduler.now(), res = [];
8456
+ while (q.length > 0) {
8457
+ var next = q.shift();
8458
+ if (now - next.interval <= duration) {
8459
+ res.push(next.value);
8460
+ }
8461
+ }
8482
8462
 
8483
- return new CompositeDisposable(t, source.subscribe(observer));
8484
- });
8485
- };
8463
+ observer.onNext(res);
8464
+ observer.onCompleted();
8465
+ });
8466
+ });
8467
+ };
8486
8468
 
8487
- /**
8488
- * Skips elements for the specified duration from the start of the observable source sequence, using the specified scheduler to run timers.
8489
- *
8490
- * @example
8491
- * 1 - res = source.skipWithTime(5000, [optional scheduler]);
8492
- *
8493
- * @description
8494
- * Specifying a zero value for duration doesn't guarantee no elements will be dropped from the start of the source sequence.
8495
- * This is a side-effect of the asynchrony introduced by the scheduler, where the action that causes callbacks from the source sequence to be forwarded
8496
- * may not execute immediately, despite the zero due time.
8497
- *
8498
- * Errors produced by the source sequence are always forwarded to the result sequence, even if the error occurs before the duration.
8499
- * @param {Number} duration Duration for skipping elements from the start of the sequence.
8500
- * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
8501
- * @returns {Observable} An observable sequence with the elements skipped during the specified duration from the start of the source sequence.
8502
- */
8503
- observableProto.skipWithTime = function (duration, scheduler) {
8504
- var source = this;
8505
- scheduler || (scheduler = timeoutScheduler);
8506
- return new AnonymousObservable(function (observer) {
8507
- var open = false,
8508
- t = scheduler.scheduleWithRelative(duration, function () { open = true; }),
8509
- d = source.subscribe(function (x) {
8510
- if (open) {
8511
- observer.onNext(x);
8512
- }
8513
- }, observer.onError.bind(observer), observer.onCompleted.bind(observer));
8469
+ /**
8470
+ * Takes elements for the specified duration from the start of the observable source sequence, using the specified scheduler to run timers.
8471
+ *
8472
+ * @example
8473
+ * 1 - res = source.takeWithTime(5000, [optional scheduler]);
8474
+ * @description
8475
+ * This operator accumulates a queue with a length enough to store elements received during the initial duration window.
8476
+ * As more elements are received, elements older than the specified duration are taken from the queue and produced on the
8477
+ * result sequence. This causes elements to be delayed with duration.
8478
+ * @param {Number} duration Duration for taking elements from the start of the sequence.
8479
+ * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
8480
+ * @returns {Observable} An observable sequence with the elements taken during the specified duration from the start of the source sequence.
8481
+ */
8482
+ observableProto.takeWithTime = function (duration, scheduler) {
8483
+ var source = this;
8484
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
8485
+ return new AnonymousObservable(function (observer) {
8486
+ return new CompositeDisposable(scheduler.scheduleWithRelative(duration, observer.onCompleted.bind(observer)), source.subscribe(observer));
8487
+ });
8488
+ };
8514
8489
 
8515
- return new CompositeDisposable(t, d);
8516
- });
8517
- };
8490
+ /**
8491
+ * Skips elements for the specified duration from the start of the observable source sequence, using the specified scheduler to run timers.
8492
+ *
8493
+ * @example
8494
+ * 1 - res = source.skipWithTime(5000, [optional scheduler]);
8495
+ *
8496
+ * @description
8497
+ * Specifying a zero value for duration doesn't guarantee no elements will be dropped from the start of the source sequence.
8498
+ * This is a side-effect of the asynchrony introduced by the scheduler, where the action that causes callbacks from the source sequence to be forwarded
8499
+ * may not execute immediately, despite the zero due time.
8500
+ *
8501
+ * Errors produced by the source sequence are always forwarded to the result sequence, even if the error occurs before the duration.
8502
+ * @param {Number} duration Duration for skipping elements from the start of the sequence.
8503
+ * @param {Scheduler} scheduler Scheduler to run the timer on. If not specified, defaults to Rx.Scheduler.timeout.
8504
+ * @returns {Observable} An observable sequence with the elements skipped during the specified duration from the start of the source sequence.
8505
+ */
8506
+ observableProto.skipWithTime = function (duration, scheduler) {
8507
+ var source = this;
8508
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
8509
+ return new AnonymousObservable(function (observer) {
8510
+ var open = false;
8511
+ return new CompositeDisposable(
8512
+ scheduler.scheduleWithRelative(duration, function () { open = true; }),
8513
+ source.subscribe(function (x) { open && observer.onNext(x); }, observer.onError.bind(observer), observer.onCompleted.bind(observer)));
8514
+ });
8515
+ };
8518
8516
 
8519
8517
  /**
8520
8518
  * Skips elements from the observable source sequence until the specified start time, using the specified scheduler to run timers.
@@ -8528,7 +8526,7 @@
8528
8526
  * @returns {Observable} An observable sequence with the elements skipped until the specified start time.
8529
8527
  */
8530
8528
  observableProto.skipUntilWithTime = function (startTime, scheduler) {
8531
- scheduler || (scheduler = timeoutScheduler);
8529
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
8532
8530
  var source = this, schedulerMethod = startTime instanceof Date ?
8533
8531
  'scheduleWithAbsolute' :
8534
8532
  'scheduleWithRelative';
@@ -8555,7 +8553,7 @@
8555
8553
  * @returns {Observable} An observable sequence with the elements taken until the specified end time.
8556
8554
  */
8557
8555
  observableProto.takeUntilWithTime = function (endTime, scheduler) {
8558
- scheduler || (scheduler = timeoutScheduler);
8556
+ isScheduler(scheduler) || (scheduler = timeoutScheduler);
8559
8557
  var source = this, schedulerMethod = endTime instanceof Date ?
8560
8558
  'scheduleWithAbsolute' :
8561
8559
  'scheduleWithRelative';
@@ -8566,6 +8564,116 @@
8566
8564
  });
8567
8565
  };
8568
8566
 
8567
+ /*
8568
+ * Performs a exclusive waiting for the first to finish before subscribing to another observable.
8569
+ * Observables that come in between subscriptions will be dropped on the floor.
8570
+ * @returns {Observable} A exclusive observable with only the results that happen when subscribed.
8571
+ */
8572
+ observableProto.exclusive = function () {
8573
+ var sources = this;
8574
+ return new AnonymousObservable(function (observer) {
8575
+ var hasCurrent = false,
8576
+ isStopped = false,
8577
+ m = new SingleAssignmentDisposable(),
8578
+ g = new CompositeDisposable();
8579
+
8580
+ g.add(m);
8581
+
8582
+ m.setDisposable(sources.subscribe(
8583
+ function (innerSource) {
8584
+ if (!hasCurrent) {
8585
+ hasCurrent = true;
8586
+
8587
+ isPromise(innerSource) && (innerSource = observableFromPromise(innerSource));
8588
+
8589
+ var innerSubscription = new SingleAssignmentDisposable();
8590
+ g.add(innerSubscription);
8591
+
8592
+ innerSubscription.setDisposable(innerSource.subscribe(
8593
+ observer.onNext.bind(observer),
8594
+ observer.onError.bind(observer),
8595
+ function () {
8596
+ g.remove(innerSubscription);
8597
+ hasCurrent = false;
8598
+ if (isStopped && g.length === 1) {
8599
+ observer.onCompleted();
8600
+ }
8601
+ }));
8602
+ }
8603
+ },
8604
+ observer.onError.bind(observer),
8605
+ function () {
8606
+ isStopped = true;
8607
+ if (!hasCurrent && g.length === 1) {
8608
+ observer.onCompleted();
8609
+ }
8610
+ }));
8611
+
8612
+ return g;
8613
+ });
8614
+ };
8615
+ /*
8616
+ * Performs a exclusive map waiting for the first to finish before subscribing to another observable.
8617
+ * Observables that come in between subscriptions will be dropped on the floor.
8618
+ * @param {Function} selector Selector to invoke for every item in the current subscription.
8619
+ * @param {Any} [thisArg] An optional context to invoke with the selector parameter.
8620
+ * @returns {Observable} An exclusive observable with only the results that happen when subscribed.
8621
+ */
8622
+ observableProto.exclusiveMap = function (selector, thisArg) {
8623
+ var sources = this;
8624
+ return new AnonymousObservable(function (observer) {
8625
+ var index = 0,
8626
+ hasCurrent = false,
8627
+ isStopped = true,
8628
+ m = new SingleAssignmentDisposable(),
8629
+ g = new CompositeDisposable();
8630
+
8631
+ g.add(m);
8632
+
8633
+ m.setDisposable(sources.subscribe(
8634
+ function (innerSource) {
8635
+
8636
+ if (!hasCurrent) {
8637
+ hasCurrent = true;
8638
+
8639
+ innerSubscription = new SingleAssignmentDisposable();
8640
+ g.add(innerSubscription);
8641
+
8642
+ isPromise(innerSource) && (innerSource = observableFromPromise(innerSource));
8643
+
8644
+ innerSubscription.setDisposable(innerSource.subscribe(
8645
+ function (x) {
8646
+ var result;
8647
+ try {
8648
+ result = selector.call(thisArg, x, index++, innerSource);
8649
+ } catch (e) {
8650
+ observer.onError(e);
8651
+ return;
8652
+ }
8653
+
8654
+ observer.onNext(result);
8655
+ },
8656
+ observer.onError.bind(observer),
8657
+ function () {
8658
+ g.remove(innerSubscription);
8659
+ hasCurrent = false;
8660
+
8661
+ if (isStopped && g.length === 1) {
8662
+ observer.onCompleted();
8663
+ }
8664
+ }));
8665
+ }
8666
+ },
8667
+ observer.onError.bind(observer),
8668
+ function () {
8669
+ isStopped = true;
8670
+ if (g.length === 1 && !hasCurrent) {
8671
+ observer.onCompleted();
8672
+ }
8673
+ }));
8674
+ return g;
8675
+ });
8676
+ };
8569
8677
  /** Provides a set of extension methods for virtual time scheduling. */
8570
8678
  Rx.VirtualTimeScheduler = (function (_super) {
8571
8679
 
@@ -8875,25 +8983,21 @@
8875
8983
  }
8876
8984
 
8877
8985
  function s(observer) {
8878
- var autoDetachObserver = new AutoDetachObserver(observer);
8879
- if (currentThreadScheduler.scheduleRequired()) {
8880
- currentThreadScheduler.schedule(function () {
8881
- try {
8882
- autoDetachObserver.setDisposable(fixSubscriber(subscribe(autoDetachObserver)));
8883
- } catch (e) {
8884
- if (!autoDetachObserver.fail(e)) {
8885
- throw e;
8886
- }
8887
- }
8888
- });
8889
- } else {
8986
+ var setDisposable = function () {
8890
8987
  try {
8891
8988
  autoDetachObserver.setDisposable(fixSubscriber(subscribe(autoDetachObserver)));
8892
8989
  } catch (e) {
8893
8990
  if (!autoDetachObserver.fail(e)) {
8894
8991
  throw e;
8895
- }
8992
+ }
8896
8993
  }
8994
+ };
8995
+
8996
+ var autoDetachObserver = new AutoDetachObserver(observer);
8997
+ if (currentThreadScheduler.scheduleRequired()) {
8998
+ currentThreadScheduler.schedule(setDisposable);
8999
+ } else {
9000
+ setDisposable();
8897
9001
  }
8898
9002
 
8899
9003
  return autoDetachObserver;