Skip to content

Commit

Permalink
[BUGFIX beta] Make container always return the same value even if the…
Browse files Browse the repository at this point in the history
… registered value is falsy
  • Loading branch information
hibariya committed Jan 10, 2016
1 parent f34af07 commit 46257f1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/container/lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function lookup(container, _fullName, _options) {
}
}

if (container.cache[fullName] && options.singleton !== false) {
if (container.cache[fullName] !== undefined && options.singleton !== false) {
return container.cache[fullName];
}

Expand Down
12 changes: 7 additions & 5 deletions packages/container/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ function resolve(registry, normalizedName, options) {
}

var cached = registry._resolveCache[normalizedName];
if (cached) { return cached; }
if (cached !== undefined) { return cached; }
if (registry._failCache[normalizedName]) { return; }

let resolved;
Expand All @@ -849,12 +849,14 @@ function resolve(registry, normalizedName, options) {
resolved = registry.resolver.resolve(normalizedName);
}

resolved = resolved || registry.registrations[normalizedName];
if (resolved === undefined) {
resolved = registry.registrations[normalizedName];
}

if (resolved) {
registry._resolveCache[normalizedName] = resolved;
} else {
if (resolved === undefined) {
registry._failCache[normalizedName] = true;
} else {
registry._resolveCache[normalizedName] = resolved;
}

return resolved;
Expand Down
11 changes: 10 additions & 1 deletion packages/container/tests/container_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,16 @@ QUnit.test('Injecting a falsy value does not raise an error', function() {
registry.register('user:current', null, { instantiate: false });
registry.injection('controller:application', 'currentUser', 'user:current');

equal(container.lookup('controller:application').currentUser, null);
deepEqual(container.lookup('controller:application').currentUser, null);
});

QUnit.test('The container returns same value each time even if the value is falsy', function() {
var registry = new Registry();
var container = registry.container();

registry.register('falsy:value', null, { instantiate: false });

deepEqual(container.lookup('falsy:value'), container.lookup('falsy:value'));
});

QUnit.test('Destroying the container destroys any cached singletons', function() {
Expand Down
8 changes: 8 additions & 0 deletions packages/container/tests/registry_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ QUnit.test('The registered factory returned from resolve is the same factory eac
deepEqual(registry.resolve('controller:post'), registry.resolve('controller:post'), 'The return of resolve is always the same');
});

QUnit.test('The registered value returned from resolve is the same value each time even if the value is falsy', function() {
var registry = new Registry();

registry.register('falsy:value', null, { instantiate: false });

deepEqual(registry.resolve('falsy:value'), registry.resolve('falsy:value'), 'The return of resolve is always the same');
});

QUnit.test('A registered factory returns true for `has` if an item is registered', function() {
var registry = new Registry();
var PostController = factory();
Expand Down

0 comments on commit 46257f1

Please sign in to comment.