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 = (function () { return !!Date.now ? Date.now : function () { return +new Date; }; }());
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 = (function () { return !!Date.now ? Date.now : function () { return +new Date; }; }()),
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.';
@@ -1578,9 +1581,13 @@
1578
1581
  return;
1579
1582
  }
1580
1583
 
1584
+ // Check if promise
1585
+ var currentValue = currentItem.value;
1586
+ isPromise(currentValue) && (currentValue = observableFromPromise(currentValue));
1587
+
1581
1588
  var d = new SingleAssignmentDisposable();
1582
1589
  subscription.setDisposable(d);
1583
- d.setDisposable(currentItem.value.subscribe(
1590
+ d.setDisposable(currentValue.subscribe(
1584
1591
  observer.onNext.bind(observer),
1585
1592
  observer.onError.bind(observer),
1586
1593
  function () { self(); })
@@ -1627,9 +1634,13 @@
1627
1634
  return;
1628
1635
  }
1629
1636
 
1637
+ // Check if promise
1638
+ var currentValue = currentItem.value;
1639
+ isPromise(currentValue) && (currentValue = observableFromPromise(currentValue));
1640
+
1630
1641
  var d = new SingleAssignmentDisposable();
1631
1642
  subscription.setDisposable(d);
1632
- d.setDisposable(currentItem.value.subscribe(
1643
+ d.setDisposable(currentValue.subscribe(
1633
1644
  observer.onNext.bind(observer),
1634
1645
  function (exn) {
1635
1646
  lastException = exn;
@@ -2026,6 +2037,9 @@
2026
2037
  } catch (e) {
2027
2038
  return observableThrow(e).subscribe(observer);
2028
2039
  }
2040
+
2041
+ // Check if promise
2042
+ isPromise(result) && (result = observableFromPromise(result));
2029
2043
  return result.subscribe(observer);
2030
2044
  });
2031
2045
  };
@@ -3433,168 +3447,190 @@
3433
3447
  };
3434
3448
  };
3435
3449
 
3436
- function fixEvent(event) {
3437
- var stopPropagation = function () {
3438
- this.cancelBubble = true;
3439
- };
3440
-
3441
- var preventDefault = function () {
3442
- this.bubbledKeyCode = this.keyCode;
3443
- if (this.ctrlKey) {
3444
- try {
3445
- this.keyCode = 0;
3446
- } catch (e) { }
3447
- }
3448
- this.defaultPrevented = true;
3449
- this.returnValue = false;
3450
- this.modified = true;
3451
- };
3450
+ function fixEvent(event) {
3451
+ var stopPropagation = function () {
3452
+ this.cancelBubble = true;
3453
+ };
3452
3454
 
3453
- event || (event = window.event);
3454
- if (!event.target) {
3455
- event.target = event.target || event.srcElement;
3455
+ var preventDefault = function () {
3456
+ this.bubbledKeyCode = this.keyCode;
3457
+ if (this.ctrlKey) {
3458
+ try {
3459
+ this.keyCode = 0;
3460
+ } catch (e) { }
3461
+ }
3462
+ this.defaultPrevented = true;
3463
+ this.returnValue = false;
3464
+ this.modified = true;
3465
+ };
3456
3466
 
3457
- if (event.type == 'mouseover') {
3458
- event.relatedTarget = event.fromElement;
3459
- }
3460
- if (event.type == 'mouseout') {
3461
- event.relatedTarget = event.toElement;
3462
- }
3463
- // Adding stopPropogation and preventDefault to IE
3464
- if (!event.stopPropagation){
3465
- event.stopPropagation = stopPropagation;
3466
- event.preventDefault = preventDefault;
3467
- }
3468
- // Normalize key events
3469
- switch(event.type){
3470
- case 'keypress':
3471
- var c = ('charCode' in event ? event.charCode : event.keyCode);
3472
- if (c == 10) {
3473
- c = 0;
3474
- event.keyCode = 13;
3475
- } else if (c == 13 || c == 27) {
3476
- c = 0;
3477
- } else if (c == 3) {
3478
- c = 99;
3479
- }
3480
- event.charCode = c;
3481
- event.keyChar = event.charCode ? String.fromCharCode(event.charCode) : '';
3482
- break;
3483
- }
3484
- }
3467
+ event || (event = root.event);
3468
+ if (!event.target) {
3469
+ event.target = event.target || event.srcElement;
3485
3470
 
3486
- return event;
3471
+ if (event.type == 'mouseover') {
3472
+ event.relatedTarget = event.fromElement;
3473
+ }
3474
+ if (event.type == 'mouseout') {
3475
+ event.relatedTarget = event.toElement;
3476
+ }
3477
+ // Adding stopPropogation and preventDefault to IE
3478
+ if (!event.stopPropagation){
3479
+ event.stopPropagation = stopPropagation;
3480
+ event.preventDefault = preventDefault;
3481
+ }
3482
+ // Normalize key events
3483
+ switch(event.type){
3484
+ case 'keypress':
3485
+ var c = ('charCode' in event ? event.charCode : event.keyCode);
3486
+ if (c == 10) {
3487
+ c = 0;
3488
+ event.keyCode = 13;
3489
+ } else if (c == 13 || c == 27) {
3490
+ c = 0;
3491
+ } else if (c == 3) {
3492
+ c = 99;
3493
+ }
3494
+ event.charCode = c;
3495
+ event.keyChar = event.charCode ? String.fromCharCode(event.charCode) : '';
3496
+ break;
3497
+ }
3487
3498
  }
3488
3499
 
3489
- function createListener (element, name, handler) {
3490
- // Node.js specific
3491
- if (element.addListener) {
3492
- element.addListener(name, handler);
3493
- return disposableCreate(function () {
3494
- element.removeListener(name, handler);
3495
- });
3496
- }
3497
- // Standards compliant
3498
- if (element.addEventListener) {
3499
- element.addEventListener(name, handler, false);
3500
- return disposableCreate(function () {
3501
- element.removeEventListener(name, handler, false);
3502
- });
3503
- } else if (element.attachEvent) {
3504
- // IE Specific
3505
- var innerHandler = function (event) {
3506
- handler(fixEvent(event));
3507
- };
3508
- element.attachEvent('on' + name, innerHandler);
3509
- return disposableCreate(function () {
3510
- element.detachEvent('on' + name, innerHandler);
3511
- });
3512
- } else {
3513
- // Level 1 DOM Events
3514
- element['on' + name] = handler;
3515
- return disposableCreate(function () {
3516
- element['on' + name] = null;
3517
- });
3518
- }
3519
- }
3500
+ return event;
3501
+ }
3520
3502
 
3521
- function createEventListener (el, eventName, handler) {
3522
- var disposables = new CompositeDisposable();
3503
+ function createListener (element, name, handler) {
3504
+ // Node.js specific
3505
+ if (element.addListener) {
3506
+ element.addListener(name, handler);
3507
+ return disposableCreate(function () {
3508
+ element.removeListener(name, handler);
3509
+ });
3510
+ }
3511
+ // Standards compliant
3512
+ if (element.addEventListener) {
3513
+ element.addEventListener(name, handler, false);
3514
+ return disposableCreate(function () {
3515
+ element.removeEventListener(name, handler, false);
3516
+ });
3517
+ }
3518
+ if (element.attachEvent) {
3519
+ // IE Specific
3520
+ var innerHandler = function (event) {
3521
+ handler(fixEvent(event));
3522
+ };
3523
+ element.attachEvent('on' + name, innerHandler);
3524
+ return disposableCreate(function () {
3525
+ element.detachEvent('on' + name, innerHandler);
3526
+ });
3527
+ }
3528
+ // Level 1 DOM Events
3529
+ element['on' + name] = handler;
3530
+ return disposableCreate(function () {
3531
+ element['on' + name] = null;
3532
+ });
3533
+ }
3523
3534
 
3524
- // Asume NodeList
3525
- if (el && el.length) {
3526
- for (var i = 0, len = el.length; i < len; i++) {
3527
- disposables.add(createEventListener(el[i], eventName, handler));
3528
- }
3529
- } else if (el) {
3530
- disposables.add(createListener(el, eventName, handler));
3531
- }
3535
+ function createEventListener (el, eventName, handler) {
3536
+ var disposables = new CompositeDisposable();
3532
3537
 
3533
- return disposables;
3538
+ // Asume NodeList
3539
+ if (typeof el.item === 'function' && typeof el.length === 'number') {
3540
+ for (var i = 0, len = el.length; i < len; i++) {
3541
+ disposables.add(createEventListener(el.item(i), eventName, handler));
3542
+ }
3543
+ } else if (el) {
3544
+ disposables.add(createListener(el, eventName, handler));
3534
3545
  }
3535
3546
 
3536
- /**
3537
- * Creates an observable sequence by adding an event listener to the matching DOMElement or each item in the NodeList.
3538
- *
3539
- * @example
3540
- * var source = Rx.Observable.fromEvent(element, 'mouseup');
3541
- *
3542
- * @param {Object} element The DOMElement or NodeList to attach a listener.
3543
- * @param {String} eventName The event name to attach the observable sequence.
3544
- * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
3545
- * @returns {Observable} An observable sequence of events from the specified element and the specified event.
3546
- */
3547
- Observable.fromEvent = function (element, eventName, selector) {
3548
- return new AnonymousObservable(function (observer) {
3549
- return createEventListener(
3550
- element,
3551
- eventName,
3552
- function handler (e) {
3553
- var results = e;
3547
+ return disposables;
3548
+ }
3554
3549
 
3555
- if (selector) {
3556
- try {
3557
- results = selector(arguments);
3558
- } catch (err) {
3559
- observer.onError(err);
3560
- return
3561
- }
3562
- }
3550
+ // Check for Angular/jQuery/Zepto support
3551
+ var jq =
3552
+ !!root.angular && !!angular.element ? angular.element :
3553
+ (!!root.jQuery ? root.jQuery : (
3554
+ !!root.Zepto ? root.Zepto : null));
3563
3555
 
3564
- observer.onNext(results);
3565
- });
3566
- }).publish().refCount();
3567
- };
3568
- /**
3569
- * Creates an observable sequence from an event emitter via an addHandler/removeHandler pair.
3570
- * @param {Function} addHandler The function to add a handler to the emitter.
3571
- * @param {Function} [removeHandler] The optional function to remove a handler from an emitter.
3572
- * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
3573
- * @returns {Observable} An observable sequence which wraps an event from an event emitter
3574
- */
3575
- Observable.fromEventPattern = function (addHandler, removeHandler, selector) {
3576
- return new AnonymousObservable(function (observer) {
3577
- function innerHandler (e) {
3578
- var result = e;
3579
- if (selector) {
3580
- try {
3581
- result = selector(arguments);
3582
- } catch (err) {
3583
- observer.onError(err);
3584
- return;
3585
- }
3586
- }
3587
- observer.onNext(result);
3556
+ // Check for ember
3557
+ var ember = !!root.Ember && typeof root.Ember.addListener === 'function';
3558
+
3559
+ /**
3560
+ * Creates an observable sequence by adding an event listener to the matching DOMElement or each item in the NodeList.
3561
+ *
3562
+ * @example
3563
+ * var source = Rx.Observable.fromEvent(element, 'mouseup');
3564
+ *
3565
+ * @param {Object} element The DOMElement or NodeList to attach a listener.
3566
+ * @param {String} eventName The event name to attach the observable sequence.
3567
+ * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
3568
+ * @returns {Observable} An observable sequence of events from the specified element and the specified event.
3569
+ */
3570
+ Observable.fromEvent = function (element, eventName, selector) {
3571
+ if (ember) {
3572
+ return fromEventPattern(
3573
+ function (h) { Ember.addListener(element, eventName); },
3574
+ function (h) { Ember.removeListener(element, eventName); },
3575
+ selector);
3576
+ }
3577
+ if (jq) {
3578
+ var $elem = jq(elem);
3579
+ return fromEventPattern(
3580
+ function (h) { $elem.on(eventName, h); },
3581
+ function (h) { $elem.off(eventName, h); },
3582
+ selector);
3583
+ }
3584
+ return new AnonymousObservable(function (observer) {
3585
+ return createEventListener(
3586
+ element,
3587
+ eventName,
3588
+ function handler (e) {
3589
+ var results = e;
3590
+
3591
+ if (selector) {
3592
+ try {
3593
+ results = selector(arguments);
3594
+ } catch (err) {
3595
+ observer.onError(err);
3596
+ return
3588
3597
  }
3598
+ }
3589
3599
 
3590
- var returnValue = addHandler(innerHandler);
3591
- return disposableCreate(function () {
3592
- if (removeHandler) {
3593
- removeHandler(innerHandler, returnValue);
3594
- }
3595
- });
3596
- }).publish().refCount();
3597
- };
3600
+ observer.onNext(results);
3601
+ });
3602
+ }).publish().refCount();
3603
+ };
3604
+ /**
3605
+ * Creates an observable sequence from an event emitter via an addHandler/removeHandler pair.
3606
+ * @param {Function} addHandler The function to add a handler to the emitter.
3607
+ * @param {Function} [removeHandler] The optional function to remove a handler from an emitter.
3608
+ * @param {Function} [selector] A selector which takes the arguments from the event handler to produce a single item to yield on next.
3609
+ * @returns {Observable} An observable sequence which wraps an event from an event emitter
3610
+ */
3611
+ var fromEventPattern = Observable.fromEventPattern = function (addHandler, removeHandler, selector) {
3612
+ return new AnonymousObservable(function (observer) {
3613
+ function innerHandler (e) {
3614
+ var result = e;
3615
+ if (selector) {
3616
+ try {
3617
+ result = selector(arguments);
3618
+ } catch (err) {
3619
+ observer.onError(err);
3620
+ return;
3621
+ }
3622
+ }
3623
+ observer.onNext(result);
3624
+ }
3625
+
3626
+ var returnValue = addHandler(innerHandler);
3627
+ return disposableCreate(function () {
3628
+ if (removeHandler) {
3629
+ removeHandler(innerHandler, returnValue);
3630
+ }
3631
+ });
3632
+ }).publish().refCount();
3633
+ };
3598
3634
 
3599
3635
  /**
3600
3636
  * Converts a Promise to an Observable sequence
@@ -3611,6 +3647,12 @@
3611
3647
  function (reason) {
3612
3648
  observer.onError(reason);
3613
3649
  });
3650
+
3651
+ return function () {
3652
+ if (promise && promise.abort) {
3653
+ promise.abort();
3654
+ }
3655
+ }
3614
3656
  });
3615
3657
  };
3616
3658
  /*
@@ -4674,22 +4716,16 @@
4674
4716
  });
4675
4717
  };
4676
4718
 
4677
- /**
4678
- * Pauses the underlying observable sequence based upon the observable sequence which yields true/false.
4679
- * @example
4680
- * var pauser = new Rx.Subject();
4681
- * var source = Rx.Observable.interval(100).pausable(pauser);
4682
- * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
4683
- * @returns {Observable} The observable sequence which is paused based upon the pauser.
4684
- */
4685
- observableProto.pausable = function (pauser) {
4686
- var self = this;
4687
- return new AnonymousObservable(function (observer) {
4688
- var conn = self.publish(),
4719
+ var PausableObservable = (function (_super) {
4720
+
4721
+ inherits(PausableObservable, _super);
4722
+
4723
+ function subscribe(observer) {
4724
+ var conn = this.source.publish(),
4689
4725
  subscription = conn.subscribe(observer),
4690
4726
  connection = disposableEmpty;
4691
4727
 
4692
- var pausable = pauser.distinctUntilChanged().subscribe(function (b) {
4728
+ var pausable = this.subject.distinctUntilChanged().subscribe(function (b) {
4693
4729
  if (b) {
4694
4730
  connection = conn.connect();
4695
4731
  } else {
@@ -4699,7 +4735,45 @@
4699
4735
  });
4700
4736
 
4701
4737
  return new CompositeDisposable(subscription, connection, pausable);
4702
- });
4738
+ }
4739
+
4740
+ function PausableObservable(source, subject) {
4741
+ this.source = source;
4742
+ this.subject = subject || new Subject();
4743
+ this.isPaused = true;
4744
+ _super.call(this, subscribe);
4745
+ }
4746
+
4747
+ PausableObservable.prototype.pause = function () {
4748
+ if (this.isPaused === true){
4749
+ return;
4750
+ }
4751
+ this.isPaused = true;
4752
+ this.subject.onNext(false);
4753
+ };
4754
+
4755
+ PausableObservable.prototype.resume = function () {
4756
+ if (this.isPaused === false){
4757
+ return;
4758
+ }
4759
+ this.isPaused = false;
4760
+ this.subject.onNext(true);
4761
+ };
4762
+
4763
+ return PausableObservable;
4764
+
4765
+ }(Observable));
4766
+
4767
+ /**
4768
+ * Pauses the underlying observable sequence based upon the observable sequence which yields true/false.
4769
+ * @example
4770
+ * var pauser = new Rx.Subject();
4771
+ * var source = Rx.Observable.interval(100).pausable(pauser);
4772
+ * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
4773
+ * @returns {Observable} The observable sequence which is paused based upon the pauser.
4774
+ */
4775
+ observableProto.pausable = function (pauser) {
4776
+ return new PausableObservable(this, pauser);
4703
4777
  };
4704
4778
  function combineLatestSource(source, subject, resultSelector) {
4705
4779
  return new AnonymousObservable(function (observer) {
@@ -4745,24 +4819,17 @@
4745
4819
  });
4746
4820
  }
4747
4821
 
4748
- /**
4749
- * Pauses the underlying observable sequence based upon the observable sequence which yields true/false,
4750
- * and yields the values that were buffered while paused.
4751
- * @example
4752
- * var pauser = new Rx.Subject();
4753
- * var source = Rx.Observable.interval(100).pausableBuffered(pauser);
4754
- * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
4755
- * @returns {Observable} The observable sequence which is paused based upon the pauser.
4756
- */
4757
- observableProto.pausableBuffered = function (subject) {
4758
- var source = this;
4759
- return new AnonymousObservable(function (observer) {
4822
+ var PausableBufferedObservable = (function (_super) {
4823
+
4824
+ inherits(PausableBufferedObservable, _super);
4825
+
4826
+ function subscribe(observer) {
4760
4827
  var q = [], previous = true;
4761
4828
 
4762
4829
  var subscription =
4763
4830
  combineLatestSource(
4764
- source,
4765
- subject.distinctUntilChanged(),
4831
+ this.source,
4832
+ this.subject.distinctUntilChanged(),
4766
4833
  function (data, shouldFire) {
4767
4834
  return { data: data, shouldFire: shouldFire };
4768
4835
  })
@@ -4787,10 +4854,49 @@
4787
4854
  observer.onCompleted.bind(observer)
4788
4855
  );
4789
4856
 
4790
- subject.onNext(false);
4857
+ this.subject.onNext(false);
4791
4858
 
4792
- return subscription;
4793
- });
4859
+ return subscription;
4860
+ }
4861
+
4862
+ function PausableBufferedObservable(source, subject) {
4863
+ this.source = source;
4864
+ this.subject = subject || new Subject();
4865
+ this.isPaused = true;
4866
+ _super.call(this, subscribe);
4867
+ }
4868
+
4869
+ PausableBufferedObservable.prototype.pause = function () {
4870
+ if (this.isPaused === true){
4871
+ return;
4872
+ }
4873
+ this.isPaused = true;
4874
+ this.subject.onNext(false);
4875
+ };
4876
+
4877
+ PausableBufferedObservable.prototype.resume = function () {
4878
+ if (this.isPaused === false){
4879
+ return;
4880
+ }
4881
+ this.isPaused = false;
4882
+ this.subject.onNext(true);
4883
+ };
4884
+
4885
+ return PausableBufferedObservable;
4886
+
4887
+ }(Observable));
4888
+
4889
+ /**
4890
+ * Pauses the underlying observable sequence based upon the observable sequence which yields true/false,
4891
+ * and yields the values that were buffered while paused.
4892
+ * @example
4893
+ * var pauser = new Rx.Subject();
4894
+ * var source = Rx.Observable.interval(100).pausableBuffered(pauser);
4895
+ * @param {Observable} pauser The observable sequence used to pause the underlying sequence.
4896
+ * @returns {Observable} The observable sequence which is paused based upon the pauser.
4897
+ */
4898
+ observableProto.pausableBuffered = function (subject) {
4899
+ return new PausableBufferedObservable(this, subject);
4794
4900
  };
4795
4901
 
4796
4902
  /**