Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed singleton disposal

  • Loading branch information...
commit 9684982d9b7f70efc2519eb52ed0e35dcab18f83 1 parent 48fbab0
@RoyJacobs authored
Showing with 20 additions and 2 deletions.
  1. +17 −1 spec/spec/singletonSpecs.js
  2. +3 −1 src/container.js
View
18 spec/spec/singletonSpecs.js
@@ -3,7 +3,13 @@ describe("A singleton registration", function() {
var _this = this;
this.constructionCount = 0;
- this.container = intravenous.create();
+ this.disposalCount = {};
+ this.container = intravenous.create({
+ onDispose: function(obj, key) {
+ if (!_this.disposalCount[key]) _this.disposalCount[key] = 0;
+ _this.disposalCount[key]++;
+ }
+ });
this.a = function() {
_this.constructionCount++;
@@ -20,5 +26,15 @@ describe("A singleton registration", function() {
it("should only construct the object once", function() {
expect(this.constructionCount).toEqual(1);
});
+
+ describe("and disposed again", function() {
+ beforeEach(function() {
+ this.container.dispose();
+ })
+
+ it("should only dispose the object once", function() {
+ expect(this.disposalCount.a).toEqual(1);
+ });
+ });
});
});
View
4 src/container.js
@@ -71,6 +71,7 @@
var singletonLifecycle = function(container, parentLifecycle) {
this.container = container;
this.cache = [];
+ this.refCounts = {};
this.parent = parentLifecycle;
};
@@ -93,10 +94,11 @@
set: function(cacheItem) {
this.cache.push(cacheItem);
+ this.refCounts[cacheItem.registration.key] = this.refCounts[cacheItem.registration.key]++ || 1;
},
release: function(cacheItem) {
- return cacheItem.registration.container === this.container;
+ return !--this.refCounts[cacheItem.registration.key];
},
resolveStarted: function(key) {
Please sign in to comment.
Something went wrong with that request. Please try again.