import type { ActionInstance } from './ActionInstance'; import type { SpecialProperty } from './SpecialProperty'; import { getContext } from './utils/createActionProxy'; import { SpecialPropertyMessage } from '../messages/SpecialPropertyMessage'; import { SpecialPropertyOnlyClientMethodError } from './SpecialPropertyOnlyClientMethodError'; export class Event implements SpecialProperty { public propertyName!: string; public meta = { typeName: 'event' }; public action!: ActionInstance; public async emit(data: T) { const context = getContext(this.action); await context.controller.send( this.createMessage(data), ); } // eslint-disable-next-line @typescript-eslint/no-unused-vars public subscribe(cb: (data: T) => void) { throw new SpecialPropertyOnlyClientMethodError(); } // eslint-disable-next-line @typescript-eslint/no-unused-vars public unsubscribe(cb: (data: T) => void): boolean { throw new SpecialPropertyOnlyClientMethodError(); } public unsubscribeAll() { throw new SpecialPropertyOnlyClientMethodError(); } protected createMessage(data: T): SpecialPropertyMessage { const context = getContext(this.action); const instanceId = context.getInstanceId(); return { type: 'special', instanceId, actionName: context.actionBox.name, propertyName: this.propertyName, data, }; } }