Skip to content

Commit

Permalink
refactor(remit): update any event typing
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Jan 28, 2024
1 parent e61a0cf commit 2cdf112
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export type Fn = (...args: any[]) => any;
export type AnyEventData<
TConfig,
TEventName extends keyof TConfig = keyof TConfig
> = { event: TEventName; data: TConfig[TEventName] };
> = TEventName extends any
? { event: TEventName; data: TConfig[TEventName] }
: never;

export type AnyRemitterListener<TConfig> = (
data: AnyEventData<TConfig>
Expand Down
36 changes: 36 additions & 0 deletions test/remit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,40 @@ describe("remit", () => {
expect(spy1).toHaveBeenCalledTimes(0);
expect(spy1Disposer).toHaveBeenCalledTimes(0);
});

it("should remit a remitter", () => {
interface SourceEventData {
a: number;
b: string;
}
const source = new Remitter<SourceEventData>();

interface RemitterEventData {
c: number;
d: string;
}
const remitter = new Remitter<RemitterEventData>();

remitter.remit(remitter.ANY_EVENT, () =>
source.on(source.ANY_EVENT, ({ event, data }) => {
if (event === "a") {
remitter.emit("c", data);
} else if (event === "b") {
remitter.emit("d", data);
}
})
);

const spy = vi.fn();
remitter.once("c", spy);

expect(spy).toHaveBeenCalledTimes(0);

source.emit("a", 1);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).lastCalledWith(1);

source.emit("a", 2);
expect(spy).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 2cdf112

Please sign in to comment.