From 3e9508a74fa0599bfabfe07af66a574760c66696 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Tue, 21 May 2024 10:24:09 -0600 Subject: [PATCH 1/3] Call deconstructors on plugin unload --- main.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index 0d2a98d..f4f1eee 100644 --- a/main.ts +++ b/main.ts @@ -88,7 +88,8 @@ export default class CustomJS extends Plugin { } } - onunload() { + async onunload() { + await this.deconstructLoadedFiles(); delete window.customJS; } From 17cf555f74491eec4ffb6a5d3b2b20caca603315 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Tue, 21 May 2024 10:45:27 -0600 Subject: [PATCH 2/3] Fix linter --- main.ts | 5 ++++- types.d.ts | 18 +++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/main.ts b/main.ts index f4f1eee..cd7f7cc 100644 --- a/main.ts +++ b/main.ts @@ -11,6 +11,7 @@ import { import * as obsidian from 'obsidian'; import compareVersions from 'compare-versions'; import debuggableEval from 'debuggable-eval'; +import { CustomJSType } from './types'; interface CustomJSSettings { jsFiles: string; @@ -52,7 +53,9 @@ export default class CustomJS extends Plugin { await this.initCustomJS(); }; - window.cJS = async (moduleOrCallback?: string|Function) => { + window.cJS = async ( + moduleOrCallback?: string | ((customJS: CustomJSType) => void), + ) => { if (!window.customJS?.state?._ready) { await this.initCustomJS(); } diff --git a/types.d.ts b/types.d.ts index b04a86f..4dbf095 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1,16 +1,20 @@ import * as obsidian from 'obsidian'; import { DataviewAPI } from 'obsidian-dataview'; +export type CustomJSType = { + obsidian?: typeof obsidian; + app?: obsidian.App; + state?: Record; + [scriptName: string]: unknown; +}; + declare global { interface Window { forceLoadCustomJS?: () => Promise; - cJS?: (moduleOrCallback?: string|Function) => Promise; - customJS?: { - obsidian?: typeof obsidian; - app?: obsidian.App; - state?: Record; - [scriptName: string]: unknown; - }; + cJS?: ( + moduleOrCallback?: string | ((customJS: CustomJSType) => void), + ) => Promise; + customJS?: CustomJSType; } } From 1c3fcb93c2947b2fe428ac0f6f2ba67ba6002ef8 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Tue, 21 May 2024 10:47:46 -0600 Subject: [PATCH 3/3] Fix linter --- main.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.ts b/main.ts index cd7f7cc..e623c35 100644 --- a/main.ts +++ b/main.ts @@ -41,7 +41,7 @@ export default class CustomJS extends Plugin { settings: CustomJSSettings; deconstructorsOfLoadedFiles: { deconstructor: () => void; name: string }[] = []; - loaderPromise: Promise|null = null; + loaderPromise: Promise | null = null; async onload() { // eslint-disable-next-line no-console @@ -59,7 +59,7 @@ export default class CustomJS extends Plugin { if (!window.customJS?.state?._ready) { await this.initCustomJS(); } - + if (moduleOrCallback) { if ('string' === typeof moduleOrCallback) { return window.customJS[moduleOrCallback]; @@ -229,7 +229,7 @@ export default class CustomJS extends Plugin { this.loaderPromise = null; }); } - + await this.loaderPromise; }