UNPKG

@politie/sherlock-rxjs

Version:

An extension to Sherlock that provides interop with RxJS.

43 lines (38 loc) 1.78 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var sherlock = require('@politie/sherlock'); var rxjs = require('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 rxjs.Observable(function (subscriber) { return sherlock._internal.Reactor.create(derivable, function (value) { return subscriber.next(value); }, options, function () { return subscriber.closed || subscriber.complete(); }); }); } function fromObservable(observable) { var atom$ = sherlock.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 sherlock.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$; } exports.fromObservable = fromObservable; exports.toObservable = toObservable; //# sourceMappingURL=sherlock-rxjs.cjs.js.map