core/src/plugin/decorators/eventlistener.ts

27 lines
766 B
TypeScript

import { Auto } from './auto';
/**
* Listen for an event targeted towards this plugin.
* @param event Event name
*/
export function EventListener(event: string) {
return (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor,
) => {
const originalMethod = descriptor.value;
descriptor.value = function(...args: any[]): void {
// This ugly hack because TS thinks 'this'
// is still referring to 'PropertyDescriptor'
const self = this as any;
self.stream.on(self.name, event, (...data: any[]) =>
originalMethod.apply(self, data),
);
};
// Set the function to be autoexecuted when the plugin is initialized.
Auto()(target, propertyKey, descriptor);
return descriptor;
};
}