@samitouri / QOS-React / commits / 07b1da8964

Removed PooledClass (and tests) (#11053)

Brian Vaughn committed Oct 2, 2017 at 16:54 UTC 07b1da89642fa9939e86488825b0476411babfe6
3 files changed -240
scripts/rollup/packaging.js
-2
@@ -24,8 +24,6 @@ const facebookWWWSrcDependencies = [
24
25 // these files need to be copied to the react-native build
26 const reactNativeSrcDependencies = [
27 - // TODO: copy this to RN repository and delete from React
28 - 'src/renderers/native/PooledClass.js',
27 'src/renderers/shared/fiber/isomorphic/ReactTypes.js',
28 'src/renderers/native/ReactNativeTypes.js',
29 ];
src/renderers/native/PooledClass.js deleted
-121
@@ -1,121 +0,0 @@
1 -/**
2 - * Copyright (c) 2013-present, Facebook, Inc.
3 - *
4 - * This source code is licensed under the MIT license found in the
5 - * LICENSE file in the root directory of this source tree.
6 - *
7 - * @providesModule PooledClass
8 - * @flow
9 - */
10 -
11 -'use strict';
12 -
13 -var invariant = require('fbjs/lib/invariant');
14 -
15 -/**
16 - * Static poolers. Several custom versions for each potential number of
17 - * arguments. A completely generic pooler is easy to implement, but would
18 - * require accessing the `arguments` object. In each of these, `this` refers to
19 - * the Class itself, not an instance. If any others are needed, simply add them
20 - * here, or in their own files.
21 - */
22 -var oneArgumentPooler = function(copyFieldsFrom) {
23 - var Klass = this;
24 - if (Klass.instancePool.length) {
25 - var instance = Klass.instancePool.pop();
26 - Klass.call(instance, copyFieldsFrom);
27 - return instance;
28 - } else {
29 - return new Klass(copyFieldsFrom);
30 - }
31 -};
32 -
33 -var twoArgumentPooler = function(a1, a2) {
34 - var Klass = this;
35 - if (Klass.instancePool.length) {
36 - var instance = Klass.instancePool.pop();
37 - Klass.call(instance, a1, a2);
38 - return instance;
39 - } else {
40 - return new Klass(a1, a2);
41 - }
42 -};
43 -
44 -var threeArgumentPooler = function(a1, a2, a3) {
45 - var Klass = this;
46 - if (Klass.instancePool.length) {
47 - var instance = Klass.instancePool.pop();
48 - Klass.call(instance, a1, a2, a3);
49 - return instance;
50 - } else {
51 - return new Klass(a1, a2, a3);
52 - }
53 -};
54 -
55 -var fourArgumentPooler = function(a1, a2, a3, a4) {
56 - var Klass = this;
57 - if (Klass.instancePool.length) {
58 - var instance = Klass.instancePool.pop();
59 - Klass.call(instance, a1, a2, a3, a4);
60 - return instance;
61 - } else {
62 - return new Klass(a1, a2, a3, a4);
63 - }
64 -};
65 -
66 -var standardReleaser = function(instance) {
67 - var Klass = this;
68 - invariant(
69 - instance instanceof Klass,
70 - 'Trying to release an instance into a pool of a different type.',
71 - );
72 - instance.destructor();
73 - if (Klass.instancePool.length < Klass.poolSize) {
74 - Klass.instancePool.push(instance);
75 - }
76 -};
77 -
78 -var DEFAULT_POOL_SIZE = 10;
79 -var DEFAULT_POOLER = oneArgumentPooler;
80 -
81 -type Pooler = any;
82 -
83 -/**
84 - * Augments `CopyConstructor` to be a poolable class, augmenting only the class
85 - * itself (statically) not adding any prototypical fields. Any CopyConstructor
86 - * you give this may have a `poolSize` property, and will look for a
87 - * prototypical `destructor` on instances.
88 - *
89 - * @param {Function} CopyConstructor Constructor that can be used to reset.
90 - * @param {Function} pooler Customizable pooler.
91 - */
92 -var addPoolingTo = function<T>(
93 - CopyConstructor: Class<T>,
94 - pooler: Pooler,
95 -): Class<T> & {
96 - getPooled(
97 - ...args: $ReadOnlyArray<mixed>
98 - ): /* arguments of the constructor */ T,
99 - release(instance: mixed): void,
100 -} {
101 - // Casting as any so that flow ignores the actual implementation and trusts
102 - // it to match the type we declared
103 - var NewKlass = (CopyConstructor: any);
104 - NewKlass.instancePool = [];
105 - NewKlass.getPooled = pooler || DEFAULT_POOLER;
106 - if (!NewKlass.poolSize) {
107 - NewKlass.poolSize = DEFAULT_POOL_SIZE;
108 - }
109 - NewKlass.release = standardReleaser;
110 - return NewKlass;
111 -};
112 -
113 -var PooledClass = {
114 - addPoolingTo: addPoolingTo,
115 - oneArgumentPooler: (oneArgumentPooler: Pooler),
116 - twoArgumentPooler: (twoArgumentPooler: Pooler),
117 - threeArgumentPooler: (threeArgumentPooler: Pooler),
118 - fourArgumentPooler: (fourArgumentPooler: Pooler),
119 -};
120 -
121 -module.exports = PooledClass;
src/renderers/native/__tests__/PooledClass-test.js deleted
-117
@@ -1,117 +0,0 @@
1 -/**
2 - * Copyright (c) 2014-present, Facebook, Inc.
3 - *
4 - * This source code is licensed under the MIT license found in the
5 - * LICENSE file in the root directory of this source tree.
6 - *
7 - * @emails react-core
8 - */
9 -
10 -'use strict';
11 -
12 -var PooledClass;
13 -var PoolableClass;
14 -
15 -describe('Pooled class', () => {
16 - beforeEach(() => {
17 - PooledClass = require('PooledClass');
18 - PoolableClass = function() {};
19 - PoolableClass.prototype.destructor = function() {};
20 - PooledClass.addPoolingTo(PoolableClass);
21 - });
22 -
23 - it('should initialize a pool correctly', () => {
24 - expect(PoolableClass.instancePool).toBeDefined();
25 - });
26 -
27 - it('should return a new instance when the pool is empty', () => {
28 - var instance = PoolableClass.getPooled();
29 - expect(instance instanceof PoolableClass).toBe(true);
30 - });
31 -
32 - it('should return the instance back into the pool when it gets released', () => {
33 - var instance = PoolableClass.getPooled();
34 - PoolableClass.release(instance);
35 - expect(PoolableClass.instancePool.length).toBe(1);
36 - expect(PoolableClass.instancePool[0]).toBe(instance);
37 - });
38 -
39 - it('should return an old instance if available in the pool', () => {
40 - var instance = PoolableClass.getPooled();
41 - PoolableClass.release(instance);
42 - var instance2 = PoolableClass.getPooled();
43 - expect(instance).toBe(instance2);
44 - });
45 -
46 - it('should call the destructor when instance gets released', () => {
47 - var log = [];
48 - var PoolableClassWithDestructor = function() {};
49 - PoolableClassWithDestructor.prototype.destructor = function() {
50 - log.push('released');
51 - };
52 - PooledClass.addPoolingTo(PoolableClassWithDestructor);
53 - var instance = PoolableClassWithDestructor.getPooled();
54 - PoolableClassWithDestructor.release(instance);
55 - expect(log).toEqual(['released']);
56 - });
57 -
58 - it('should accept poolers with different arguments', () => {
59 - var log = [];
60 - var PoolableClassWithMultiArguments = function(a, b) {
61 - log.push(a, b);
62 - };
63 - PoolableClassWithMultiArguments.prototype.destructor = function() {};
64 - PooledClass.addPoolingTo(
65 - PoolableClassWithMultiArguments,
66 - PooledClass.twoArgumentPooler,
67 - );
68 - PoolableClassWithMultiArguments.getPooled('a', 'b', 'c');
69 - expect(log).toEqual(['a', 'b']);
70 - });
71 -
72 - it('should call a new constructor with arguments', () => {
73 - var log = [];
74 - var PoolableClassWithOneArgument = function(a) {
75 - log.push(a);
76 - };
77 - PoolableClassWithOneArgument.prototype.destructor = function() {};
78 - PooledClass.addPoolingTo(PoolableClassWithOneArgument);
79 - PoolableClassWithOneArgument.getPooled('new');
80 - expect(log).toEqual(['new']);
81 - });
82 -
83 - it('should call an old constructor with arguments', () => {
84 - var log = [];
85 - var PoolableClassWithOneArgument = function(a) {
86 - log.push(a);
87 - };
88 - PoolableClassWithOneArgument.prototype.destructor = function() {};
89 - PooledClass.addPoolingTo(PoolableClassWithOneArgument);
90 - var instance = PoolableClassWithOneArgument.getPooled('new');
91 - PoolableClassWithOneArgument.release(instance);
92 - PoolableClassWithOneArgument.getPooled('old');
93 - expect(log).toEqual(['new', 'old']);
94 - });
95 -
96 - it('should throw when the class releases an instance of a different type', () => {
97 - var RandomClass = function() {};
98 - RandomClass.prototype.destructor = function() {};
99 - PooledClass.addPoolingTo(RandomClass);
100 - var randomInstance = RandomClass.getPooled();
101 - PoolableClass.getPooled();
102 - expect(function() {
103 - PoolableClass.release(randomInstance);
104 - }).toThrowError(
105 - 'Trying to release an instance into a pool of a different type.',
106 - );
107 - });
108 -
109 - it('should throw if no destructor is defined', () => {
110 - var ImmortalClass = function() {};
111 - PooledClass.addPoolingTo(ImmortalClass);
112 - var inst = ImmortalClass.getPooled();
113 - expect(function() {
114 - ImmortalClass.release(inst);
115 - }).toThrow();
116 - });
117 -});