From 110622415ee135c2491142b5dbf173b260681c42 Mon Sep 17 00:00:00 2001 From: SebastianMC <23032356+SebastianMC@users.noreply.github.com> Date: Wed, 7 May 2025 15:14:29 +0200 Subject: [PATCH] #205 - recursive monkey-patching bug - fix addressing the bug --- src/main.ts | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/main.ts b/src/main.ts index 86debae..bc80c5e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -101,6 +101,8 @@ export default class CustomSortPlugin sortSpecCache?: SortSpecsCollection | null customSortAppliedAtLeastOnce: boolean = false + uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller|undefined = undefined + showNotice(message: string, timeout?: number) { if (this.settings.notificationsEnabled || (Platform.isMobile && this.settings.mobileNotificationsEnabled)) { new Notice(message, timeout) @@ -231,21 +233,12 @@ export default class CustomSortPlugin // For the idea of monkey-patching credits go to https://github.com/nothingislost/obsidian-bartender patchFileExplorer(patchableFileExplorer: FileExplorerLeaf): FileExplorerLeaf|undefined { let plugin = this; - const requestStandardObsidianSortAfter = (patchUninstaller: MonkeyAroundUninstaller|undefined) => { - return () => { - if (patchUninstaller) patchUninstaller() - - const fileExplorerOrError= this.checkFileExplorerIsAvailableAndPatchable() - if (fileExplorerOrError.v && fileExplorerOrError.v.view) { - fileExplorerOrError.v.view.requestSort?.() - } - } - } // patching file explorer might fail here because of various non-error reasons. // That's why not showing and not logging error message here if (patchableFileExplorer) { - const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.view.constructor.prototype, { + this.uninstallFileExplorerPatchIfInstalled() + this.uninstallerOfFolderSortFunctionWrapper = around(patchableFileExplorer.view.constructor.prototype, { getSortedFolderItems(old: any) { return function (...args: any[]) { // quick check for plugin status @@ -272,7 +265,6 @@ export default class CustomSortPlugin }; } }) - this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper)) return patchableFileExplorer } else { return undefined @@ -379,6 +371,8 @@ export default class CustomSortPlugin this.registerCommands() + this.registerUninstallerHandler() + this.initialize(); } @@ -576,6 +570,33 @@ export default class CustomSortPlugin }) } + uninstallFileExplorerPatchIfInstalled() { + if (this.uninstallerOfFolderSortFunctionWrapper) { + try { + this.uninstallerOfFolderSortFunctionWrapper() + } catch { + + } + this.uninstallerOfFolderSortFunctionWrapper = undefined + } + } + + registerUninstallerHandler() { + let plugin = this; + const requestStandardObsidianSortAfterFileExplorerPatchUninstaller = () => { + return () => { + plugin.uninstallFileExplorerPatchIfInstalled() + + const fileExplorerOrError= this.checkFileExplorerIsAvailableAndPatchable() + if (fileExplorerOrError.v && fileExplorerOrError.v.view) { + fileExplorerOrError.v.view.requestSort?.() + } + } + } + + this.register(requestStandardObsidianSortAfterFileExplorerPatchUninstaller()) + } + registerCommands() { const plugin: CustomSortPlugin = this this.addCommand({