Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/webgal/src/Core/gameScripts/playEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export const playEffect = (sentence: ISentence): IPerform => {
}
}
};
seElement.addEventListener('error', (e) => {
logger.error(`播放效果音失败: ${url}`);
// 播放失败提前结束
seElement.onended?.(e);
});
Comment on lines +103 to +107

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Calling seElement.onended from the error event handler creates a tight coupling between two semantically different events. This can be brittle if the onended handler's logic changes in the future to be more specific to successful playback completion.

Furthermore, the loop that is implicitly called (inside the onended handler) is inefficient and potentially unsafe if the list of performances is modified during iteration.

A better approach is to implement the cleanup logic directly in the error handler using find() to safely locate and clean up the performance object. This decouples the error handling from the onended event and is more robust.

        seElement.addEventListener('error', (e) => {
          logger.error(`播放效果音失败: ${url}`);
          // 播放失败提前结束
          const perf = WebGAL.gameplay.performController.performList.find(
            (p) => p.performName === performInitName
          );
          if (perf) {
            isOver = true;
            perf.stopFunction();
            WebGAL.gameplay.performController.unmountPerform(perf.performName);
          }
        });

}, 1);
}),
};
Expand Down