fix: cancel editor reveal frames (#3412)
This commit is contained in:
@@ -2012,6 +2012,7 @@ describe('createEditorSlice activateMarkdownLink', () => {
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
@@ -2207,6 +2208,52 @@ describe('createEditorSlice activateMarkdownLink', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('cancels superseded line-anchor reveal frames', async () => {
|
||||
const store = createEditorStore()
|
||||
pathExistsMock.mockResolvedValue(true)
|
||||
let nextFrameId = 1
|
||||
const pendingFrames = new Map<number, FrameRequestCallback>()
|
||||
const canceledFrameIds = new Set<number>()
|
||||
vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
|
||||
const frameId = nextFrameId++
|
||||
pendingFrames.set(frameId, callback)
|
||||
return frameId
|
||||
})
|
||||
vi.stubGlobal('cancelAnimationFrame', (frameId: number) => {
|
||||
canceledFrameIds.add(frameId)
|
||||
pendingFrames.delete(frameId)
|
||||
})
|
||||
|
||||
await store.getState().activateMarkdownLink('./first.md#L3', {
|
||||
sourceFilePath: '/repo/docs/note.md',
|
||||
worktreeId: 'wt-1',
|
||||
worktreeRoot: '/repo'
|
||||
})
|
||||
await store.getState().activateMarkdownLink('./second.md#L9', {
|
||||
sourceFilePath: '/repo/docs/note.md',
|
||||
worktreeId: 'wt-1',
|
||||
worktreeRoot: '/repo'
|
||||
})
|
||||
|
||||
expect(canceledFrameIds).toContain(1)
|
||||
while (pendingFrames.size > 0) {
|
||||
const nextPendingFrame = pendingFrames.entries().next()
|
||||
if (nextPendingFrame.done) {
|
||||
break
|
||||
}
|
||||
const [frameId, callback] = nextPendingFrame.value
|
||||
pendingFrames.delete(frameId)
|
||||
callback(0)
|
||||
}
|
||||
expect(store.getState().pendingEditorReveal).toEqual({
|
||||
filePath: '/repo/docs/second.md',
|
||||
fileId: '/repo/docs/second.md',
|
||||
line: 9,
|
||||
column: 1,
|
||||
matchLength: 0
|
||||
})
|
||||
})
|
||||
|
||||
it('reveals active-runtime markdown line anchors on the owner-qualified tab id', async () => {
|
||||
const store = createEditorStore()
|
||||
pathExistsMock.mockResolvedValue(true)
|
||||
|
||||
@@ -205,6 +205,36 @@ export type PendingEditorReveal = {
|
||||
matchLength: number
|
||||
}
|
||||
|
||||
const pendingEditorLineRevealFrameIds = new Set<number>()
|
||||
|
||||
function cancelPendingEditorLineRevealFrames(): void {
|
||||
if (typeof cancelAnimationFrame === 'function') {
|
||||
for (const frameId of pendingEditorLineRevealFrameIds) {
|
||||
cancelAnimationFrame(frameId)
|
||||
}
|
||||
}
|
||||
pendingEditorLineRevealFrameIds.clear()
|
||||
}
|
||||
|
||||
function trackEditorLineRevealFrameId(frameId: number): void {
|
||||
pendingEditorLineRevealFrameIds.add(frameId)
|
||||
}
|
||||
|
||||
function requestTrackedEditorLineRevealFrame(callback: FrameRequestCallback): void {
|
||||
let completed = false
|
||||
let frameId: number | undefined
|
||||
frameId = requestAnimationFrame((timestamp) => {
|
||||
completed = true
|
||||
if (frameId !== undefined) {
|
||||
pendingEditorLineRevealFrameIds.delete(frameId)
|
||||
}
|
||||
callback(timestamp)
|
||||
})
|
||||
if (!completed) {
|
||||
trackEditorLineRevealFrameId(frameId)
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleEditorLineReveal(
|
||||
get: () => AppState,
|
||||
filePath: string,
|
||||
@@ -214,9 +244,10 @@ function scheduleEditorLineReveal(
|
||||
): void {
|
||||
// Why: openFile can replace a preview and remount Monaco asynchronously; the
|
||||
// reveal must land after that remount or the old editor can clear it.
|
||||
cancelPendingEditorLineRevealFrames()
|
||||
get().setPendingEditorReveal(null)
|
||||
requestAnimationFrame(() => {
|
||||
requestAnimationFrame(() => {
|
||||
requestTrackedEditorLineRevealFrame(() => {
|
||||
requestTrackedEditorLineRevealFrame(() => {
|
||||
get().setPendingEditorReveal({
|
||||
filePath,
|
||||
fileId,
|
||||
|
||||
Reference in New Issue
Block a user