Skip to content

Commit

Permalink
Remove debug -> metal cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
chadhietala committed Mar 14, 2017
1 parent dfbd82b commit 49a40bb
Show file tree
Hide file tree
Showing 158 changed files with 483 additions and 572 deletions.
167 changes: 68 additions & 99 deletions packages/container/lib/container.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert, deprecate, runInDebug, isFeatureEnabled } from 'ember-debug';
/* globals Proxy */
import {
dictionary,
Expand All @@ -9,12 +10,6 @@ import {
HAS_NATIVE_PROXY
} from 'ember-utils';
import { ENV } from 'ember-environment';
import {
assert,
deprecate,
runInDebug,
isFeatureEnabled
} from 'ember-metal';

const CONTAINER_OVERRIDE = symbol('CONTAINER_OVERRIDE');
export const FACTORY_FOR = symbol('FACTORY_FOR');
Expand All @@ -34,10 +29,10 @@ export const LOOKUP_FACTORY = symbol('LOOKUP_FACTORY');
@class Container
*/
export default function Container(registry, options) {
this.registry = registry;
this.owner = options && options.owner ? options.owner : null;
this.cache = dictionary(options && options.cache ? options.cache : null);
this.factoryCache = dictionary(options && options.factoryCache ? options.factoryCache : null);
this.registry = registry;
this.owner = options && options.owner ? options.owner : null;
this.cache = dictionary(options && options.cache ? options.cache : null);
this.factoryCache = dictionary(options && options.factoryCache ? options.factoryCache : null);
this.validationCache = dictionary(options && options.validationCache ? options.validationCache : null);
this._fakeContainerToInject = buildFakeContainerWithDeprecations(this);
this[CONTAINER_OVERRIDE] = undefined;
Expand Down Expand Up @@ -83,43 +78,30 @@ Container.prototype = {

/**
Given a fullName return a corresponding instance.
The default behaviour is for lookup to return a singleton instance.
The default behaviour is for lookup to return a singleton instance.
The singleton is scoped to the container, allowing multiple containers
to all have their own locally scoped singletons.
```javascript
```javascript
let registry = new Registry();
let container = registry.container();
registry.register('api:twitter', Twitter);
let twitter = container.lookup('api:twitter');
twitter instanceof Twitter; // => true
// by default the container will return singletons
registry.register('api:twitter', Twitter);
let twitter = container.lookup('api:twitter');
twitter instanceof Twitter; // => true
// by default the container will return singletons
let twitter2 = container.lookup('api:twitter');
twitter2 instanceof Twitter; // => true
twitter === twitter2; //=> true
twitter === twitter2; //=> true
```
If singletons are not wanted, an optional flag can be provided at lookup.
```javascript
If singletons are not wanted, an optional flag can be provided at lookup.
```javascript
let registry = new Registry();
let container = registry.container();
registry.register('api:twitter', Twitter);
let twitter = container.lookup('api:twitter', { singleton: false });
registry.register('api:twitter', Twitter);
let twitter = container.lookup('api:twitter', { singleton: false });
let twitter2 = container.lookup('api:twitter', { singleton: false });
twitter === twitter2; //=> false
twitter === twitter2; //=> false
```
@private
@private
@method lookup
@param {String} fullName
@param {Object} [options]
Expand All @@ -133,8 +115,7 @@ Container.prototype = {

/**
Given a fullName, return the corresponding factory.
@private
@private
@method lookupFactory
@param {String} fullName
@param {Object} [options]
Expand All @@ -144,11 +125,7 @@ Container.prototype = {
lookupFactory(fullName, options) {
assert('fullName must be a proper full name', this.registry.validateFullName(fullName));

deprecate(
'Using "_lookupFactory" is deprecated. Please use container.factoryFor instead.',
!isFeatureEnabled('ember-factory-for'),
{ id: 'container-lookupFactory', until: '2.13.0', url: 'http://emberjs.com/deprecations/v2.x/#toc_migrating-from-_lookupfactory-to-factoryfor' }
);
deprecate('Using "_lookupFactory" is deprecated. Please use container.factoryFor instead.', !isFeatureEnabled('ember-factory-for'), { id: 'container-lookupFactory', until: '2.13.0', url: 'http://emberjs.com/deprecations/v2.x/#toc_migrating-from-_lookupfactory-to-factoryfor' });

return deprecatedFactoryFor(this, this.registry.normalize(fullName), options);
},
Expand All @@ -174,7 +151,9 @@ Container.prototype = {
}
}
let factory = this[LOOKUP_FACTORY](fullName, options);
if (factory === undefined) { return; }
if (factory === undefined) {
return;
}
let manager = new DeprecatedFactoryManager(this, factory, fullName);

runInDebug(() => {
Expand All @@ -187,8 +166,7 @@ Container.prototype = {
/**
A depth first traversal, destroying the container, its descendant containers and all
their managed objects.
@private
@private
@method destroy
*/
destroy() {
Expand All @@ -203,8 +181,7 @@ Container.prototype = {

/**
Clear either the entire cache or just the cache for a particular key.
@private
@private
@method reset
@param {String} fullName optional key to reset; if missing, resets everything
*/
Expand All @@ -219,8 +196,7 @@ Container.prototype = {
/**
Returns an object that can be used to provide an owner to a
manually created instance.
@private
@private
@method ownerInjection
@returns { Object }
*/
Expand Down Expand Up @@ -271,8 +247,7 @@ if (isFeatureEnabled('ember-factory-for')) {
is responsible for the destruction of any factory instances, as there is no
way for the container to ensure instances are destroyed when it itself is
destroyed.
@public
@public
@method factoryFor
@param {String} fullName
@param {Object} [options]
Expand All @@ -286,12 +261,16 @@ if (isFeatureEnabled('ember-factory-for')) {
if (options.source) {
normalizedName = this.registry.expandLocalLookup(fullName, options);
// if expandLocalLookup returns falsey, we do not support local lookup
if (!normalizedName) { return; }
if (!normalizedName) {
return;
}
}

let factory = this.registry.resolve(normalizedName);

if (factory === undefined) { return; }
if (factory === undefined) {
return;
}

let manager = new FactoryManager(this, factory, fullName, normalizedName);

Expand All @@ -316,7 +295,9 @@ function lookup(container, fullName, options = {}) {
fullName = container.registry.expandLocalLookup(fullName, options);

// if expandLocalLookup returns falsey, we do not support local lookup
if (!fullName) { return; }
if (!fullName) {
return;
}
}

if (container.cache[fullName] !== undefined && options.singleton !== false) {
Expand All @@ -329,7 +310,9 @@ function lookup(container, fullName, options = {}) {
let factory = deprecatedFactoryFor(container, fullName);
let value = instantiate(factory, {}, container, fullName);

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

if (isSingleton(container, fullName) && options.singleton !== false) {
container.cache[fullName] = value;
Expand All @@ -340,29 +323,27 @@ function lookup(container, fullName, options = {}) {
}

function isSingletonClass(container, fullName, { instantiate, singleton }) {
return (singleton !== false && isSingleton(container, fullName)) &&
(!instantiate && !shouldInstantiate(container, fullName));
return singleton !== false && isSingleton(container, fullName) && !instantiate && !shouldInstantiate(container, fullName);
}

function isSingletonInstance(container, fullName, { instantiate, singleton }) {
return (singleton !== false && isSingleton(container, fullName)) &&
(instantiate !== false && shouldInstantiate(container, fullName));
return singleton !== false && isSingleton(container, fullName) && instantiate !== false && shouldInstantiate(container, fullName);
}

function isFactoryClass(container, fullname, { instantiate, singleton }) {
return (singleton === false || !isSingleton(container, fullname)) &&
(instantiate === false && !shouldInstantiate(container, fullname));
return (singleton === false || !isSingleton(container, fullname)) && instantiate === false && !shouldInstantiate(container, fullname);
}

function isFactoryInstance(container, fullName, { instantiate, singleton }) {
return (singleton !== false || isSingleton(container, fullName)) &&
(instantiate !== false && shouldInstantiate(container, fullName));
return (singleton !== false || isSingleton(container, fullName)) && instantiate !== false && shouldInstantiate(container, fullName);
}

function instantiateFactory(container, fullName, options) {
let factoryManager = container[FACTORY_FOR](fullName);

if (factoryManager === undefined) { return; }
if (factoryManager === undefined) {
return;
}

// SomeClass { singleton: true, instantiate: true } | { singleton: true } | { instantiate: true } | {}
// By default majority of objects fall into this case
Expand Down Expand Up @@ -391,7 +372,7 @@ function areInjectionsDynamic(injections) {
return !!injections._dynamic;
}

function buildInjections(/* container, ...injections */) {
function buildInjections() /* container, ...injections */{
let hash = {};

if (arguments.length > 1) {
Expand Down Expand Up @@ -427,18 +408,22 @@ function deprecatedFactoryFor(container, fullName, options = {}) {
if (options.source) {
fullName = registry.expandLocalLookup(fullName, options);
// if expandLocalLookup returns falsey, we do not support local lookup
if (!fullName) { return; }
if (!fullName) {
return;
}
}

let cache = container.factoryCache;
if (cache[fullName]) {
return cache[fullName];
}
let factory = registry.resolve(fullName);
if (factory === undefined) { return; }
if (factory === undefined) {
return;
}

let type = fullName.split(':')[0];
if (!factory || typeof factory.extend !== 'function' || (!ENV.MODEL_FACTORY_INJECTIONS && type === 'model')) {
if (!factory || typeof factory.extend !== 'function' || !ENV.MODEL_FACTORY_INJECTIONS && type === 'model') {
if (factory && typeof factory._onLookup === 'function') {
factory._onLookup(fullName);
}
Expand Down Expand Up @@ -477,9 +462,7 @@ function injectionsFor(container, fullName) {
let splitName = fullName.split(':');
let type = splitName[0];

let injections = buildInjections(container,
registry.getTypeInjections(type),
registry.getInjections(fullName));
let injections = buildInjections(container, registry.getTypeInjections(type), registry.getInjections(fullName));
injections._debugContainerKey = fullName;

setOwner(injections, container.owner);
Expand All @@ -498,8 +481,7 @@ function instantiate(factory, props, container, fullName) {

if (factory) {
if (typeof factory.create !== 'function') {
throw new Error(`Failed to create an instance of '${fullName}'. Most likely an improperly defined class or` +
` an invalid module export.`);
throw new Error(`Failed to create an instance of '${fullName}'. Most likely an improperly defined class or` + ` an invalid module export.`);
}

validationCache = container.validationCache;
Expand Down Expand Up @@ -549,9 +531,7 @@ function factoryInjectionsFor(container, fullName) {
let splitName = fullName.split(':');
let type = splitName[0];

let factoryInjections = buildInjections(container,
registry.getFactoryTypeInjections(type),
registry.getFactoryInjections(fullName));
let factoryInjections = buildInjections(container, registry.getFactoryTypeInjections(type), registry.getFactoryInjections(fullName));
factoryInjections._debugContainerKey = fullName;

return factoryInjections;
Expand All @@ -563,18 +543,12 @@ function injectDeprecatedContainer(object, container) {
configurable: true,
enumerable: false,
get() {
deprecate('Using the injected `container` is deprecated. Please use the `getOwner` helper instead to access the owner of this object.',
false,
{ id: 'ember-application.injected-container', until: '3.0.0', url: 'http://emberjs.com/deprecations/v2.x#toc_injected-container-access' });
deprecate('Using the injected `container` is deprecated. Please use the `getOwner` helper instead to access the owner of this object.', false, { id: 'ember-application.injected-container', until: '3.0.0', url: 'http://emberjs.com/deprecations/v2.x#toc_injected-container-access' });
return this[CONTAINER_OVERRIDE] || container;
},

set(value) {
deprecate(
`Providing the \`container\` property to ${this} is deprecated. Please use \`Ember.setOwner\` or \`owner.ownerInjection()\` instead to provide an owner to the instance being created.`,
false,
{ id: 'ember-application.injected-container', until: '3.0.0', url: 'http://emberjs.com/deprecations/v2.x#toc_injected-container-access' }
);
deprecate(`Providing the \`container\` property to ${this} is deprecated. Please use \`Ember.setOwner\` or \`owner.ownerInjection()\` instead to provide an owner to the instance being created.`, false, { id: 'ember-application.injected-container', until: '3.0.0', url: 'http://emberjs.com/deprecations/v2.x#toc_injected-container-access' });

this[CONTAINER_OVERRIDE] = value;

Expand All @@ -598,7 +572,7 @@ function eachDestroyable(container, callback) {
}

function resetCache(container) {
eachDestroyable(container, (value) => {
eachDestroyable(container, value => {
if (value.destroy) {
value.destroy();
}
Expand Down Expand Up @@ -636,16 +610,12 @@ export function buildFakeContainerWithDeprecations(container) {
}

function buildFakeContainerFunction(container, containerProperty, ownerProperty) {
return function() {
deprecate(
`Using the injected \`container\` is deprecated. Please use the \`getOwner\` helper to access the owner of this object and then call \`${ownerProperty}\` instead.`,
false,
{
id: 'ember-application.injected-container',
until: '3.0.0',
url: 'http://emberjs.com/deprecations/v2.x#toc_injected-container-access'
}
);
return function () {
deprecate(`Using the injected \`container\` is deprecated. Please use the \`getOwner\` helper to access the owner of this object and then call \`${ownerProperty}\` instead.`, false, {
id: 'ember-application.injected-container',
until: '3.0.0',
url: 'http://emberjs.com/deprecations/v2.x#toc_injected-container-access'
});
return container[containerProperty](...arguments);
};
}
Expand Down Expand Up @@ -691,8 +661,7 @@ class FactoryManager {
});

if (!this.class.create) {
throw new Error(`Failed to create an instance of '${this.normalizedName}'. Most likely an improperly defined class or` +
` an invalid module export.`);
throw new Error(`Failed to create an instance of '${this.normalizedName}'. Most likely an improperly defined class or` + ` an invalid module export.`);
}

if (this.class.prototype) {
Expand All @@ -701,4 +670,4 @@ class FactoryManager {

return this.class.create(props);
}
}
}
2 changes: 1 addition & 1 deletion packages/container/lib/registry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dictionary, EmptyObject, assign, intern } from 'ember-utils';
import { assert, deprecate } from 'ember-metal';
import { assert, deprecate } from 'ember-debug';
import Container from './container';

const VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+$/;
Expand Down
3 changes: 2 additions & 1 deletion packages/container/tests/container_test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getOwner, OWNER } from 'ember-utils';
import { ENV } from 'ember-environment';
import { get, isFeatureEnabled } from 'ember-metal';
import { get } from 'ember-metal';
import { Registry } from '../index';
import { factory } from 'internal-test-helpers';
import { isFeatureEnabled } from 'ember-debug';
import { LOOKUP_FACTORY, FACTORY_FOR } from 'container';

let originalModelInjections;
Expand Down
Loading

0 comments on commit 49a40bb

Please sign in to comment.