UNPKG

@visactor/vrender-core

Version:

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

80 lines (75 loc) 3.21 kB
import { LayerFactory } from "../factory"; import { application } from "../application"; function defaultLayerHandlerFactory(layerMode) { const handlerFactory = application.layerHandlerFactory; if (!handlerFactory) throw new Error("Layer handler factory is not configured."); return handlerFactory(layerMode); } export class DefaultLayerService { static GenerateLayerId() { return `${DefaultLayerService.idprefix}_${DefaultLayerService.prefix_count++}`; } constructor(global = application.global, layerFactory = new LayerFactory, layerHandlerFactory = defaultLayerHandlerFactory) { this.layerFactory = layerFactory, this.layerHandlerFactory = layerHandlerFactory, this.layerMap = new Map, this.global = global; } tryInit() { this.inited || (this.staticLayerCountInEnv = this.global.getStaticCanvasCount(), this.dynamicLayerCountInEnv = this.global.getDynamicCanvasCount(), this.inited = !0); } getStageLayer(stage) { return this.layerMap.get(stage); } getRecommendedLayerType(layerMode) { return layerMode || (0 !== this.staticLayerCountInEnv ? "static" : 0 !== this.dynamicLayerCountInEnv ? "dynamic" : "virtual"); } getLayerHandler(layerMode) { return this.layerHandlerFactory(layerMode); } createLayer(stage, options = { main: !1 }) { var _a; this.tryInit(); let layerMode = this.getRecommendedLayerType(options.layerMode); layerMode = options.main || options.canvasId ? "static" : layerMode; const layerHandler = this.getLayerHandler(layerMode), layer = this.layerFactory.create({ stage: stage, global: this.global, window: stage.window, params: Object.assign(Object.assign({ main: !1 }, options), { layerMode: layerMode, canvasId: null !== (_a = options.canvasId) && void 0 !== _a ? _a : DefaultLayerService.GenerateLayerId(), layerHandler: layerHandler }) }), stageLayers = this.layerMap.get(stage) || []; return stageLayers.push(layer), this.layerMap.set(stage, stageLayers), this.staticLayerCountInEnv--, layer; } prepareStageLayer(stage) { let mainHandler; stage.forEachChildren((l => { const handler = l.getNativeHandler(); "virtual" === handler.type ? (handler.mainHandler = mainHandler, mainHandler.secondaryHandlers.push(handler)) : (mainHandler = handler, mainHandler.secondaryHandlers = []); })); } releaseLayer(stage, layer) { layer.release(); const stageLayers = this.layerMap.get(stage) || []; this.layerMap.set(stage, stageLayers.filter((l => l !== layer))); } layerCount(stage) { return (this.layerMap.get(stage) || []).length; } restLayerCount(stage) { return "browser" === this.global.env ? 10 : 0; } releaseStage(stage) { this.layerMap.delete(stage); } } DefaultLayerService.idprefix = "visactor_layer", DefaultLayerService.prefix_count = 0; //# sourceMappingURL=layer-service.js.map