debounce the mutation observer
This commit is contained in:
@@ -1,8 +1,19 @@
|
|||||||
const { Plugin } = require('obsidian');
|
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 {
|
module.exports = class RibbonOrderLock extends Plugin {
|
||||||
async onload() {
|
async onload() {
|
||||||
this.settings = Object.assign({ order: [] }, await this.loadData());
|
this.settings = Object.assign({ order: [] }, await this.loadData());
|
||||||
|
this.debouncedEnforce = debounce(() => this.enforceOrder(), 200);
|
||||||
|
|
||||||
this.app.workspace.onLayoutReady(() => {
|
this.app.workspace.onLayoutReady(() => {
|
||||||
this.patchRibbon();
|
this.patchRibbon();
|
||||||
@@ -15,7 +26,7 @@ module.exports = class RibbonOrderLock extends Plugin {
|
|||||||
const ribbon = this.app.workspace.leftRibbon;
|
const ribbon = this.app.workspace.leftRibbon;
|
||||||
const containerEl = ribbon.ribbonItemsEl;
|
const containerEl = ribbon.ribbonItemsEl;
|
||||||
|
|
||||||
this.observer = new MutationObserver(() => this.enforceOrder());
|
this.observer = new MutationObserver(() => this.debouncedEnforce());
|
||||||
this.observer.observe(containerEl, { childList: true });
|
this.observer.observe(containerEl, { childList: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user