PooledClass source code

PooledClass.addPoolingTo(CopyConstructor) is used to convert the constructor CopyConstructor into a factory function, which means to manage the creation and destruction of instance data, and push the instance of the destroyed data into the instance pool CopyConstructor.instancePool.

 

'use strict';

var _prodInvariant = require('./reactProdInvariant');// Production environment React form with url error

// invariant(condition,format,a,b,c,d,e,f) condition is negative, replace "%s" in format, and throw error  
var invariant = require('fbjs/lib/invariant');

// Create an instance with a single parameter, or provide a single parameter data for the instance
var oneArgumentPooler = function (copyFieldsFrom) {
  var Klass = this;
  if (Klass.instancePool.length) {
    var instance = Klass.instancePool.pop();
    Klass.call(instance, copyFieldsFrom);
    return instance;
  } else {
    return new Klass(copyFieldsFrom);
  }
};

var twoArgumentPooler = function (a1, a2) {
  var Klass = this;
  if (Klass.instancePool.length) {
    var instance = Klass.instancePool.pop();
    Klass.call(instance, a1, a2);
    return instance;
  } else {
    return new Klass(a1, a2);
  }
};

var threeArgumentPooler = function (a1, a2, a3) {
  var Klass = this;
  if (Klass.instancePool.length) {
    var instance = Klass.instancePool.pop();
    Klass.call(instance, a1, a2, a3);
    return instance;
  } else {
    return new Klass(a1, a2, a3);
  }
};

var fourArgumentPooler = function (a1, a2, a3, a4) {
  var Klass = this;
  if (Klass.instancePool.length) {
    var instance = Klass.instancePool.pop();
    Klass.call(instance, a1, a2, a3, a4);
    return instance;
  } else {
    return new Klass(a1, a2, a3, a4);
  }
};

// Destroy the instance data and push the instance into the instance pool Klass.instancePool
var standardReleaser = function (instance) {
  var Klass = this;
  !(instance instanceof Klass) ? process.env.NODE_ENV !== 'production' ?
    invariant(false, 'Trying to release an instance into a pool of a different type.')
    : _prodInvariant ('25 '): void 0;
  instance.destructor();
  if (Klass.instancePool.length < Klass.poolSize) {
    Klass.instancePool.push(instance);
  }
};

var DEFAULT_POOL_SIZE = 10;
var DEFAULT_POOLER = oneArgumentPooler;

// Factory the constructor CopyConstructor, call the getPooled method to generate an instance, and the release method to destroy the instance data
// While the release method destroys the instance data, it pushes the instance into the instancePool instance pool, which can be retrieved by the getPooled method
// The execution of the release method requires the constructor CopyConstructor to contain the destructor prototype method
// poolSize agrees on the number of instance storage, the default is 10
// When the secondary parameter pooler exists, use the pooler instead of the default oneArgumentPooler method to create an instance, and pass the single parameter into the constructor
var addPoolingTo = function (CopyConstructor, pooler) {
  var NewKlass = CopyConstructor;
  NewKlass.instancePool = [];
  NewKlass.getPooled = pooler || DEFAULT_POOLER;
  if (!NewKlass.poolSize) {
    NewKlass.poolSize = DEFAULT_POOL_SIZE;
  }
  NewKlass.release = standardReleaser;
  return NewKlass;
};

var PooledClass = {
  addPoolingTo: addPoolingTo,// Convert the constructor into a factory function, provide an instance pool, and manage the destruction and generation of instance data
  oneArgumentPooler: oneArgumentPooler,// create an instance with a single parameter
  twoArgumentPooler: twoArgumentPooler, // double parameter to create an instance
  threeArgumentPooler: threeArgumentPooler,
  fourArgumentPooler: fourArgumentPooler
};

module.exports = PooledClass;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327033629&siteId=291194637