Skip to content

Commit acfa39c

Browse files
authored
fix(chat): stop the context row re-laying out on every frame (#59)
* fix(chat): stop the context row re-laying out on every frame ContextRowOverflowController observes class changes on the context row and ends every layout pass with rowEl.toggleClass(expanded). Obsidian's toggleClass calls classList.add or remove unconditionally, which rewrites the class attribute even when nothing changes, so each pass queued a mutation record that scheduled the next pass. Every chat tab ran this loop once per display frame while Obsidian was visible. The renderer stayed busy, and the cost grew with the number of tabs and with attached note chips. Write the expanded class only when it changes, drop the records a layout pass produced itself, and cancel the pending frame on destroy. The test double for toggleClass now writes unconditionally like Obsidian, and new tests check that a blank or collapsed row stops scheduling frames once it settles and that destroy cancels the pending frame. Add AGENTS.md with this class-helper sharp edge, plus the CLAUDE.md pointer. * no-mistakes(review): Drop agent memory files, explain takeRecords purpose
1 parent 7acdd3d commit acfa39c

3 files changed

Lines changed: 88 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ version with its date and start a fresh empty `[Unreleased]` above it.
1111

1212
## [Unreleased]
1313

14+
### Fixed
15+
16+
- Qoderian tabs no longer keep Obsidian's renderer busy while idle. The
17+
context row above the composer re-checked its chip layout on every display
18+
frame for as long as Obsidian was visible, once per open tab, and each frame
19+
did more work when a note was attached. The row now re-measures only when
20+
its chips or width change.
21+
1422
## [1.0.7] - 2026-09-02
1523

1624
### Added

‎src/features/chat/controllers/context-row-overflow.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class ContextRowOverflowController {
1111
private readonly measureEl: HTMLElement;
1212
private readonly resizeObserver: ResizeObserver;
1313
private readonly mutationObserver: MutationObserver;
14-
private layoutScheduled = false;
14+
private layoutFrame: number | null = null;
1515
private expanded = false;
1616
private destroyed = false;
1717

@@ -53,21 +53,37 @@ export class ContextRowOverflowController {
5353

5454
destroy(): void {
5555
this.destroyed = true;
56+
if (this.layoutFrame !== null) {
57+
window.cancelAnimationFrame(this.layoutFrame);
58+
this.layoutFrame = null;
59+
}
5660
this.resizeObserver.disconnect();
5761
this.mutationObserver.disconnect();
5862
this.pillEl.remove();
5963
this.measureEl.remove();
6064
}
6165

6266
private scheduleLayout(): void {
63-
if (this.layoutScheduled) return;
64-
this.layoutScheduled = true;
65-
window.requestAnimationFrame(() => {
66-
this.layoutScheduled = false;
67+
if (this.layoutFrame !== null) return;
68+
this.layoutFrame = window.requestAnimationFrame(() => {
69+
this.layoutFrame = null;
6770
if (!this.destroyed) this.layout();
6871
});
6972
}
7073

74+
private layout(): void {
75+
this.applyLayout();
76+
// Obsidian's class helpers rewrite the class attribute even when the class
77+
// does not change, and the observer above reports every such write, so
78+
// applyState checks each class before writing it. Records still queued
79+
// here describe DOM this pass has already read; dropping them saves a
80+
// second clone-and-measure pass a frame after every real change, so the
81+
// row settles in one frame. Either the class checks or this call alone
82+
// stops layout rescheduling itself on every frame, and the regression
83+
// tests cover only the two together.
84+
this.mutationObserver.takeRecords();
85+
}
86+
7187
/** Content items are row children that are currently meant to be visible. */
7288
private contentItems(): HTMLElement[] {
7389
return Array.from(this.rowEl.children).filter(
@@ -76,7 +92,7 @@ export class ContextRowOverflowController {
7692
);
7793
}
7894

79-
private layout(): void {
95+
private applyLayout(): void {
8096
const items = this.contentItems();
8197

8298
if (items.length === 0 || !this.rowEl.hasClass('has-content')) {
@@ -174,7 +190,9 @@ export class ContextRowOverflowController {
174190
}
175191
});
176192

177-
this.rowEl.toggleClass('qoderian-context-row--expanded', this.expanded);
193+
if (this.expanded !== this.rowEl.hasClass('qoderian-context-row--expanded')) {
194+
this.rowEl.toggleClass('qoderian-context-row--expanded', this.expanded);
195+
}
178196

179197
const hiddenCount = items.length - (this.expanded ? items.length : visibleCount);
180198
const showPill = this.expanded || hiddenCount > 0;

‎tests/unit/features/chat/controllers/context-row-overflow.test.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ function installDomMocks(): void {
4646
};
4747
}
4848
if (!proto.toggleClass) {
49+
// Mirrors Obsidian: toggleClass always calls classList.add or remove, which
50+
// rewrites the class attribute and queues a MutationObserver record even
51+
// when nothing changes. classList.toggle(cls, force) skips that write and
52+
// would hide an observer that keeps re-triggering itself.
4953
proto.toggleClass = function toggleClass(this: HTMLElement, cls: string, force: boolean) {
50-
this.classList.toggle(cls, force);
54+
if (force) this.classList.add(cls);
55+
else this.classList.remove(cls);
5156
return this;
5257
};
5358
}
@@ -370,4 +375,53 @@ describe('ContextRowOverflowController', () => {
370375

371376
controller.destroy();
372377
});
378+
379+
it('stops scheduling layout frames once a blank row settles', async () => {
380+
// A new tab starts with nothing attached, so its row has no content.
381+
const row = createRow();
382+
row.removeClass('has-content');
383+
384+
const controller = new ContextRowOverflowController(row);
385+
await settle();
386+
387+
const requestFrame = jest.spyOn(window, 'requestAnimationFrame');
388+
await new Promise(resolve => setTimeout(resolve, 200));
389+
expect(requestFrame).not.toHaveBeenCalled();
390+
391+
requestFrame.mockRestore();
392+
controller.destroy();
393+
});
394+
395+
it('stops scheduling layout frames once a collapsed row settles', async () => {
396+
const row = createRow();
397+
[createChip(100), createChip(100), createChip(100)].forEach(chip => row.appendChild(chip));
398+
rowClientWidth = 200;
399+
400+
const controller = new ContextRowOverflowController(row);
401+
await settle();
402+
const pill = row.querySelector('.qoderian-context-overflow-pill') as HTMLElement;
403+
expect(pill.hasClass('qoderian-hidden')).toBe(false);
404+
405+
const requestFrame = jest.spyOn(window, 'requestAnimationFrame');
406+
await new Promise(resolve => setTimeout(resolve, 200));
407+
expect(requestFrame).not.toHaveBeenCalled();
408+
409+
requestFrame.mockRestore();
410+
controller.destroy();
411+
});
412+
413+
it('cancels its pending layout frame on destroy', () => {
414+
const row = createRow();
415+
const requestFrame = jest.spyOn(window, 'requestAnimationFrame');
416+
const cancelFrame = jest.spyOn(window, 'cancelAnimationFrame');
417+
418+
const controller = new ContextRowOverflowController(row);
419+
const pendingFrame = requestFrame.mock.results[0]?.value as number;
420+
controller.destroy();
421+
422+
expect(cancelFrame).toHaveBeenCalledWith(pendingFrame);
423+
424+
requestFrame.mockRestore();
425+
cancelFrame.mockRestore();
426+
});
373427
});

0 commit comments

Comments
 (0)