Skip to content

Commit

Permalink
Merge pull request #15710 from bekzod/reset-cache
Browse files Browse the repository at this point in the history
correctly reset container cache
  • Loading branch information
rwjblue authored Oct 18, 2017
2 parents 922aed6 + 78d1aa6 commit 9bacd68
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/container/lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ Container.prototype = {
@param {String} fullName optional key to reset; if missing, resets everything
*/
reset(fullName) {
if (fullName !== undefined) {
resetMember(this, this.registry.normalize(fullName));
} else {
if (fullName === undefined) {
resetCache(this);
} else {
resetMember(this, this.registry.normalize(fullName));
}
},

Expand Down Expand Up @@ -366,7 +366,8 @@ function destroyDestroyables(container) {

function resetCache(container) {
destroyDestroyables(container);
container.cache.dict = dictionary(null);
container.cache = dictionary(null);
container.factoryManagerCache = dictionary(null);
}

function resetMember(container, fullName) {
Expand Down
25 changes: 25 additions & 0 deletions packages/container/tests/container_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,31 @@ QUnit.test('#factoryFor instance have a common parent', (assert) => {
assert.deepEqual(instance1.constructor, instance2.constructor);
});

QUnit.test('can properly reset cache', (assert) => {
let registry = new Registry();
let container = registry.container();

let Component = factory();
registry.register('component:foo-bar', Component);

let factory1 = container.factoryFor('component:foo-bar');
let factory2 = container.factoryFor('component:foo-bar');

let instance1 = container.lookup('component:foo-bar');
let instance2 = container.lookup('component:foo-bar');

assert.equal(instance1, instance2);
assert.equal(factory1, factory2);

container.reset();

let factory3 = container.factoryFor('component:foo-bar');
let instance3 = container.lookup('component:foo-bar');

assert.notEqual(instance1, instance3);
assert.notEqual(factory1, factory3);
});

QUnit.test('#factoryFor created instances come with instance injections', (assert) => {
let registry = new Registry();
let container = registry.container();
Expand Down

0 comments on commit 9bacd68

Please sign in to comment.