import { NULL_FUNCTION } from "polar-shared/src/util/Functions" ; import { SnapshotUnsubscriber } from "polar-shared/src/util/Snapshots" ; export class BackgroundListeners { public static create < V > ( listenable ) { let started = false ; let value ; let snapshotUnsubscriber = NULL_FUNCTION ; return { start : async ( ) => { if ( started ) { return ; } value = await listenable . get ( ) ; snapshotUnsubscriber = await listenable . onSnapshot ( currentValue => { value = currentValue ; } ) ; started = true ; } , get : ( ) => { if ( ! started ) { throw new Error ( "Not started" ) ; } return value ! ; } , stop : ( ) => { if ( ! started ) { return ; } snapshotUnsubscriber ( ) ; } } ; } } export interface BackgroundListener < V > { start ( ) : Promise < void > ; get ( ) : V ; stop ( ) : void ; } export interface BackgroundListenable < V > { get ( ) : Promise < V > ; onSnapshot ( handler ) : Promise < SnapshotUnsubscriber > ; }