Skip to content

Commit ea3207e

Browse files
authored
Merge pull request #3394 from obsidian-tasks-group/fix-debounce-result-redraws
fix: Improve performance when multiple files are edited quickly
2 parents 9bf72dd + 8a9ef65 commit ea3207e

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Obsidian/Cache.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import type { CachedMetadata, EventRef, HeadingCache, ListItemCache, SectionCache, Workspace } from 'obsidian';
1+
import {
2+
type CachedMetadata,
3+
type EventRef,
4+
type HeadingCache,
5+
type ListItemCache,
6+
type SectionCache,
7+
type Workspace,
8+
debounce,
9+
} from 'obsidian';
210
import { MetadataCache, Notice, TAbstractFile, TFile, Vault } from 'obsidian';
311
import { Mutex } from 'async-mutex';
412
import { TasksFile } from '../Scripting/TasksFile';
@@ -33,6 +41,12 @@ export class Cache {
3341
private state: State;
3442
private tasks: Task[];
3543

44+
private readonly notifySubscribersDebounced = debounce(
45+
() => this.notifySubscribersNotDebounced(),
46+
100, // Long enough to prevent successive redraws slowing performance; short enough for edits via context menus to appear snappy
47+
true,
48+
);
49+
3650
/**
3751
* We cannot know if this class will be instantiated because obsidian started
3852
* or because the plugin was activated later. This means we have to load the
@@ -111,6 +125,11 @@ export class Cache {
111125

112126
private notifySubscribers(): void {
113127
this.logger.debug('Cache.notifySubscribers()');
128+
this.notifySubscribersDebounced();
129+
}
130+
131+
private notifySubscribersNotDebounced(): void {
132+
this.logger.debug('Cache.notifySubscribersNotDebounced()');
114133
this.events.triggerCacheUpdate({
115134
tasks: this.tasks,
116135
state: this.state,

0 commit comments

Comments
 (0)