jimbojw / memorelay

In-memory Nostr relay.
Apache License 2.0
0 stars 0 forks source link

Simplify creating handlers for common/known event types #53

Open jimbojw opened 1 year ago

jimbojw commented 1 year ago

The main API classes, Memorelay and MemorelayClient both offer onEvent() methods for registering event listeners. This works, but it's verbose. Here's the skeleton of an example plugin:

function examplePlugin(memorelay: Memorelay) {
  memorelay.onEvent(
    MemorelayClientCreatedEvent,
    ({ details: { memorelayClient } }: MemorelayClientCreatedEvent) => {
      memorelayClient.onEvent(
        IncomingEventMessageEvent,
        (incomingEventMessageEvent: IncomingEventMessageEvent) => {
          // TODO: Do something with the event.
        }
      );
    }
  );
}

Since it's common to listen for MemorelayClientCreatedEvent and IncomingEventMessageEvent, it would be helpful to have method signatures to make this easy. They could be on the objects themselves:

function examplePlugin(memorelay: Memorelay) {
  memorelay.onMemorelayClientCreated(({ details: { memorelayClient } }) => {
    memorelayClient.onIncomingEventMessage((incomingEventMessageEvent) => {
        // TODO: Do something with the event.
    });
  });
}

Or there could be wrapper functions:

function examplePlugin(memorelay: Memorelay) {
  onMemorelayClientCreated(memorelay, ({ details: { memorelayClient } }) => {
    onIncomingEeventMessage(memorelayClient, (incomingEventMessageEven) => {
        // TODO: Do something with the event.
    });
  });
}

The former is probably clearer since the API user would be able to use instrumentation to enumerate all the commonly available event types by hitting Ctrl+Tab.