Add custom emitter and wrapper target

This commit is contained in:
Jonathan Staab
2023-09-14 13:07:01 -07:00
parent 3b9b6f9dc0
commit 47ddbf6498
10 changed files with 57 additions and 13 deletions
+8 -2
View File
@@ -1,9 +1,12 @@
import {EventEmitter} from 'events'
import type {Event, Filter} from './types'
import type {Connection} from './Connection'
import type {Emitter} from './util/Emitter'
import type {Message} from './util/Socket'
type Target = EventEmitter & {
export type Target = Emitter & {
connections: Connection[]
send: (...args: Message) => void
cleanup: () => void
}
type EventCallback = (url: string, event: Event) => void
@@ -46,6 +49,7 @@ export class Executor {
},
}
}
publish(event: Event, {verb = 'EVENT', onOk, onError}: PublishOpts) {
const okListener = (url: string, id: string, ...payload: any[]) => id === event.id && onOk(url, id, ...payload)
const errorListener = (url: string, id: string, ...payload: any[]) => id === event.id && onError(url, id, ...payload)
@@ -61,6 +65,7 @@ export class Executor {
}
}
}
count(filters: Filter[], {onCount}: CountOpts) {
const id = createSubId('COUNT')
const countListener = (url: string, subid: string, ...payload: any[]) => {
@@ -77,6 +82,7 @@ export class Executor {
unsubscribe: () => this.target.off('COUNT', countListener)
}
}
handleAuth({onAuth, onOk}: AuthOpts) {
this.target.on('AUTH', onAuth)
this.target.on('OK', onOk)