feat(event): 新增 HasEventListener 接口,支持检测事件是否已被监听 - #288
Open
isolamenter wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
动机 / 背景 (Motivation)
在实际游戏业务开发中(如剧情播放器、UI 弹窗生命周期管理、网络重连等场景),经常需要在分发事件、反注册事件或重复注册前,预先探测某个事件是否已存在监听回调。
目前 TEngine 的 `GameEvent` 仅提供了 `AddEventListener` 与 `RemoveEventListener`。如果在未注册的情况下调用 `RemoveEventListener`,底层会直接打印 `Log.Fatal("Delete handle failed, not exist...")`。增加 `HasEventListener` 能够大幅提升事件管理的防御性编程体验与健壮性。
改动内容 (Changes)
使用示例 (Usage Example)
```csharp
// 1. int 事件检测
if (GameEvent.HasEventListener(PlotEvent.EVENT_PLOT_SCENE_FINISH, OnPlotSceneFinished))
{
GameEvent.RemoveEventListener(PlotEvent.EVENT_PLOT_SCENE_FINISH, OnPlotSceneFinished);
}
// 2. 带参数泛型事件检测
if (!GameEvent.HasEventListener<int, string>(GameEventId.OnPlayerLevelUp, OnLevelUp))
{
GameEvent.AddEventListener<int, string>(GameEventId.OnPlayerLevelUp, OnLevelUp);
}
// 3. string 事件检测
if (GameEvent.HasEventListener("OnGoldChanged", OnGoldChanged))
{
// ...
}
```
兼容性 (Compatibility)