如何在 TypeScript 中实现发布订阅模式? [英] How to implement the publish-subscribe pattern in TypeScript?
问题描述
我正在为我的游戏创建一个事件系统,目前我的代码如下所示:
I'm working on creating an events system for my game, and my code currently looks like this:
export const enum ET { Collision, Dying, Damage }
type ActionCallback = (scene: Scene, event: GameEvent) => void;
subscribe(eventType: ET, callback: ActionCallback) {
this.subscriptions[eventType].push(callback);
}
然后使用这个函数的一些代码的例子是这样的:
And then an example of some code which uses this function is like this:
scene.events.subscribe(ET.Dying, handleEntityDeath);
handleEntityDeath = (scene: Scene, event: DyingEvent) => {
scene.deleteEntity(event.entity);
}
问题在于 TypeScript 无法编译并显示如下内容:event 的类型必须是 GameEvent 而不是 DyingEvent
.
The problem is that TypeScript fails to compile and says something like: event's type must be GameEvent and not DyingEvent
.
基本上,我需要一种方法来链接"ET.Dying
和 DyingEvent
,但我不知道该怎么做.我想如果我能做到这一点,那么我可以像上面这样的事件处理程序,如果第一个参数是类似 ET.Dying
的东西,它只会编译,第二个参数是一个回调,它需要一个 死亡事件
.如果回调具有 DamageEvent
参数,我希望它无法编译,如果这是有道理的.
Basically, I need a way to "link" ET.Dying
and DyingEvent
, but I'm not sure how to do it. I think if I can accomplish this, then I can event handlers like the above where it will only compile if the first parameter is something like ET.Dying
and the second parameter is a callback which takes a DyingEvent
. I would want it to fail to compile if the callback instead had a DamageEvent
parameter, if that makes sense.
这可以做到吗,如果可以,怎么做?
Can this be done, and if so, how?
推荐答案
想通了:
interface EventMap {
[ET.Collision]: CollisionEvent;
[ET.Dying]: DyingEvent;
// etc
}
subscribe = <T extends ET>(eventType: T, callback: (scene: Scene, event: EventMap[T]) => void) => {
this.subscriptions[eventType].push(callback);
}
// Example calling code below here:
scene.events.subscribe(ET.Dying, handleEntityDeath);
handleEntityDeath = (scene: Scene, event: DyingEvent) => {
scene.deleteEntity(event.entity);
}
这篇关于如何在 TypeScript 中实现发布订阅模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!