@visactor/vrender-core
Version:
```typescript import { xxx } from '@visactor/vrender-core'; ```
85 lines (79 loc) • 3.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.DefaultLayerService = void 0;
const factory_1 = require("../factory"), application_1 = require("../application");
function defaultLayerHandlerFactory(layerMode) {
const handlerFactory = application_1.application.layerHandlerFactory;
if (!handlerFactory) throw new Error("Layer handler factory is not configured.");
return handlerFactory(layerMode);
}
class DefaultLayerService {
static GenerateLayerId() {
return `${DefaultLayerService.idprefix}_${DefaultLayerService.prefix_count++}`;
}
constructor(global = application_1.application.global, layerFactory = new factory_1.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);
}
}
exports.DefaultLayerService = DefaultLayerService, DefaultLayerService.idprefix = "visactor_layer",
DefaultLayerService.prefix_count = 0;
//# sourceMappingURL=layer-service.js.map