import type { Server } from '../Server'; import type { Context } from '../Context'; import type { ActionConfig } from '../ActionConfig'; import type { Connection } from '../adapter/Connection'; import { Event } from '../Event'; import { action, method, property } from '../decorators'; import { ConnectionController } from '../ConnectionController'; import { getContext } from '../utils/createActionProxy'; import { ClientActionConfig } from '../../messages/ClientActionConfig'; @action({ permanent: true, maxInstances: 1, placeOfInit: 'client', momentOfInit: 'create', sendMetaOnCreate: true, }) export class Meta { public updated = new Event(); @property({ out: true, hooks: { onBeforeSend(value: any, propertyName: string, set: (v: any) => void, context: Context): Promise | void { const server = context.get('server'); const sendValue = [...server.actions.keys()]; set(sendValue); context.set(propertyName, sendValue); }, }, }) public actions: string[] = []; @property({ out: true, hooks: { async onBeforeSend(value: any, propertyName: string, set: (v: any) => void, context: Context): Promise { const controller = context.get('controller') as ConnectionController; const { server } = controller; const actions: { [name: string]: any } = {}; const all: Promise[] = []; controller.activeActionProxies.forEach((proxies, actionName) => { const actionBox = server.getActionBox(actionName); const { config } = actionBox; if (config.momentOfInit === 'connect') { const proxy = proxies.get('default'); all.push(getContext(proxy).getOutgoingContext().then((_context) => { actions[actionName] = { meta: config, context: _context }; })); } }); await Promise.all(all); set(actions); }, }, }) public createdOnConnectActions: { [actionName: string]: { meta: ClientActionConfig; context: any; } } = {}; @property({ get: 'server' }) public server!: Server; @property({ get: 'connection' }) public connection!: Connection; @property({ get: 'connection-controller' }) public controller!: ConnectionController; @method() public async getActionMeta(actionName: string): Promise<{ meta: ClientActionConfig; context: any }> { if (this.actions.indexOf(actionName) === -1) { throw new Error(`The ${actionName} action is not allow.`); } const ab = this.server.getActionBox(actionName); return { meta: ab.config as any, context: ab.initialContext, }; } public update() { } }