UNPKG

@aimee-blue/ab-service-kit

Version:
96 lines (84 loc) 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeOnNotifications = executeOnNotifications; var _rxjs = require("rxjs"); var _publishStream = require("./publishStream"); var _registerError = require("./registerError"); var _logging = require("./logging"); function executeOnNotifications(notifications, cb, logger = (0, _logging.defaultBasicLogger)()) { return stream => { if (notifications.length === 0) { return stream; } return new _rxjs.Observable(subscriber => { const observables = notifications.filter(_rxjs.isObservable); if (notifications.includes('subscribe')) { cb({ notification: 'subscribe' }); } const shared = (0, _publishStream.publishStream)(stream); let lastValue; subscriber.add(shared.subscribe({ next: value => { lastValue = value; }, error: _err => { // we can simply ignore the error because it // goes to the subscriber below anyway, however, // we cannot remove this handler otherwise the // error bubbles up to the process.onUnhandled return; }, ...(notifications.includes('next') && { next: value => { lastValue = value; cb({ notification: 'next', value }); } }), ...(notifications.includes('complete') && { complete: () => cb({ notification: 'complete', lastValue }) }), ...(notifications.includes('error') && { error: error => cb({ notification: 'error', error }) }) })); if (observables.length > 0) { subscriber.add((0, _rxjs.merge)(...observables).subscribe({ next: notification => { cb({ notification, lastValue }); }, error: err => { (0, _registerError.registerError)(err); logger.log('💥 Logging notifications generated an error', err); } })); } subscriber.add(shared.subscribe(subscriber)); subscriber.add(shared.connect()); if (notifications.includes('unsubscribe')) { subscriber.add(() => { cb({ notification: 'unsubscribe', lastValue }); }); } }); }; } //# sourceMappingURL=notifications.js.map