import { EventDetails , EventListenerOptions , EventListenerRegister , ScryptedInterface , SystemDeviceState } from "@scrypted/sdk/types" ; export class EventListenerRegisterImpl implements EventListenerRegister { removeListener : ( ) => void ; constructor ( removeListener ) { this . removeListener = removeListener ; } } const allowedEventInterfaces = new Set < string > ( [ ScryptedInterface . ScryptedDevice , 'Logger' , 'Storage' ] ) ; export class EventRegistry { systemListeners = new Set < ( id : string , eventDetails : EventDetails , eventData : any ) => void > ( ) ; listeners : { [ token ] : Set < ( eventDetails : EventDetails , eventData : any ) => void > ; } = { } ; listen ( callback ) { const events = this . systemListeners ; let cb = callback ; events . add ( cb ) ; return new EventListenerRegisterImpl ( ( ) => { events . delete ( cb ) ; cb = undefined ; } ) ; } listenDevice ( id , options , callback ) { let { event } = ( options || { } ) as EventListenerOptions ; if ( ! event && typeof options === 'string' ) event = options as string ; if ( ! event ) event = undefined ; const token = `${ id }#${ event }` ; let events = this . listeners [ token ] ; if ( ! events ) { events = new Set ( ) ; this . listeners [ token ] = events ; } events . add ( callback ) ; return new EventListenerRegisterImpl ( ( ) => { events . delete ( callback ) ; callback = undefined ; } ) ; } notify ( id , eventTime , eventInterface , property , value , changed ? ) { if ( property && ! changed ) return false ; const eventDetails = { changed , eventInterface , eventTime , property , } ; if ( property || allowedEventInterfaces . has ( eventInterface ) ) { for ( const event of this . systemListeners ) { event ( id , eventDetails , value ) ; } } const events = this . listeners [ `${ id }#${ eventInterface }` ] ; if ( events ) { for ( const event of events ) { event ( eventDetails , value ) ; } } const allEvents = this . listeners [ `${ id }#${ undefined }` ] ; if ( allEvents ) { for ( const event of allEvents ) { event ( eventDetails , value ) ; } } return true ; } }