rxjs-interop
What is it?
A package that contains interop helper functions and types for use with RxJS.
Why might you need it?
Observables exist independently of RxJS. There are TC39 and WHATWG observable proposals. The helpers in this package make it easy to implement observables that will work with or without RxJS and will still play nice - with RxJS - if Symbol.observable
is not configured.
Install
Install the package using NPM:
npm install rxjs-interop --save
Usage
Interop observables expose an observable factory via the Symbol.observable
property, like this:
export const interop = {
[Symbol.observable]: () => {
return {
subscribe(nextOrObserver, error, complete) {
return () => { };
}
};
}
});
For this to work with RxJS, the caller will need to initialize Symbol.observable
- which makes things more complicated.
You can use the patch
and toObserver
helpers in this package to more easily implement subscribe
and to make sure that the interop observable will work with RxJS without Symbol.observable
having to be initialized:
import { patch, toObserver } from "rxjs-interop";
export const interop = patch({
[Symbol.observable]: () => {
return {
subscribe(nextOrObserver, error, complete) {
const observer = toObserver(nextOrObserver, error, complete);
return () => { };
}
};
}
});
patch
can be passed either an interop observable instance or the constructor of an interop observable class.