CallbackQueue source code

The CallbackQueue module is used to add, execute, and reset the callback function queue. The function is the same as the module of the same name in jquery.

In react, use var Export=PooledClass.addPoolingTo(CallbackQueue) to factory callbackQueue constructor, the realization function is to manage the creation of CallbackQueue instance - Export.getPooled method, destruction of instance data (callback function and its execution context) - Export. release method. The instance of the destroyed data is stored in the Export.instancePool instance pool, which can be retrieved through the Export.getPooled method.

 

'use strict';

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

// The verification constructor must be called with the new keyword and cannot be used as a normal function
function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

// PooledClass.addPoolingTo(CopyConstructor) is used to convert the constructor CopyConstructor into a factory function
// The meaning is to manage the creation and destruction of instance data, and push the instance of the destroyed data into the instance pool CopyConstructor.instancePool
var PooledClass = require('./PooledClass');

// 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');

// Used to add, execute, and reset the callback function queue; the actual use in react is to mount hook methods such as componentDidMount
// Manage the creation of instances through the PooledClass module CallbackQueue.getPooled, and the destruction of instance data CallbackQueue.release
var CallbackQueue = function () {
  function CallbackQueue(arg) {
    _classCallCheck(this, CallbackQueue);

    this._callbacks = null;
    this._contexts = null;
    this._arg = arg;
  }

  // Add the callback function and its execution context to the callback queue, triggered by the notifyAll method
  CallbackQueue.prototype.enqueue = function enqueue(callback, context) {
    this._callbacks = this._callbacks || [];
    this._callbacks.push(callback);
    this._contexts = this._contexts || [];
    this._contexts.push(context);
  };

  // Trigger the execution of functions in the callback function queue; if the number of callback functions does not match the number of execution contexts, an error will be reported
  CallbackQueue.prototype.notifyAll = function notifyAll() {
    var callbacks = this._callbacks;
    var contexts = this._contexts;
    var arg = this._arg;
    if (callbacks && contexts) {
      !(callbacks.length === contexts.length) ?
        process.env.NODE_ENV !== 'production' ?
          invariant(false, 'Mismatched list of contexts in callback queue')
          : _prodInvariant ('24 ')
        : void 0;
      this._callbacks = null;
      this._contexts = null;
      for (var i = 0; i < callbacks.length; i++) {
        callbacks[i].call(contexts[i], arg);
      }
      callbacks.length = 0;
      contexts.length = 0;
    }
  };

  // Get the number of callback functions in the callback function queue
  CallbackQueue.prototype.checkpoint = function checkpoint() {
    return this._callbacks ? this._callbacks.length : 0;
  };

  // Set the number of callback functions in the callback function queue as the parameter len
  CallbackQueue.prototype.rollback = function rollback(len) {
    if (this._callbacks && this._contexts) {
      this._callbacks.length = len;
      this._contexts.length = len;
    }
  };

  // reset the callback function queue
  CallbackQueue.prototype.reset = function reset() {
    this._callbacks = null;
    this._contexts = null;
  };

  // PooledClass module decoration needs, set the destructor method for the release method to use to destroy instance data
  CallbackQueue.prototype.destructor = function destructor() {
    this.reset();
  };

  return CallbackQueue;
}();

// Manage the creation of instances through the PooledClass module CallbackQueue.getPooled, and the destruction of instance data CallbackQueue.release
module.exports = PooledClass.addPoolingTo(CallbackQueue);

 

Guess you like

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