UNPKG

@visactor/vrender-core

Version:

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

87 lines (79 loc) 4.26 kB
import { GraphicFactory, LayerFactory, StageFactory } from "../factory"; import { ContributionRegistry, PickerRegistry, PluginRegistry, RendererRegistry } from "../registry"; import { DefaultDrawContribution } from "../render/contributions/render/draw-contribution"; import { DrawItemInterceptor } from "../render/contributions/render/draw-interceptor"; import { DefaultRenderService } from "../render/render-service"; const APP_CONTEXT_RELEASED_ERROR = "AppContext has been released"; function createRegistryContributionProvider(registry, key) { return { getContributions: () => registry.get(key) }; } export 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 RendererRegistry, picker: null !== (_d = null === (_c = context.registry) || void 0 === _c ? void 0 : _c.picker) && void 0 !== _d ? _d : new PickerRegistry, contribution: null !== (_f = null === (_e = context.registry) || void 0 === _e ? void 0 : _e.contribution) && void 0 !== _f ? _f : new ContributionRegistry, plugin: null !== (_h = null === (_g = context.registry) || void 0 === _g ? void 0 : _g.plugin) && void 0 !== _h ? _h : new PluginRegistry }, this.factory = { stage: null !== (_k = null === (_j = context.factory) || void 0 === _j ? void 0 : _j.stage) && void 0 !== _k ? _k : new StageFactory(void 0, (() => this.createStageDeps())), layer: null !== (_m = null === (_l = context.factory) || void 0 === _l ? void 0 : _l.layer) && void 0 !== _m ? _m : new LayerFactory, graphic: null !== (_p = null === (_o = context.factory) || void 0 === _o ? void 0 : _o.graphic) && void 0 !== _p ? _p : new 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 DefaultRenderService(new DefaultDrawContribution([], createRegistryContributionProvider(this.registry.contribution, DrawItemInterceptor), { rendererRegistry: this.registry.renderer })) }; } } //# sourceMappingURL=app-context.js.map