83d8035dc2
* root + server * frontend * frontend-2 * dui3 * dui3 * tailwind theme * ui-components * preview service * viewer * viewer-sandbox * fileimport-service * webhook service * objectloader * shared * ui-components-nuxt * WIP full config * WIP full linter * eslint projectwide util * minor fix * removing redundant ci * clean up test errors * fixed prettier formatting * CI improvements * TSC lint fix * 'buildBatch' needs to be async since some batch types (like Text) require it. Removed a disabled liniting rule from ObjLoader * removed unnecessary void --------- Co-authored-by: AlexandruPopovici <alexandrupopoviciioan@gmail.com>
26 lines
923 B
TypeScript
26 lines
923 B
TypeScript
import type { Emitter } from 'nanoevents'
|
|
import { createNanoEvents } from 'nanoevents'
|
|
|
|
/**
|
|
* A simple (typed) event emitter base class that host applications can use to send messages (and data) to the web ui,
|
|
* e.g. via `browser.executeScriptAsync("myBindings.on('eventName', serializedData)")`.
|
|
*/
|
|
export class BaseBridge {
|
|
public emitter: Emitter
|
|
|
|
constructor() {
|
|
this.emitter = createNanoEvents()
|
|
}
|
|
|
|
// NOTE: these do not need to be typed extra in here, as they will be properly typed on the specific binding's interface.
|
|
on(event: string | number, callback: (...args: unknown[]) => void) {
|
|
return this.emitter.on(event, callback)
|
|
}
|
|
|
|
// NOTE: this could be private - as it should be only used by the host application.
|
|
emit(eventName: string, payload: string) {
|
|
const parsedPayload = payload ? (JSON.parse(payload) as unknown) : null
|
|
this.emitter.emit(eventName, parsedPayload)
|
|
}
|
|
}
|