UNPKG

@aire-ux/aire-condensation

Version:

Client-side serialization library for Aire-UX

46 lines (45 loc) 1.6 kB
export default class TypeRegistry { constructor() { this.types = new Map(); } register(type) { if (!this.types.has(type)) { this.types.set(type, { alias: type.name }); } } contains(type) { return this.types.has(type); } configure(type, cfg) { let [t, registration] = this.check(type); registration = Object.assign({}, cfg); this.types.set(t, registration); } resolveConfiguration(type) { const [_, registration] = this.check(type); return registration; } check(type) { const toConfigure = this.types.get(type); if (!toConfigure) { throw new Error(`Error: Attempting to configure type ${type}, but it has not been registered. Have you annotated ${type} with '@RootElement'?`); } return [type, toConfigure]; } defineProperty(target, propertyName, configuration) { const [type, registration] = this.check(target); registration.properties = registration.properties || new Map(); registration.properties.set(propertyName, readPropertyDefinition(propertyName, configuration)); } } function readPropertyDefinition(name, cfg) { var _a, _b; return { type: cfg.type, realName: name, readAlias: ((_a = cfg === null || cfg === void 0 ? void 0 : cfg.read) === null || _a === void 0 ? void 0 : _a.alias) || name, writeAlias: ((_b = cfg === null || cfg === void 0 ? void 0 : cfg.write) === null || _b === void 0 ? void 0 : _b.alias) || name, }; }