Skip to content

Commit

Permalink
Add abstract Subscriber class
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jan 21, 2025
1 parent 447def9 commit 58757b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/subscriber/subscriber.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ConfigObject } from "../configuration.js";
import type { ContextDecorator } from "../context.js";
import type { SubscribedEvent } from "./event.js";

Expand All @@ -14,3 +15,15 @@ export interface SubscriberInterface<T extends SubscribedEvent> {

getSubscribedEvents(): T[];
}

export abstract class Subscriber<T extends SubscribedEvent> implements SubscriberInterface<T> {
protected config: ConfigObject;

constructor(config: ConfigObject) {
this.config = config;
}

abstract process(context: ContextDecorator<T>): Promise<void>;

abstract getSubscribedEvents(): T[];
}
26 changes: 26 additions & 0 deletions tests/subscriber/subscriber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 [];
}
}

describe("generic subscriber", () => {
test("Can create a generic subscriber", () => {
const subscriber = new GenericSubscriber({} as any);

expect(subscriber).toBeInstanceOf(Subscriber);
});
});

0 comments on commit 58757b9

Please sign in to comment.