Skip to content

Commit d7f2021

Browse files
authored
Fix custom broadcaster usages (#404)
* Fix custom broadcaster usages * improve test * lint * styleci
1 parent 5d3ed31 commit d7f2021

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/echo.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
SocketIoPrivateChannel,
1515
} from './channel';
1616
import { Connector, PusherConnector, SocketIoConnector, NullConnector } from './connector';
17+
import { isConstructor } from './util';
1718

1819
/**
1920
* This class is the primary API for interacting with broadcasting.
@@ -60,8 +61,8 @@ export default class Echo<T extends keyof Broadcaster> {
6061
this.connector = new SocketIoConnector(this.options);
6162
} else if (this.options.broadcaster == 'null') {
6263
this.connector = new NullConnector(this.options);
63-
} else if (typeof this.options.broadcaster == 'function') {
64-
this.connector = this.options.broadcaster(this.options as EchoOptions<'function'>);
64+
} else if (typeof this.options.broadcaster == 'function' && isConstructor(this.options.broadcaster)) {
65+
this.connector = new this.options.broadcaster(this.options as EchoOptions<'function'>);
6566
} else {
6667
throw new Error(
6768
`Broadcaster ${typeof this.options.broadcaster} ${this.options.broadcaster} is not supported.`
@@ -261,11 +262,13 @@ type Broadcaster = {
261262
};
262263
};
263264

265+
type Constructor<T = {}> = new (...args: any[]) => T;
266+
264267
type EchoOptions<T extends keyof Broadcaster> = {
265268
/**
266269
* The broadcast connector.
267270
*/
268-
broadcaster: T extends 'function' ? (options: EchoOptions<T>) => any : T;
271+
broadcaster: T extends 'function' ? Constructor<InstanceType<Broadcaster[T]['connector']>> : T;
269272

270273
[key: string]: any;
271274
};

src/util/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
function isConstructor(obj: any): obj is new (...args: any[]) => any {
2+
try {
3+
new obj();
4+
} catch (err) {
5+
if (err.message.includes('is not a constructor')) return false;
6+
}
7+
return true;
8+
}
9+
10+
export { isConstructor };
111
export * from './event-formatter';

tests/echo.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Echo from '../src/echo';
2+
import { NullConnector } from '../src/connector';
23

34
describe('Echo', () => {
45
test('it will not throw error for supported driver', () => {
@@ -15,7 +16,10 @@ describe('Echo', () => {
1516
);
1617

1718
expect(() => new Echo({ broadcaster: 'null' })).not.toThrowError('Broadcaster string null is not supported.');
19+
expect(() => new Echo({ broadcaster: NullConnector })).not.toThrowError();
1820

21+
// eslint-disable-next-line
22+
// @ts-ignore
1923
// eslint-disable-next-line @typescript-eslint/no-empty-function
2024
expect(() => new Echo({ broadcaster: () => {} })).not.toThrowError('Broadcaster function is not supported.');
2125
});

0 commit comments

Comments
 (0)