Skip to content

Commit

Permalink
feat(std/relays): WithPool to create relay pools
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Mar 22, 2024
1 parent 493ee6a commit b7dea5c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
39 changes: 39 additions & 0 deletions std/relays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import type {
ClientToRelayMessage,
EventKind,
NostrEvent,
RelayUrl,
SubscriptionFilter,
} from "@lophus/core/protocol";
import {
Relay,
RelayLike,
RelayLikeConfig,
RelayLikeOptions,
RelayOptions,
SubscriptionOptions,
} from "@lophus/core/relays";

Expand Down Expand Up @@ -68,3 +71,39 @@ export class RelayGroup implements RelayLike {
await Promise.resolve();
}
}

export interface WithPool<
R extends typeof Relay,
> {
pool: Map<RelayUrl, InstanceType<R>>;
new (url: RelayUrl, options?: RelayOptions): InstanceType<R>;
}

export function WithPool<
R extends typeof Relay,
>(
BaseRelay: R,
): WithPool<R> {
// @ts-ignore allow concrete arguments for constructor
return class Self extends BaseRelay {
static readonly pool = new Map<RelayUrl, InstanceType<R>>();

constructor(
url: RelayUrl,
options?: RelayOptions,
) {
const pooled = Self.pool.get(url);
if (pooled) {
return pooled;
}
super(url, options);
Self.pool.set(url, this as InstanceType<R>);
return this;
}

override close() {
Self.pool.delete(this.config.url);
return super.close();
}
};
}
36 changes: 35 additions & 1 deletion std/relays_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
import { MockWebSocket } from "@lophus/lib/testing";
import { NostrEvent, Relay, SubscriptionId } from "@lophus/nips";
import { RelayGroup } from "./relays.ts";
import { RelayGroup, WithPool } from "./relays.ts";

describe("RelayGroup", () => {
let relays: Relay[];
Expand Down Expand Up @@ -113,3 +113,37 @@ describe("RelayGroup", () => {
});
});
});

describe("WithPool", () => {
let Pooled: WithPool<typeof Relay>;

beforeAll(() => {
globalThis.WebSocket = MockWebSocket;
});

it("should accept a NIP-enabled relay as an argument", () => {
Pooled = WithPool(Relay);
});

it("should have no relays in the pool initially", () => {
assertEquals(Pooled.pool.size, 0);
});

it("should add a relay to the pool and return it", () => {
const relay = new Pooled("ws://localhost:80");
assertEquals(Pooled.pool.size, 1);
assertEquals(Pooled.pool.has(relay.config.url), true);
});

it("should return the pooled relay if it exists", () => {
const relay = new Pooled("ws://localhost:80");
assertEquals(Pooled.pool.size, 1);
assertEquals(Pooled.pool.has(relay.config.url), true);
});

it("should remove a relay from the pool when it is closed", async () => {
const relay = new Pooled("ws://localhost:80");
await relay.close();
assertEquals(Pooled.pool.size, 0);
});
});

0 comments on commit b7dea5c

Please sign in to comment.