forked from Netflix/falcor-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(get, set, call): support both Rx4 and Rx5+ formats
In order to support backward compatibility with other falcor toolsets, both Rx4 style and modern observable style signatures must be supported. This still returns an Rx5 observable, but it may be subscribed to and disposed in the Rx4 style
- Loading branch information
Showing
6 changed files
with
235 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// WHY NOT BOTH? | ||
module.exports = function rxNewToRxNewAndOld(rxNewObservable) { | ||
var _subscribe = rxNewObservable.subscribe; | ||
|
||
rxNewObservable.subscribe = function (observerOrNextFn, errFn, compFn) { | ||
var subscription; | ||
switch (typeof observerOrNextFn) { | ||
case 'function': | ||
subscription = _subscribe.call(this, | ||
observerOrNextFn, errFn, compFn); | ||
break; | ||
case 'object': | ||
var observer = observerOrNextFn; | ||
if (typeof observerOrNextFn.onNext === 'function') { | ||
// old observer! | ||
observer = { | ||
next: function (x) { | ||
var destination = this.destination; | ||
destination.onNext(x); | ||
}, | ||
error: function (err) { | ||
var destination = this.destination; | ||
if (destination.onError) { | ||
destination.onError(err); | ||
} | ||
}, | ||
complete: function () { | ||
var destination = this.destination; | ||
if (destination.onCompleted) { | ||
destination.onCompleted(); | ||
} | ||
}, | ||
destination: observerOrNextFn | ||
} | ||
} | ||
subscription = _subscribe.call(this, observer); | ||
break; | ||
case 'undefined': | ||
subscription = _subscribe.call(this); | ||
break; | ||
default: | ||
throw new TypeError('cannot subscribe to observable with ' + | ||
'type ' + typeof observerOrNextFn); | ||
} | ||
|
||
var _unsubscribe = subscription.unsubscribe; | ||
|
||
subscription.unsubscribe = subscription.dispose = function () { | ||
this.isDisposed = true; | ||
_unsubscribe.call(subscription); | ||
}; | ||
|
||
return subscription; | ||
} | ||
|
||
return rxNewObservable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
var Observable = require('rxjs/Rx').Observable; | ||
var rxNewToRxNewAndOld = | ||
require('../../../../src/run/conversion/rxNewToRxNewAndOld'); | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
|
||
describe('rxNewToRxNewAndOld', function () { | ||
it('should work with "old" observers', function () { | ||
var source = Observable.of(1, 2, 3); | ||
var results = []; | ||
|
||
var sub = rxNewToRxNewAndOld(source).subscribe({ | ||
onNext: function (x) { | ||
results.push(x); | ||
}, | ||
onError: function (err) { | ||
throw err; | ||
}, | ||
onCompleted: function () { | ||
results.push('done'); | ||
} | ||
}); | ||
|
||
expect(sub.dispose).to.be.a('function'); | ||
expect(sub.unsubscribe).to.be.a('function'); | ||
expect(results).to.deep.equal([1, 2, 3, 'done']); | ||
}); | ||
|
||
it('should work with "new" observers', function () { | ||
var source = Observable.of(1, 2, 3); | ||
var results = []; | ||
|
||
var sub = rxNewToRxNewAndOld(source).subscribe({ | ||
next: function (x) { | ||
results.push(x); | ||
}, | ||
error: function (err) { | ||
throw err; | ||
}, | ||
complete: function () { | ||
results.push('done'); | ||
} | ||
}); | ||
|
||
expect(sub.dispose).to.be.a('function'); | ||
expect(sub.unsubscribe).to.be.a('function'); | ||
expect(results).to.deep.equal([1, 2, 3, 'done']); | ||
}); | ||
|
||
it('should work with three functions', function () { | ||
var source = Observable.of(1, 2, 3); | ||
var results = []; | ||
|
||
var sub = rxNewToRxNewAndOld(source).subscribe( | ||
function (x) { | ||
results.push(x); | ||
}, | ||
function (err) { | ||
throw err; | ||
}, | ||
function () { | ||
results.push('done'); | ||
} | ||
); | ||
|
||
expect(sub.dispose).to.be.a('function'); | ||
expect(sub.unsubscribe).to.be.a('function'); | ||
expect(results).to.deep.equal([1, 2, 3, 'done']); | ||
}); | ||
|
||
it('should work with no arguments', function () { | ||
var source = Observable.of(1, 2, 3); | ||
var results = []; | ||
|
||
var sub = rxNewToRxNewAndOld(source).subscribe(); | ||
|
||
expect(sub.dispose).to.be.a('function'); | ||
expect(sub.unsubscribe).to.be.a('function'); | ||
expect(results).to.deep.equal([1, 2, 3, 'done']); | ||
}); | ||
|
||
it('should unsubscribe with `dispose`', function () { | ||
var source = Observable.of('hello'); | ||
var results = []; | ||
|
||
var sub = rxNewToRxNewAndOld(source).subscribe( | ||
function (x) { | ||
results.push(x); | ||
}, | ||
function (err) { | ||
throw err; | ||
}, | ||
function () { | ||
results.push('done'); | ||
} | ||
); | ||
|
||
sub.dispose(); | ||
|
||
expect(sub.closed).to.be(true); | ||
expect(sub.isDisposed).to.be(true); | ||
expect(results).to.deep.equal([]); | ||
}); | ||
|
||
it('should unsubscribe with `unsubscribe`', function () { | ||
var source = Observable.of('hello'); | ||
var results = []; | ||
|
||
var sub = rxNewToRxNewAndOld(source).subscribe( | ||
function (x) { | ||
results.push(x); | ||
}, | ||
function (err) { | ||
throw err; | ||
}, | ||
function () { | ||
results.push('done'); | ||
} | ||
); | ||
|
||
sub.unsubscribe(); | ||
|
||
expect(sub.closed).to.be(true); | ||
expect(sub.isDisposed).to.be(true); | ||
expect(results).to.deep.equal([]); | ||
}); | ||
}) |