import { Subscriber , Subscription } from 'most' ; import { Subject } from 'most-subject' ; import { get } from '../helpers' ; export function replicateSinks < Sinks > ( sinks , sinkProxies ) { return Object . keys ( sinks ) . filter ( ( sinkName ) => get ( sinkProxies , sinkName ) ) . map ( function createSubscription ( sinkName ) : Subscription < any > { const sink = get ( sinks , sinkName ) ; const sinkProxy = get ( sinkProxies , sinkName ) ; return sink . subscribe ( createSubscriber ( sinkProxy , sinkName ) ) ; } ) ; } function createSubscriber ( subject , sinkName ) { return { next ( value ) { subject . next ( value ) ; } , error ( err ) { subject . error ( err ) ; logError ( err , sinkName ) ; } , complete ( value ? ) { subject . complete ( value ) ; } } ; } function logError ( err , driverName ) { console . error ( `${ driverName } has failed for the following reason: ` + `${ err . message }` + `${ err . stack || err }` ) ; }