Skip to content

Commit

Permalink
Allow additional arbitrary static values in ListenerUtil.on/once
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zajrik committed May 20, 2017
1 parent 88e20a0 commit 422b6ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/util/ListenerUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ListenerUtil
if (listener.attached) continue;
listener.attached = true;
emitter[listener.once ? 'once' : 'on'](listener.event,
(...eventArgs: any[]) => (<any> listenerTarget)[listener.method](...eventArgs));
(...eventArgs: any[]) => (<any> listenerTarget)[listener.method](...eventArgs, ...listener.args));
}
}

Expand All @@ -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);
}

/**
Expand All @@ -69,24 +71,26 @@ 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);
}

/**
* Returns a MethodDecorator that handles setting the appropriate listener
* 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<T extends EventEmitter>(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;
};
Expand All @@ -102,5 +106,6 @@ type ListenerMetadata =
event: string;
method: string;
once: boolean;
args: any[];
attached?: boolean;
};
5 changes: 3 additions & 2 deletions test/test_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ class Test extends Client
logger.error('Test', 'Testing Logger#error()');
}

@once('clientReady')
private async _onClientReady(): Promise<void>
@once('clientReady', 'foo', 1)
private async _onClientReady(foo: string, bar: number): Promise<void>
{
logger.debug('Test', foo, bar.toString());
await this.setDefaultSetting('foo', 'bar');
}
}
Expand Down

0 comments on commit 422b6ad

Please sign in to comment.