@samitouri / QOS-React / commits / 16d8f20576

Add destructors to pooled classes in ReactChildren

And make destructors mandatory so we're less likely to forget again.

Ben Alpert committed Aug 26, 2015 at 17:19 UTC 16d8f20576088690e0c5cd2a749f550b7217dde1
4 files changed +32 -3
src/isomorphic/children/ReactChildren.js
+11
@@ -35,6 +35,11 @@ function ForEachBookKeeping(forEachFunction, forEachContext) {
35 this.context = forEachContext;
36 this.count = 0;
37 }
38 +ForEachBookKeeping.prototype.destructor = function() {
39 + this.func = null;
40 + this.context = null;
41 + this.count = 0;
42 +};
43 PooledClass.addPoolingTo(ForEachBookKeeping, twoArgumentPooler);
44
45 function forEachSingleChild(traverseContext, child, name) {
@@ -79,6 +84,12 @@ function MapBookKeeping(mapResult, mapFunction, mapContext) {
84 this.context = mapContext;
85 this.count = 0;
86 }
87 +MapBookKeeping.prototype.destructor = function() {
88 + this.result = null;
89 + this.func = null;
90 + this.context = null;
91 + this.count = 0;
92 +};
93 PooledClass.addPoolingTo(MapBookKeeping, threeArgumentPooler);
94
95 function mapSingleChildIntoContext(traverseContext, child, name) {
src/renderers/dom/client/eventPlugins/FallbackCompositionState.js
+6
@@ -35,6 +35,12 @@ function FallbackCompositionState(root) {
35 }
36
37 assign(FallbackCompositionState.prototype, {
38 + destructor: function() {
39 + this._root = null;
40 + this._startText = null;
41 + this._fallbackText = null;
42 + },
43 +
44 /**
45 * Get current text of input.
46 *
src/shared/utils/PooledClass.js
+1 -3
@@ -81,9 +81,7 @@ var standardReleaser = function(instance) {
81 instance instanceof Klass,
82 'Trying to release an instance into a pool of a different type.'
83 );
84 - if (instance.destructor) {
85 - instance.destructor();
86 - }
84 + instance.destructor();
85 if (Klass.instancePool.length < Klass.poolSize) {
86 Klass.instancePool.push(instance);
87 }
src/shared/utils/__tests__/PooledClass-test.js
+14
@@ -18,6 +18,7 @@ describe('Pooled class', function() {
18 beforeEach(function() {
19 PooledClass = require('PooledClass');
20 PoolableClass = function() {};
21 + PoolableClass.prototype.destructor = function() {};
22 PooledClass.addPoolingTo(PoolableClass);
23 });
24
@@ -63,6 +64,7 @@ describe('Pooled class', function() {
64 var PoolableClassWithMultiArguments = function(a, b) {
65 log.push(a, b);
66 };
67 + PoolableClassWithMultiArguments.prototype.destructor = function() {};
68 PooledClass.addPoolingTo(
69 PoolableClassWithMultiArguments,
70 PooledClass.twoArgumentPooler
@@ -76,6 +78,7 @@ describe('Pooled class', function() {
78 var PoolableClassWithOneArgument = function(a) {
79 log.push(a);
80 };
81 + PoolableClassWithOneArgument.prototype.destructor = function() {};
82 PooledClass.addPoolingTo(
83 PoolableClassWithOneArgument
84 );
@@ -88,6 +91,7 @@ describe('Pooled class', function() {
91 var PoolableClassWithOneArgument = function(a) {
92 log.push(a);
93 };
94 + PoolableClassWithOneArgument.prototype.destructor = function() {};
95 PooledClass.addPoolingTo(
96 PoolableClassWithOneArgument
97 );
@@ -100,6 +104,7 @@ describe('Pooled class', function() {
104 it('should throw when the class releases an instance of a different type',
105 function() {
106 var RandomClass = function() {};
107 + RandomClass.prototype.destructor = function() {};
108 PooledClass.addPoolingTo(RandomClass);
109 var randomInstance = RandomClass.getPooled();
110 PoolableClass.getPooled();
@@ -111,4 +116,13 @@ describe('Pooled class', function() {
116 );
117 }
118 );
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 + });
128 });