Skip to content

fix(core): flush debounced change before silent mutations - #355

Open
xujing-sys wants to merge 1 commit into
floatboatai:mainfrom
xujing-sys:fix/core-flush-before-silent-mutation
Open

xujing-sys wants to merge 1 commit into
floatboatai:mainfrom
xujing-sys:fix/core-flush-before-silent-mutation

Conversation

@xujing-sys

Copy link
Copy Markdown

Summary / 摘要

在 silent 的 setDocument / replaceRange 执行前,先 flush 尚未触发的 debounced onChange,避免定时器在新文档加载后仍 emit 旧 Markdown,保证宿主最后一次用户编辑只通知一次,且 AST 与当前缓冲区一致。

Motivation / 背景与动机

parseDelayMs > 0 时,用户编辑会延迟触发 onChange 与 AST 同步。宿主在 debounce 窗口内用 setDocument(..., { silent: true })(例如打开文件、React/Vue 受控 value 同步)或 silent replaceRange 替换内容时,旧的 parse 定时器仍可能随后触发,向 已加载的新文档 emit 上一版 Markdown,造成 getDocument() / getAst() / onChange 不一致。

Changes / 变更内容

  • packages/core:
    • performSetDocument:当 opts.silent === true 时,在替换文档前调用 flushScheduledChangeNow()
    • replaceRange:当 opts.silent === true 时,在 dispatch 前同样 flush。
    • 行为:若存在 pending timer,先对 当前 view.state.doc emit 一次 change,再执行 silent 突变;silent 路径仍不触发多余的 host onChange
  • packages/plugin-*: N/A
  • apps/electron-demo: N/A
  • openspec/: N/A

Testing / 测试

  • targeted:npx pnpm@9.15.4 exec vitest run packages/core/test/editor.test.ts -t "flushes a pending" — 2 passed
  • npx pnpm@9.15.4 --filter @floatboat/nexus-core build — 通过
  • pnpm test 全仓(建议在 CI 或本地补跑后勾选)
  • New / updated vitest cases:
    • flushes a pending user change before a silent setDocument
    • flushes a pending user change before a silent replaceRange
  • Manual UI check in electron-demo:N/A(core 时序修复,无 UI 变更)

Compliance / 合规自检

  • CLA signed — 首次贡献,PR 创建后按 CLA bot 提示签署
  • AI disclosure
    • 本 PR 为面试开源贡献作业,允许使用 AI 工具
    • 问题定位、flush 时机与测试场景经本人理解;实现与测试在 Cursor/AI 辅助下完成,提交前已阅读 diff。
    • AI-assisted notes / AI 使用说明:AI 辅助编写 editor.ts 改动与 editor.test.ts 回归用例;设计决策(仅 silent 路径 flush、复用现有 flushScheduledChangeNow)由贡献者确认。
  • New dependencies: none
  • No build artifacts committed
  • No secrets / .env committed

Checklist / 自检清单

  • Title follows Conventional Commits
  • Public API changes — 无公共 API 变更,无需改 README
  • Touched live-preview-table.ts — 未修改
  • New capability / breaking change — 不适用,无 OpenSpec
  • Change aligns with project scope (GOVERNANCE.md §4) — core 引擎 bug fix

Screenshots / Recordings · 截图或录屏 (UI changes)

N/A — 无可见 UI 变更。

When parseDelayMs is enabled, a pending onChange timer could fire after a
silent setDocument or replaceRange and emit stale Markdown against the
new document. Flush the scheduled change first so hosts see the last user
edit once and AST stays aligned with the loaded buffer.

Co-authored-by: Cursor <cursoragent@cursor.com>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
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