use EventEmitter

This commit is contained in:
ennmichael
2023-03-01 00:10:53 +01:00
parent 6ef866feb3
commit a32d806532
5 changed files with 131 additions and 51 deletions

View File

@ -3,11 +3,15 @@ import { EventId, Event, EventKind, SignedEvent, RawEvent } from "../event"
import { PrivateKey, PublicKey } from "../keypair"
import { Conn, IncomingKind, OutgoingKind } from "./conn"
import * as secp from "@noble/secp256k1"
import { EventEmitter } from "./emitter"
/**
* A nostr client.
*
* TODO Document the events here
* TODO Move all this to that file and add the newListener and removeListener events there as well
*/
export class Nostr {
export class Nostr extends EventEmitter {
// TODO NIP-44 AUTH, leave this for later
/**
* Open connections to relays.
@ -19,39 +23,6 @@ export class Nostr {
*/
readonly #subscriptions: Map<string, Filters[]> = new Map()
#eventCallback?: EventCallback
#noticeCallback?: NoticeCallback
#errorCallback?: ErrorCallback
/**
* Add a new callback for received events.
*/
on(on: "event", cb: EventCallback | undefined | null): void
/**
* Add a new callback for received notices.
*/
on(on: "notice", cb: NoticeCallback | undefined | null): void
/**
* Add a new callback for errors.
*/
on(on: "error", cb: ErrorCallback | undefined | null): void
// TODO Also add on: ("subscribed", subscriptionId) which checks "OK"/"NOTICE" and makes a callback?
// TODO Also add on: ("sent", eventId) which checks "OK"/"NOTICE" and makes a callback?
on(
on: "event" | "notice" | "error",
cb: EventCallback | NoticeCallback | ErrorCallback | undefined | null
) {
if (on === "event") {
this.#eventCallback = (cb as EventCallback) ?? undefined
} else if (on === "notice") {
this.#noticeCallback = (cb as NoticeCallback) ?? undefined
} else if (on === "error") {
this.#errorCallback = (cb as ErrorCallback) ?? undefined
} else {
throw new Error(`unexpected input: ${on}`)
}
}
/**
* Open a connection and start communicating with a relay. This method recreates all existing
* subscriptions on the new relay as well. If there is already an existing connection,
@ -83,7 +54,8 @@ export class Nostr {
conn.on("message", async (msg) => {
try {
if (msg.kind === IncomingKind.Event) {
this.#eventCallback?.(
this.emit(
"event",
{
signed: msg.signed,
subscriptionId: msg.subscriptionId,
@ -92,23 +64,23 @@ export class Nostr {
this
)
} else if (msg.kind === IncomingKind.Notice) {
this.#noticeCallback?.(msg.notice, this)
this.emit("notice", msg.notice, this)
} else {
const err = new ProtocolError(`invalid message ${msg}`)
try {
this.#errorCallback?.(err, this)
this.emit("error", err, this)
} catch (err) {
// Don't propagate errors from the error callback.
}
}
} catch (err) {
this.#errorCallback?.(err, this)
this.emit("error", err, this)
}
})
// Forward connection errors to the error callbacks.
conn.on("error", (err) => {
this.#errorCallback?.(err, this)
this.emit("error", err, this)
})
// Resend existing subscriptions to this connection.
@ -286,6 +258,7 @@ export class SubscriptionId {
}
}
// TODO Rethink this type. Maybe it's not very idiomatic.
/**
* A prefix filter. These filters match events which have the appropriate prefix.
* This also means that exact matches pass the filters. No special syntax is required.
@ -322,13 +295,3 @@ export interface Filters {
until?: Date
limit?: number
}
export type EventCallback = (params: EventParams, nostr: Nostr) => unknown
export type NoticeCallback = (notice: string, nostr: Nostr) => unknown
export type ErrorCallback = (error: unknown, nostr: Nostr) => unknown
export interface EventParams {
signed: SignedEvent
subscriptionId: SubscriptionId
raw: RawEvent
}