import { TimeService } from './time.service' ; import { Component , OnDestroy , OnInit } from '@angular/core' ; import { SubSink } from 'subsink' ; let counter = 0 ; @ Component ( { selector : 'aw-sub-sink' , template : ` <p>SubSink time: {{ time }}</p>` } ) export class SubSinkComponent implements OnInit , OnDestroy { prefix = '' ; time : string ; private subs = new SubSink ( ) ; ngOnInit ( ) { counter += 1 ; const time$ = ( name ) => this . timeService . time$ ( `SubSink #${ counter } ${ name }` ) ; const subscriber = ( ( ) => { const that = this ; return { next ( time ) { that . time = `${ time }` ; } , error ( err ) { console . error ( err ) ; } , complete ( ) { console . log ( 'SubSinkComponent completed' ) ; } } ; } ) ( ) ; this . subs . sink = time$ ( 'A' ) . subscribe ( subscriber ) ; this . subs . sink = time$ ( 'B' ) . subscribe ( subscriber ) ; this . subs . sink = time$ ( 'C' ) . subscribe ( subscriber ) ; this . subs . add ( time$ ( 'D' ) . subscribe ( subscriber ) , time$ ( 'E' ) . subscribe ( subscriber ) ) ; } ngOnDestroy ( ) { this . subs . unsubscribe ( ) ; console . log ( `SubSinkComponent #${ counter } destroyed` ) ; } constructor ( private timeService ) { } }