@politie/sherlock-rxjs
Version:
An extension to Sherlock that provides interop with RxJS.
38 lines (35 loc) • 1.66 kB
JavaScript
import { _internal, atom, ErrorWrapper } from '@politie/sherlock';
import { Observable } from 'rxjs';
/**
* Creates an RxJS Observable from a Derivable. Optionally accepts a `ReactorOptions` that governs RxJS emissions
* and lifecycle equivalent to {@link Derivable#react} {@link ReactorOptions}.
* @param derivable Derivable to create an RxJS Observable from.
* @param options Partial `ReactorOptions`.
*/
function toObservable(derivable, options) {
return new Observable(function (subscriber) {
return _internal.Reactor.create(derivable, function (value) { return subscriber.next(value); }, options, function () { return subscriber.closed || subscriber.complete(); });
});
}
function fromObservable(observable) {
var atom$ = atom.unresolved();
var subscription;
atom$.connected$.react(function () {
if (atom$.connected && !subscription) {
subscription = observable.subscribe({
next: function (value) { return atom$.set(value); },
error: function (err) { return atom$.setFinal(new ErrorWrapper(err)); },
complete: function () { return atom$.setFinal(atom$.getState()); },
});
}
// This is not chained with the previous as an `else` branch, because this can be true immediately after
// the subscription occurs. Observables can complete synchronously on subscription.
if (!atom$.connected && subscription) {
subscription.unsubscribe();
subscription = undefined;
}
});
return atom$;
}
export { fromObservable, toObservable };
//# sourceMappingURL=sherlock-rxjs.esm.js.map