Skip to content

Commit

Permalink
Merge pull request #16615 from emberjs/fix-namespace-destroy
Browse files Browse the repository at this point in the history
[BUGFIX release] Fix NAMESPACES_BY_ID leaks
  • Loading branch information
krisselden authored May 7, 2018
2 parents 2501d75 + 2a90820 commit 56bc672
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 25 deletions.
8 changes: 2 additions & 6 deletions packages/@ember/application/tests/application_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*globals EmberDev */
import VERSION from 'ember/version';
import { ENV, context } from 'ember-environment';
import { libraries, setNamespaceSearchDisabled } from 'ember-metal';
import { libraries } from 'ember-metal';
import { getDebugFunction, setDebugFunction } from '@ember/debug';
import Application from '..';
import { Router, NoneLocation, Route as EmberRoute } from 'ember-routing';
Expand Down Expand Up @@ -232,11 +232,7 @@ moduleFor(
}

[`@test acts like a namespace`](assert) {
let lookup = (context.lookup = {});

lookup.TestApp = this.application = this.runTask(() => this.createApplication());

setNamespaceSearchDisabled(false);
this.application = this.runTask(() => this.createApplication());
let Foo = (this.application.Foo = EmberObject.extend());
assert.equal(Foo.toString(), 'TestApp.Foo', 'Classes pick up their parent namespace');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ moduleFor(
expectAssertion(() => {
this.application.FooService = EmberObject.extend();
this.privateRegistry.resolve('service:foo');
}, /Expected service:foo to resolve to an Ember.Service but instead it was \.FooService\./);
}, /Expected service:foo to resolve to an Ember.Service but instead it was TestApp\.FooService\./);
}

[`@test no deprecation warning for service factories that extend from Service`](assert) {
Expand All @@ -219,7 +219,7 @@ moduleFor(
expectAssertion(() => {
this.application.FooComponent = EmberObject.extend();
this.privateRegistry.resolve('component:foo');
}, /Expected component:foo to resolve to an Ember\.Component but instead it was \.FooComponent\./);
}, /Expected component:foo to resolve to an Ember\.Component but instead it was TestApp\.FooComponent\./);
}

[`@test no deprecation warning for component factories that extend from Component`]() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ moduleFor(

['@test factories'](assert) {
let PostFactory = this.applicationInstance.factoryFor('model:post').class;
assert.equal(PostFactory.toString(), '.Post', 'expecting the model to be post');
assert.equal(PostFactory.toString(), 'TestApp.Post', 'expecting the model to be post');
}

['@test instances'](assert) {
let post = this.applicationInstance.lookup('model:post');
let guid = guidFor(post);

assert.equal(post.toString(), '<.Post:' + guid + '>', 'expecting the model to be post');
assert.equal(
post.toString(),
'<TestApp.Post:' + guid + '>',
'expecting the model to be post'
);
}
}
);
Expand Down
19 changes: 9 additions & 10 deletions packages/ember-metal/lib/namespace_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ export function addNamespace(namespace) {
}

export function removeNamespace(namespace) {
let id = namespace.toString();
if (id) {
delete NAMESPACES_BY_ID[id];
if (namespace === context.lookup[id]) {
context.lookup[id] = undefined;
}
}

let name = getName(namespace);
delete NAMESPACES_BY_ID[name];
NAMESPACES.splice(NAMESPACES.indexOf(namespace), 1);
if (name in context.lookup && namespace === context.lookup[name]) {
context.lookup[name] = undefined;
}
}

export function findNamespaces() {
Expand Down Expand Up @@ -117,7 +114,10 @@ export function setUnprocessedMixins() {
function _processNamespace(paths, root, seen) {
let idx = paths.length;

NAMESPACES_BY_ID[paths.join('.')] = root;
let id = paths.join('.');

NAMESPACES_BY_ID[id] = root;
setName(root, id);

// Loop over all of the keys in the namespace, looking for classes
for (let key in root) {
Expand All @@ -144,7 +144,6 @@ function _processNamespace(paths, root, seen) {
continue;
}
seen.add(obj);

// Process the child namespace
_processNamespace(paths, obj, seen);
}
Expand Down
10 changes: 7 additions & 3 deletions packages/ember-runtime/lib/system/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
processAllNamespaces,
removeNamespace,
} from 'ember-metal'; // Preloaded into namespaces
import { getName } from 'ember-utils';
import { getName, guidFor, setName } from 'ember-utils';
import EmberObject from './object';

/**
Expand Down Expand Up @@ -45,9 +45,13 @@ const Namespace = EmberObject.extend({
if (name) {
return name;
}

findNamespaces();
return getName(this);
name = getName(this);
if (name === undefined) {
name = guidFor(this);
setName(this, name);
}
return name;
},

nameClasses() {
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-runtime/tests/system/namespace/base_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { context } from 'ember-environment';
import { run } from '@ember/runloop';
import { get, setNamespaceSearchDisabled } from 'ember-metal';
import { guidFor } from 'ember-utils';
import EmberObject from '../../../lib/system/object';
import Namespace from '../../../lib/system/namespace';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
Expand Down Expand Up @@ -87,7 +88,7 @@ moduleFor(

['@test Lowercase namespaces are no longer supported'](assert) {
let nsC = (lookup.namespaceC = Namespace.create());
assert.equal(nsC.toString(), undefined);
assert.equal(nsC.toString(), guidFor(nsC));
}

['@test A namespace can be assigned a custom name'](assert) {
Expand Down
9 changes: 8 additions & 1 deletion packages/internal-test-helpers/lib/ember-dev/namespaces.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { run } from '@ember/runloop';
import { NAMESPACES } from 'ember-metal';
import { NAMESPACES, NAMESPACES_BY_ID } from 'ember-metal';

function NamespacesAssert(env) {
this.env = env;
Expand All @@ -20,6 +20,13 @@ NamespacesAssert.prototype = {
}
});
}
let keys = Object.keys(NAMESPACES_BY_ID);
if (keys.length > 0) {
assert.ok(false, 'Should not have any NAMESPACES_BY_ID after tests');
for (let i = 0; i < keys.length; i++) {
delete NAMESPACES_BY_ID[keys[i]];
}
}
},
restore: function() {},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class ApplicationTestCase extends AbstractApplicationTestCase {

get applicationOptions() {
return assign(super.applicationOptions, {
name: 'TestApp',
autoboot: false,
Resolver: DefaultResolver,
});
Expand Down

0 comments on commit 56bc672

Please sign in to comment.