Re-organize connection management

This commit is contained in:
Jonathan Staab
2023-08-14 16:31:17 -07:00
parent 3caa4c31d1
commit ee1ac84ab7
18 changed files with 2470 additions and 2050 deletions
+30 -14
View File
@@ -1,18 +1,34 @@
import {EventEmitter} from 'events'
import type {Event, Filter} from './types'
import type {Message} from './util/Socket'
const createSubId = prefix => [prefix, Math.random().toString().slice(2, 10)].join('-')
type Target = EventEmitter & {
send: (...args: Message) => void
}
type EventCallback = (url: string, event: Event) => void
type EoseCallback = (url: string) => void
type AuthCallback = (url: string, challenge: string) => void
type OkCallback = (url: string, id: string, ...extra: any[]) => void
type ErrorCallback = (url: string, id: string, ...extra: any[]) => void
type CountCallback = (url: string, ...extra: any[]) => void
type SubscribeOpts = {onEvent: EventCallback, onEose: EoseCallback}
type PublishOpts = {verb: string, onOk: OkCallback, onError: ErrorCallback}
type CountOpts = {onCount: CountCallback}
type AuthOpts = {onAuth: AuthCallback, onOk: OkCallback}
const createSubId = (prefix: string) => [prefix, Math.random().toString().slice(2, 10)].join('-')
export class Executor {
target: EventEmitter
constructor(target) {
this.target = target
}
subscribe(filters, {onEvent, onEose}) {
constructor(readonly target: Target) {}
subscribe(filters: Filter[], {onEvent, onEose}: SubscribeOpts) {
let closed = false
const id = createSubId('REQ')
const eventListener = (url, subid, e) => subid === id && onEvent?.(url, e)
const eoseListener = (url, subid) => subid === id && onEose?.(url)
const eventListener = (url: string, subid: string, e: Event) => subid === id && onEvent?.(url, e)
const eoseListener = (url: string, subid: string) => subid === id && onEose?.(url)
this.target.on('EVENT', eventListener)
this.target.on('EOSE', eoseListener)
@@ -30,9 +46,9 @@ export class Executor {
},
}
}
publish(event, {verb = 'EVENT', onOk, onError} = {}) {
const okListener = (url, id, ...payload) => id === event.id && onOk(url, id, ...payload)
const errorListener = (url, id, ...payload) => id === event.id && onError(url, id, ...payload)
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)
this.target.on('OK', okListener)
this.target.on('ERROR', errorListener)
@@ -45,9 +61,9 @@ export class Executor {
}
}
}
count(filters, {onCount}) {
count(filters: Filter[], {onCount}: CountOpts) {
const id = createSubId('COUNT')
const countListener = (url, subid, ...payload) => {
const countListener = (url: string, subid: string, ...payload: any[]) => {
if (subid === id) {
onCount(url, ...payload)
this.target.off('COUNT', countListener)
@@ -61,7 +77,7 @@ export class Executor {
unsubscribe: () => this.target.off('COUNT', countListener)
}
}
handleAuth({onAuth, onOk}) {
handleAuth({onAuth, onOk}: AuthOpts) {
this.target.on('AUTH', onAuth)
this.target.on('OK', onOk)