import { RNFirebase } from '@ulangi/react-native-firebase' ; import { EventChannel , eventChannel } from 'redux-saga' ; import { FirebaseAdapter } from '../adapters/FirebaseAdapter' ; export class FirebaseEventChannel { private firebase : FirebaseAdapter ; public constructor ( firebase ) { this . firebase = firebase ; } public createChannelByPaths ( paths ) { return eventChannel ( ( emitter ) : ( ( ) => void ) => { const pathListenerPairs = paths . map ( ( path ) : [ string , ( snapshot : RNFirebase . database . DataSnapshot ) => void ] => { return [ path , ( snapshot ) : void => emitter ( { [ path ] : snapshot } ) ] ; } ) ; pathListenerPairs . forEach ( ( [ path , listener ] ) : void => { this . firebase . addListener ( path , listener , ( error ) : void => { console . warn ( error ) ; } ) ; } ) ; return ( ) : void => this . firebase . removeListeners ( pathListenerPairs ) ; } ) ; } }