let register() handle the teardown

This commit is contained in:
bdeshi committed 2026-08-02 20:03:55 +06:00
1 parent 92c7a27dc2
commit d3fb5b4383
1 file changed
+16 -19
+16 -19
View File
@@ -4,32 +4,23 @@ module.exports = class RibbonClasp extends Plugin {
async onload() { async onload() {
this.settings = Object.assign({ order: [] }, await this.loadData()); this.settings = Object.assign({ order: [] }, await this.loadData());
this.enforcing = false; this.enforcing = false;
this.alive = true;
this.register(() => { this.alive = false; });
// icons can attach within milliseconds of each other on startup // icons can attach within milliseconds of each other on startup
// (resetTimer, or it fires 200ms after the first one instead of the last) // (resetTimer, or it fires 200ms after the first one instead of the last)
this.debouncedEnforce = debounce(() => this.enforceOrder(), 200, true); this.debouncedEnforce = debounce(() => this.enforceOrder(), 200, true);
// disconnecting the observer doesn't stop an already-scheduled call
this.register(() => this.debouncedEnforce.cancel());
this.app.workspace.onLayoutReady(() => { this.app.workspace.onLayoutReady(() => {
// not tied to our lifecycle; still fires if we were disabled during boot // not tied to our lifecycle; still fires if we were disabled during boot
if (this.unloaded) return; if (!this.alive) return;
this.patchRibbon(); this.patchRibbon();
this.enforceOrder(); this.enforceOrder();
this.observeRibbon(); this.observeRibbon();
}); });
} }
onunload() {
this.unloaded = true;
this.observer?.disconnect();
// disconnect doesn't stop an already-scheduled call
this.debouncedEnforce?.cancel();
const ribbon = this.getRibbon();
// don't restore over someone else's patch, just go inert instead
if (ribbon && ribbon.onChange === this.wrapper) {
ribbon.onChange = this.originalOnChange;
}
}
getRibbon() { getRibbon() {
return this.app.workspace.leftRibbon ?? null; return this.app.workspace.leftRibbon ?? null;
} }
@@ -44,8 +35,9 @@ module.exports = class RibbonClasp extends Plugin {
const containerEl = this.getRibbonContainerEl(ribbon); const containerEl = this.getRibbonContainerEl(ribbon);
if (!containerEl) return; if (!containerEl) return;
this.observer = new MutationObserver(() => this.debouncedEnforce()); const observer = new MutationObserver(() => this.debouncedEnforce());
this.observer.observe(containerEl, { childList: true }); observer.observe(containerEl, { childList: true });
this.register(() => observer.disconnect());
} }
// hook the ribbon's own onChange so drags and hide/show toggles update // hook the ribbon's own onChange so drags and hide/show toggles update
@@ -57,13 +49,18 @@ module.exports = class RibbonClasp extends Plugin {
const original = ribbon.onChange.bind(ribbon); const original = ribbon.onChange.bind(ribbon);
this.originalOnChange = original; this.originalOnChange = original;
this.wrapper = (save) => { const wrapper = (save) => {
original(save); original(save);
if (save && !this.enforcing && !this.unloaded) { if (save && !this.enforcing && this.alive) {
this.recordOrder(ribbon); this.recordOrder(ribbon);
} }
}; };
ribbon.onChange = this.wrapper; ribbon.onChange = wrapper;
this.register(() => {
// don't restore over someone else's patch, just go inert instead
if (ribbon.onChange === wrapper) ribbon.onChange = original;
});
} }
enforceOrder() { enforceOrder() {