@opra/common
Version:
Opra common package
76 lines (75 loc) • 2.47 kB
JavaScript
import { omitUndefined } from '@jsopen/objects';
import { asMutable } from 'ts-gems';
import { ResponsiveMap } from '../../helpers/index.js';
import { OpraSchema } from '../../schema/index.js';
import { DataTypeMap } from '../common/data-type-map.js';
import { DocumentElement } from '../common/document-element.js';
import { CLASS_NAME_PATTERN, DECORATOR, kDataTypeMap } from '../constants.js';
import { WSControllerDecoratorFactory } from '../decorators/ws-controller.decorator.js';
import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom, } from '../utils/inspect.util.js';
/**
* WSController
*/
export const WSController = function (...args) {
// ClassDecorator
if (!this)
return WSController[DECORATOR].apply(undefined, args);
// Constructor
const [owner, initArgs] = args;
DocumentElement.call(this, owner);
if (!CLASS_NAME_PATTERN.test(initArgs.name))
throw new TypeError(`Invalid resource name (${initArgs.name})`);
const _this = asMutable(this);
_this.kind = OpraSchema.WSController.Kind;
_this.types = _this.node[kDataTypeMap] = new DataTypeMap();
_this.operations = new ResponsiveMap();
_this.name = initArgs.name;
_this.description = initArgs.description;
_this.instance = initArgs.instance;
_this.ctor = initArgs.ctor;
_this._controllerReverseMap = new WeakMap();
_this._initialize?.(initArgs);
};
/**
*
* @class WSController
*/
class WSControllerClass extends DocumentElement {
/**
*
*/
toString() {
return `[WSController ${this.name}]`;
}
/**
*
*/
toJSON() {
const out = omitUndefined({
kind: this.kind,
description: this.description,
});
if (this.operations.size) {
out.operations = {};
for (const v of this.operations.values()) {
out.operations[v.name] = v.toJSON();
}
}
if (this.types.size) {
out.types = {};
for (const v of this.types.values()) {
out.types[v.name] = v.toJSON();
}
}
return out;
}
/**
*
*/
[nodeInspectCustom]() {
return `[${colorFgYellow}WSController${colorFgMagenta + this.name + colorReset}]`;
}
}
WSController.prototype = WSControllerClass.prototype;
Object.assign(WSController, WSControllerDecoratorFactory);
WSController[DECORATOR] = WSControllerDecoratorFactory;