UNPKG

@visactor/vrender-core

Version:

```typescript import { xxx } from '@visactor/vrender-core'; ```

66 lines (54 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.createGraphic = exports.registerGraphic = exports.graphicCreator = exports.getGraphicRegistryState = exports.GRAPHIC_REGISTRY_SYMBOL = void 0; const graphic_factory_1 = require("../factory/graphic-factory"); function createGraphicCtor(creator) { return class { constructor(attributes) { try { return new creator(attributes); } catch (error) { return creator(attributes); } } }; } class GraphicCreator { constructor() { this.store = new Map; } registerStore(name, creator) { this.store.set(name, creator), this[name] = creator; } RegisterGraphicCreator(name, creator) { registerGraphic(name, creator); } CreateGraphic(name, attributes) { return this.store.has(name) ? createGraphic(name, attributes) : null; } } function createGraphicRegistryState() { return { graphicCreator: new GraphicCreator, graphicFactory: new graphic_factory_1.GraphicFactory }; } function getGraphicRegistryState() { const scope = globalThis; return scope[exports.GRAPHIC_REGISTRY_SYMBOL] || (scope[exports.GRAPHIC_REGISTRY_SYMBOL] = createGraphicRegistryState()), scope[exports.GRAPHIC_REGISTRY_SYMBOL]; } exports.GRAPHIC_REGISTRY_SYMBOL = Symbol.for("@visactor/vrender-core/graphic-registry"), exports.getGraphicRegistryState = getGraphicRegistryState; const sharedGraphicRegistry = getGraphicRegistryState(); function registerGraphic(name, creator) { if (!name) throw new Error("Graphic registration requires a non-empty graphic type"); exports.graphicCreator.registerStore(name, creator), sharedGraphicRegistry.graphicFactory.register(name, createGraphicCtor(creator)); } function createGraphic(name, attributes) { return sharedGraphicRegistry.graphicFactory.create(name, attributes); } exports.graphicCreator = sharedGraphicRegistry.graphicCreator, exports.registerGraphic = registerGraphic, exports.createGraphic = createGraphic; //# sourceMappingURL=graphic-registry.js.map