-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isObservable): a new method for checking to see if an object is …
…an RxJS Observable I've seen a lot of code that checks to see if something is an observable by doing an `typeof` or even `instanceOf` check. It's plausible that in the future these tests might break, as they're testing implementation details of the library. It is recommended that people use this `isObservable` method to see if an object is a compatible RxJS Observable.
- Loading branch information
Showing
3 changed files
with
37 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Observable, isObservable } from 'rxjs'; | ||
import { expect } from 'chai'; | ||
|
||
describe('isObservable', () => { | ||
it('should return true for RxJS Observable', () => { | ||
const o = new Observable<any>(); | ||
expect(isObservable(o)).to.be.true; | ||
}); | ||
|
||
it('should return true for an observable that comes from another RxJS 5+ library', () => { | ||
const o: any = { | ||
lift() { /* noop */ }, | ||
subscribe() { /* noop */ }, | ||
}; | ||
|
||
expect(isObservable(o)).to.be.true; | ||
}); | ||
|
||
it('should NOT return true for any old subscribable', () => { | ||
const o: any = { | ||
subscribe() { /* noop */ }, | ||
}; | ||
|
||
expect(isObservable(o)).to.be.false; | ||
}); | ||
}); |
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,10 @@ | ||
import { Observable } from '../Observable'; | ||
import { ObservableInput } from '../types'; | ||
|
||
/** | ||
* Tests to see if the object is an RxJS {@link Observable} | ||
* @param obj the object to test | ||
*/ | ||
export function isObservable<T>(obj: any): obj is Observable<T> { | ||
return obj && obj instanceof Observable || (typeof obj.lift === 'function' && typeof obj.subscribe === 'function'); | ||
} |