Skip to content

Commit 1106224

Browse files
committed
#205 - recursive monkey-patching bug
- fix addressing the bug
1 parent f6f1d9b commit 1106224

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/main.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ export default class CustomSortPlugin
101101
sortSpecCache?: SortSpecsCollection | null
102102
customSortAppliedAtLeastOnce: boolean = false
103103

104+
uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller|undefined = undefined
105+
104106
showNotice(message: string, timeout?: number) {
105107
if (this.settings.notificationsEnabled || (Platform.isMobile && this.settings.mobileNotificationsEnabled)) {
106108
new Notice(message, timeout)
@@ -231,21 +233,12 @@ export default class CustomSortPlugin
231233
// For the idea of monkey-patching credits go to https://github.com/nothingislost/obsidian-bartender
232234
patchFileExplorer(patchableFileExplorer: FileExplorerLeaf): FileExplorerLeaf|undefined {
233235
let plugin = this;
234-
const requestStandardObsidianSortAfter = (patchUninstaller: MonkeyAroundUninstaller|undefined) => {
235-
return () => {
236-
if (patchUninstaller) patchUninstaller()
237-
238-
const fileExplorerOrError= this.checkFileExplorerIsAvailableAndPatchable()
239-
if (fileExplorerOrError.v && fileExplorerOrError.v.view) {
240-
fileExplorerOrError.v.view.requestSort?.()
241-
}
242-
}
243-
}
244236

245237
// patching file explorer might fail here because of various non-error reasons.
246238
// That's why not showing and not logging error message here
247239
if (patchableFileExplorer) {
248-
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.view.constructor.prototype, {
240+
this.uninstallFileExplorerPatchIfInstalled()
241+
this.uninstallerOfFolderSortFunctionWrapper = around(patchableFileExplorer.view.constructor.prototype, {
249242
getSortedFolderItems(old: any) {
250243
return function (...args: any[]) {
251244
// quick check for plugin status
@@ -272,7 +265,6 @@ export default class CustomSortPlugin
272265
};
273266
}
274267
})
275-
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
276268
return patchableFileExplorer
277269
} else {
278270
return undefined
@@ -379,6 +371,8 @@ export default class CustomSortPlugin
379371

380372
this.registerCommands()
381373

374+
this.registerUninstallerHandler()
375+
382376
this.initialize();
383377
}
384378

@@ -576,6 +570,33 @@ export default class CustomSortPlugin
576570
})
577571
}
578572

573+
uninstallFileExplorerPatchIfInstalled() {
574+
if (this.uninstallerOfFolderSortFunctionWrapper) {
575+
try {
576+
this.uninstallerOfFolderSortFunctionWrapper()
577+
} catch {
578+
579+
}
580+
this.uninstallerOfFolderSortFunctionWrapper = undefined
581+
}
582+
}
583+
584+
registerUninstallerHandler() {
585+
let plugin = this;
586+
const requestStandardObsidianSortAfterFileExplorerPatchUninstaller = () => {
587+
return () => {
588+
plugin.uninstallFileExplorerPatchIfInstalled()
589+
590+
const fileExplorerOrError= this.checkFileExplorerIsAvailableAndPatchable()
591+
if (fileExplorerOrError.v && fileExplorerOrError.v.view) {
592+
fileExplorerOrError.v.view.requestSort?.()
593+
}
594+
}
595+
}
596+
597+
this.register(requestStandardObsidianSortAfterFileExplorerPatchUninstaller())
598+
}
599+
579600
registerCommands() {
580601
const plugin: CustomSortPlugin = this
581602
this.addCommand({

0 commit comments

Comments
 (0)