【发布时间】:2018-10-22 13:10:39
【问题描述】:
我正在编写一个简短的装饰器辅助函数来将一个类变成一个事件监听器
我的问题是装饰器会将装饰方法注册为传入事件的回调,但装饰方法不会保留其原始 this 上下文。
主要问题如何在这种情况下保留装饰方法的 this 上下文?
实施:
export function EventHandler (topicKey: any): ClassDecorator {
return function (target: any) {
const subscriptions = Reflect.getMetadata('subscriptions', target.prototype)
const topic = Container.get<DomainTopicInterface>(topicKey)
topic.subscribe(event => {
if (subscriptions.length === 0) {
throw new Error(`Event received for '${target.constructor.name}' but no handlers defined`)
}
subscriptions.forEach((subscription: any) => {
subscription.callback(event) // <---- the this context is undefined
})
})
return target
}
}
export function Subscribe (targetClass: StaticDomainEvent<any>): MethodDecorator {
return function (target: Function, methodName: string, descriptor: TypedPropertyDescriptor<any>) {
let originalMethod = descriptor.value
let subscriptions = Reflect.getMetadata('subscriptions', target)
if (!subscriptions) { Reflect.defineMetadata('subscriptions', subscriptions = [], target) }
subscriptions.push({
methodName,
targetClass,
callback: originalMethod
})
}
}
示例用法:
@EventHandler(Infra.DOMAIN_TOPIC)
export class JobHandler {
constructor (
@Inject() private service: JobService
) {}
@Subscribe(JobCreated)
jobCreated (events: Observable<JobCreated>) {
console.log(this) // undefined
}
}
【问题讨论】:
标签: typescript decorator