Type PooledClass (#7578)
This one was really interesting to type as it's doing a lot of unusual JavaScript. Fortunately flow is now pretty kick ass and I've been able to mostly type it. The only missing piece is that it won't check the constructor arguments. If you are a fb employee, you can follow the discussion here: https://www.facebook.com/groups/flowtype/permalink/1132359430146004/
Christopher Chedeau committed
Aug 28, 2016 at 10:43 UTC
19b8eadb242054e2668cf90e83490ef552bcdbfe
2 files changed
+20
-10
src/renderers/shared/utils/CallbackQueue.js
+2
-3
@@ -7,6 +7,7 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule CallbackQueue
10
+ * @flow
11
*/
12
13
'use strict';
@@ -101,6 +102,4 @@ Object.assign(CallbackQueue.prototype, {
102
103
});
104
104
-PooledClass.addPoolingTo(CallbackQueue);
105
-
106
-module.exports = CallbackQueue;
105
+module.exports = PooledClass.addPoolingTo(CallbackQueue);
src/shared/utils/PooledClass.js
+18
-7
@@ -7,6 +7,7 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule PooledClass
10
+ * @flow
11
*/
12
13
'use strict';
@@ -90,6 +91,8 @@ var standardReleaser = function(instance) {
91
var DEFAULT_POOL_SIZE = 10;
92
var DEFAULT_POOLER = oneArgumentPooler;
93
94
+type Pooler = any;
95
+
96
/**
97
* Augments `CopyConstructor` to be a poolable class, augmenting only the class
98
* itself (statically) not adding any prototypical fields. Any CopyConstructor
@@ -99,8 +102,16 @@ var DEFAULT_POOLER = oneArgumentPooler;
102
* @param {Function} CopyConstructor Constructor that can be used to reset.
103
* @param {Function} pooler Customizable pooler.
104
*/
102
-var addPoolingTo = function(CopyConstructor, pooler) {
103
- var NewKlass = CopyConstructor;
105
+var addPoolingTo = function<T>(
106
+ CopyConstructor: Class<T>,
107
+ pooler: Pooler,
108
+): Class<T> & {
109
+ getPooled(/* arguments of the constructor */): T;
110
+ release(): void;
111
+} {
112
+ // Casting as any so that flow ignores the actual implementation and trusts
113
+ // it to match the type we declared
114
+ var NewKlass = (CopyConstructor: any);
115
NewKlass.instancePool = [];
116
NewKlass.getPooled = pooler || DEFAULT_POOLER;
117
if (!NewKlass.poolSize) {
@@ -112,11 +123,11 @@ var addPoolingTo = function(CopyConstructor, pooler) {
123
124
var PooledClass = {
125
addPoolingTo: addPoolingTo,
115
- oneArgumentPooler: oneArgumentPooler,
116
- twoArgumentPooler: twoArgumentPooler,
117
- threeArgumentPooler: threeArgumentPooler,
118
- fourArgumentPooler: fourArgumentPooler,
119
- fiveArgumentPooler: fiveArgumentPooler,
126
+ oneArgumentPooler: (oneArgumentPooler: Pooler),
127
+ twoArgumentPooler: (twoArgumentPooler: Pooler),
128
+ threeArgumentPooler: (threeArgumentPooler: Pooler),
129
+ fourArgumentPooler: (fourArgumentPooler: Pooler),
130
+ fiveArgumentPooler: (fiveArgumentPooler: Pooler),
131
};
132
133
module.exports = PooledClass;