Skip to content

Commit

Permalink
feat(core/nodes): add on method
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Feb 27, 2024
1 parent 784fa1b commit ec5cd2b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
7 changes: 1 addition & 6 deletions core/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
NostrNode,
NostrNodeBase,
NostrNodeConfig,
NostrNodeEvent,
NostrNodeModule,
} from "./nodes.ts";

Expand Down Expand Up @@ -47,7 +46,7 @@ export class Client extends NostrNodeBase<
this.ws.addEventListener("message", (ev: MessageEvent<string>) => {
const message = JSON.parse(ev.data) as ClientToRelayMessage;
// TODO: Validate the message.
this.dispatchEvent(new ClientEvent("message", message));
this.dispatch("message", message);
});
}
}
Expand All @@ -62,10 +61,6 @@ export interface ClientEventTypeRecord {

export type ClientEventType = keyof ClientEventTypeRecord;

export class ClientEvent<
T extends ClientEventType = ClientEventType,
> extends NostrNodeEvent<ClientEventTypeRecord, T> {}

// ------------------------------
// Modules
// ------------------------------
Expand Down
22 changes: 20 additions & 2 deletions core/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ export interface NostrNode<
* and `data`.
*/
dispatch<T extends EventType<R>>(type: T, data: R[T]): void;

/**
* A convenience method to add an event listener for the given `type` that
* calls the given `listener` when the event is dispatched.
*/
on<T extends EventType<R>>(
type: T,
// deno-lint-ignore no-explicit-any
listener: (data: R[T]) => any,
): void;
}

/**
Expand Down Expand Up @@ -108,8 +118,16 @@ export class NostrNodeBase<
dispatch<T extends EventType<R>>(
type: T,
data: R[T],
): void {
this.dispatchEvent(new NostrNodeEvent(type, data));
) {
this.dispatchEvent(new NostrNodeEvent<R, T>(type, data));
}

on<T extends EventType<R>>(
type: T,
// deno-lint-ignore no-explicit-any
listener: (data: R[T]) => any,
) {
this.addEventListener(type, ({ data }) => listener(data));
}
}

Expand Down
2 changes: 1 addition & 1 deletion nips/01/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare module "../../core/clients.ts" {
}

export const install: ClientModule["install"] = (client) => {
client.addEventListener("message", ({ data: message }) => {
client.on("message", (message) => {
switch (message[0]) {
case "EVENT": {
const event = message[1];
Expand Down
14 changes: 7 additions & 7 deletions nips/01/clients_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ describe("NIP-01/Client", () => {

it("should receive an event and send a OK message", async () => {
const event = { id: "test-ok", kind: 0 };
const received = new Promise<ClientToRelayMessage<"EVENT">>((resolve) => {
client.addEventListener("message", ({ data: message }) => {
if (message[0] === "EVENT") resolve(message);
});
});
const received = new Promise<ClientToRelayMessage<"EVENT">>((resolve) =>
client.on("message", (msg) => {
if (msg[0] === "EVENT") resolve(msg);
})
);
const replied = new Promise<RelayToClientMessage<"OK">>((resolve) => {
ws.remote.addEventListener("message", (ev: MessageEvent<string>) => {
resolve(JSON.parse(ev.data));
Expand All @@ -50,8 +50,8 @@ describe("NIP-01/Client", () => {
subid = "test-req" as SubscriptionId;
const request: ClientToRelayMessage<"REQ"> = ["REQ", subid, { kinds: [1] }];
const received = new Promise<ClientToRelayMessage<"REQ">>((resolve) => {
client.addEventListener("message", ({ data: message }) => {
if (message[0] === "REQ" && message[1] === subid) resolve(message);
client.on("message", (msg) => {
if (msg[0] === "REQ" && msg[1] === subid) resolve(msg);
});
});
ws.remote.send(JSON.stringify(request));
Expand Down
20 changes: 11 additions & 9 deletions nips/01/relays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EventRejected, RelayModule } from "../../core/relays.ts";
export class SubscriptionClosed extends Error {}

export const install: RelayModule["install"] = (relay) => {
relay.addEventListener("message", ({ data: message }) => {
relay.on("message", (message) => {
switch (message[0]) {
case "EVENT":
case "OK":
Expand All @@ -14,9 +14,9 @@ export const install: RelayModule["install"] = (relay) => {
return; // Ignore unknown messages.
}
});
relay.addEventListener("subscribe", (ev) => {
const { id, filters, options, controller } = ev.data;
relay.addEventListener(id, ({ data: message }) => {

relay.on("subscribe", ({ id, filters, options, controller }) => {
relay.on(id, (message) => {
switch (message[0]) {
case "EVENT": {
const [, , event] = message;
Expand All @@ -35,17 +35,19 @@ export const install: RelayModule["install"] = (relay) => {
});
relay.send(["REQ", id, ...filters]);
});
relay.addEventListener("resubscribe", ({ data: { id, filters } }) => {

relay.on("resubscribe", ({ id, filters }) => {
relay.send(["REQ", id, ...filters]);
});
relay.addEventListener("unsubscribe", ({ data: { id } }) => {

relay.on("unsubscribe", ({ id }) => {
if (relay.status === WebSocket.OPEN) {
relay.send(["CLOSE", id]);
}
});
relay.addEventListener("publish", (ev) => {
const { event, resolve, reject } = ev.data;
relay.addEventListener(event.id, ({ data: message }) => {

relay.on("publish", ({ event, resolve, reject }) => {
relay.on(event.id, (message) => {
if (message[0] !== "OK") {
// This NIP only supports OK messages.
return;
Expand Down
2 changes: 1 addition & 1 deletion nips/42/relays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare module "../../core/relays.ts" {
}

const install: RelayModule["install"] = (relay) => {
relay.addEventListener("message", ({ data: message }) => {
relay.on("message", (message) => {
if (message[0] !== "AUTH") {
// This NIP only handles AUTH messages
return;
Expand Down

0 comments on commit ec5cd2b

Please sign in to comment.