-
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.
- Loading branch information
1 parent
4f46b8c
commit d5fcfb9
Showing
3 changed files
with
81 additions
and
0 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,28 @@ | ||
import type { SubscribedEvent } from "./event.js"; | ||
import type { SubscriberRegistryInterface } from "./registry.js"; | ||
import type { SubscriberInterface } from "./subscriber.js"; | ||
|
||
export interface SubscriberProviderInterface { | ||
/** | ||
* Gets all subscribers which are subscribed to the received event. | ||
* | ||
* @param event Event received. | ||
*/ | ||
getSubscribersForEvent<T extends SubscribedEvent>(event: T): SubscriberInterface<T>[]; | ||
} | ||
|
||
export class SubscriberProvider implements SubscriberProviderInterface { | ||
private subscribers: SubscriberInterface<SubscribedEvent>[]; | ||
|
||
constructor(registry: SubscriberRegistryInterface) { | ||
this.subscribers = registry.all(); | ||
} | ||
|
||
getSubscribersForEvent<T extends SubscribedEvent>(event: T): SubscriberInterface<T>[] { | ||
const subscribers = this.subscribers.filter((subscriber): boolean => | ||
subscriber.getSubscribedEvents().includes(event), | ||
) as SubscriberInterface<T>[]; | ||
|
||
return 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 PushSubscriber extends Subscriber<"push"> { | ||
constructor(config: ConfigObject) { | ||
super(config); | ||
} | ||
|
||
async process(context: ContextDecorator<"push">): Promise<void> { | ||
await Promise.resolve(); | ||
} | ||
|
||
getSubscribedEvents(): ["push"] { | ||
return ["push"]; | ||
} | ||
} |
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,36 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { ConfigObject } from "../../src/configuration"; | ||
import { map } from "../../src/subscriber/map"; | ||
import { SubscriberProvider } from "../../src/subscriber/provider"; | ||
import { SubscriberRegistry } from "../../src/subscriber/registry"; | ||
import { GenericSubscriber } from "../fixtures/generic-subscriber"; | ||
import { PushSubscriber } from "../fixtures/push-subscriber"; | ||
|
||
describe("subscriber provider", () => { | ||
const config = { | ||
version: 1, | ||
settings: { | ||
active_subscribers: ["generic", "push"], | ||
}, | ||
} as ConfigObject; | ||
|
||
map.set("generic", new GenericSubscriber(config)); | ||
map.set("push", new PushSubscriber(config)); | ||
|
||
const registry = new SubscriberRegistry(config.settings.active_subscribers, map); | ||
|
||
test("Provider can get subscribers for event", () => { | ||
const provider = new SubscriberProvider(registry); | ||
const subscribers = provider.getSubscribersForEvent("push"); | ||
|
||
expect(subscribers.length).toBe(1); | ||
expect(subscribers[0]).toBeInstanceOf(PushSubscriber); | ||
}); | ||
|
||
test("Provider returns empty array if no subscribers found", () => { | ||
const provider = new SubscriberProvider(registry); | ||
const subscribers = provider.getSubscribersForEvent("pull_request.opened"); | ||
|
||
expect(subscribers.length).toBe(0); | ||
}); | ||
}); |