diff --git a/main.ts b/main.ts index 0d2a98d..e623c35 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; @@ -40,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 @@ -52,11 +53,13 @@ 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(); } - + if (moduleOrCallback) { if ('string' === typeof moduleOrCallback) { return window.customJS[moduleOrCallback]; @@ -88,7 +91,8 @@ export default class CustomJS extends Plugin { } } - onunload() { + async onunload() { + await this.deconstructLoadedFiles(); delete window.customJS; } @@ -225,7 +229,7 @@ export default class CustomJS extends Plugin { this.loaderPromise = null; }); } - + await this.loaderPromise; } 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; } }