2023-10-24 10:57:17 -04:00
|
|
|
import {
|
|
|
|
ScopeEventEmitter,
|
|
|
|
ScopeEventName,
|
|
|
|
} from '../../../../../types/ide/scope-event-emitter'
|
|
|
|
|
|
|
|
export class ReactScopeEventEmitter implements ScopeEventEmitter {
|
|
|
|
// eslint-disable-next-line no-useless-constructor
|
2024-01-05 05:05:07 -05:00
|
|
|
constructor(private readonly eventEmitter: EventTarget) {}
|
2023-10-24 10:57:17 -04:00
|
|
|
|
|
|
|
emit(eventName: ScopeEventName, broadcast: boolean, ...detail: unknown[]) {
|
2024-01-05 05:05:07 -05:00
|
|
|
this.eventEmitter.dispatchEvent(new CustomEvent(eventName, { detail }))
|
2023-10-24 10:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
on(eventName: ScopeEventName, listener: (...args: unknown[]) => void) {
|
|
|
|
// A listener attached via useScopeEventListener expects an event as the
|
|
|
|
// first parameter. We don't have one, so just provide an empty object
|
2024-01-09 04:14:09 -05:00
|
|
|
const wrappedListener = (event: CustomEvent<unknown[]>) => {
|
|
|
|
listener({}, ...event.detail)
|
2023-10-24 10:57:17 -04:00
|
|
|
}
|
2024-01-09 04:14:09 -05:00
|
|
|
this.eventEmitter.addEventListener(
|
|
|
|
eventName,
|
|
|
|
wrappedListener as EventListener
|
|
|
|
)
|
2023-10-24 10:57:17 -04:00
|
|
|
return () => {
|
2024-01-09 04:14:09 -05:00
|
|
|
this.eventEmitter.removeEventListener(
|
|
|
|
eventName,
|
|
|
|
wrappedListener as EventListener
|
|
|
|
)
|
2023-10-24 10:57:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|