1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-11-22 15:36:10 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee
Files
react-native-update/harmony/pushy/src/main/ets/EventHub.ts
波仔糕 18d9b75545 update pushy reference method (#499)
* update pushy reference method

* update
2025-05-12 14:23:45 +08:00

39 lines
926 B
TypeScript

type EventCallback = (data: any) => void;
export class EventHub {
private static instance: EventHub;
private listeners: Map<string, Set<EventCallback>>;
private rnInstance: any;
private constructor() {
this.listeners = new Map();
}
public static getInstance(): EventHub {
if (!EventHub.instance) {
EventHub.instance = new EventHub();
}
return EventHub.instance;
}
public on(event: string, callback: EventCallback): void {
if (!this.listeners.has(event)) {
this.listeners.set(event, new Set());
}
this.listeners.get(event)?.add(callback);
}
public off(event: string, callback: EventCallback): void {
this.listeners.get(event)?.delete(callback);
}
public emit(event: string, data: any): void {
if (this.rnInstance) {
this.rnInstance.emitDeviceEvent(event, data);
}
}
setRNInstance(instance: any) {
this.rnInstance = instance;
}
}