Skip to content

Unregister plugin event bus on unload; expose registerCustomEventBus on window.siyuan#17066

Open
TCOTC wants to merge 2 commits intosiyuan-note:devfrom
TCOTC:feat/16910
Open

Unregister plugin event bus on unload; expose registerCustomEventBus on window.siyuan#17066
TCOTC wants to merge 2 commits intosiyuan-note:devfrom
TCOTC:feat/16910

Conversation

@TCOTC
Copy link
Copy Markdown
Contributor

@TCOTC TCOTC commented Feb 18, 2026

Description / 描述

  • 插件关闭时自动取消注册事件总线 插件关闭时自动移除所有事件监听器 #16910
  • 暴露一个 registerCustomEventBus() 函数到 window.siyuan,供 JS 代码片段或者主题使用事件总线(直接用内部事件总线的性能应该要比写一堆监听器更好)
  • 封装了 emitToEventBus 函数,修改了几乎所有 emit 处的代码,同时删除了一部分不再需要使用的参数。比如 lockScreen() 函数去掉了 app 参数,另外还需要修改 petal

Type of change / 变更类型

  • Bug fix
    缺陷修复
  • New feature
    新功能
  • Text updates or new language additions
    修改文案或增加新语言

Checklist / 检查清单

  • I have performed a self-review of my own code
    我对自己的代码进行了自我审查
  • I have full rights to the submitted code and agree to license it under this project's AGPL-3.0 license
    我拥有所提交代码的完整权利,并同意其以本项目的 AGPL-3.0 许可证授权
  • PR is submitted to the dev branch and has no merge conflicts
    PR 提交到 dev 分支,并且没有合并冲突

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the plugin event bus lifecycle and makes the event bus available to non-plugin code (JS snippets/themes) by exposing registerCustomEventBus() on window.siyuan, while also refactoring many internal event emissions to go through a shared emitToEventBus() helper.

Changes:

  • Add EventBus.destroy() and invoke it on plugin uninstall to auto-unregister event listeners on unload.
  • Expose registerCustomEventBus() on window.siyuan (and type it) so snippets/themes can register their own event buses.
  • Refactor many plugin.eventBus.emit(...) loops to a centralized emitToEventBus(...) helper and simplify some call signatures (e.g., lockScreen, processSync, flashcard emit helper).

Reviewed changes

Copilot reviewed 26 out of 27 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
app/src/window/index.ts Adds registerCustomEventBus to window.siyuan and routes websocket/plugin events via emitToEventBus.
app/src/types/index.d.ts Types window.siyuan.registerCustomEventBus() for external usage.
app/src/search/util.ts Routes input-search emission through emitToEventBus.
app/src/protyle/wysiwyg/index.ts Routes editor click event emission through emitToEventBus.
app/src/protyle/util/setEditMode.ts Routes protyle mode switch emission through emitToEventBus.
app/src/protyle/util/paste.ts Extends paste event handling to include customEventBuses in addition to plugin buses.
app/src/protyle/util/onGet.ts Routes protyle load events through emitToEventBus.
app/src/protyle/util/destroy.ts Routes protyle destroy event through emitToEventBus.
app/src/protyle/toolbar/index.ts Routes several toolbar-related plugin events through emitToEventBus.
app/src/plugin/uninstall.ts Calls plugin.eventBus.destroy() on uninstall instead of removing DOM comment nodes directly.
app/src/plugin/EventBus.ts Implements listener tracking + destroy(), adds emitToEventBus, customEventBuses, and registerCustomEventBus.
app/src/mobile/util/onMessage.ts Uses new processSync(data) signature and minor formatting fixes.
app/src/mobile/util/keyboardToolbar.ts Routes mobile keyboard show/hide events through emitToEventBus.
app/src/mobile/util/MobileBackFoward.ts Routes protyle switch/load events through emitToEventBus.
app/src/mobile/menu/index.ts Updates lockScreen() call to new signature.
app/src/mobile/index.ts Adds registerCustomEventBus to window.siyuan and routes websocket events via emitToEventBus.
app/src/mobile/editor.ts Routes switch-protyle emission through emitToEventBus.
app/src/menus/workspace.ts Updates lockScreen() call to new signature.
app/src/layout/dock/Files.ts Routes notebook open/close events through emitToEventBus.
app/src/index.ts Adds registerCustomEventBus to window.siyuan and routes websocket/plugin events via emitToEventBus.
app/src/editor/util.ts Routes switch-protyle emission through emitToEventBus.
app/src/editor/openLink.ts Routes open-siyuan-url-block emission through emitToEventBus.
app/src/dialog/processSystem.ts Updates lockScreen() and processSync() to use emitToEventBus instead of iterating plugins.
app/src/card/openCard.ts Routes flashcard action emission through emitToEventBus and removes redundant app parameter.
app/src/boot/globalEvent/keydown.ts Updates lockScreen() call to new signature.
app/src/boot/globalEvent/command/global.ts Updates lockScreen() call to new signature.
app/src/asset/index.ts Routes PDF click event through emitToEventBus.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@TCOTC TCOTC marked this pull request as draft February 28, 2026 06:02
@TCOTC TCOTC marked this pull request as ready for review February 28, 2026 06:38
@TCOTC TCOTC requested a review from Copilot February 28, 2026 06:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 33 out of 34 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants