UNPKG

@politie/sherlock-rxjs

Version:

An extension to Sherlock that provides interop with RxJS.

46 lines (41 loc) 2.31 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@politie/sherlock'), require('rxjs')) : typeof define === 'function' && define.amd ? define(['exports', '@politie/sherlock', 'rxjs'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SherlockRxjs = {}, global.Sherlock, global.Rx)); })(this, (function (exports, sherlock, rxjs) { 'use strict'; /** * 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; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=sherlock-rxjs.umd.js.map