Skip to content

Commit

Permalink
Add SubscriberRegistry and SubscriberRegistryInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jan 21, 2025
1 parent 58757b9 commit d529fab
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 16 deletions.
32 changes: 32 additions & 0 deletions src/subscriber/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { SubscribedEvent } from "./event.js";
import type { SubscriberMap } from "./map.js";
import type { SubscriberInterface } from "./subscriber.js";

/**
* Registers the subscribers' id in the configuration file.
*/
export interface SubscriberRegistryInterface {
register(...id: string[]): void;

all(): SubscriberInterface<SubscribedEvent>[];
}

export class SubscriberRegistry implements SubscriberRegistryInterface {
private subscribers: SubscriberInterface<SubscribedEvent>[] = [];
private map: SubscriberMap;

constructor(subscribersId: string[], map: SubscriberMap) {
this.map = map;
this.register(...subscribersId);
}

register(...id: string[]): void {
const subscribers = id.filter((id) => this.map.has(id)).map((id) => this.map.get(id)!);

this.subscribers.push(...subscribers);
}

all(): SubscriberInterface<SubscribedEvent>[] {
return this.subscribers;
}
}
17 changes: 17 additions & 0 deletions tests/fixtures/generic-subscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ConfigObject } from "../../src/configuration";
import { ContextDecorator } from "../../src/context";
import { Subscriber } from "../../src/subscriber/subscriber";

export class GenericSubscriber extends Subscriber<any> {
constructor(config: ConfigObject) {
super(config);
}

async process(context: ContextDecorator<any>): Promise<void> {
await Promise.resolve();
}

getSubscribedEvents(): any[] {
return [];
}
}
46 changes: 46 additions & 0 deletions tests/subscriber/registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, test } from "vitest";
import { ConfigObject } from "../../src/configuration.js";
import { map } from "../../src/subscriber/map.js";
import { SubscriberRegistry } from "../../src/subscriber/registry.js";
import { GenericSubscriber } from "../fixtures/generic-subscriber.js";

describe("subscriber registry", () => {
const config = {
version: 1,
settings: {
active_subscribers: ["test_1", "test_2"],
},
} as ConfigObject;

map.set("test_1", new GenericSubscriber(config));
map.set("test_2", new GenericSubscriber(config));

test("Registry can register subscribers", () => {
const registry = new SubscriberRegistry(config.settings.active_subscribers, map);
const allSubscribers = registry.all();

expect(allSubscribers.length).toBe(2);
expect(allSubscribers[0]).toBeInstanceOf(GenericSubscriber);
expect(allSubscribers[1]).toBeInstanceOf(GenericSubscriber);
});

test("Registry filters out unmatched subscribers", () => {
const registry = new SubscriberRegistry(["test_1", "test_2", "test_3"], map);
const allSubscribers = registry.all();

expect(allSubscribers.length).toBe(2);
expect(allSubscribers[0]).toBeInstanceOf(GenericSubscriber);
expect(allSubscribers[1]).toBeInstanceOf(GenericSubscriber);
});

test('Registry can register subscribers with "register" method', () => {
const registry = new SubscriberRegistry([], map);
registry.register("test_1", "test_2");

const allSubscribers = registry.all();

expect(allSubscribers.length).toBe(2);
expect(allSubscribers[0]).toBeInstanceOf(GenericSubscriber);
expect(allSubscribers[1]).toBeInstanceOf(GenericSubscriber);
})
});
17 changes: 1 addition & 16 deletions tests/subscriber/subscriber.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { describe, expect, test } from "vitest";
import { ConfigObject } from "../../src/configuration";
import { ContextDecorator } from "../../src/context";
import { Subscriber } from "../../src/subscriber/subscriber";

class GenericSubscriber extends Subscriber<any> {
constructor(config: ConfigObject) {
super(config);
}

async process(context: ContextDecorator<any>): Promise<void> {
await Promise.resolve();
}

getSubscribedEvents(): any[] {
return [];
}
}
import { GenericSubscriber } from "../../tests/fixtures/generic-subscriber";

describe("generic subscriber", () => {
test("Can create a generic subscriber", () => {
Expand Down

0 comments on commit d529fab

Please sign in to comment.