Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Use type-safe EventEmitter interface #402

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jonahsnider
Copy link

@jonahsnider jonahsnider commented Mar 15, 2021

This replaces the SharderEvents enum with an object type that maps event names to listener function signatures. In combination with the TypedEventEmitter interface from @pizzafox/util this means TypeScript users get automatic type-checking of their event listeners, and the library gets safe calls to emit.

In fact, implementing this revealed 2 non-compliant ShardingManager#emit usages:

private _shardready(message: NodeMessage) {
const { d: { shardID } } = message.data;
this._debug(`Shard ${shardID} became ready`);
this.manager.emit(SharderEvents.SHARD_READY, shardID);
}

private _shardreconnect(message: NodeMessage) {
const { d: { shardID } } = message.data;
this._debug(`Shard ${shardID} tries to reconnect`);
this.manager.emit(SharderEvents.SHARD_RECONNECT, shardID);
}

Adding the dependency will slightly increase bundle size, even though it's just used at build-time for TypeScript users. The official recommendation is to list it in the dependencies field of package.json.

In its current state this PR is a breaking change since SharderEvents is no longer an enum. This could easily be changed back to an enum and rename the new type to something like SharderEventListeners.

@DevYukine
Copy link
Owner

This seems interesting, i will have a look on the weekend at it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants