Skip to content

Commit 024f715

Browse files
committed
Merge pull request facebook#4720 from spicyj/destructor
Add destructors to pooled classes in ReactChildren
2 parents bdc7ce9 + 16d8f20 commit 024f715

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/isomorphic/children/ReactChildren.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ function ForEachBookKeeping(forEachFunction, forEachContext) {
3535
this.context = forEachContext;
3636
this.count = 0;
3737
}
38+
ForEachBookKeeping.prototype.destructor = function() {
39+
this.func = null;
40+
this.context = null;
41+
this.count = 0;
42+
};
3843
PooledClass.addPoolingTo(ForEachBookKeeping, twoArgumentPooler);
3944

4045
function forEachSingleChild(traverseContext, child, name) {
@@ -79,6 +84,12 @@ function MapBookKeeping(mapResult, mapFunction, mapContext) {
7984
this.context = mapContext;
8085
this.count = 0;
8186
}
87+
MapBookKeeping.prototype.destructor = function() {
88+
this.result = null;
89+
this.func = null;
90+
this.context = null;
91+
this.count = 0;
92+
};
8293
PooledClass.addPoolingTo(MapBookKeeping, threeArgumentPooler);
8394

8495
function mapSingleChildIntoContext(traverseContext, child, name) {

src/renderers/dom/client/eventPlugins/FallbackCompositionState.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ function FallbackCompositionState(root) {
3535
}
3636

3737
assign(FallbackCompositionState.prototype, {
38+
destructor: function() {
39+
this._root = null;
40+
this._startText = null;
41+
this._fallbackText = null;
42+
},
43+
3844
/**
3945
* Get current text of input.
4046
*

src/shared/utils/PooledClass.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ var standardReleaser = function(instance) {
8181
instance instanceof Klass,
8282
'Trying to release an instance into a pool of a different type.'
8383
);
84-
if (instance.destructor) {
85-
instance.destructor();
86-
}
84+
instance.destructor();
8785
if (Klass.instancePool.length < Klass.poolSize) {
8886
Klass.instancePool.push(instance);
8987
}

src/shared/utils/__tests__/PooledClass-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('Pooled class', function() {
1818
beforeEach(function() {
1919
PooledClass = require('PooledClass');
2020
PoolableClass = function() {};
21+
PoolableClass.prototype.destructor = function() {};
2122
PooledClass.addPoolingTo(PoolableClass);
2223
});
2324

@@ -63,6 +64,7 @@ describe('Pooled class', function() {
6364
var PoolableClassWithMultiArguments = function(a, b) {
6465
log.push(a, b);
6566
};
67+
PoolableClassWithMultiArguments.prototype.destructor = function() {};
6668
PooledClass.addPoolingTo(
6769
PoolableClassWithMultiArguments,
6870
PooledClass.twoArgumentPooler
@@ -76,6 +78,7 @@ describe('Pooled class', function() {
7678
var PoolableClassWithOneArgument = function(a) {
7779
log.push(a);
7880
};
81+
PoolableClassWithOneArgument.prototype.destructor = function() {};
7982
PooledClass.addPoolingTo(
8083
PoolableClassWithOneArgument
8184
);
@@ -88,6 +91,7 @@ describe('Pooled class', function() {
8891
var PoolableClassWithOneArgument = function(a) {
8992
log.push(a);
9093
};
94+
PoolableClassWithOneArgument.prototype.destructor = function() {};
9195
PooledClass.addPoolingTo(
9296
PoolableClassWithOneArgument
9397
);
@@ -100,6 +104,7 @@ describe('Pooled class', function() {
100104
it('should throw when the class releases an instance of a different type',
101105
function() {
102106
var RandomClass = function() {};
107+
RandomClass.prototype.destructor = function() {};
103108
PooledClass.addPoolingTo(RandomClass);
104109
var randomInstance = RandomClass.getPooled();
105110
PoolableClass.getPooled();
@@ -111,4 +116,13 @@ describe('Pooled class', function() {
111116
);
112117
}
113118
);
119+
120+
it('should throw if no destructor is defined', function() {
121+
var ImmortalClass = function() {};
122+
PooledClass.addPoolingTo(ImmortalClass);
123+
var inst = ImmortalClass.getPooled();
124+
expect(function() {
125+
ImmortalClass.release(inst);
126+
}).toThrow();
127+
});
114128
});

0 commit comments

Comments
 (0)