Repository navigation
feat: introduce SyncRegistry for granular state synchronization - #198
Conversation
…e reset-viewBox UI.
Summary of ChangesHello @xxMudCloudxx, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求引入了全新的 SyncRegistry 机制,旨在解决现有编辑器状态管理中事件通知粒度过粗的问题。通过提供路径级订阅、自动通知和递归防护等功能,SyncRegistry 实现了更精细、高效的状态同步。同时,核心工具函数 applyOptionUpdates 也得到了增强,支持深度比较和父路径冒泡通知,进一步优化了状态变更的检测与传播。这些改进使得状态管理更加灵活和可控,并为未来的功能扩展奠定了基础。 Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
你好,感谢你为这个项目做出的贡献。这是一个很棒的 PR,引入了 SyncRegistry 机制,将原有的广播式事件通知重构为更精细的、基于路径的订阅模式,这大大提高了状态同步的效率和可维护性。
主要改动点包括:
- 全新的
SyncRegistry:实现清晰,考虑了递归调用保护和资源清理,做得很好。 - 增强的
applyOptionUpdates:新的collector和bubbleUp选项功能强大,并且有充分的单元测试覆盖。 - 代码重构:将
Editor和ResetViewBox插件中的旧事件监听逻辑迁移到新的SyncRegistry,使代码更现代化、更一致。
整体代码质量很高,改动范围虽广但逻辑清晰。我只发现了一个小问题,位于 applyOptionUpdates 函数中对父路径进行排序的逻辑,可能会导致根路径('')和顶层属性的通知顺序不稳定。我已经在一个具体的 review comment 中提出了修改建议。
除此之外,这是一个非常出色的 PR!
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #198 +/- ##
==========================================
+ Coverage 45.59% 45.89% +0.29%
==========================================
Files 335 338 +3
Lines 26834 26986 +152
Branches 2151 2189 +38
==========================================
+ Hits 12236 12386 +150
- Misses 14588 14590 +2
Partials 10 10
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
…to CoreSyncPlugin
|
在最新的一个commit中,我将原先硬编码在 Editor 内部的 viewBox 与 padding 同步逻辑抽取为独立插件。消除了核心逻辑与插件逻辑的实现差异,使 Editor 类不再直接处理 DOM 属性同步,仅负责系统编排,实现了架构层面的深度解耦。 // Load core plugin: CoreSyncPlugin (handles viewBox/padding sync)
const corePlugin = new CoreSyncPlugin();
const userPlugins = options.plugins || [];
plugin.init(
{
emitter,
editor: this,
commander,
state,
},
[corePlugin, ...userPlugins],
); |
📋 背景
在现有的 Editor 架构中,状态变更通知主要依赖于
EventEmitter进行广播式同步。这种方式存在以下问题:options:change事件的组件都会收到通知,无论变更是否与其相关。design.background、viewBox)进行精准订阅。emit。如果要监听某个新属性,必须侵入核心代码在StateManager中添加对应的emitter.emit('new-prop:change')。对于外部插件开发者来说,这是不可能完成的。本次改动引入了 SyncRegistry 机制,提供了一种细粒度的状态同步解决方案。它采用 「路径订阅 + 自动代理」 模式,开放了对整个
options树的监听能力。开发者无需修改任何核心代码,即可通过 API 监听任意层级的数据变更。🔧 主要改动
核心功能:SyncRegistry 状态同步注册表
design.background)订阅变更immediate模式,注册时立即执行一次回调isDispatching锁,防止 handler 中修改状态导致无限循环destroy()方法和自动 unregister 函数applyOptionUpdates 工具增强
isEqual进行值比较,避免无效通知collector参数,可收集所有变更路径bubbleUp选项,支持通知父级路径(如design.background变更时也通知design),默认false不启用undefined时执行属性删除SyncRegistry 使用指南 (Usage Guide)
✅ 适用场景
editor.registerSync('design.background', cb)。viewBox:change等硬编码事件。❌ 不适用场景
options:data:item:add等携带详细索引信息的事件。演示示例
📁 文件变更分类
🆕 新增文件
src/editor/managers/sync-registry.tssrc/editor/types/sync.tstests/unit/editor/managers/sync-registry.test.ts✏️ 修改文件
核心模块
src/editor/editor.tssrc/editor/managers/state.tssrc/editor/utils/object.tsapplyOptionUpdates,支持 collector 和 bubbleUp类型导出
src/editor/types/editor.tssyncRegistry属性src/editor/types/state.tssrc/editor/types/index.tssrc/editor/managers/index.tssrc/editor/index.tssrc/index.ts插件适配
src/editor/plugins/reset-viewbox.tssrc/editor/plugins/components/icons.ts测试文件
tests/unit/editor/managers/state-manager.test.tstests/unit/utils/object.test.tsapplyOptionUpdates测试重构,覆盖新功能tests/unit/editor/plugins/reset-viewbox.test.ts