Support for multiple event handlers
leonardssh opened this issue · 2 comments
leonardssh commented
I got a dm from a guy who used mp.events.add
something like:
mp.events.add({
'eventName': (eventArgs) => { },
...
});
I sat and looked over the code for a bit, and managed to make a variant, except that after the first event, typescript concatenates the arguments with the next, resulting in a very large and useless type.
interface IServerEvents {
entityCreated: (entity: any) => void;
entityModelChange: (entity: any, oldModel: number) => void;
packagesLoaded: () => void;
playerChat: (player: any, text: string) => void;
playerCommand: (player: any, command: string) => void;
}
declare function add<K extends keyof IServerEvents>(eventHandlers: Record<K, IServerEvents[K]>): void;
declare function add<K extends keyof IServerEvents>(eventName: K, callback: IServerEvents[K]): void;
declare function add(eventName: string, callback: (...args: any[]) => void): void;
add('entityCreated', () => {});
add('playerCommand', (player, command) => {})
add({
// (property) playerChat: (player: any, text: string) => void
'playerChat': (player, text) => {},
// (property) entityCreated: ((entity: any) => void) | ((player: any, text: string) => void)
'playerCommand': (player, command) => {},
'entityCreated': () => {}
})
See in Typescript Playground
leonardssh commented
I've added a quick fix for this problem until I figure out how to fix it. See commit leonardssh@db5ed55.
leonardssh commented
Thx to @vladfrangu. 😊