UNPKG

6.92 kBJavaScriptView Raw
1/**
2 * Root Container
3 * @see /dev-docs/IoC 容器、依赖注入与服务说明.md
4 */
5import 'reflect-metadata';
6import { Container } from 'inversify';
7import getDecorators from 'inversify-inject-decorators';
8import { ComponentManager } from './ComponentManager';
9import { ResourcePool } from './components/framegraph/ResourcePool';
10import { FrameGraphSystem } from './components/framegraph/System';
11import { GeometryComponent } from './components/geometry/GeometryComponent';
12import { GeometrySystem } from './components/geometry/System'; // import { InteractionSystem } from './components/interaction/System';
13
14import { MaterialComponent } from './components/material/MaterialComponent';
15import { MaterialSystem } from './components/material/System';
16import { CullableComponent } from './components/mesh/CullableComponent';
17import { MeshComponent } from './components/mesh/MeshComponent';
18import { MeshSystem } from './components/mesh/System';
19import { CopyPass } from './components/renderer/passes/CopyPass';
20import { PixelPickingPass } from './components/renderer/passes/PixelPickingPass';
21import { RenderPass } from './components/renderer/passes/RenderPass';
22import { RendererSystem } from './components/renderer/System';
23import { HierarchyComponent } from './components/scenegraph/HierarchyComponent';
24import { NameComponent } from './components/scenegraph/NameComponent';
25import { SceneGraphSystem } from './components/scenegraph/System';
26import { TransformComponent } from './components/scenegraph/TransformComponent';
27import { IDENTIFIER } from './identifier';
28import { ConfigService } from './services/config/ConfigService';
29import { InteractorService } from './services/interactor/IteractorService';
30import ShaderModuleService from './services/shader-module/ShaderModuleService'; // @see https://github.com/inversify/InversifyJS/blob/master/wiki/container_api.md#defaultscope
31
32export var container = new Container(); // @see https://github.com/inversify/InversifyJS/blob/master/wiki/inheritance.md#what-can-i-do-when-my-base-class-is-provided-by-a-third-party-module
33// decorate(injectable(), EventEmitter);
34// container.bind(IDENTIFIER.IEventEmitter).to(EventEmitter);
35// 支持使用 new 而非容器实例化的场景,同时禁止 lazyInject cache
36// @see https://github.com/inversify/inversify-inject-decorators#caching-vs-non-caching-behaviour
37
38var DECORATORS = getDecorators(container, false);
39// Add babel legacy decorators support
40// @see https://github.com/inversify/InversifyJS/issues/1050
41// @see https://github.com/inversify/InversifyJS/issues/1026#issuecomment-504936034
42export var lazyInject = function lazyInject(serviceIdentifier) {
43 var original = DECORATORS.lazyInject(serviceIdentifier); // the 'descriptor' parameter is actually always defined for class fields for Babel, but is considered undefined for TSC
44 // so we just hack it with ?/! combination to avoid "TS1240: Unable to resolve signature of property decorator when called as an expression"
45
46 return function (proto, key, descriptor) {
47 // make it work as usual
48 original.call(this, proto, key); // return link to proto, so own value wont be 'undefined' after component's creation
49
50 if (descriptor) {
51 descriptor.initializer = function () {
52 return proto[key];
53 };
54 }
55 };
56};
57export var lazyMultiInject = function lazyMultiInject(serviceIdentifier) {
58 var original = DECORATORS.lazyMultiInject(serviceIdentifier); // the 'descriptor' parameter is actually always defined for class fields for Babel, but is considered undefined for TSC
59 // so we just hack it with ?/! combination to avoid "TS1240: Unable to resolve signature of property decorator when called as an expression"
60
61 return function (proto, key, descriptor) {
62 // make it work as usual
63 original.call(this, proto, key);
64
65 if (descriptor) {
66 // return link to proto, so own value wont be 'undefined' after component's creation
67 descriptor.initializer = function () {
68 return proto[key];
69 };
70 }
71 };
72};
73/** global services */
74
75container.bind(IDENTIFIER.ShaderModuleService).to(ShaderModuleService).inSingletonScope();
76/**
77 * bind global component managers in root container
78 */
79
80container.bind(IDENTIFIER.NameComponentManager).toConstantValue(new ComponentManager(NameComponent));
81container.bind(IDENTIFIER.HierarchyComponentManager).toConstantValue(new ComponentManager(HierarchyComponent));
82container.bind(IDENTIFIER.TransformComponentManager).toConstantValue(new ComponentManager(TransformComponent));
83container.bind(IDENTIFIER.MeshComponentManager).toConstantValue(new ComponentManager(MeshComponent));
84container.bind(IDENTIFIER.CullableComponentManager).toConstantValue(new ComponentManager(CullableComponent));
85container.bind(IDENTIFIER.GeometryComponentManager).toConstantValue(new ComponentManager(GeometryComponent));
86container.bind(IDENTIFIER.MaterialComponentManager).toConstantValue(new ComponentManager(MaterialComponent)); // https://github.com/inversify/InversifyJS/blob/master/wiki/hierarchical_di.md#support-for-hierarchical-di-systems
87
88export function createWorldContainer() {
89 var worldContainer = new Container();
90 worldContainer.parent = container;
91 /**
92 * bind systems
93 */
94
95 worldContainer.bind(IDENTIFIER.Systems).to(SceneGraphSystem).inSingletonScope().whenTargetNamed(IDENTIFIER.SceneGraphSystem);
96 worldContainer.bind(IDENTIFIER.Systems).to(FrameGraphSystem).inSingletonScope().whenTargetNamed(IDENTIFIER.FrameGraphSystem);
97 worldContainer.bind(IDENTIFIER.Systems).to(MeshSystem).inSingletonScope().whenTargetNamed(IDENTIFIER.MeshSystem);
98 worldContainer.bind(IDENTIFIER.Systems).to(GeometrySystem).inSingletonScope().whenTargetNamed(IDENTIFIER.GeometrySystem);
99 worldContainer.bind(IDENTIFIER.Systems).to(MaterialSystem).inSingletonScope().whenTargetNamed(IDENTIFIER.MaterialSystem);
100 worldContainer.bind(IDENTIFIER.Systems).to(RendererSystem).inSingletonScope().whenTargetNamed(IDENTIFIER.RendererSystem); // 资源池
101
102 worldContainer.bind(IDENTIFIER.ResourcePool).to(ResourcePool).inSingletonScope();
103 worldContainer.bind(IDENTIFIER.ConfigService).to(ConfigService).inSingletonScope();
104 worldContainer.bind(IDENTIFIER.InteractorService).to(InteractorService).inSingletonScope();
105 /**
106 * bind render passes
107 */
108
109 worldContainer.bind(IDENTIFIER.RenderPass).to(RenderPass).inSingletonScope().whenTargetNamed(RenderPass.IDENTIFIER);
110 worldContainer.bind(IDENTIFIER.RenderPass).to(CopyPass).inSingletonScope().whenTargetNamed(CopyPass.IDENTIFIER);
111 worldContainer.bind(IDENTIFIER.RenderPass).to(PixelPickingPass).inSingletonScope().whenTargetNamed(PixelPickingPass.IDENTIFIER);
112 worldContainer.bind(IDENTIFIER.RenderPassFactory).toFactory(function (context) {
113 return function (name) {
114 return context.container.getNamed(IDENTIFIER.RenderPass, name);
115 };
116 });
117 return worldContainer;
118}
119//# sourceMappingURL=inversify.config.js.map
\No newline at end of file