Skip to content

Commit

Permalink
imp: Improved type definitions for Publisher class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byloth committed Nov 21, 2024
1 parent 055f3e5 commit de8323b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/models/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ReferenceException } from "./exceptions/index.js";

export type Subscriber<A extends unknown[] = [], R = void> = (...args: A) => R;

export default class Publisher<T extends { [K in keyof T]: [unknown[], unknown] } = Record<string, [[], void]>>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default class Publisher<T extends { [K in keyof T]: Subscriber<any[], any> } = Record<string, Subscriber>>
{
protected _subscribers: Map<keyof T, Subscriber<unknown[], unknown>[]>;

Expand All @@ -11,17 +12,17 @@ export default class Publisher<T extends { [K in keyof T]: [unknown[], unknown]
this._subscribers = new Map();
}

public subscribe<K extends keyof T, A extends T[K][0], R extends T[K][1]>(event: K, subscriber: Subscriber<A, R>)
public subscribe<K extends keyof T, S extends T[K]>(event: K, subscriber: S)
: () => void
{
if (!(this._subscribers.has(event))) { this._subscribers.set(event, []); }

const subscribers = this._subscribers.get(event)!;
subscribers.push(subscriber as Subscriber<unknown[], unknown>);
subscribers.push(subscriber);

return () =>
{
const index = subscribers.indexOf(subscriber as Subscriber<unknown[], unknown>);
const index = subscribers.indexOf(subscriber);
if (index < 0)
{
throw new ReferenceException("Unable to unsubscribe the required subscriber. " +
Expand All @@ -32,7 +33,7 @@ export default class Publisher<T extends { [K in keyof T]: [unknown[], unknown]
};
}

public publish<K extends keyof T, A extends T[K][0], R extends T[K][1]>(event: K, ...args: A): R[]
public publish<K extends keyof T, A extends Parameters<T[K]>, R extends ReturnType<T[K]>>(event: K, ...args: A): R[]
{
const subscribers = this._subscribers.get(event);
if (!(subscribers)) { return []; }
Expand Down
6 changes: 3 additions & 3 deletions src/models/timers/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Publisher from "../publisher.js";

interface ClockEventMap
{
start: [[], void];
stop: [[], void];
tick: [[number], void];
start: () => void;
stop: () => void;
tick: (elapsedTime: number) => void;
}

export default class Clock extends GameLoop
Expand Down
8 changes: 4 additions & 4 deletions src/models/timers/countdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import Publisher from "../publisher.js";

interface CountdownEventMap
{
start: [[], void];
stop: [[unknown], void];
tick: [[number], void];
expire: [[], void];
start: () => void;
stop: (reason: unknown) => void;
tick: (remainingTime: number) => void;
expire: () => void;
}

export default class Countdown extends GameLoop
Expand Down

0 comments on commit de8323b

Please sign in to comment.