Replace Subscription with Subscribe, add NetworkContext
This commit is contained in:
@@ -2,6 +2,7 @@ import type {Event, Filter} from 'nostr-tools'
|
||||
import type {Emitter} from '@coracle.social/lib'
|
||||
import type {Connection} from './Connection'
|
||||
import type {Message} from './Socket'
|
||||
import {NetworkContext} from './Context'
|
||||
|
||||
export type Target = Emitter & {
|
||||
connections: Connection[]
|
||||
@@ -11,25 +12,37 @@ export type Target = Emitter & {
|
||||
|
||||
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 SubscribeOpts = {onEvent?: EventCallback, onEose?: EoseCallback}
|
||||
type PublishOpts = {verb?: string, onOk?: OkCallback, onError?: ErrorCallback}
|
||||
type AuthOpts = {onAuth: AuthCallback, onOk: OkCallback}
|
||||
|
||||
const createSubId = (prefix: string) => [prefix, Math.random().toString().slice(2, 10)].join('-')
|
||||
|
||||
export class Executor {
|
||||
|
||||
constructor(readonly target: Target) {}
|
||||
constructor(readonly target: Target) {
|
||||
target.on('AUTH', NetworkContext.onAuth)
|
||||
target.on('OK', NetworkContext.onOk)
|
||||
}
|
||||
|
||||
subscribe(filters: Filter[], {onEvent, onEose}: SubscribeOpts = {}) {
|
||||
let closed = false
|
||||
|
||||
const id = createSubId('REQ')
|
||||
const eventListener = (url: string, subid: string, e: Event) => subid === id && onEvent?.(url, e)
|
||||
const eoseListener = (url: string, subid: string) => subid === id && onEose?.(url)
|
||||
|
||||
const eventListener = (url: string, subid: string, e: Event) => {
|
||||
if (subid === id) {
|
||||
NetworkContext.onEvent(url, e)
|
||||
onEvent?.(url, e)
|
||||
}
|
||||
}
|
||||
|
||||
const eoseListener = (url: string, subid: string) => {
|
||||
if (subid === id) {
|
||||
onEose?.(url)
|
||||
}
|
||||
}
|
||||
|
||||
this.target.on('EVENT', eventListener)
|
||||
this.target.on('EOSE', eoseListener)
|
||||
@@ -49,8 +62,18 @@ 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)
|
||||
const okListener = (url: string, id: string, ...payload: any[]) => {
|
||||
if (id === event.id) {
|
||||
NetworkContext.onEvent(url, event)
|
||||
onOk?.(url, id, ...payload)
|
||||
}
|
||||
}
|
||||
|
||||
const errorListener = (url: string, id: string, ...payload: any[]) => {
|
||||
if (id === event.id) {
|
||||
onError?.(url, id, ...payload)
|
||||
}
|
||||
}
|
||||
|
||||
this.target.on('OK', okListener)
|
||||
this.target.on('ERROR', errorListener)
|
||||
@@ -63,17 +86,5 @@ export class Executor {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleAuth({onAuth, onOk}: AuthOpts) {
|
||||
this.target.on('AUTH', onAuth)
|
||||
this.target.on('OK', onOk)
|
||||
|
||||
return {
|
||||
unsubscribe: () => {
|
||||
this.target.off('AUTH', onAuth)
|
||||
this.target.off('OK', onOk)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user