Skip to content

Commit eb08991

Browse files
committed
#205 - recursive monkey-patching bug
- renames and refactoring for better code readability
1 parent 1106224 commit eb08991

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

src/main.ts

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

104-
uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller|undefined = undefined
104+
uninstallerOfFileExplorerPatch: MonkeyAroundUninstaller|undefined = undefined
105105

106106
showNotice(message: string, timeout?: number) {
107107
if (this.settings.notificationsEnabled || (Platform.isMobile && this.settings.mobileNotificationsEnabled)) {
@@ -238,7 +238,7 @@ export default class CustomSortPlugin
238238
// That's why not showing and not logging error message here
239239
if (patchableFileExplorer) {
240240
this.uninstallFileExplorerPatchIfInstalled()
241-
this.uninstallerOfFolderSortFunctionWrapper = around(patchableFileExplorer.view.constructor.prototype, {
241+
this.uninstallerOfFileExplorerPatch = around(patchableFileExplorer.view.constructor.prototype, {
242242
getSortedFolderItems(old: any) {
243243
return function (...args: any[]) {
244244
// quick check for plugin status
@@ -371,7 +371,7 @@ export default class CustomSortPlugin
371371

372372
this.registerCommands()
373373

374-
this.registerUninstallerHandler()
374+
this.registerPluginUnloadHandler()
375375

376376
this.initialize();
377377
}
@@ -571,30 +571,28 @@ export default class CustomSortPlugin
571571
}
572572

573573
uninstallFileExplorerPatchIfInstalled() {
574-
if (this.uninstallerOfFolderSortFunctionWrapper) {
574+
if (this.uninstallerOfFileExplorerPatch) {
575575
try {
576-
this.uninstallerOfFolderSortFunctionWrapper()
576+
this.uninstallerOfFileExplorerPatch()
577577
} catch {
578578

579579
}
580-
this.uninstallerOfFolderSortFunctionWrapper = undefined
580+
this.uninstallerOfFileExplorerPatch = undefined
581581
}
582582
}
583583

584-
registerUninstallerHandler() {
584+
registerPluginUnloadHandler() {
585585
let plugin = this;
586-
const requestStandardObsidianSortAfterFileExplorerPatchUninstaller = () => {
587-
return () => {
588-
plugin.uninstallFileExplorerPatchIfInstalled()
589586

590-
const fileExplorerOrError= this.checkFileExplorerIsAvailableAndPatchable()
591-
if (fileExplorerOrError.v && fileExplorerOrError.v.view) {
592-
fileExplorerOrError.v.view.requestSort?.()
593-
}
594-
}
595-
}
587+
this.register(() => {
588+
plugin.uninstallFileExplorerPatchIfInstalled()
596589

597-
this.register(requestStandardObsidianSortAfterFileExplorerPatchUninstaller())
590+
// Request standard File Explorer sorting to remove any custom sorting cached by File Explorer
591+
const fileExplorerOrError= plugin.checkFileExplorerIsAvailableAndPatchable()
592+
if (fileExplorerOrError.v && fileExplorerOrError.v.view) {
593+
fileExplorerOrError.v.view.requestSort?.()
594+
}
595+
})
598596
}
599597

600598
registerCommands() {

0 commit comments

Comments
 (0)