packageDocumentation module import { UiEvent } from "./UiEvent" ; public export interface UiSyncEventArgs { eventIds : Set < string > ; } public export class UiSyncEvent extends UiEvent < UiSyncEventArgs > { } public export class UiEventDispatcher { private _syncEventTimerId : number | undefined ; private _eventIds : Set < string > ; private _eventIdAdded ; private _uiSyncEvent : UiSyncEvent ; private _timeoutPeriod ; private _secondaryTimeoutPeriod ; constructor ( ) { this . _eventIds = new Set < string > ( ) ; this . _eventIdAdded = false ; this . _uiSyncEvent = new UiSyncEvent ( ) ; this . _timeoutPeriod = 100 ; this . _secondaryTimeoutPeriod = this . _timeoutPeriod / 2 ; } internal public setTimeoutPeriod ( period ) { this . _timeoutPeriod = period ; this . _secondaryTimeoutPeriod = Math . floor ( this . _timeoutPeriod / 2 ) ; if ( this . _secondaryTimeoutPeriod < 1 ) this . _secondaryTimeoutPeriod = 1 ; if ( this . _syncEventTimerId ) { window . clearTimeout ( this . _syncEventTimerId ) ; this . _syncEventTimerId = undefined ; } if ( this . _eventIds ) this . _eventIds . clear ( ) ; this . _eventIdAdded = false ; } /** The current timeout period */ public get timeoutPeriod ( ) : number { return this . _timeoutPeriod ; } /** Return set of event ids that will be sent to listeners/. */ public get syncEventIds ( ) : Set < string > { return this . _eventIds ; } /** Return UiSyncEvent so callers can register an event callback. */ public get onSyncUiEvent ( ) : UiSyncEvent { return this . _uiSyncEvent ; } /** Immediately trigger sync event processing. */ public dispatchImmediateSyncUiEvent ( eventId ) { const eventIds = new Set < string > ( ) ; eventIds . add ( eventId . toLowerCase ( ) ) ; this . onSyncUiEvent . emit ( { eventIds } ) ; } /** Save eventId in Set for processing. */ public dispatchSyncUiEvent ( eventId ) { if ( 0 === this . _timeoutPeriod ) { return ; } this . syncEventIds . add ( eventId . toLowerCase ( ) ) ; if ( ! this . _syncEventTimerId ) { this . _syncEventTimerId = window . setTimeout ( ( ) => { this . checkForAdditionalIds ( ) ; } , this . _timeoutPeriod ) ; } else { this . _eventIdAdded = true ; } } /** Save multiple eventIds in Set for processing. */ public dispatchSyncUiEvents ( eventIds ) { if ( 0 === this . _timeoutPeriod ) { return ; } eventIds . forEach ( ( id ) => this . syncEventIds . add ( id . toLowerCase ( ) ) ) ; if ( ! this . _syncEventTimerId ) { this . _syncEventTimerId = window . setTimeout ( ( ) => { this . checkForAdditionalIds ( ) ; } , this . _timeoutPeriod ) ; } else { this . _eventIdAdded = true ; } } /** Trigger registered event processing when timer has expired and no addition eventId are added. */ public checkForAdditionalIds ( ) { if ( ! this . _eventIdAdded ) { if ( this . _syncEventTimerId ) { window . clearTimeout ( this . _syncEventTimerId ) ; this . _syncEventTimerId = undefined ; } this . _eventIdAdded = false ; if ( this . syncEventIds . size > 0 ) { const eventIds = new Set < string > ( ) ; this . syncEventIds . forEach ( ( value ) => eventIds . add ( value ) ) ; this . _eventIds . clear ( ) ; this . onSyncUiEvent . emit ( { eventIds } ) ; } return ; } if ( this . _syncEventTimerId ) { window . clearTimeout ( this . _syncEventTimerId ) ; this . _syncEventTimerId = undefined ; } this . _eventIdAdded = false ; this . _syncEventTimerId = window . setTimeout ( ( ) => { this . checkForAdditionalIds ( ) ; } , this . _secondaryTimeoutPeriod ) ; } /** Checks to see if an eventId of interest is contained in the set of eventIds */ public hasEventOfInterest ( eventIds , idsOfInterest ) { if ( ( idsOfInterest . length > 0 ) && idsOfInterest . some ( ( value ) : boolean => eventIds . has ( value . toLowerCase ( ) ) ) ) return true ; return false ; } }