Skip to content

Commit

Permalink
fix(transport): fix listener checker
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Nov 30, 2023
1 parent d487ec2 commit d683f95
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,9 @@ export abstract class Transport<T extends BaseInteraction = any> {
public listen<K extends keyof T['listen']>(name: K, fn: T['listen'][K]) {
if (typeof name === 'string') {
if (this[originalListensMapKey].get(name)) {
if (__DEV__) {
console.warn(
`Failed to listen to the event "${name}", the event "${name}" is already listened to.`
);
}
return;
throw new Error(
`Failed to listen to the event "${name}", the event "${name}" is already listened to.`
);
}
if (typeof fn === 'function') {
this[originalListensMapKey].set(name, fn);
Expand Down
7 changes: 7 additions & 0 deletions test/other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ test('enable verbose', async () => {
text: 'hello 42 Universe',
});

expect(() => {
// @ts-expect-error
external.listen('hello', () => {});
}).toThrowErrorMatchingInlineSnapshot(
`"Failed to listen to the event "hello", the event "hello" is already listened to."`
);

const warnLog = jest.spyOn(console, 'warn').mockImplementation(() => {});

dispose?.();
Expand Down

0 comments on commit d683f95

Please sign in to comment.