diff --git a/manifest.json b/manifest.json index 42b416d..fed0bf0 100644 --- a/manifest.json +++ b/manifest.json @@ -8,4 +8,4 @@ "fundingUrl": "https://www.buymeacoffee.com/turulix", "isDesktopOnly": false, "version": "1.0.27" -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json index 3528d6e..8256fb5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-sample-plugin", - "version": "1.0.22", + "version": "1.0.27", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "obsidian-sample-plugin", - "version": "1.0.22", + "version": "1.0.27", "license": "MIT", "devDependencies": { "@types/node": "16.11.6", diff --git a/package.json b/package.json index d7a210a..0206ff1 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,9 @@ "@typescript-eslint/parser": "5.2.0", "builtin-modules": "3.2.0", "esbuild": "0.13.12", + "eslint": "8.14.0", "obsidian": "latest", "tslib": "2.3.1", - "typescript": "4.4.4", - "eslint": "8.14.0" + "typescript": "4.4.4" } } diff --git a/src/main.ts b/src/main.ts index b728beb..dcb4515 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,10 @@ import {App, Plugin, PluginManifest} from 'obsidian'; import {IndexContentProcessorModule} from "./modules/IndexContentProcessorModule"; import {GraphManipulatorModule} from "./modules/GraphManipulatorModule"; -import {EventEmitter} from "events"; import {DEFAULT_SETTINGS, PluginSetting, PluginSettingsTab} from "./models/PluginSettingsTab"; import {FolderNoteModule} from "./modules/FolderNoteModule"; import {ContextMenuModule} from "./modules/ContextMenuModule"; - +import CustomEventTarget from "./modules/CustomEventTarget"; // Remember to rename these classes and interfaces! export default class FolderIndexPlugin extends Plugin { @@ -30,7 +29,7 @@ export default class FolderIndexPlugin extends Plugin { async onload() { // eslint-disable-next-line no-console console.log("Loading FolderTableContent") - this.eventManager = new EventEmitter() + this.eventManager = new CustomEventTarget() await this.loadSettings(); diff --git a/src/modules/CustomEventTarget.ts b/src/modules/CustomEventTarget.ts new file mode 100644 index 0000000..ae97e03 --- /dev/null +++ b/src/modules/CustomEventTarget.ts @@ -0,0 +1,46 @@ +export default class CustomEventTarget extends EventTarget { + // Map to track all listeners for each event name + private listenersMap: Map = new Map(); + + emit(eventName: string, detail?: T): void { + const event = new CustomEvent(eventName, {detail}); + this.dispatchEvent(event); + } + + on(eventName: string, callback: (event: CustomEvent) => void): void { + // @ts-ignore + this.addEventListener(eventName, callback); + + // Track the listener in our map + const listeners = this.listenersMap.get(eventName) || []; + // @ts-ignore + listeners.push(callback); + this.listenersMap.set(eventName, listeners); + } + + removeAllListeners(): void { + for (const [eventName, listeners] of this.listenersMap.entries()) { + for (const listener of listeners) { + this.removeEventListener(eventName, listener); + } + } + + this.listenersMap.clear(); + } + + off(eventName: string, callback: (event: CustomEvent) => void): void { + const listeners = this.listenersMap.get(eventName); + // remove the listener we set to "off" + if (listeners) { + // @ts-ignore + const listenerIndex = listeners.indexOf(callback); + if (listenerIndex !== -1) { + // @ts-ignore + this.removeEventListener(eventName, callback); + + listeners.splice(listenerIndex, 1); + this.listenersMap.set(eventName, listeners); + } + } + } +}