#205 - recursive monkey-patching bug

- fix addressing the bug
This commit is contained in:
SebastianMC 2025-05-07 15:14:29 +02:00
parent f6f1d9b687
commit 110622415e
1 changed files with 33 additions and 12 deletions

View File

@ -101,6 +101,8 @@ export default class CustomSortPlugin
sortSpecCache?: SortSpecsCollection | null sortSpecCache?: SortSpecsCollection | null
customSortAppliedAtLeastOnce: boolean = false customSortAppliedAtLeastOnce: boolean = false
uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller|undefined = undefined
showNotice(message: string, timeout?: number) { showNotice(message: string, timeout?: number) {
if (this.settings.notificationsEnabled || (Platform.isMobile && this.settings.mobileNotificationsEnabled)) { if (this.settings.notificationsEnabled || (Platform.isMobile && this.settings.mobileNotificationsEnabled)) {
new Notice(message, timeout) 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 // For the idea of monkey-patching credits go to https://github.com/nothingislost/obsidian-bartender
patchFileExplorer(patchableFileExplorer: FileExplorerLeaf): FileExplorerLeaf|undefined { patchFileExplorer(patchableFileExplorer: FileExplorerLeaf): FileExplorerLeaf|undefined {
let plugin = this; 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. // patching file explorer might fail here because of various non-error reasons.
// That's why not showing and not logging error message here // That's why not showing and not logging error message here
if (patchableFileExplorer) { if (patchableFileExplorer) {
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.view.constructor.prototype, { this.uninstallFileExplorerPatchIfInstalled()
this.uninstallerOfFolderSortFunctionWrapper = around(patchableFileExplorer.view.constructor.prototype, {
getSortedFolderItems(old: any) { getSortedFolderItems(old: any) {
return function (...args: any[]) { return function (...args: any[]) {
// quick check for plugin status // quick check for plugin status
@ -272,7 +265,6 @@ export default class CustomSortPlugin
}; };
} }
}) })
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
return patchableFileExplorer return patchableFileExplorer
} else { } else {
return undefined return undefined
@ -379,6 +371,8 @@ export default class CustomSortPlugin
this.registerCommands() this.registerCommands()
this.registerUninstallerHandler()
this.initialize(); 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() { registerCommands() {
const plugin: CustomSortPlugin = this const plugin: CustomSortPlugin = this
this.addCommand({ this.addCommand({