typescript - 如何在 TypeScript 中实现发布订阅模式?
问题描述
我正在为我的游戏创建一个事件系统,我的代码目前如下所示:
export const enum ET { Collision, Dying, Damage }
type ActionCallback = (scene: Scene, event: GameEvent) => void;
subscribe(eventType: ET, callback: ActionCallback) {
this.subscriptions[eventType].push(callback);
}
然后一些使用此函数的代码示例如下:
scene.events.subscribe(ET.Dying, handleEntityDeath);
handleEntityDeath = (scene: Scene, event: DyingEvent) => {
scene.deleteEntity(event.entity);
}
问题是 TypeScript 无法编译并显示类似:event's type must be GameEvent and not DyingEvent
.
基本上,我需要一种“链接”ET.Dying
和的方法DyingEvent
,但我不知道该怎么做。我想如果我能做到这一点,那么我可以像上面这样的事件处理程序,它只会在第一个参数类似于ET.Dying
并且第二个参数是一个回调的情况下编译DyingEvent
。如果回调有一个DamageEvent
参数,我希望它无法编译,如果这有意义的话。
可以做到这一点,如果可以,怎么做?
解决方案
弄清楚了:
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);
}
推荐阅读
- google-apps-script - 如何以编程方式将触发器添加到电子表格 onformsubmit
- java - JQuery Ajax POST 方法不起作用?$.post
- ios - 尝试修复 UIViewAlertForUnsatisfiableConstraints 时遇到问题
- authentication - Dotnet 核心 API 身份验证添加 PIN 登录(如在 Windows 10 中)
- ruby-on-rails - Rails 资产预编译和 Heroku 管道
- c# - 获取DataRow字段或默认值的函数
- postgresql - 第一次做postgres复制——不工作
- php - 使用 LDAP 从 Active Directory 获取用户凭据
- python - 使用 64 位系统时使用 Chatterbot 时出错
- python - 使用条件创建行组合(二元组)