Skip to content

Commit d801dc3

Browse files
authored
Merge pull request #13657 from emberjs/cleanup-container
cleanup packages/container/
2 parents 31d1265 + 62b235f commit d801dc3

File tree

7 files changed

+352
-357
lines changed

7 files changed

+352
-357
lines changed

packages/container/lib/container.js

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ const CONTAINER_OVERRIDE = symbol('CONTAINER_OVERRIDE');
2020
@private
2121
@class Container
2222
*/
23-
function Container(registry, options) {
23+
export default function Container(registry, options) {
2424
this.registry = registry;
2525
this.owner = options && options.owner ? options.owner : null;
2626
this.cache = dictionary(options && options.cache ? options.cache : null);
2727
this.factoryCache = dictionary(options && options.factoryCache ? options.factoryCache : null);
2828
this.validationCache = dictionary(options && options.validationCache ? options.validationCache : null);
2929
this._fakeContainerToInject = buildFakeContainerWithDeprecations(this);
3030
this[CONTAINER_OVERRIDE] = undefined;
31+
this.isDestroyed = false;
3132
}
3233

3334
Container.prototype = {
@@ -75,17 +76,17 @@ Container.prototype = {
7576
to all have their own locally scoped singletons.
7677
7778
```javascript
78-
var registry = new Registry();
79-
var container = registry.container();
79+
let registry = new Registry();
80+
let container = registry.container();
8081
8182
registry.register('api:twitter', Twitter);
8283
83-
var twitter = container.lookup('api:twitter');
84+
let twitter = container.lookup('api:twitter');
8485
8586
twitter instanceof Twitter; // => true
8687
8788
// by default the container will return singletons
88-
var twitter2 = container.lookup('api:twitter');
89+
let twitter2 = container.lookup('api:twitter');
8990
twitter2 instanceof Twitter; // => true
9091
9192
twitter === twitter2; //=> true
@@ -94,13 +95,13 @@ Container.prototype = {
9495
If singletons are not wanted, an optional flag can be provided at lookup.
9596
9697
```javascript
97-
var registry = new Registry();
98-
var container = registry.container();
98+
let registry = new Registry();
99+
let container = registry.container();
99100
100101
registry.register('api:twitter', Twitter);
101102
102-
var twitter = container.lookup('api:twitter', { singleton: false });
103-
var twitter2 = container.lookup('api:twitter', { singleton: false });
103+
let twitter = container.lookup('api:twitter', { singleton: false });
104+
let twitter2 = container.lookup('api:twitter', { singleton: false });
104105
105106
twitter === twitter2; //=> false
106107
```
@@ -140,7 +141,7 @@ Container.prototype = {
140141
@method destroy
141142
*/
142143
destroy() {
143-
eachDestroyable(this, function(item) {
144+
eachDestroyable(this, item => {
144145
if (item.destroy) {
145146
item.destroy();
146147
}
@@ -193,7 +194,7 @@ function lookup(container, fullName, options = {}) {
193194
return container.cache[fullName];
194195
}
195196

196-
var value = instantiate(container, fullName);
197+
let value = instantiate(container, fullName);
197198

198199
if (value === undefined) { return; }
199200

@@ -213,22 +214,22 @@ function areInjectionsDynamic(injections) {
213214
}
214215

215216
function buildInjections(/* container, ...injections */) {
216-
var hash = {};
217+
let hash = {};
217218

218219
if (arguments.length > 1) {
219-
var container = arguments[0];
220-
var injections = [];
221-
var injection;
220+
let container = arguments[0];
221+
let injections = [];
222+
let injection;
222223

223-
for (var i = 1; i < arguments.length; i++) {
224+
for (let i = 1; i < arguments.length; i++) {
224225
if (arguments[i]) {
225226
injections = injections.concat(arguments[i]);
226227
}
227228
}
228229

229230
container.registry.validateInjections(injections);
230231

231-
for (i = 0; i < injections.length; i++) {
232+
for (let i = 0; i < injections.length; i++) {
232233
injection = injections[i];
233234
hash[injection.property] = lookup(container, injection.fullName);
234235
if (!isSingleton(container, injection.fullName)) {
@@ -250,14 +251,14 @@ function factoryFor(container, fullName, options = {}) {
250251
if (!fullName) { return; }
251252
}
252253

253-
var cache = container.factoryCache;
254+
let cache = container.factoryCache;
254255
if (cache[fullName]) {
255256
return cache[fullName];
256257
}
257-
var factory = registry.resolve(fullName);
258+
let factory = registry.resolve(fullName);
258259
if (factory === undefined) { return; }
259260

260-
var type = fullName.split(':')[0];
261+
let type = fullName.split(':')[0];
261262
if (!factory || typeof factory.extend !== 'function' || (!ENV.MODEL_FACTORY_INJECTIONS && type === 'model')) {
262263
if (factory && typeof factory._onLookup === 'function') {
263264
factory._onLookup(fullName);
@@ -268,13 +269,13 @@ function factoryFor(container, fullName, options = {}) {
268269
cache[fullName] = factory;
269270
return factory;
270271
} else {
271-
var injections = injectionsFor(container, fullName);
272-
var factoryInjections = factoryInjectionsFor(container, fullName);
273-
var cacheable = !areInjectionsDynamic(injections) && !areInjectionsDynamic(factoryInjections);
272+
let injections = injectionsFor(container, fullName);
273+
let factoryInjections = factoryInjectionsFor(container, fullName);
274+
let cacheable = !areInjectionsDynamic(injections) && !areInjectionsDynamic(factoryInjections);
274275

275276
factoryInjections._toString = registry.makeToString(factory, fullName);
276277

277-
var injectedFactory = factory.extend(injections);
278+
let injectedFactory = factory.extend(injections);
278279

279280
// TODO - remove all `container` injections when Ember reaches v3.0.0
280281
injectDeprecatedContainer(injectedFactory.prototype, container);
@@ -293,11 +294,11 @@ function factoryFor(container, fullName, options = {}) {
293294
}
294295

295296
function injectionsFor(container, fullName) {
296-
var registry = container.registry;
297-
var splitName = fullName.split(':');
298-
var type = splitName[0];
297+
let registry = container.registry;
298+
let splitName = fullName.split(':');
299+
let type = splitName[0];
299300

300-
var injections = buildInjections(container,
301+
let injections = buildInjections(container,
301302
registry.getTypeInjections(type),
302303
registry.getInjections(fullName));
303304
injections._debugContainerKey = fullName;
@@ -308,11 +309,11 @@ function injectionsFor(container, fullName) {
308309
}
309310

310311
function factoryInjectionsFor(container, fullName) {
311-
var registry = container.registry;
312-
var splitName = fullName.split(':');
313-
var type = splitName[0];
312+
let registry = container.registry;
313+
let splitName = fullName.split(':');
314+
let type = splitName[0];
314315

315-
var factoryInjections = buildInjections(container,
316+
let factoryInjections = buildInjections(container,
316317
registry.getFactoryTypeInjections(type),
317318
registry.getFactoryInjections(fullName));
318319
factoryInjections._debugContainerKey = fullName;
@@ -321,22 +322,22 @@ function factoryInjectionsFor(container, fullName) {
321322
}
322323

323324
function instantiate(container, fullName) {
324-
var factory = factoryFor(container, fullName);
325-
var lazyInjections, validationCache;
325+
let factory = factoryFor(container, fullName);
326+
let lazyInjections, validationCache;
326327

327328
if (container.registry.getOption(fullName, 'instantiate') === false) {
328329
return factory;
329330
}
330331

331332
if (factory) {
332333
if (typeof factory.create !== 'function') {
333-
throw new Error('Failed to create an instance of \'' + fullName + '\'. ' +
334-
'Most likely an improperly defined class or an invalid module export.');
334+
throw new Error(`Failed to create an instance of '${fullName}'. Most likely an improperly defined class or` +
335+
` an invalid module export.`);
335336
}
336337

337338
validationCache = container.validationCache;
338339

339-
runInDebug(function() {
340+
runInDebug(() => {
340341
// Ensure that all lazy injections are valid at instantiation time
341342
if (!validationCache[fullName] && typeof factory._lazyInjections === 'function') {
342343
lazyInjections = factory._lazyInjections();
@@ -403,13 +404,12 @@ function injectDeprecatedContainer(object, container) {
403404
}
404405

405406
function eachDestroyable(container, callback) {
406-
var cache = container.cache;
407-
var keys = Object.keys(cache);
408-
var key, value;
407+
let cache = container.cache;
408+
let keys = Object.keys(cache);
409409

410-
for (var i = 0; i < keys.length; i++) {
411-
key = keys[i];
412-
value = cache[key];
410+
for (let i = 0; i < keys.length; i++) {
411+
let key = keys[i];
412+
let value = cache[key];
413413

414414
if (container.registry.getOption(key, 'instantiate') !== false) {
415415
callback(value);
@@ -418,7 +418,7 @@ function eachDestroyable(container, callback) {
418418
}
419419

420420
function resetCache(container) {
421-
eachDestroyable(container, function(value) {
421+
eachDestroyable(container, (value) => {
422422
if (value.destroy) {
423423
value.destroy();
424424
}
@@ -428,7 +428,7 @@ function resetCache(container) {
428428
}
429429

430430
function resetMember(container, fullName) {
431-
var member = container.cache[fullName];
431+
let member = container.cache[fullName];
432432

433433
delete container.factoryCache[fullName];
434434

@@ -440,5 +440,3 @@ function resetMember(container, fullName) {
440440
}
441441
}
442442
}
443-
444-
export default Container;

packages/container/lib/owner.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import symbol from 'ember-metal/symbol';
77

88
export const OWNER = symbol('OWNER');
99

10-
1110
/**
1211
Framework objects in an Ember application (components, services, routes, etc.)
1312
are created via a factory and dependency injection system. Each of these

0 commit comments

Comments
 (0)