Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(IOS-compatibility): replacing events package by native EventTarge… #92

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"fundingUrl": "https://www.buymeacoffee.com/turulix",
"isDesktopOnly": false,
"version": "1.0.27"
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
5 changes: 2 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();

Expand Down
46 changes: 46 additions & 0 deletions src/modules/CustomEventTarget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export default class CustomEventTarget extends EventTarget {
// Map to track all listeners for each event name
private listenersMap: Map<string, EventListener[]> = new Map();

emit<T = any>(eventName: string, detail?: T): void {
const event = new CustomEvent<T>(eventName, {detail});
this.dispatchEvent(event);
}

on<T = any>(eventName: string, callback: (event: CustomEvent<T>) => 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<T = any>(eventName: string, callback: (event: CustomEvent<T>) => 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);
}
}
}
}
Loading