author Santo Pfingsten
 *  see https import { settings } from "../lib/settings" ; import { messageUtil } from "../lib/messageUtil" ; import { someItemsMatch } from "./backgroundShared" ; const DOMAIN_LEAVE_SETTINGS_KEYS = [ "domainLeave.enabled" , "domainLeave.delay" ] ; export class CleanupScheduler { private delayTime : number = 0 ; private enabled : boolean = false ; private readonly handler : ( domain : string ) => void ; private domainTimeouts : { [ s ] : ReturnType < typeof setTimeout > ; } = { } ; private snoozing : boolean ; private readonly snoozedDomains : { [ s ] : boolean ; } = { } ; public constructor ( handler , snoozing ) { this . snoozing = snoozing ; this . handler = handler ; this . updateSettings ( ) ; messageUtil . receive ( "settingsChanged" , ( changedKeys ) => { if ( someItemsMatch ( changedKeys , DOMAIN_LEAVE_SETTINGS_KEYS ) ) this . updateSettings ( ) ; } ) ; } private updateSettings ( ) { const enabled = settings . get ( "domainLeave.enabled" ) ; if ( enabled !== this . enabled ) { this . enabled = enabled ; if ( ! enabled ) { this . clearAllTimeouts ( ) ; for ( const domain in this . snoozedDomains ) delete this . snoozedDomains [ domain ] ; } } this . delayTime = settings . get ( "domainLeave.delay" ) * 1000 ; } private clearAllTimeouts ( ) { for ( const domain in this . domainTimeouts ) { clearTimeout ( this . domainTimeouts [ domain ] ) ; delete this . domainTimeouts [ domain ] ; } } public schedule ( domain ) { if ( ! this . enabled ) return ; if ( this . domainTimeouts [ domain ] ) { clearTimeout ( this . domainTimeouts [ domain ] ) ; delete this . domainTimeouts [ domain ] ; } if ( this . snoozing ) this . snoozedDomains [ domain ] = true ; else if ( this . delayTime <= 0 ) this . handler ( domain ) ; else { this . domainTimeouts [ domain ] = setTimeout ( ( ) => { if ( this . enabled ) { if ( this . snoozing ) this . snoozedDomains [ domain ] = true ; else this . handler ( domain ) ; } delete this . domainTimeouts [ domain ] ; } , this . delayTime ) ; } } public setSnoozing ( snoozing ) { this . snoozing = snoozing ; if ( snoozing ) { for ( const domain in this . domainTimeouts ) { this . snoozedDomains [ domain ] = true ; clearTimeout ( this . domainTimeouts [ domain ] ) ; delete this . domainTimeouts [ domain ] ; } } else { for ( const domain in this . snoozedDomains ) { this . schedule ( domain ) ; delete this . snoozedDomains [ domain ] ; } } } public getSnoozedDomainsToClean ( ) { return Object . getOwnPropertyNames ( this . snoozedDomains ) ; } public getScheduledDomainsToClean ( ) { return Object . getOwnPropertyNames ( this . domainTimeouts ) ; } }