Kurasuta icon indicating copy to clipboard operation
Kurasuta copied to clipboard

[Feat] Use type-safe EventEmitter interface

Open jonahsnider opened this issue 3 years ago • 1 comments

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:

https://github.com/DevYukine/Kurasuta/blob/16a2cfb1c34c23eab30cb4974f5ca423454d7b71/src/IPC/MasterIPC.ts#L62-L66 https://github.com/DevYukine/Kurasuta/blob/16a2cfb1c34c23eab30cb4974f5ca423454d7b71/src/IPC/MasterIPC.ts#L68-L72

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.

jonahsnider avatar Mar 15 '21 07:03 jonahsnider

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

DevYukine avatar Mar 18 '21 21:03 DevYukine