import * as Toolkit from 'chipmunk.client.toolkit' ; import { Subject , Observable } from 'rxjs' ; export interface ISize { width : number ; height : number ; } export class ViewsEventsService { private _logger : Toolkit . Logger = new Toolkit . Logger ( 'ViewsEventsService' ) ; private _subjects : { onResize : Subject < ISize > ; onRowHover : Subject < number > ; } = { onResize : new Subject < ISize > ( ) , onRowHover : new Subject < number > ( ) , } ; constructor ( ) { this . _onWindowResize = this . _onWindowResize . bind ( this ) ; window . addEventListener ( 'resize' , this . _onWindowResize ) ; } destroy ( ) { window . removeEventListener ( 'resize' , this . _onWindowResize ) ; } public getObservable ( ) { return { onResize : this . _subjects . onResize . asObservable ( ) , onRowHover : this . _subjects . onRowHover . asObservable ( ) , } ; } public once ( ) { return { onResize : ( handler ) => { const subscription = this . _subjects . onResize . asObservable ( ) . subscribe ( ( size ) => { subscription . unsubscribe ( ) ; handler ( size ) ; } ) ; } , onRowHover : ( handler ) => { const subscription = this . _subjects . onRowHover . asObservable ( ) . subscribe ( ( row ) => { subscription . unsubscribe ( ) ; handler ( row ) ; } ) ; } , } ; } public fire ( ) { return { onResize : ( ) => { this . _onWindowResize ( ) ; } , onRowHover : ( position ) => { this . _subjects . onRowHover . next ( position ) ; } , } ; } private _onWindowResize ( ) { this . _subjects . onResize . next ( { height : window . innerHeight , width : window . innerWidth , } ) ; } } export default new ViewsEventsService ( ) ;