Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
/**
2
* Copyright 2013-2015, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule PooledClass
10
*/
11
12
'use strict';
13
14
var invariant = require("./invariant");
15
16
/**
17
* Static poolers. Several custom versions for each potential number of
18
* arguments. A completely generic pooler is easy to implement, but would
19
* require accessing the `arguments` object. In each of these, `this` refers to
20
* the Class itself, not an instance. If any others are needed, simply add them
21
* here, or in their own files.
22
*/
23
var oneArgumentPooler = function(copyFieldsFrom) {
24
var Klass = this;
25
if (Klass.instancePool.length) {
26
var instance = Klass.instancePool.pop();
27
Klass.call(instance, copyFieldsFrom);
28
return instance;
29
} else {
30
return new Klass(copyFieldsFrom);
31
}
32
};
33
34
var twoArgumentPooler = function(a1, a2) {
35
var Klass = this;
36
if (Klass.instancePool.length) {
37
var instance = Klass.instancePool.pop();
38
Klass.call(instance, a1, a2);
39
return instance;
40
} else {
41
return new Klass(a1, a2);
42
}
43
};
44
45
var threeArgumentPooler = function(a1, a2, a3) {
46
var Klass = this;
47
if (Klass.instancePool.length) {
48
var instance = Klass.instancePool.pop();
49
Klass.call(instance, a1, a2, a3);
50
return instance;
51
} else {
52
return new Klass(a1, a2, a3);
53
}
54
};
55
56
var fiveArgumentPooler = function(a1, a2, a3, a4, a5) {
57
var Klass = this;
58
if (Klass.instancePool.length) {
59
var instance = Klass.instancePool.pop();
60
Klass.call(instance, a1, a2, a3, a4, a5);
61
return instance;
62
} else {
63
return new Klass(a1, a2, a3, a4, a5);
64
}
65
};
66
67
var standardReleaser = function(instance) {
68
var Klass = this;
69
("production" !== process.env.NODE_ENV ? invariant(
70
instance instanceof Klass,
71
'Trying to release an instance into a pool of a different type.'
72
) : invariant(instance instanceof Klass));
73
if (instance.destructor) {
74
instance.destructor();
75
}
76
if (Klass.instancePool.length < Klass.poolSize) {
77
Klass.instancePool.push(instance);
78
}
79
};
80
81
var DEFAULT_POOL_SIZE = 10;
82
var DEFAULT_POOLER = oneArgumentPooler;
83
84
/**
85
* Augments `CopyConstructor` to be a poolable class, augmenting only the class
86
* itself (statically) not adding any prototypical fields. Any CopyConstructor
87
* you give this may have a `poolSize` property, and will look for a
88
* prototypical `destructor` on instances (optional).
89
*
90
* @param {Function} CopyConstructor Constructor that can be used to reset.
91
* @param {Function} pooler Customizable pooler.
92
*/
93
var addPoolingTo = function(CopyConstructor, pooler) {
94
var NewKlass = CopyConstructor;
95
NewKlass.instancePool = [];
96
NewKlass.getPooled = pooler || DEFAULT_POOLER;
97
if (!NewKlass.poolSize) {
98
NewKlass.poolSize = DEFAULT_POOL_SIZE;
99
}
100
NewKlass.release = standardReleaser;
101
return NewKlass;
102
};
103
104
var PooledClass = {
105
addPoolingTo: addPoolingTo,
106
oneArgumentPooler: oneArgumentPooler,
107
twoArgumentPooler: twoArgumentPooler,
108
threeArgumentPooler: threeArgumentPooler,
109
fiveArgumentPooler: fiveArgumentPooler
110
};
111
112
module.exports = PooledClass;
113
114