diff --git a/main.js b/main.js index c837b76..053020a 100644 --- a/main.js +++ b/main.js @@ -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 }); }