Repository navigation
fix: respect injected Stage ownership and scoped event sources - #4725
Conversation
xile611
left a comment
There was a problem hiding this comment.
针对共享 Canvas 的宿主集成,保留注入 Stage 的调度/释放 ownership、补齐绘制前准备与订阅解绑的方向是合理的。当前 HEAD 04a6008b3a6464699a665f1b8fc6fab3784ecdd1 的主要问题在事件源语义:示例和类型注释推荐 eventSource: stage.eventSystem.manager.dispatch,但这个 emitter 并不等价于原来的 DOM canvas/window 事件源。
需要修正的行为
manager.dispatch 包含图元切换产生的 pointerout/over。指针在同一画布内从图元 A 移到 B 时也会收到 pointerout;DimensionHoverEvent 恰好订阅 canvas-source pointerout 并清空维度缓存,因此会把普通图元切换当成离开画布,产生额外的 leave/enter。底层事件链的最小复现已确认 sibling mark 切换会在 dispatch 上产生 pointerout,不需要真的离开画布。
另外,这个 eventSource 只替换 Compiler 管理的订阅,无法接管 VRender DataZoom/Slider 在浏览器中直接注册的 vglobal 拖拽监听。宿主过滤/取消的完整性需要引擎及组件联动,不能仅凭 canvas/window 订阅被替换就认定已完成。
当前方案与建议方案
| 边界 | 当前 PR 的推荐接法 | 建议 |
|---|---|---|
| chart/图元交互 | 继续走 Stage | 保持,保留图元 target 与冒泡语义 |
| canvas 事件源 | 使用 manager.dispatch,混入图元 out/over | 明确逻辑画布边界;图元切换不触发 canvas leave |
| window/拖拽事件源 | 同一个 emitter,依赖图元事件映射 | 接收该 Stage 获准的输入,明确画布外 move/up 的分发范围 |
| 取消 | 依赖底层 reset 产生事件及宿主 hideTooltip | 显式处理 pointercancel/逻辑离开,清理维度交互与拖拽状态;tooltip API 只负责其显示 |
| 输入归属与坐标 | 各 Stage 配置 native binding/filter/mapPoint | 宿主统一路由到 Engine 的同一输入入口;组件和图表使用一致的 Stage 坐标 |
建议配合 VRender #2146 的方案对比与验证 review 收口一套“已接受输入流”:宿主先仲裁,再显式转发;EventManager 统一维护取消状态;组件与 canvas/window 订阅在定义好作用范围后接入。仅将这里的 emitter 换个名字还不够,尤其不能把图元 pointerout 直接当作逻辑画布 leave。如果改为使用 pointercancel 清理维度 hover,需要同步补上 DimensionHoverEvent 的取消订阅;新输入源不能假装与旧 DOM source 完全同义。
建议增加以下回归:
- 同一维度内跨图元移动保持 move,不额外 leave/enter;真正离开逻辑画布或宿主取消时正确清理。
- DataZoom/Slider 拖拽开始后宿主接管,迟到 move/up 不更新;短暂忽略一个 move 后允许继续拖拽。
- 画布外拖拽、宿主仿射变换、DPR 1/2、两个 Stage 的输入隔离。
- chart.release 只移除本图表订阅与 hook,注入的 Stage/source 仍由调用方持有;新 source 的测试需检查事件含义,而不只是有没有计数。
已在配套 VRender HEAD ccae87586e308549abd750bbd0ebdff1f0fd5272 上做新事件边界的最小原型:真实浏览器/组件 81 场景、584 项断言通过,覆盖宿主转发、取消、坐标、越界及两个 Stage。恢复旧组件监听方式的反例确认了宿主过滤旁路。该结果不代表完整 VChart 链路已验证,维度 hover、tooltip、移动端、多指和正式事件源契约仍需在两仓联调中补齐。
|
补充具体修改步骤,对应前面的 review。当前基线为 验证进展与修订: VRender 原型补测中新增 scope/取消/debounce 62/62 场景通过,连同旧场景为 143/143。确认窗口范围越界仍是一次 pointerup、canvas 不收该 up;跨 sibling 图元不会产生 canvas out。取消回调内的新输入需要由引擎串行处理;up 的终止事务在回调完成前仍可取消。DataZoom 必须缓存坐标标量、正常 up flush、cancel 丢弃 pending,并增加 finishing 保护。这些是引擎/组件验证,不代表下面完整 VChart hover/tooltip 测试已完成。 1. 把 eventSource 改为有范围的订阅协议修改 interface ScopedEventSource {
on(type: string, listener: (event: any) => void): unknown;
off(type: string, listener: (event: any) => void): unknown;
}
// IRenderOption 内;未提供时沿用原 DOM 订阅。
// 这是本 Draft 新增接口的修订,不需要保留单 emitter 的并行实现。
eventSource?: {
canvas: ScopedEventSource;
window: ScopedEventSource;
};接入示例改成: const chart = new VChart(spec, {
stage,
autoFit: false,
eventSource: stage.eventSystem.input
});这里的 语义必须同时写进注释/接入文档:
上述 scope、click/dblclick、mouse down/up 别名及所列 debounce 路径已加入本轮补测;完整别名表、实际 VChart 和移动端默认行为仍是正式实现的验收项。 2. Compiler 按 source 绑定,解绑使用实际绑定对象修改 // canvas/window 分支的选择逻辑;chart 分支保持原有 Stage 绑定。
const emitter =
source === Event_Source_Type.canvas
? this._option.eventSource?.canvas
: this._option.eventSource?.window;
if (emitter) {
emitter.on(type, wrappedCallback);
} else {
domTarget?.addEventListener(type, wrappedCallback);
}
具体收尾:
3. 维度 hover 明确处理取消,清理操作幂等修改:
建议清理方法的关键逻辑: // 保留现有 params 合法性、disableDimensionHoverEvent 等判断后:
const previous = this._cacheDimensionInfo;
if (previous === null) {
return;
}
// 必须先清缓存再回调,避免回调中重入后又把新状态清掉。
this._cacheDimensionInfo = null;
this._callback.call(null, {
...params,
action: 'leave',
dimensionInfo: previous.slice()
});因此:
同时检查 tooltip 的实际事件订阅链:维度 tooltip 应随上述 leave 收起;mark tooltip 要由引擎取消时对旧 target 的正确离开事件或显式取消处理清理,二者选择现有链路中最直接的一处,不再叠加全局 DOM 监听。用真实可见 tooltip 断言确认,不能只调用宿主 hideTooltip 掩盖取消链路未完成。 4. 把现有 injected-stage 测试改为真正检查事件含义修改 建议拆出
DataZoom/Slider 的 vglobal 旁路必须在 VRender components 中迁移;VChart 此处不能通过另加 capture/filter 弥补。独立 Slider 的场景留在 components 测试,VChart 验收聚焦图表实际使用的组件和事件链。 5. 合并顺序与交付边界先完成配套 VRender 的输入协议与组件迁移,再联调 VChart;此时前面“不需要新 VRender API”的描述需要更新,因为推荐接法已经明确依赖新增的 scope 输入协议。不要在未提供新 API 的旧依赖上退回 manager.dispatch,使错误语义成为长期兼容分支。 提交可分成“source 类型/Compiler 绑定解绑”和“取消、hover/tooltip 与真实图表回归”两组;每组包含对应失败用例和修复。布局测量修复与本输入方案独立,可单独评审,避免回归来源混杂。 实际浏览器测试通过后,再运行本仓标准编译与视觉检查;若依赖预检阻塞,记录具体阻塞和已完成的定向验证,不把原型结果当成完整图表回归通过。 |
4d6ad97 to
df8d4c8
Compare
There was a problem hiding this comment.
Latest reviewed commit: 963402d
No confirmed new issues found.
|
复审结论:当前 HEAD [P1] pointercancel 只加入了类型和订阅代码,没有加入运行时事件白名单相关位置:
实测复现环境:Node.js 22.23.3、Playwright 1.63.0 / Chromium、当前 PR 源码构建产物、锁定依赖 VRender 1.1.14。 真实浏览器中创建柱状图,注入真实 Stage,并使用 const counts = { raw: 0, chart: 0 };
const actions = [];
stage.eventSystem.input.canvas.on('pointercancel', () => counts.raw++);
chart.on('pointercancel', { source: 'canvas' }, () => counts.chart++);
chart.on('dimensionHover', params => actions.push(params.action));
// 用真实鼠标移动进入柱图维度后执行;本次 Playwright 鼠标 pointerId 为 1。
stage.eventSystem.cancelPointer(1);
console.log(counts, actions);当前实际结果: 预期 VChart 的 cancel 回调也应收到 1 次。 另用实现了 on/off 的独立 canvas/window source 验证:注册后 canvas 实际只有 现有 injected-stage 用例会漏掉此问题。 本次直接执行当前产物上的该用例 exercise/verify 仍然通过,actions 为 建议修复
需要补的回归测试
本次新增布局单测 2/2 通过;当前产物的 injected-stage 交互断言通过,但上述独立 cancel 复现失败。标准 |
|
复审 HEAD [P2] off dimensionHover 后 canvas cancel handler 未移除,旧回调仍会执行并持续累积DimensionHoverEvent.unregister 第 48 行传入: this._eventDispatcher.unregister('pointercancel', {
query: null,
callback: this.onMouseOut
});但 EventDispatcher.unregister 根据 复现与实测结果环境:Node.js 22.23.3、Playwright 1.63.0 / Chromium、当前 HEAD 源码构建产物、VRender 1.1.14。 真实浏览器创建柱状图,使用调用方 Stage 和 const actions = [];
const onHover = params => actions.push(params.action);
chart.on('dimensionHover', onHover);
// 用真实鼠标移动进入维度,actions 此时为 ['enter']。
chart.off('dimensionHover', onHover);
stage.eventSystem.cancelPointer(1); // 本次 Playwright 鼠标 pointerId 为 1
console.log(actions);预期: 另用真实 Compiler、EventDispatcher、Event、DimensionHoverEvent 和调用方 EventEmitter,经过 Event.on/off 的组合事件分支复现: 注意,累积的是内部 bubble handler;底层 emitter 的代理监听可能仍只有 1 个,不能只检查 emitter.listenerCount 来证明没有泄漏。 作为定向验证,仅在独立复现脚本中临时让 unregister 按注册时的 source 解绑,结果变为:off 后源监听和内部 handler 都为 0;cancel 不再触发旧回调;额外 10 轮 on/off 后 handler 仍为 0。没有修改 PR 源码。 建议修复
需要补的测试
当前新增单测中的 本轮验证范围
|
|
复审 1. 注入事件源时,公开的防抖回调读到了后续输入。 用当前提交的构建产物在 Chromium 中创建两个图表。宿主图表走默认 DOM 输入,另一个图表注入同一个 Stage 和
两组订阅都只监听了 建议修复及回归:在引擎事件仍有效时,为需要延迟消费的订阅保存稳定的事件数据。覆盖 debounce 和 throttle 的尾部回调,避免给同步监听增加每次输入的复制开销。增加真实 Stage 的浏览器回归,按上述顺序输入,断言防抖回调恰好调用一次,且仍读到 2. 新增的防抖解绑测试在仓库默认 Electron runner 下报错。 Node.js 22.23.3 下,以仓库默认 Jest 配置运行 建议修复及回归:把该用例改为真实异步测试。按顺序注册两个相同 debounce 时长的维度 hover 回调 A、B,触发一次 move 后立即 off A。用 B 的回调 resolve 一个 Promise,等待它完成,再断言 A 调用 0 次、B 调用 1 次。这样可以验证防抖窗口确实经过,同时覆盖 off 只取消自身订阅。请用仓库默认 Electron runner 运行修改后的用例,并确认临时移除待执行回调的取消逻辑时,该断言会失败。 |
8c721d8 to
100bdec
Compare
Preserve caller-owned Stage rendering and lifecycle while binding scoped canvas/window input with matching cleanup. Track pointer-driven and programmatic dimension hover so cancellation emits leave once. Keep synchronous rate-limited callbacks on the original event and retain complete gesture payloads in delayed snapshots with VRender 1.1.14. Cancel pending callbacks when removing subscriptions or releasing the chart. Track subscriptions by handler identity so callback reuse across sources and Event instances preserves ownership during off, release and timer cancellation. Keep dimension-click and public chart cleanup consistent. Add event-chain regressions and deterministic browser interaction cases.
100bdec to
963402d
Compare
Problem and resulting behavior
Injected charts must preserve caller-owned Stage scheduling and consume input with correct canvas/window boundaries. A single EventManager dispatch emitter exposes sibling-mark out events as canvas leave and is not an accepted-input protocol.
The
eventSourceoption has separate canvas and window subscriptions. Compiler records each actual binding for cleanup, even if options change; releasing a chart keeps its caller-owned Stage/source alive. Chart-target events remain on the scenegraph. Without eventSource, the existing DOM subscription path is retained.This recommended input source requires the paired VRender implementation in VisActor/VRender#2146. Do not substitute manager.dispatch on older engines. Canvas out/leave are logical viewport transitions; window contains only Stage-owned input, including outside drag termination. Source events are pooled; synchronous chart callbacks use the original event and delayed callbacks retain a snapshot.
Dimension hover tracks pointer-driven and programmatic
setDimensionIndexactivation before invoking callbacks. Canvas cancel/out clears active hover and emits leave once. Unregister reuses the actual parsed handlers, preserving canvas/chart source ownership and cancelling pending debounce/throttle callbacks while other subscribers remain active.Rate limiting keeps the standard vutils throttle/debounce controls. Synchronous callbacks receive the original engine event, preserving stopImmediatePropagation, stopPropagation and preventDefault. Only callbacks left pending retain snapshots of pooled pointer/wheel events, including Gesture and custom extension payloads. Reusable coordinate and path buffers are copied separately. This delayed-event fix works with installed VRender 1.1.14 and does not require a clone API fix. Removing subscriptions, clearing dispatchers, and releasing charts cancel pending callbacks.
Injected Stage drawing preparation stays on the existing Compiler hooks; no external-chart mode or host geometry/input policy is added to VChart.
Subscriptions are retained by actual handler identity, including each resolved source. Removing a callback or event type only removes subscriptions owned by that Event instance; explicit source removal leaves other sources intact. Bubble also uses handler identity, so another instance can reuse the same callback without losing its listeners or pending timers. DimensionClick keeps its original handler for cleanup, and VChart.off removes all matching outer registration records.
Validation
Current revision: 963402d. The PR remains one commit on its existing base; its complete Git tree matches the validated working tree.
fdb63276cf8b71f5a6cca128c409e009cc94615a: 268 passed, 0 pixel differences, 0 execution errors, 2 new_baseline cases (injected-stage and event-subscription-ownership). Both candidates pass; develop fails their business assertions. Baseline confirmation is still required.Remaining checks
The two new-baseline cases require maintainer confirmation. A mixed Electron run still hits two existing core crosshair/tooltip failures with
application.graphicService.creator[graphicType] is not a function. The preceding commit's original source/tests reproduce the same failures and messages under the same ordering; the core suite passes independently. This is not an all-green mixed-suite result.Remote CI and maintainer review remain separate from local verification. Existing broken repository hooks were bypassed in favor of the explicit checks above. Physical mobile acceptance is not included.