Support any Observable library and polyfill
npm install any-observableYou must also install the Observable library you want:
npm install zen-observableimport Observable from 'any-observable'; // Using `zen-observable` since it's installed
Observable.of(1, 2).forEach(value => {
console.log(value);
});
//=> 1
//=> 2If you want to attempt loading without throwing, use the optional entrypoint. It returns undefined when no implementation is available:
import Observable from 'any-observable/optional';
if (Observable === undefined) {
console.log('No Observable implementation available');
}This adds the following shortcut registrations:
rxjs-min: Bare bones RxJs Observable implementation. See the RxJs Installation Instructions for details on patching additional methods into that implementation.rxjs: Same asrxjs-min, but adds the somewhat standardObservable.ofandObservable.from.rxjs-all: The kitchen sink approach to Observables.zen: Thezen-observableimplementation.
Shortcut registration can be done as follows:
import 'any-observable/register/zen';It's especially handy for environments that support preloading ESM modules:
node --import=any-observable/register/zen test.jsUse the async helper when you want to probe multiple implementations in a browser native ESM setup:
import registerAsync from 'any-observable/register/async';
await registerAsync([
'rxjs',
'zen-observable'
]);
const {default: Observable} = await import('any-observable');This requires import maps or URL specifiers for the implementations you probe, and it must run before any import 'any-observable'.
any-observable expects an ES Observable implementation (a constructor), not just interop on stream instances. Libraries like most, Kefir, Bacon, xstream, and Flyd are not supported directly unless they provide an ES Observable constructor. If your library can convert to an ES Observable, use that and register explicitly:
import register from 'any-observable/register';
register('custom', {Observable: YourObservable});If your library depends on Observables via any-observable, follow these guidelines:
- Do not call
registeryourself. Leave Observable implementation choice to the end user. - Do not rely on non-standard features. Stick to the ES Observable spec so any compliant implementation works.
- Document
any-observablesupport prominently. Let users know they need to install and register an Observable implementation.
- is-observable - Check if a value is an Observable
- observable-to-promise - Convert an Observable to a Promise