UNPKG

@visactor/vrender-core

Version:

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

85 lines (79 loc) 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: !0 }), exports.AppContext = void 0; const factory_1 = require("../factory"), registry_1 = require("../registry"), draw_contribution_1 = require("../render/contributions/render/draw-contribution"), draw_interceptor_1 = require("../render/contributions/render/draw-interceptor"), render_service_1 = require("../render/render-service"), APP_CONTEXT_RELEASED_ERROR = "AppContext has been released"; function createRegistryContributionProvider(registry, key) { return { getContributions: () => registry.get(key) }; } class AppContext { constructor(context = {}) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; this.stageResources = new Set, this._released = !1, this.registry = { renderer: null !== (_b = null === (_a = context.registry) || void 0 === _a ? void 0 : _a.renderer) && void 0 !== _b ? _b : new registry_1.RendererRegistry, picker: null !== (_d = null === (_c = context.registry) || void 0 === _c ? void 0 : _c.picker) && void 0 !== _d ? _d : new registry_1.PickerRegistry, contribution: null !== (_f = null === (_e = context.registry) || void 0 === _e ? void 0 : _e.contribution) && void 0 !== _f ? _f : new registry_1.ContributionRegistry, plugin: null !== (_h = null === (_g = context.registry) || void 0 === _g ? void 0 : _g.plugin) && void 0 !== _h ? _h : new registry_1.PluginRegistry }, this.factory = { stage: null !== (_k = null === (_j = context.factory) || void 0 === _j ? void 0 : _j.stage) && void 0 !== _k ? _k : new factory_1.StageFactory(void 0, (() => this.createStageDeps())), layer: null !== (_m = null === (_l = context.factory) || void 0 === _l ? void 0 : _l.layer) && void 0 !== _m ? _m : new factory_1.LayerFactory, graphic: null !== (_p = null === (_o = context.factory) || void 0 === _o ? void 0 : _o.graphic) && void 0 !== _p ? _p : new factory_1.GraphicFactory }; } get released() { return this._released; } getRegistry(name) { return this.assertActive(), this.registry[name]; } getFactory(name) { return this.assertActive(), this.factory[name]; } installPlugin(plugin) { this.assertActive(), this.registry.plugin.install(plugin), plugin.install(this); } installPlugins(plugins) { this.assertActive(); for (const plugin of plugins) this.installPlugin(plugin); } uninstallPlugin(name) { var _a; this.assertActive(); const plugin = this.registry.plugin.get(name); plugin && (null === (_a = plugin.uninstall) || void 0 === _a || _a.call(plugin), this.registry.plugin.uninstall(name)); } createStage(params = {}) { var _a; this.assertActive(); const stage = this.factory.stage.create(params), releasableStage = stage, release = null === (_a = releasableStage.release) || void 0 === _a ? void 0 : _a.bind(stage); let released = !1; return release && (releasableStage.release = () => { released || (released = !0, this.stageResources.delete(releasableStage), release()); }), this.stageResources.add(releasableStage), stage; } release() { var _a, _b; if (this._released) return; const plugins = this.registry.plugin.getAll(); for (const resource of this.stageResources) null === (_a = resource.release) || void 0 === _a || _a.call(resource); this.stageResources.clear(); for (const plugin of plugins) null === (_b = plugin.uninstall) || void 0 === _b || _b.call(plugin); this.registry.plugin.clear(), this.registry.renderer.clear(), this.registry.picker.clear(), this.registry.contribution.clear(), this._released = !0; } assertActive() { if (this._released) throw new Error(APP_CONTEXT_RELEASED_ERROR); } createStageDeps() { return { renderService: new render_service_1.DefaultRenderService(new draw_contribution_1.DefaultDrawContribution([], createRegistryContributionProvider(this.registry.contribution, draw_interceptor_1.DrawItemInterceptor), { rendererRegistry: this.registry.renderer })) }; } } exports.AppContext = AppContext; //# sourceMappingURL=app-context.js.map