import Log from 'sap/base/Log'; import type { ExternalAction } from '@sap-ux-private/control-property-editor-common'; import { startPostMessageCommunication } from '@sap-ux-private/control-property-editor-common'; import { getError } from '../utils/error.js'; import type { ActionHandler } from './types.js'; export class CommunicationService { /** * Sends an action to the CPE. */ static sendAction: (action: ExternalAction) => void; private static readonly actionHandlers: ActionHandler[] = []; static { const { sendAction } = startPostMessageCommunication( window.parent, async (action: ExternalAction) => { for (const handler of this.actionHandlers) { try { await handler(action); } catch (error) { Log.error('Handler Failed: ', getError(error)); } } } ); this.sendAction = sendAction; } /** * Creates a listener to receive actions from the CPE. * * @param handler - Action handler. */ static subscribe(handler: ActionHandler): void { this.actionHandlers.push(handler); } }