From 422b6ad888ac3e46d95227c22b522d82f21f0f0b Mon Sep 17 00:00:00 2001 From: Zack Campbell Date: Sat, 20 May 2017 00:23:35 -0500 Subject: [PATCH] Allow additional arbitrary static values in ListenerUtil.on/once Useful if linking multiple listeners to the same method. This would allow passing an arbitrary distinguishing value that helps determine which of the events linked to the method is the one that called it and then execute appropriately --- src/util/ListenerUtil.ts | 19 ++++++++++++------- test/test_client.ts | 5 +++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/util/ListenerUtil.ts b/src/util/ListenerUtil.ts index 8225871a..b61bc547 100644 --- a/src/util/ListenerUtil.ts +++ b/src/util/ListenerUtil.ts @@ -37,7 +37,7 @@ export class ListenerUtil if (listener.attached) continue; listener.attached = true; emitter[listener.once ? 'once' : 'on'](listener.event, - (...eventArgs: any[]) => ( listenerTarget)[listener.method](...eventArgs)); + (...eventArgs: any[]) => ( listenerTarget)[listener.method](...eventArgs, ...listener.args)); } } @@ -51,11 +51,13 @@ export class ListenerUtil * @static * @method on * @param {string} event The name of the event to handle + * @param {...any[]} args Additional static values to pass to the method. + * Will be passed after any args passed by the event * @returns {MethodDecorator} */ - public static on(event: string): MethodDecorator + public static on(event: string, ...args: any[]): MethodDecorator { - return ListenerUtil._setListenerMetadata(event); + return ListenerUtil._setListenerMetadata(event, false, ...args); } /** @@ -69,11 +71,13 @@ export class ListenerUtil * @static * @method once * @param {string} event The name of the event to handle + * @param {...any[]} args Additional static values to pass to the method. + * Will be passed after any args passed by the event * @returns {MethodDecorator} */ - public static once(event: string): MethodDecorator + public static once(event: string, ...args: any[]): MethodDecorator { - return ListenerUtil._setListenerMetadata(event, true); + return ListenerUtil._setListenerMetadata(event, true, ...args); } /** @@ -81,12 +85,12 @@ export class ListenerUtil * metadata for a class method * @private */ - private static _setListenerMetadata(event: string, once: boolean = false): MethodDecorator + private static _setListenerMetadata(event: string, once: boolean, ...args: any[]): MethodDecorator { return function(target: T, key: string, descriptor: PropertyDescriptor): PropertyDescriptor { const listeners: ListenerMetadata[] = Reflect.getMetadata('listeners', target) || []; - listeners.push({ event: event, method: key, once: once }); + listeners.push({ event: event, method: key, once: once, args: args }); Reflect.defineMetadata('listeners', listeners, target); return descriptor; }; @@ -102,5 +106,6 @@ type ListenerMetadata = event: string; method: string; once: boolean; + args: any[]; attached?: boolean; }; diff --git a/test/test_client.ts b/test/test_client.ts index 5a0f58f1..cb61fc17 100644 --- a/test/test_client.ts +++ b/test/test_client.ts @@ -52,9 +52,10 @@ class Test extends Client logger.error('Test', 'Testing Logger#error()'); } - @once('clientReady') - private async _onClientReady(): Promise + @once('clientReady', 'foo', 1) + private async _onClientReady(foo: string, bar: number): Promise { + logger.debug('Test', foo, bar.toString()); await this.setDefaultSetting('foo', 'bar'); } }