react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / utils / PooledClass.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule PooledClass9*/1011"use strict";1213var invariant = require('invariant');1415/**16* Static poolers. Several custom versions for each potential number of17* arguments. A completely generic pooler is easy to implement, but would18* require accessing the `arguments` object. In each of these, `this` refers to19* the Class itself, not an instance. If any others are needed, simply add them20* here, or in their own files.21*/22var oneArgumentPooler = function(copyFieldsFrom) {23var Klass = this;24if (Klass.instancePool.length) {25var instance = Klass.instancePool.pop();26Klass.call(instance, copyFieldsFrom);27return instance;28} else {29return new Klass(copyFieldsFrom);30}31};3233var twoArgumentPooler = function(a1, a2) {34var Klass = this;35if (Klass.instancePool.length) {36var instance = Klass.instancePool.pop();37Klass.call(instance, a1, a2);38return instance;39} else {40return new Klass(a1, a2);41}42};4344var threeArgumentPooler = function(a1, a2, a3) {45var Klass = this;46if (Klass.instancePool.length) {47var instance = Klass.instancePool.pop();48Klass.call(instance, a1, a2, a3);49return instance;50} else {51return new Klass(a1, a2, a3);52}53};5455var fiveArgumentPooler = function(a1, a2, a3, a4, a5) {56var Klass = this;57if (Klass.instancePool.length) {58var instance = Klass.instancePool.pop();59Klass.call(instance, a1, a2, a3, a4, a5);60return instance;61} else {62return new Klass(a1, a2, a3, a4, a5);63}64};6566var standardReleaser = function(instance) {67var Klass = this;68invariant(69instance instanceof Klass,70'Trying to release an instance into a pool of a different type.'71);72if (instance.destructor) {73instance.destructor();74}75if (Klass.instancePool.length < Klass.poolSize) {76Klass.instancePool.push(instance);77}78};7980var DEFAULT_POOL_SIZE = 10;81var DEFAULT_POOLER = oneArgumentPooler;8283/**84* Augments `CopyConstructor` to be a poolable class, augmenting only the class85* itself (statically) not adding any prototypical fields. Any CopyConstructor86* you give this may have a `poolSize` property, and will look for a87* prototypical `destructor` on instances (optional).88*89* @param {Function} CopyConstructor Constructor that can be used to reset.90* @param {Function} pooler Customizable pooler.91*/92var addPoolingTo = function(CopyConstructor, pooler) {93var NewKlass = CopyConstructor;94NewKlass.instancePool = [];95NewKlass.getPooled = pooler || DEFAULT_POOLER;96if (!NewKlass.poolSize) {97NewKlass.poolSize = DEFAULT_POOL_SIZE;98}99NewKlass.release = standardReleaser;100return NewKlass;101};102103var PooledClass = {104addPoolingTo: addPoolingTo,105oneArgumentPooler: oneArgumentPooler,106twoArgumentPooler: twoArgumentPooler,107threeArgumentPooler: threeArgumentPooler,108fiveArgumentPooler: fiveArgumentPooler109};110111module.exports = PooledClass;112113114