mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-15 11:38:06 +01:00
This commit is contained in:
parent
9bbfd2a3e4
commit
8fe520f4af
8 changed files with 67 additions and 8 deletions
23
app/src/plugin/EventBus.ts
Normal file
23
app/src/plugin/EventBus.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
export class EventBus<DetailType = any> {
|
||||
private eventTarget: EventTarget;
|
||||
|
||||
constructor(name = "") {
|
||||
this.eventTarget = document.appendChild(document.createComment(name));
|
||||
}
|
||||
|
||||
on(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
|
||||
this.eventTarget.addEventListener(type, listener);
|
||||
}
|
||||
|
||||
once(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
|
||||
this.eventTarget.addEventListener(type, listener, {once: true});
|
||||
}
|
||||
|
||||
off(type: TEventBus, listener: (event: CustomEvent<DetailType>) => void) {
|
||||
this.eventTarget.removeEventListener(type, listener);
|
||||
}
|
||||
|
||||
emit(type: TEventBus, detail?: DetailType) {
|
||||
return this.eventTarget.dispatchEvent(new CustomEvent(type, {detail}));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue