UNPKG

10 kBJavaScriptView Raw
1define('@glimmer/di', ['exports'], function (exports) { 'use strict';
2
3function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5var Container = function () {
6 function Container(registry) {
7 var resolver = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
8
9 _classCallCheck(this, Container);
10
11 this._registry = registry;
12 this._resolver = resolver;
13 this._lookups = {};
14 this._factoryDefinitionLookups = {};
15 }
16
17 Container.prototype.factoryFor = function factoryFor(specifier) {
18 var factoryDefinition = this._factoryDefinitionLookups[specifier];
19 if (!factoryDefinition) {
20 if (this._resolver) {
21 factoryDefinition = this._resolver.retrieve(specifier);
22 }
23 if (!factoryDefinition) {
24 factoryDefinition = this._registry.registration(specifier);
25 }
26 if (factoryDefinition) {
27 this._factoryDefinitionLookups[specifier] = factoryDefinition;
28 }
29 }
30 if (!factoryDefinition) {
31 return;
32 }
33 return this.buildFactory(specifier, factoryDefinition);
34 };
35
36 Container.prototype.lookup = function lookup(specifier) {
37 var singleton = this._registry.registeredOption(specifier, 'singleton') !== false;
38 if (singleton) {
39 var lookup = this._lookups[specifier];
40 if (lookup) {
41 return lookup.instance;
42 }
43 }
44 var factory = this.factoryFor(specifier);
45 if (!factory) {
46 return;
47 }
48 if (this._registry.registeredOption(specifier, 'instantiate') === false) {
49 return factory.class;
50 }
51 var instance = factory.create();
52 if (singleton && instance) {
53 this._lookups[specifier] = { factory: factory, instance: instance };
54 }
55 return instance;
56 };
57
58 Container.prototype.defaultInjections = function defaultInjections(specifier) {
59 return {};
60 };
61
62 Container.prototype.teardown = function teardown() {
63 var specifiers = Object.keys(this._lookups);
64 for (var i = 0; i < specifiers.length; i++) {
65 var specifier = specifiers[i];
66 var _lookups$specifier = this._lookups[specifier],
67 factory = _lookups$specifier.factory,
68 instance = _lookups$specifier.instance;
69
70 factory.teardown(instance);
71 }
72 };
73
74 Container.prototype.defaultTeardown = function defaultTeardown(instance) {};
75
76 Container.prototype.buildInjections = function buildInjections(specifier) {
77 var hash = this.defaultInjections(specifier);
78 var injections = this._registry.registeredInjections(specifier);
79 var injection = void 0;
80 for (var i = 0; i < injections.length; i++) {
81 injection = injections[i];
82 hash[injection.property] = this.lookup(injection.source);
83 }
84 return hash;
85 };
86
87 Container.prototype.buildFactory = function buildFactory(specifier, factoryDefinition) {
88 var _this = this;
89
90 var injections = this.buildInjections(specifier);
91 return {
92 class: factoryDefinition,
93 teardown: function (instance) {
94 if (factoryDefinition.teardown) {
95 factoryDefinition.teardown(instance);
96 } else {
97 _this.defaultTeardown(instance);
98 }
99 },
100 create: function (options) {
101 var mergedOptions = Object.assign({}, injections, options);
102 return factoryDefinition.create(mergedOptions);
103 }
104 };
105 };
106
107 return Container;
108}();
109
110function _classCallCheck$1(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
111
112var Registry = function () {
113 function Registry(options) {
114 _classCallCheck$1(this, Registry);
115
116 this._registrations = {};
117 this._registeredOptions = {};
118 this._registeredInjections = {};
119 if (options && options.fallback) {
120 this._fallback = options.fallback;
121 }
122 }
123
124 Registry.prototype.register = function register(specifier, factoryDefinition, options) {
125 this._registrations[specifier] = factoryDefinition;
126 if (options) {
127 this._registeredOptions[specifier] = options;
128 }
129 };
130
131 Registry.prototype.registration = function registration(specifier) {
132 var registration = this._registrations[specifier];
133 if (registration === undefined && this._fallback) {
134 registration = this._fallback.registration(specifier);
135 }
136 return registration;
137 };
138
139 Registry.prototype.unregister = function unregister(specifier) {
140 delete this._registrations[specifier];
141 delete this._registeredOptions[specifier];
142 delete this._registeredInjections[specifier];
143 };
144
145 Registry.prototype.registerOption = function registerOption(specifier, option, value) {
146 var options = this._registeredOptions[specifier];
147 if (!options) {
148 options = {};
149 this._registeredOptions[specifier] = options;
150 }
151 options[option] = value;
152 };
153
154 Registry.prototype.registeredOption = function registeredOption(specifier, option) {
155 var result = void 0;
156 var options = this.registeredOptions(specifier);
157 if (options) {
158 result = options[option];
159 }
160 if (result === undefined && this._fallback !== undefined) {
161 result = this._fallback.registeredOption(specifier, option);
162 }
163 return result;
164 };
165
166 Registry.prototype.registeredOptions = function registeredOptions(specifier) {
167 var options = this._registeredOptions[specifier];
168 if (options === undefined) {
169 var type = specifier.split(':')[0];
170 options = this._registeredOptions[type];
171 }
172 return options;
173 };
174
175 Registry.prototype.unregisterOption = function unregisterOption(specifier, option) {
176 var options = this._registeredOptions[specifier];
177 if (options) {
178 delete options[option];
179 }
180 };
181
182 Registry.prototype.registerInjection = function registerInjection(specifier, property, source) {
183 var injections = this._registeredInjections[specifier];
184 if (injections === undefined) {
185 this._registeredInjections[specifier] = injections = [];
186 }
187 injections.push({
188 property: property,
189 source: source
190 });
191 };
192
193 Registry.prototype.registeredInjections = function registeredInjections(specifier) {
194 var type = specifier.split(':')[0];
195 var injections = this._fallback ? this._fallback.registeredInjections(specifier) : [];
196 Array.prototype.push.apply(injections, this._registeredInjections[type]);
197 Array.prototype.push.apply(injections, this._registeredInjections[specifier]);
198 return injections;
199 };
200
201 return Registry;
202}();
203
204// TODO - use symbol
205var OWNER = '__owner__';
206function getOwner(object) {
207 return object[OWNER];
208}
209function setOwner(object, owner) {
210 object[OWNER] = owner;
211}
212
213function isSpecifierStringAbsolute(specifier) {
214 var split = specifier.split(':');
215 var type = split[0];
216 var path = split[1];
217 return !!(type && path && path.indexOf('/') === 0 && path.split('/').length > 3);
218}
219function isSpecifierObjectAbsolute(specifier) {
220 return specifier.rootName !== undefined && specifier.collection !== undefined && specifier.name !== undefined && specifier.type !== undefined;
221}
222function serializeSpecifier(specifier) {
223 var type = specifier.type;
224 var path = serializeSpecifierPath(specifier);
225 if (path) {
226 return type + ':' + path;
227 } else {
228 return type;
229 }
230}
231function serializeSpecifierPath(specifier) {
232 var path = [];
233 if (specifier.rootName) {
234 path.push(specifier.rootName);
235 }
236 if (specifier.collection) {
237 path.push(specifier.collection);
238 }
239 if (specifier.namespace) {
240 path.push(specifier.namespace);
241 }
242 if (specifier.name) {
243 path.push(specifier.name);
244 }
245 if (path.length > 0) {
246 var fullPath = path.join('/');
247 if (isSpecifierObjectAbsolute(specifier)) {
248 fullPath = '/' + fullPath;
249 }
250 return fullPath;
251 }
252}
253function deserializeSpecifier(specifier) {
254 var obj = {};
255 if (specifier.indexOf(':') > -1) {
256 var split = specifier.split(':');
257 var type = split[0];
258 var path = split[1];
259 obj.type = type;
260 var pathSegments = void 0;
261 if (path.indexOf('/') === 0) {
262 pathSegments = path.substr(1).split('/');
263 if (path.substr(1).startsWith('@')) {
264 obj.rootName = pathSegments.shift() + '/' + pathSegments.shift();
265 } else {
266 obj.rootName = pathSegments.shift();
267 }
268 obj.collection = pathSegments.shift();
269 } else {
270 pathSegments = path.split('/');
271 }
272 if (pathSegments.length > 0) {
273 obj.name = pathSegments.pop();
274 if (pathSegments.length > 0) {
275 obj.namespace = pathSegments.join('/');
276 }
277 }
278 } else {
279 obj.type = specifier;
280 }
281 return obj;
282}
283
284exports.Container = Container;
285exports.Registry = Registry;
286exports.getOwner = getOwner;
287exports.setOwner = setOwner;
288exports.OWNER = OWNER;
289exports.isSpecifierStringAbsolute = isSpecifierStringAbsolute;
290exports.isSpecifierObjectAbsolute = isSpecifierObjectAbsolute;
291exports.serializeSpecifier = serializeSpecifier;
292exports.deserializeSpecifier = deserializeSpecifier;
293
294Object.defineProperty(exports, '__esModule', { value: true });
295
296});