generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from Load-Ma/fix/IOS
fix(IOS-compatibility): replacing events package by native EventTarge…
- Loading branch information
Showing
5 changed files
with
53 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
"fundingUrl": "https://www.buymeacoffee.com/turulix", | ||
"isDesktopOnly": false, | ||
"version": "1.0.27" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |