overleaf/services/web/frontend/js/features/ide-react/scope-event-emitter/react-scope-event-emitter.ts
Tim Down 537673cdf6 Merge pull request #15415 from overleaf/td-scope-store-and-emitter-fixed
IDE scope store and emitter with fixed PDF URLs

GitOrigin-RevId: 9d33bad8a006bb55714878332f78932538dd8921
2023-10-25 08:05:07 +00:00

26 lines
910 B
TypeScript

import {
ScopeEventEmitter,
ScopeEventName,
} from '../../../../../types/ide/scope-event-emitter'
import EventEmitter from 'events'
export class ReactScopeEventEmitter implements ScopeEventEmitter {
// eslint-disable-next-line no-useless-constructor
constructor(private readonly eventEmitter: EventEmitter) {}
emit(eventName: ScopeEventName, broadcast: boolean, ...detail: unknown[]) {
this.eventEmitter.emit(eventName, ...detail)
}
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
const wrappedListener = (...detail: unknown[]) => {
listener({}, ...detail)
}
this.eventEmitter.on(eventName, wrappedListener)
return () => {
this.eventEmitter.off(eventName, wrappedListener)
}
}
}