Skip to content

Commit a65c5e3

Browse files
committed
feat: migrate to Broadcast Channel API
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
1 parent f0e9195 commit a65c5e3

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

lib/SimpleBus.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ import { EventHandler } from "./EventHandler.js";
55
export class SimpleBus implements EventBus {
66

77
private handlers = new Map<string, EventHandler[]>();
8+
private channel: BroadcastChannel;
9+
10+
constructor() {
11+
this.channel = new BroadcastChannel('nextcloud')
12+
13+
console.debug('SimpleBus 2 created', this)
14+
// Global broadcast channel
15+
this.channel.onmessage = (event) => {
16+
(this.handlers.get(event.data.name) || []).forEach(h => {
17+
try {
18+
h(event.data.event)
19+
} catch (e) {
20+
console.error('could not invoke event listener', e)
21+
}
22+
})
23+
}
24+
}
825

926
getVersion(): string {
1027
return globalThis.__pkg_version;
@@ -19,13 +36,12 @@ export class SimpleBus implements EventBus {
1936
}
2037

2138
emit(name: string, event: Event): void {
22-
(this.handlers.get(name) || []).forEach(h => {
23-
try {
24-
h(event)
25-
} catch (e) {
26-
console.error('could not invoke event listener', e)
27-
}
28-
})
39+
try {
40+
const data = { name, event }
41+
console.debug('Bus emit', data);
42+
this.channel.postMessage(data);
43+
} catch (e) {
44+
console.error('could not emit event', e)
45+
}
2946
}
30-
3147
}

0 commit comments

Comments
 (0)