import { of , Observable } from 'rxjs' ; import { filter , map , reduce } from 'rxjs/operators' ; import { tap } from 'rxjs/operators' ; import { timerData$ } from '../core/helpers' ; /**
 * Observer logs to the console with optional name prefix
 * When observable nexts, errors, or completes
 */ export const loggingObserver = ( name ? ) => ( { next ( value ) { name ? console . log ( name , value ) : console . log ( value ) ; } , error ( err ) { console . error ( ( name ? `${ name }: ` : '' ) + err ) ; } , complete ( ) { console . log ( ( name ? `${ name }: ` : '' ) + 'Completed' ) ; } } ) ; /** Custom operator that taps into observable and logs it with the loggingObserver */ export const logOp = ( name ? ) => ( o ) => o . pipe ( tap ( loggingObserver ( name ) ) ) ; export function play ( ... args ) { const observable$ = timerData$ . pipe ( logOp ( 'Custom logger' ) ) ; return observable$ ; } /** Observer logs to the console when observable nexts, errors, or completes */ export const logObserver = ( ) => ( { next ( value ) { console . log ( value ) ; } , error ( err ) { console . error ( err ) ; } , complete ( ) { console . log ( 'Completed' ) ; } } ) ; /** Operator fn: takes an observable; returns an observable */ export const log = ( o ) => o . pipe ( tap ( logObserver ( ) ) ) ; const numbers$ = of ( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ; const result$ = numbers$ . pipe ( filter ( i => i % 2 === 1 ) , log , map ( i => i * 2 ) , reduce ( ( acc , curr ) => acc + curr , 0 ) ) ;