-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
SubscriberRegistry
and SubscriberRegistryInterface
- Loading branch information
1 parent
58757b9
commit d529fab
Showing
4 changed files
with
96 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters