UNPKG

8.17 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.createWorldContainer = createWorldContainer;
9exports.lazyMultiInject = exports.lazyInject = exports.container = void 0;
10
11require("reflect-metadata");
12
13var _inversify = require("inversify");
14
15var _inversifyInjectDecorators = _interopRequireDefault(require("inversify-inject-decorators"));
16
17var _ComponentManager = require("./ComponentManager");
18
19var _ResourcePool = require("./components/framegraph/ResourcePool");
20
21var _System = require("./components/framegraph/System");
22
23var _GeometryComponent = require("./components/geometry/GeometryComponent");
24
25var _System2 = require("./components/geometry/System");
26
27var _MaterialComponent = require("./components/material/MaterialComponent");
28
29var _System3 = require("./components/material/System");
30
31var _CullableComponent = require("./components/mesh/CullableComponent");
32
33var _MeshComponent = require("./components/mesh/MeshComponent");
34
35var _System4 = require("./components/mesh/System");
36
37var _CopyPass = require("./components/renderer/passes/CopyPass");
38
39var _PixelPickingPass = require("./components/renderer/passes/PixelPickingPass");
40
41var _RenderPass = require("./components/renderer/passes/RenderPass");
42
43var _System5 = require("./components/renderer/System");
44
45var _HierarchyComponent = require("./components/scenegraph/HierarchyComponent");
46
47var _NameComponent = require("./components/scenegraph/NameComponent");
48
49var _System6 = require("./components/scenegraph/System");
50
51var _TransformComponent = require("./components/scenegraph/TransformComponent");
52
53var _identifier = require("./identifier");
54
55var _ConfigService = require("./services/config/ConfigService");
56
57var _IteractorService = require("./services/interactor/IteractorService");
58
59var _ShaderModuleService = _interopRequireDefault(require("./services/shader-module/ShaderModuleService"));
60
61/**
62 * Root Container
63 * @see /dev-docs/IoC 容器、依赖注入与服务说明.md
64 */
65// import { InteractionSystem } from './components/interaction/System';
66// @see https://github.com/inversify/InversifyJS/blob/master/wiki/container_api.md#defaultscope
67var container = new _inversify.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
68// decorate(injectable(), EventEmitter);
69// container.bind(IDENTIFIER.IEventEmitter).to(EventEmitter);
70// 支持使用 new 而非容器实例化的场景,同时禁止 lazyInject cache
71// @see https://github.com/inversify/inversify-inject-decorators#caching-vs-non-caching-behaviour
72
73exports.container = container;
74var DECORATORS = (0, _inversifyInjectDecorators.default)(container, false);
75
76// Add babel legacy decorators support
77// @see https://github.com/inversify/InversifyJS/issues/1050
78// @see https://github.com/inversify/InversifyJS/issues/1026#issuecomment-504936034
79var lazyInject = function lazyInject(serviceIdentifier) {
80 var original = DECORATORS.lazyInject(serviceIdentifier); // the 'descriptor' parameter is actually always defined for class fields for Babel, but is considered undefined for TSC
81 // so we just hack it with ?/! combination to avoid "TS1240: Unable to resolve signature of property decorator when called as an expression"
82
83 return function (proto, key, descriptor) {
84 // make it work as usual
85 original.call(this, proto, key); // return link to proto, so own value wont be 'undefined' after component's creation
86
87 if (descriptor) {
88 descriptor.initializer = function () {
89 return proto[key];
90 };
91 }
92 };
93};
94
95exports.lazyInject = lazyInject;
96
97var lazyMultiInject = function lazyMultiInject(serviceIdentifier) {
98 var original = DECORATORS.lazyMultiInject(serviceIdentifier); // the 'descriptor' parameter is actually always defined for class fields for Babel, but is considered undefined for TSC
99 // so we just hack it with ?/! combination to avoid "TS1240: Unable to resolve signature of property decorator when called as an expression"
100
101 return function (proto, key, descriptor) {
102 // make it work as usual
103 original.call(this, proto, key);
104
105 if (descriptor) {
106 // return link to proto, so own value wont be 'undefined' after component's creation
107 descriptor.initializer = function () {
108 return proto[key];
109 };
110 }
111 };
112};
113/** global services */
114
115
116exports.lazyMultiInject = lazyMultiInject;
117container.bind(_identifier.IDENTIFIER.ShaderModuleService).to(_ShaderModuleService.default).inSingletonScope();
118/**
119 * bind global component managers in root container
120 */
121
122container.bind(_identifier.IDENTIFIER.NameComponentManager).toConstantValue(new _ComponentManager.ComponentManager(_NameComponent.NameComponent));
123container.bind(_identifier.IDENTIFIER.HierarchyComponentManager).toConstantValue(new _ComponentManager.ComponentManager(_HierarchyComponent.HierarchyComponent));
124container.bind(_identifier.IDENTIFIER.TransformComponentManager).toConstantValue(new _ComponentManager.ComponentManager(_TransformComponent.TransformComponent));
125container.bind(_identifier.IDENTIFIER.MeshComponentManager).toConstantValue(new _ComponentManager.ComponentManager(_MeshComponent.MeshComponent));
126container.bind(_identifier.IDENTIFIER.CullableComponentManager).toConstantValue(new _ComponentManager.ComponentManager(_CullableComponent.CullableComponent));
127container.bind(_identifier.IDENTIFIER.GeometryComponentManager).toConstantValue(new _ComponentManager.ComponentManager(_GeometryComponent.GeometryComponent));
128container.bind(_identifier.IDENTIFIER.MaterialComponentManager).toConstantValue(new _ComponentManager.ComponentManager(_MaterialComponent.MaterialComponent)); // https://github.com/inversify/InversifyJS/blob/master/wiki/hierarchical_di.md#support-for-hierarchical-di-systems
129
130function createWorldContainer() {
131 var worldContainer = new _inversify.Container();
132 worldContainer.parent = container;
133 /**
134 * bind systems
135 */
136
137 worldContainer.bind(_identifier.IDENTIFIER.Systems).to(_System6.SceneGraphSystem).inSingletonScope().whenTargetNamed(_identifier.IDENTIFIER.SceneGraphSystem);
138 worldContainer.bind(_identifier.IDENTIFIER.Systems).to(_System.FrameGraphSystem).inSingletonScope().whenTargetNamed(_identifier.IDENTIFIER.FrameGraphSystem);
139 worldContainer.bind(_identifier.IDENTIFIER.Systems).to(_System4.MeshSystem).inSingletonScope().whenTargetNamed(_identifier.IDENTIFIER.MeshSystem);
140 worldContainer.bind(_identifier.IDENTIFIER.Systems).to(_System2.GeometrySystem).inSingletonScope().whenTargetNamed(_identifier.IDENTIFIER.GeometrySystem);
141 worldContainer.bind(_identifier.IDENTIFIER.Systems).to(_System3.MaterialSystem).inSingletonScope().whenTargetNamed(_identifier.IDENTIFIER.MaterialSystem);
142 worldContainer.bind(_identifier.IDENTIFIER.Systems).to(_System5.RendererSystem).inSingletonScope().whenTargetNamed(_identifier.IDENTIFIER.RendererSystem); // 资源池
143
144 worldContainer.bind(_identifier.IDENTIFIER.ResourcePool).to(_ResourcePool.ResourcePool).inSingletonScope();
145 worldContainer.bind(_identifier.IDENTIFIER.ConfigService).to(_ConfigService.ConfigService).inSingletonScope();
146 worldContainer.bind(_identifier.IDENTIFIER.InteractorService).to(_IteractorService.InteractorService).inSingletonScope();
147 /**
148 * bind render passes
149 */
150
151 worldContainer.bind(_identifier.IDENTIFIER.RenderPass).to(_RenderPass.RenderPass).inSingletonScope().whenTargetNamed(_RenderPass.RenderPass.IDENTIFIER);
152 worldContainer.bind(_identifier.IDENTIFIER.RenderPass).to(_CopyPass.CopyPass).inSingletonScope().whenTargetNamed(_CopyPass.CopyPass.IDENTIFIER);
153 worldContainer.bind(_identifier.IDENTIFIER.RenderPass).to(_PixelPickingPass.PixelPickingPass).inSingletonScope().whenTargetNamed(_PixelPickingPass.PixelPickingPass.IDENTIFIER);
154 worldContainer.bind(_identifier.IDENTIFIER.RenderPassFactory).toFactory(function (context) {
155 return function (name) {
156 return context.container.getNamed(_identifier.IDENTIFIER.RenderPass, name);
157 };
158 });
159 return worldContainer;
160}
161//# sourceMappingURL=inversify.config.js.map
\No newline at end of file