UNPKG

@opra/common

Version:

Opra common package

52 lines (51 loc) 1.65 kB
import { ResponsiveMap } from '../../helpers/index.js'; import { OpraSchema } from '../../schema/index.js'; import { ApiBase } from '../common/api-base.js'; import { WSController } from './ws-controller.js'; import { WSOperation } from './ws-operation.js'; /** * @class WSApi */ export class WSApi extends ApiBase { // noinspection JSUnusedGlobalSymbols _controllerReverseMap = new WeakMap(); transport = 'ws'; controllers = new ResponsiveMap(); constructor(init) { super(init); } findController(arg0) { if (typeof arg0 === 'function') { /* Check for cached mapping */ const controller = this._controllerReverseMap.get(arg0); if (controller != null) return controller; /* Lookup for ctor in all controllers */ for (const c of this.controllers.values()) { if (c.ctor === arg0) { this._controllerReverseMap.set(arg0, c); return c; } } this._controllerReverseMap.set(arg0, null); return; } return this.controllers.get(arg0); } findOperation(arg0, operationName) { const controller = this.findController(arg0); return controller?.operations.get(operationName); } toJSON() { const schema = super.toJSON(); const out = { ...schema, transport: this.transport, controllers: {}, }; for (const v of this.controllers.values()) { out.controllers[v.name] = v.toJSON(); } return out; } }