rxjs-rails 2.2.18 → 2.2.19

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,18 +25,21 @@
25
25
  internals: {},
26
26
  config: {
27
27
  Promise: root.Promise // Detect if promise exists
28
- }
28
+ },
29
+ helpers: { }
29
30
  };
30
31
 
31
32
  // Defaults
32
- function noop() { }
33
- function identity(x) { return x; }
34
- var defaultNow = Date.now;
35
- function defaultComparer(x, y) { return isEqual(x, y); }
36
- function defaultSubComparer(x, y) { return x - y; }
37
- function defaultKeySerializer(x) { return x.toString(); }
38
- function defaultError(err) { throw err; }
39
- function isPromise(p) { return typeof p.then === 'function' && p.then !== Rx.Observable.prototype.then; }
33
+ var noop = Rx.helpers.noop = function () { },
34
+ identity = Rx.helpers.identity = function (x) { return x; },
35
+ defaultNow = Rx.helpers.defaultNow = Date.now,
36
+ defaultComparer = Rx.helpers.defaultComparer = function (x, y) { return isEqual(x, y); },
37
+ defaultSubComparer = Rx.helpers.defaultSubComparer = function (x, y) { return x > y ? 1 : (x < y ? -1 : 0); },
38
+ defaultKeySerializer = Rx.helpers.defaultKeySerializer = function (x) { return x.toString(); },
39
+ defaultError = Rx.helpers.defaultError = function (err) { throw err; },
40
+ isPromise = Rx.helpers.isPromise = function (p) { return typeof p.then === 'function' && p.then !== Rx.Observable.prototype.then; },
41
+ asArray = Rx.helpers.asArray = function () { return Array.prototype.slice.call(arguments); },
42
+ not = Rx.helpers.not = function (a) { return !a; };
40
43
 
41
44
  // Errors
42
45
  var sequenceContainsNoElements = 'Sequence contains no elements.';
@@ -1460,9 +1463,13 @@
1460
1463
  return;
1461
1464
  }
1462
1465
 
1466
+ // Check if promise
1467
+ var currentValue = currentItem.value;
1468
+ isPromise(currentValue) && (currentValue = observableFromPromise(currentValue));
1469
+
1463
1470
  var d = new SingleAssignmentDisposable();
1464
1471
  subscription.setDisposable(d);
1465
- d.setDisposable(currentItem.value.subscribe(
1472
+ d.setDisposable(currentValue.subscribe(
1466
1473
  observer.onNext.bind(observer),
1467
1474
  observer.onError.bind(observer),
1468
1475
  function () { self(); })
@@ -1509,9 +1516,13 @@
1509
1516
  return;
1510
1517
  }
1511
1518
 
1519
+ // Check if promise
1520
+ var currentValue = currentItem.value;
1521
+ isPromise(currentValue) && (currentValue = observableFromPromise(currentValue));
1522
+
1512
1523
  var d = new SingleAssignmentDisposable();
1513
1524
  subscription.setDisposable(d);
1514
- d.setDisposable(currentItem.value.subscribe(
1525
+ d.setDisposable(currentValue.subscribe(
1515
1526
  observer.onNext.bind(observer),
1516
1527
  function (exn) {
1517
1528
  lastException = exn;
@@ -1908,6 +1919,9 @@
1908
1919
  } catch (e) {
1909
1920
  return observableThrow(e).subscribe(observer);
1910
1921
  }
1922
+
1923
+ // Check if promise
1924
+ isPromise(result) && (result = observableFromPromise(result));
1911
1925
  return result.subscribe(observer);
1912
1926
  });
1913
1927
  };
@@ -3315,98 +3329,122 @@
3315
3329
  };
3316
3330
  };
3317
3331
 
3318
- function createListener (element, name, handler) {
3319
- // Node.js specific
3320
- if (element.addListener) {
3321
- element.addListener(name, handler);
3322
- return disposableCreate(function () {
3323
- element.removeListener(name, handler);
3324
- });
3325
- } else if (element.addEventListener) {
3326
- element.addEventListener(name, handler, false);
3327
- return disposableCreate(function () {
3328
- element.removeEventListener(name, handler, false);
3329
- });
3330
- }
3332
+ function createListener (element, name, handler) {
3333
+ // Node.js specific
3334
+ if (element.addListener) {
3335
+ element.addListener(name, handler);
3336
+ return disposableCreate(function () {
3337
+ element.removeListener(name, handler);
3338
+ });
3339
+ }
3340
+ if (element.addEventListener) {
3341
+ element.addEventListener(name, handler, false);
3342
+ return disposableCreate(function () {
3343
+ element.removeEventListener(name, handler, false);
3344
+ });
3331
3345
  }
3346
+ throw new Error('No listener found');
3347
+ }
3332
3348
 
3333
- function createEventListener (el, eventName, handler) {
3334
- var disposables = new CompositeDisposable();
3349
+ function createEventListener (el, eventName, handler) {
3350
+ var disposables = new CompositeDisposable();
3335
3351
 
3336
- // Asume NodeList
3337
- if (el && el.length) {
3338
- for (var i = 0, len = el.length; i < len; i++) {
3339
- disposables.add(createEventListener(el[i], eventName, handler));
3340
- }
3341
- } else if (el) {
3342
- disposables.add(createListener(el, eventName, handler));
3343
- }
3344
-
3345
- return disposables;
3352
+ // Asume NodeList
3353
+ if (typeof el.item === 'function' && typeof el.length === 'number') {
3354
+ for (var i = 0, len = el.length; i < len; i++) {
3355
+ disposables.add(createEventListener(el.item(i), eventName, handler));
3356
+ }
3357
+ } else if (el) {
3358
+ disposables.add(createListener(el, eventName, handler));
3346
3359
  }
3347
3360
 
3348
- /**
3349
- * Creates an observable sequence by adding an event listener to the matching DOMElement or each item in the NodeList.
3350
- *
3351
- * @example
3352
- * var source = Rx.Observable.fromEvent(element, 'mouseup');
3353
- *
3354
- * @param {Object} element The DOMElement or NodeList to attach a listener.
3355
- * @param {String} eventName The event name to attach the observable sequence.
3356
- * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
3357
- * @returns {Observable} An observable sequence of events from the specified element and the specified event.
3358
- */
3359
- Observable.fromEvent = function (element, eventName, selector) {
3360
- return new AnonymousObservable(function (observer) {
3361
- return createEventListener(
3362
- element,
3363
- eventName,
3364
- function handler (e) {
3365
- var results = e;
3361
+ return disposables;
3362
+ }
3366
3363
 
3367
- if (selector) {
3368
- try {
3369
- results = selector(arguments);
3370
- } catch (err) {
3371
- observer.onError(err);
3372
- return
3373
- }
3374
- }
3364
+ // Check for Angular/jQuery/Zepto support
3365
+ var jq =
3366
+ !!root.angular && !!angular.element ? angular.element :
3367
+ (!!root.jQuery ? root.jQuery : (
3368
+ !!root.Zepto ? root.Zepto : null));
3375
3369
 
3376
- observer.onNext(results);
3377
- });
3378
- }).publish().refCount();
3379
- };
3380
- /**
3381
- * Creates an observable sequence from an event emitter via an addHandler/removeHandler pair.
3382
- * @param {Function} addHandler The function to add a handler to the emitter.
3383
- * @param {Function} [removeHandler] The optional function to remove a handler from an emitter.
3384
- * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
3385
- * @returns {Observable} An observable sequence which wraps an event from an event emitter
3386
- */
3387
- Observable.fromEventPattern = function (addHandler, removeHandler, selector) {
3388
- return new AnonymousObservable(function (observer) {
3389
- function innerHandler (e) {
3390
- var result = e;
3391
- if (selector) {
3392
- try {
3393
- result = selector(arguments);
3394
- } catch (err) {
3395
- observer.onError(err);
3396
- return;
3397
- }
3398
- }
3399
- observer.onNext(result);
3370
+ // Check for ember
3371
+ var ember = !!root.Ember && typeof root.Ember.addListener === 'function';
3372
+
3373
+ /**
3374
+ * Creates an observable sequence by adding an event listener to the matching DOMElement or each item in the NodeList.
3375
+ *
3376
+ * @example
3377
+ * var source = Rx.Observable.fromEvent(element, 'mouseup');
3378
+ *
3379
+ * @param {Object} element The DOMElement or NodeList to attach a listener.
3380
+ * @param {String} eventName The event name to attach the observable sequence.
3381
+ * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
3382
+ * @returns {Observable} An observable sequence of events from the specified element and the specified event.
3383
+ */
3384
+ Observable.fromEvent = function (element, eventName, selector) {
3385
+ if (ember) {
3386
+ return fromEventPattern(
3387
+ function (h) { Ember.addListener(element, eventName); },
3388
+ function (h) { Ember.removeListener(element, eventName); },
3389
+ selector);
3390
+ }
3391
+ if (jq) {
3392
+ var $elem = jq(elem);
3393
+ return fromEventPattern(
3394
+ function (h) { $elem.on(eventName, h); },
3395
+ function (h) { $elem.off(eventName, h); },
3396
+ selector);
3397
+ }
3398
+ return new AnonymousObservable(function (observer) {
3399
+ return createEventListener(
3400
+ element,
3401
+ eventName,
3402
+ function handler (e) {
3403
+ var results = e;
3404
+
3405
+ if (selector) {
3406
+ try {
3407
+ results = selector(arguments);
3408
+ } catch (err) {
3409
+ observer.onError(err);
3410
+ return
3400
3411
  }
3412
+ }
3401
3413
 
3402
- var returnValue = addHandler(innerHandler);
3403
- return disposableCreate(function () {
3404
- if (removeHandler) {
3405
- removeHandler(innerHandler, returnValue);
3406
- }
3407
- });
3408
- }).publish().refCount();
3409
- };
3414
+ observer.onNext(results);
3415
+ });
3416
+ }).publish().refCount();
3417
+ };
3418
+ /**
3419
+ * Creates an observable sequence from an event emitter via an addHandler/removeHandler pair.
3420
+ * @param {Function} addHandler The function to add a handler to the emitter.
3421
+ * @param {Function} [removeHandler] The optional function to remove a handler from an emitter.
3422
+ * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
3423
+ * @returns {Observable} An observable sequence which wraps an event from an event emitter
3424
+ */
3425
+ var fromEventPattern = Observable.fromEventPattern = function (addHandler, removeHandler, selector) {
3426
+ return new AnonymousObservable(function (observer) {
3427
+ function innerHandler (e) {
3428
+ var result = e;
3429
+ if (selector) {
3430
+ try {
3431
+ result = selector(arguments);
3432
+ } catch (err) {
3433
+ observer.onError(err);
3434
+ return;
3435
+ }
3436
+ }
3437
+ observer.onNext(result);
3438
+ }
3439
+
3440
+ var returnValue = addHandler(innerHandler);
3441
+ return disposableCreate(function () {
3442
+ if (removeHandler) {
3443
+ removeHandler(innerHandler, returnValue);
3444
+ }
3445
+ });
3446
+ }).publish().refCount();
3447
+ };
3410
3448
 
3411
3449
  /**
3412
3450
  * Converts a Promise to an Observable sequence
@@ -3423,6 +3461,12 @@
3423
3461
  function (reason) {
3424
3462
  observer.onError(reason);
3425
3463
  });
3464
+
3465
+ return function () {
3466
+ if (promise && promise.abort) {
3467
+ promise.abort();
3468
+ }
3469
+ }
3426
3470
  });
3427
3471
  };
3428
3472
  /*
@@ -4486,22 +4530,16 @@
4486
4530
  });
4487
4531
  };
4488
4532
 
4489
- /**
4490
- * Pauses the underlying observable sequence based upon the observable sequence which yields true/false.
4491
- * @example
4492
- * var pauser = new Rx.Subject();
4493
- * var source = Rx.Observable.interval(100).pausable(pauser);
4494
- * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
4495
- * @returns {Observable} The observable sequence which is paused based upon the pauser.
4496
- */
4497
- observableProto.pausable = function (pauser) {
4498
- var self = this;
4499
- return new AnonymousObservable(function (observer) {
4500
- var conn = self.publish(),
4533
+ var PausableObservable = (function (_super) {
4534
+
4535
+ inherits(PausableObservable, _super);
4536
+
4537
+ function subscribe(observer) {
4538
+ var conn = this.source.publish(),
4501
4539
  subscription = conn.subscribe(observer),
4502
4540
  connection = disposableEmpty;
4503
4541
 
4504
- var pausable = pauser.distinctUntilChanged().subscribe(function (b) {
4542
+ var pausable = this.subject.distinctUntilChanged().subscribe(function (b) {
4505
4543
  if (b) {
4506
4544
  connection = conn.connect();
4507
4545
  } else {
@@ -4511,7 +4549,45 @@
4511
4549
  });
4512
4550
 
4513
4551
  return new CompositeDisposable(subscription, connection, pausable);
4514
- });
4552
+ }
4553
+
4554
+ function PausableObservable(source, subject) {
4555
+ this.source = source;
4556
+ this.subject = subject || new Subject();
4557
+ this.isPaused = true;
4558
+ _super.call(this, subscribe);
4559
+ }
4560
+
4561
+ PausableObservable.prototype.pause = function () {
4562
+ if (this.isPaused === true){
4563
+ return;
4564
+ }
4565
+ this.isPaused = true;
4566
+ this.subject.onNext(false);
4567
+ };
4568
+
4569
+ PausableObservable.prototype.resume = function () {
4570
+ if (this.isPaused === false){
4571
+ return;
4572
+ }
4573
+ this.isPaused = false;
4574
+ this.subject.onNext(true);
4575
+ };
4576
+
4577
+ return PausableObservable;
4578
+
4579
+ }(Observable));
4580
+
4581
+ /**
4582
+ * Pauses the underlying observable sequence based upon the observable sequence which yields true/false.
4583
+ * @example
4584
+ * var pauser = new Rx.Subject();
4585
+ * var source = Rx.Observable.interval(100).pausable(pauser);
4586
+ * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
4587
+ * @returns {Observable} The observable sequence which is paused based upon the pauser.
4588
+ */
4589
+ observableProto.pausable = function (pauser) {
4590
+ return new PausableObservable(this, pauser);
4515
4591
  };
4516
4592
  function combineLatestSource(source, subject, resultSelector) {
4517
4593
  return new AnonymousObservable(function (observer) {
@@ -4557,24 +4633,17 @@
4557
4633
  });
4558
4634
  }
4559
4635
 
4560
- /**
4561
- * Pauses the underlying observable sequence based upon the observable sequence which yields true/false,
4562
- * and yields the values that were buffered while paused.
4563
- * @example
4564
- * var pauser = new Rx.Subject();
4565
- * var source = Rx.Observable.interval(100).pausableBuffered(pauser);
4566
- * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
4567
- * @returns {Observable} The observable sequence which is paused based upon the pauser.
4568
- */
4569
- observableProto.pausableBuffered = function (subject) {
4570
- var source = this;
4571
- return new AnonymousObservable(function (observer) {
4636
+ var PausableBufferedObservable = (function (_super) {
4637
+
4638
+ inherits(PausableBufferedObservable, _super);
4639
+
4640
+ function subscribe(observer) {
4572
4641
  var q = [], previous = true;
4573
4642
 
4574
4643
  var subscription =
4575
4644
  combineLatestSource(
4576
- source,
4577
- subject.distinctUntilChanged(),
4645
+ this.source,
4646
+ this.subject.distinctUntilChanged(),
4578
4647
  function (data, shouldFire) {
4579
4648
  return { data: data, shouldFire: shouldFire };
4580
4649
  })
@@ -4599,10 +4668,49 @@
4599
4668
  observer.onCompleted.bind(observer)
4600
4669
  );
4601
4670
 
4602
- subject.onNext(false);
4671
+ this.subject.onNext(false);
4603
4672
 
4604
- return subscription;
4605
- });
4673
+ return subscription;
4674
+ }
4675
+
4676
+ function PausableBufferedObservable(source, subject) {
4677
+ this.source = source;
4678
+ this.subject = subject || new Subject();
4679
+ this.isPaused = true;
4680
+ _super.call(this, subscribe);
4681
+ }
4682
+
4683
+ PausableBufferedObservable.prototype.pause = function () {
4684
+ if (this.isPaused === true){
4685
+ return;
4686
+ }
4687
+ this.isPaused = true;
4688
+ this.subject.onNext(false);
4689
+ };
4690
+
4691
+ PausableBufferedObservable.prototype.resume = function () {
4692
+ if (this.isPaused === false){
4693
+ return;
4694
+ }
4695
+ this.isPaused = false;
4696
+ this.subject.onNext(true);
4697
+ };
4698
+
4699
+ return PausableBufferedObservable;
4700
+
4701
+ }(Observable));
4702
+
4703
+ /**
4704
+ * Pauses the underlying observable sequence based upon the observable sequence which yields true/false,
4705
+ * and yields the values that were buffered while paused.
4706
+ * @example
4707
+ * var pauser = new Rx.Subject();
4708
+ * var source = Rx.Observable.interval(100).pausableBuffered(pauser);
4709
+ * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
4710
+ * @returns {Observable} The observable sequence which is paused based upon the pauser.
4711
+ */
4712
+ observableProto.pausableBuffered = function (subject) {
4713
+ return new PausableBufferedObservable(this, subject);
4606
4714
  };
4607
4715
 
4608
4716
  /**