UNPKG

@visactor/vrender-core

Version:

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

201 lines (157 loc) 10.8 kB
import { application } from "../application"; import { CanvasFactory, Context2dFactory } from "../canvas/constants"; import { ContributionStore, createContributionProvider } from "../common/contribution-provider"; import { EnvContribution } from "../constants"; import loadBuiltinContributions from "../core/contributions/modules"; import { DefaultGlobal } from "../core/global"; import { WindowHandlerContribution, DefaultWindow } from "../core/window"; import coreModule from "../core/core-modules"; import graphicModule from "../graphic/graphic-service/graphic-module"; import { getLegacyBindingContext, preLoadAllModule } from "../legacy/bootstrap"; import pickModule from "../picker/pick-modules"; import { AreaRenderContribution } from "../render/contributions/render/contributions/constants"; import { DrawItemInterceptor } from "../render/contributions/render/draw-interceptor"; import { DefaultIncrementalCanvasAreaRender } from "../render/contributions/render/incremental-area-render"; import { DefaultIncrementalDrawContribution } from "../render/contributions/render/incremental-draw-contribution"; import { DefaultIncrementalCanvasLineRender } from "../render/contributions/render/incremental-line-render"; import renderModule from "../render/render-modules"; import { GraphicRender } from "../render/contributions/render/symbol"; import loadRenderContributions from "../render/contributions/modules"; import { AutoEnablePlugins } from "../plugins/constants"; import { DefaultPluginService } from "../plugins/plugin-service"; import { getRuntimeInstallerState } from "./runtime-installer-state"; const runtimeInstallerState = getRuntimeInstallerState(), runtimeInstallerContext = runtimeInstallerState.runtimeInstallerContext, RUNTIME_RENDERER_NAMESPACE = "vrender:runtime-renderer", RUNTIME_PICKER_NAMESPACE = "vrender:runtime-picker", runtimeEntryKeys = runtimeInstallerState.runtimeEntryKeys, runtimeDrawContributions = runtimeInstallerState.runtimeDrawContributions, loadedRuntimeContributionModules = runtimeInstallerState.loadedRuntimeContributionModules, DEFAULT_RUNTIME_CONTRIBUTION_TARGETS = [ "graphic-renderer" ], noopUnbindRuntimeContributionService = () => {}; function ensureRuntimeInstallerPreloaded() { runtimeInstallerState.preloaded || (runtimeInstallerState.preloaded = !0, coreModule({ bind: runtimeInstallerContext.bind }), graphicModule({ bind: runtimeInstallerContext.bind }), renderModule({ bind: runtimeInstallerContext.bind }), pickModule({ bind: runtimeInstallerContext.bind, isBound: runtimeInstallerContext.isBound }), loadBuiltinContributions(runtimeInstallerContext), loadRenderContributions(runtimeInstallerContext)); } function resolveRuntimeNamed(serviceIdentifier, name) { const instance = getRuntimeInstallerBindingContext().getNamed(serviceIdentifier, name); if (null == instance) throw new Error(`${String(serviceIdentifier)} is not configured for ${name}`); return instance; } function registerRuntimeEntries(register, entries, prefix) { entries.forEach(((entry, index) => { var _a, _b, _c; const name = null !== (_c = null !== (_a = null == entry ? void 0 : entry.type) && void 0 !== _a ? _a : null === (_b = null == entry ? void 0 : entry.constructor) || void 0 === _b ? void 0 : _b.name) && void 0 !== _c ? _c : `${prefix}-entry`; register(`${prefix}:${String(name)}:${index}`, entry); })); } function getTrackedEntryKeys(registry, namespace) { let namespaces = runtimeEntryKeys.get(registry); namespaces || (namespaces = new Map, runtimeEntryKeys.set(registry, namespaces)); let keys = namespaces.get(namespace); return keys || (keys = new Set, namespaces.set(namespace, keys)), keys; } function clearTrackedEntryKeys(registry, namespace, unregister) { const keys = getTrackedEntryKeys(registry, namespace); unregister && keys.forEach((key => unregister(key))), keys.clear(); } function createAppRegistryContributionProvider(app, key) { return { getContributions: () => app.context.registry.contribution.get(key) }; } export function getRuntimeInstallerBindingContext() { return ensureRuntimeInstallerPreloaded(), runtimeInstallerContext; } export function refreshRuntimeInstallerContributions() { ContributionStore.refreshAllContributions(); } export function getRuntimeInstallerGlobal() { var _a; return ensureRuntimeInstallerPreloaded(), null !== (_a = runtimeInstallerState.runtimeGlobal) && void 0 !== _a || (runtimeInstallerState.runtimeGlobal = new DefaultGlobal(createContributionProvider(EnvContribution, runtimeInstallerContext))), runtimeInstallerState.runtimeGlobal; } export function configureRuntimeApplicationForApp(app) { const bindingContext = getRuntimeInstallerBindingContext(), global = getRuntimeInstallerGlobal(); application.global = global, application.canvasFactory = env => bindingContext.getNamed(CanvasFactory, env), application.context2dFactory = env => bindingContext.getNamed(Context2dFactory, env), application.windowHandlerFactory = env => resolveRuntimeNamed(WindowHandlerContribution, env), application.windowFactory = () => new DefaultWindow(global, (env => resolveRuntimeNamed(WindowHandlerContribution, env))), application.pluginServiceFactory = () => new DefaultPluginService(createContributionProvider(AutoEnablePlugins, bindingContext), { pluginRegistry: app.context.registry.plugin }), application.incrementalDrawContributionFactory = () => new DefaultIncrementalDrawContribution([], new DefaultIncrementalCanvasLineRender, new DefaultIncrementalCanvasAreaRender(createContributionProvider(AreaRenderContribution, bindingContext)), createAppRegistryContributionProvider(app, DrawItemInterceptor)); } export { RUNTIME_INSTALLER_STATE_SYMBOL, getRuntimeInstallerState } from "./runtime-installer-state"; export function installRuntimeGraphicRenderersToApp(app) { var _a; const bindingContext = getRuntimeInstallerBindingContext(); refreshRuntimeInstallerContributions(); const renderers = bindingContext.getAll(GraphicRender), registeredKeys = getTrackedEntryKeys(app.registry.renderer, RUNTIME_RENDERER_NAMESPACE); clearTrackedEntryKeys(app.registry.renderer, RUNTIME_RENDERER_NAMESPACE, null === (_a = app.registry.renderer.unregister) || void 0 === _a ? void 0 : _a.bind(app.registry.renderer)), registerRuntimeEntries(((key, renderer) => { var _a; null === (_a = null == renderer ? void 0 : renderer.reInit) || void 0 === _a || _a.call(renderer), app.registry.renderer.register(key, renderer), registeredKeys.add(key); }), renderers, RUNTIME_RENDERER_NAMESPACE); } export function installRuntimeDrawContributionsToApp(app) { var _a; const bindingContext = getRuntimeInstallerBindingContext(); refreshRuntimeInstallerContributions(); const contributions = bindingContext.getAll(DrawItemInterceptor), tracked = null !== (_a = runtimeDrawContributions.get(app.context.registry.contribution)) && void 0 !== _a ? _a : new Set; tracked.forEach((contribution => { var _a, _b; null === (_b = (_a = app.context.registry.contribution).unregister) || void 0 === _b || _b.call(_a, DrawItemInterceptor, contribution); })), tracked.clear(), runtimeDrawContributions.set(app.context.registry.contribution, tracked); const registered = new Set(app.context.registry.contribution.get(DrawItemInterceptor)); contributions.forEach((contribution => { registered.has(contribution) || (app.context.registry.contribution.register(DrawItemInterceptor, contribution), registered.add(contribution)), tracked.add(contribution); })); } export function installRuntimePickersToApp(app, serviceIdentifier) { var _a; const bindingContext = getRuntimeInstallerBindingContext(); refreshRuntimeInstallerContributions(); const pickers = bindingContext.getAll(serviceIdentifier), registeredKeys = getTrackedEntryKeys(app.registry.picker, RUNTIME_PICKER_NAMESPACE); clearTrackedEntryKeys(app.registry.picker, RUNTIME_PICKER_NAMESPACE, null === (_a = app.registry.picker.unregister) || void 0 === _a ? void 0 : _a.bind(app.registry.picker)), registerRuntimeEntries(((key, picker) => { app.registry.picker.register(key, picker), registeredKeys.add(key); }), pickers, RUNTIME_PICKER_NAMESPACE); } function getRuntimeContributionModuleIdentity(module) { return module; } function hasLoadedRuntimeContributionModule(context, module) { var _a, _b; return null !== (_b = null === (_a = loadedRuntimeContributionModules.get(context)) || void 0 === _a ? void 0 : _a.has(getRuntimeContributionModuleIdentity(module))) && void 0 !== _b && _b; } function markRuntimeContributionModuleLoaded(context, module) { let modules = loadedRuntimeContributionModules.get(context); modules || (modules = new WeakSet, loadedRuntimeContributionModules.set(context, modules)), modules.add(getRuntimeContributionModuleIdentity(module)); } function loadRuntimeContributionModuleToContext(context, module) { hasLoadedRuntimeContributionModule(context, module) || ("function" == typeof module ? module(context) : module.registry(context.bind, noopUnbindRuntimeContributionService, context.isBound, context.rebind), markRuntimeContributionModuleLoaded(context, module)); } function isRuntimePickerTarget(target) { return "object" == typeof target && null !== target && "picker" in target; } function installRuntimeContributionTargetsToApp(app, targets) { const resolvedTargets = null != targets ? targets : DEFAULT_RUNTIME_CONTRIBUTION_TARGETS; let installGraphicRenderers = !1, installDrawContributions = !1; const pickerTargets = new Set; resolvedTargets.forEach((target => { "graphic-renderer" === target ? installGraphicRenderers = !0 : "draw-contribution" === target ? installDrawContributions = !0 : isRuntimePickerTarget(target) && pickerTargets.add(target.picker); })), installDrawContributions && installRuntimeDrawContributionsToApp(app), installGraphicRenderers && installRuntimeGraphicRenderersToApp(app), pickerTargets.forEach((serviceIdentifier => { installRuntimePickersToApp(app, serviceIdentifier); })); } export function installRuntimeContributionModule(module, {app: app, legacy: legacy = !0, targets: targets} = {}) { loadRuntimeContributionModuleToContext(getRuntimeInstallerBindingContext(), module), legacy && (preLoadAllModule(), loadRuntimeContributionModuleToContext(getLegacyBindingContext(), module)), refreshRuntimeInstallerContributions(), app && installRuntimeContributionTargetsToApp(app, targets); } //# sourceMappingURL=runtime-installer.js.map