import { PubSubServiceType } from '@unisphere/runtime'; import { UnisphereBaseService } from './unisphere-base-service'; import { UnisphereWorkspaceService } from './unisphere-workspace-service'; const PUB_SUB_SERVICE_KEY = 'unisphere.service.pub-sub'; class UnisphereEventService extends UnisphereBaseService { public static subscribe = (event: string, callback: (data: { payload: { [key: string]: unknown } }) => void): number => { const workspace = UnisphereWorkspaceService.get(); const pubSubService = workspace?.getService(PUB_SUB_SERVICE_KEY); if (pubSubService) { const unsubscribe = pubSubService!.subscribe(event, callback); return UnisphereEventService.addToUnsubscribeMap(unsubscribe); } return -1; }; public static emit = (event: string, payload: Record): void => { const workspace = UnisphereWorkspaceService.get(); const pubSubService = workspace?.getService(PUB_SUB_SERVICE_KEY); pubSubService?.emit({ id: event, version: '1.0.0', payload }); }; public static destroy(): void { UnisphereEventService.unsubscribeAll(); } } export { UnisphereEventService };