debounce the mutation observer

This commit is contained in:
2026-07-04 18:35:08 +06:00
parent b5f2c62676
commit 1dbb3e8ff2
+12 -1
View File
@@ -1,8 +1,19 @@
const { Plugin } = require('obsidian');
// a bunch of ribbon icons can attach within milliseconds of each other on
// startup, so debounce instead of re-checking the order on every single one
function debounce(fn, wait) {
let timer = null;
return () => {
clearTimeout(timer);
timer = setTimeout(fn, wait);
};
}
module.exports = class RibbonOrderLock extends Plugin {
async onload() {
this.settings = Object.assign({ order: [] }, await this.loadData());
this.debouncedEnforce = debounce(() => this.enforceOrder(), 200);
this.app.workspace.onLayoutReady(() => {
this.patchRibbon();
@@ -15,7 +26,7 @@ module.exports = class RibbonOrderLock extends Plugin {
const ribbon = this.app.workspace.leftRibbon;
const containerEl = ribbon.ribbonItemsEl;
this.observer = new MutationObserver(() => this.enforceOrder());
this.observer = new MutationObserver(() => this.debouncedEnforce());
this.observer.observe(containerEl, { childList: true });
}