UNPKG

@visactor/vrender-core

Version:

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

58 lines (48 loc) 1.76 kB
import { GraphicFactory } from "../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; } } export const GRAPHIC_REGISTRY_SYMBOL = Symbol.for("@visactor/vrender-core/graphic-registry"); function createGraphicRegistryState() { return { graphicCreator: new GraphicCreator, graphicFactory: new GraphicFactory }; } export function getGraphicRegistryState() { const scope = globalThis; return scope[GRAPHIC_REGISTRY_SYMBOL] || (scope[GRAPHIC_REGISTRY_SYMBOL] = createGraphicRegistryState()), scope[GRAPHIC_REGISTRY_SYMBOL]; } const sharedGraphicRegistry = getGraphicRegistryState(); export const graphicCreator = sharedGraphicRegistry.graphicCreator; export function registerGraphic(name, creator) { if (!name) throw new Error("Graphic registration requires a non-empty graphic type"); graphicCreator.registerStore(name, creator), sharedGraphicRegistry.graphicFactory.register(name, createGraphicCtor(creator)); } export function createGraphic(name, attributes) { return sharedGraphicRegistry.graphicFactory.create(name, attributes); } //# sourceMappingURL=graphic-registry.js.map