Merge pull request #4720 from spicyj/destructor

Add destructors to pooled classes in ReactChildren
This commit is contained in:
Ben Alpert
2015-08-27 19:06:11 -07:00
4 changed files with 32 additions and 3 deletions
+11
View File
@@ -35,6 +35,11 @@ function ForEachBookKeeping(forEachFunction, forEachContext) {
this.context = forEachContext;
this.count = 0;
}
ForEachBookKeeping.prototype.destructor = function() {
this.func = null;
this.context = null;
this.count = 0;
};
PooledClass.addPoolingTo(ForEachBookKeeping, twoArgumentPooler);
function forEachSingleChild(traverseContext, child, name) {
@@ -79,6 +84,12 @@ function MapBookKeeping(mapResult, mapFunction, mapContext) {
this.context = mapContext;
this.count = 0;
}
MapBookKeeping.prototype.destructor = function() {
this.result = null;
this.func = null;
this.context = null;
this.count = 0;
};
PooledClass.addPoolingTo(MapBookKeeping, threeArgumentPooler);
function mapSingleChildIntoContext(traverseContext, child, name) {
@@ -35,6 +35,12 @@ function FallbackCompositionState(root) {
}
assign(FallbackCompositionState.prototype, {
destructor: function() {
this._root = null;
this._startText = null;
this._fallbackText = null;
},
/**
* Get current text of input.
*
+1 -3
View File
@@ -81,9 +81,7 @@ var standardReleaser = function(instance) {
instance instanceof Klass,
'Trying to release an instance into a pool of a different type.'
);
if (instance.destructor) {
instance.destructor();
}
instance.destructor();
if (Klass.instancePool.length < Klass.poolSize) {
Klass.instancePool.push(instance);
}
@@ -18,6 +18,7 @@ describe('Pooled class', function() {
beforeEach(function() {
PooledClass = require('PooledClass');
PoolableClass = function() {};
PoolableClass.prototype.destructor = function() {};
PooledClass.addPoolingTo(PoolableClass);
});
@@ -63,6 +64,7 @@ describe('Pooled class', function() {
var PoolableClassWithMultiArguments = function(a, b) {
log.push(a, b);
};
PoolableClassWithMultiArguments.prototype.destructor = function() {};
PooledClass.addPoolingTo(
PoolableClassWithMultiArguments,
PooledClass.twoArgumentPooler
@@ -76,6 +78,7 @@ describe('Pooled class', function() {
var PoolableClassWithOneArgument = function(a) {
log.push(a);
};
PoolableClassWithOneArgument.prototype.destructor = function() {};
PooledClass.addPoolingTo(
PoolableClassWithOneArgument
);
@@ -88,6 +91,7 @@ describe('Pooled class', function() {
var PoolableClassWithOneArgument = function(a) {
log.push(a);
};
PoolableClassWithOneArgument.prototype.destructor = function() {};
PooledClass.addPoolingTo(
PoolableClassWithOneArgument
);
@@ -100,6 +104,7 @@ describe('Pooled class', function() {
it('should throw when the class releases an instance of a different type',
function() {
var RandomClass = function() {};
RandomClass.prototype.destructor = function() {};
PooledClass.addPoolingTo(RandomClass);
var randomInstance = RandomClass.getPooled();
PoolableClass.getPooled();
@@ -111,4 +116,13 @@ describe('Pooled class', function() {
);
}
);
it('should throw if no destructor is defined', function() {
var ImmortalClass = function() {};
PooledClass.addPoolingTo(ImmortalClass);
var inst = ImmortalClass.getPooled();
expect(function() {
ImmortalClass.release(inst);
}).toThrow();
});
});