From 19b8eadb242054e2668cf90e83490ef552bcdbfe Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sun, 28 Aug 2016 10:43:13 -0700 Subject: [PATCH] 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/ --- src/renderers/shared/utils/CallbackQueue.js | 5 ++--- src/shared/utils/PooledClass.js | 25 +++++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/renderers/shared/utils/CallbackQueue.js b/src/renderers/shared/utils/CallbackQueue.js index 647c388710..167403dc80 100644 --- a/src/renderers/shared/utils/CallbackQueue.js +++ b/src/renderers/shared/utils/CallbackQueue.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule CallbackQueue + * @flow */ 'use strict'; @@ -101,6 +102,4 @@ Object.assign(CallbackQueue.prototype, { }); -PooledClass.addPoolingTo(CallbackQueue); - -module.exports = CallbackQueue; +module.exports = PooledClass.addPoolingTo(CallbackQueue); diff --git a/src/shared/utils/PooledClass.js b/src/shared/utils/PooledClass.js index 5ffc255ae9..45040901ff 100644 --- a/src/shared/utils/PooledClass.js +++ b/src/shared/utils/PooledClass.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule PooledClass + * @flow */ 'use strict'; @@ -90,6 +91,8 @@ var standardReleaser = function(instance) { var DEFAULT_POOL_SIZE = 10; var DEFAULT_POOLER = oneArgumentPooler; +type Pooler = any; + /** * Augments `CopyConstructor` to be a poolable class, augmenting only the class * itself (statically) not adding any prototypical fields. Any CopyConstructor @@ -99,8 +102,16 @@ var DEFAULT_POOLER = oneArgumentPooler; * @param {Function} CopyConstructor Constructor that can be used to reset. * @param {Function} pooler Customizable pooler. */ -var addPoolingTo = function(CopyConstructor, pooler) { - var NewKlass = CopyConstructor; +var addPoolingTo = function( + CopyConstructor: Class, + pooler: Pooler, +): Class & { + getPooled(/* arguments of the constructor */): T; + release(): void; +} { + // Casting as any so that flow ignores the actual implementation and trusts + // it to match the type we declared + var NewKlass = (CopyConstructor: any); NewKlass.instancePool = []; NewKlass.getPooled = pooler || DEFAULT_POOLER; if (!NewKlass.poolSize) { @@ -112,11 +123,11 @@ var addPoolingTo = function(CopyConstructor, pooler) { var PooledClass = { addPoolingTo: addPoolingTo, - oneArgumentPooler: oneArgumentPooler, - twoArgumentPooler: twoArgumentPooler, - threeArgumentPooler: threeArgumentPooler, - fourArgumentPooler: fourArgumentPooler, - fiveArgumentPooler: fiveArgumentPooler, + oneArgumentPooler: (oneArgumentPooler: Pooler), + twoArgumentPooler: (twoArgumentPooler: Pooler), + threeArgumentPooler: (threeArgumentPooler: Pooler), + fourArgumentPooler: (fourArgumentPooler: Pooler), + fiveArgumentPooler: (fiveArgumentPooler: Pooler), }; module.exports = PooledClass;