core/src/plugin/decorators/eventlistener.ts

22 lines
675 B
TypeScript

export function EventListener(event: string): (...args: any[]) => void {
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.
descriptor.value.prototype.__autoexec = 1;
return descriptor;
};
}