MCPcopy Create free account
hub / github.com/reactnativecn/react-native-update / EventHub

Class EventHub

harmony/pushy/src/main/ets/EventHub.ts:3–39  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1type EventCallback = (data: any) => void;
2
3export class EventHub {
4 private static instance: EventHub;
5 private listeners: Map<string, Set<EventCallback>>;
6 private rnInstance: any;
7
8 private constructor() {
9 this.listeners = new Map();
10 }
11
12 public static getInstance(): EventHub {
13 if (!EventHub.instance) {
14 EventHub.instance = new EventHub();
15 }
16 return EventHub.instance;
17 }
18
19 public on(event: string, callback: EventCallback): void {
20 if (!this.listeners.has(event)) {
21 this.listeners.set(event, new Set());
22 }
23 this.listeners.get(event)?.add(callback);
24 }
25
26 public off(event: string, callback: EventCallback): void {
27 this.listeners.get(event)?.delete(callback);
28 }
29
30 public emit(event: string, data: any): void {
31 if (this.rnInstance) {
32 this.rnInstance.emitDeviceEvent(event, data);
33 }
34 }
35
36 setRNInstance(instance: any) {
37 this.rnInstance = instance;
38 }
39}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected