React源码解析-UI更新(Transaction)II

  • 一、前言

上一篇文章介绍了 transaction 的基本概念和用法。今天我们将讲解在更新过程中,React 是如何通过多个 transacion 之间的协作,来有效组织代码的。

  • 二、ReactUpdatesFlushTransaction

前文讲到ReactDefaultBatchingStrategy close 的时候,会调用ReactUpdates.flushBatchedUpdates

PooledClass.addPoolingTo(ReactUpdatesFlushTransaction);

var flushBatchedUpdates = function () {
    while (dirtyComponents.length || asapEnqueued) {
        if (dirtyComponents.length) {
            var transaction = ReactUpdatesFlushTransaction.getPooled();
            transaction.perform(runBatchedUpdates, null, transaction);
            ReactUpdatesFlushTransaction.release(transaction);
        }

        if (asapEnqueued) {
            asapEnqueued = false;
            var queue = asapCallbackQueue;
            asapCallbackQueue = CallbackQueue.getPooled();
            queue.notifyAll();
            CallbackQueue.release(queue);
        }
    }
};

这里又调用了另一个 transaction 来处理后续的流程,有所不同的是 transaction 的创建不是直接 new,而是调用getPooled方法。这个方法是通过前面的PooledClass.addPoolingTo注入到ReactUpdatesFlushTransaction中的,如果对这个步骤感兴趣可以看看这篇文章。下面来看ReactUpdatesFlushTransaction的内容:

Object.assign(
    ReactUpdatesFlushTransaction.prototype,
    Transaction, 
    {
        getTransactionWrappers: function () {
            return TRANSACTION_WRAPPERS;
        },

        destructor: function () {
            this.dirtyComponentsLength = null;
            CallbackQueue.release(this.callbackQueue);
            this.callbackQueue = null;
            ReactUpdates.ReactReconcileTransaction.release(this.reconcileTransaction);
            this.reconcileTransaction = null;
        },

        perform: function (method, scope, a) {
            return Transaction.perform.call(
                this,
                this.reconcileTransaction.perform,
                this.reconcileTransaction,
                method,
                scope,
                a
            );
        },
    }
);

var TRANSACTION_WRAPPERS = [NESTED_UPDATES, UPDATE_QUEUEING];

var NESTED_UPDATES = {
    initialize: function () {
        this.dirtyComponentsLength = dirtyComponents.length;
    },
    close: function () {
        if (this.dirtyComponentsLength !== dirtyComponents.length) {
            // Additional updates were enqueued by componentDidUpdate handlers or
            // similar; before our own UPDATE_QUEUEING wrapper closes, we want to run
            // these new updates so that if A's componentDidUpdate calls setState on
            // B, B will update before the callback A's updater provided when calling
            // setState.
            dirtyComponents.splice(0, this.dirtyComponentsLength);
            flushBatchedUpdates();
        } else {
            dirtyComponents.length = 0;
        }
    },
};

var UPDATE_QUEUEING = {
    initialize: function () {
        this.callbackQueue.reset();
    },
    close: function () {
        this.callbackQueue.notifyAll();
    },
};

ReactUpdatesFlushTransaction覆盖了原型链上的perform方法,不是直接调用 callback,而是嵌套调用了this.reconcileTransaction.perform,在将 callback 透传给reconcileTransactionperform。这里的reconcileTransaction也开启了实例池。

这里要注意下NESTED_UPDATES这个 wrapper,如果dirtyComponents的数量跟 transaction 开始的时候不一样,它又会递归调用flushBatchedUpdates,直到dirtyComponents不再变化为止。UPDATE_QUEUEING这个 wrapper 暂时先忽略。

目前为止的调用关系如下:

clipboard.png

  • 三、ReactReconcileTransaction

ReactReconcileTransaction是一个普通的 transaction,定义了一些 DOM 操作相关的 wrapper:

function ReactReconcileTransaction(useCreateElement: boolean) {
  this.reinitializeTransaction();
  this.renderToStaticMarkup = false;
  this.reactMountReady = CallbackQueue.getPooled(null);
  this.useCreateElement = useCreateElement;
}

var Mixin = {
  getTransactionWrappers: function() {
    return TRANSACTION_WRAPPERS;
  },

  ...

  destructor: function() {
    CallbackQueue.release(this.reactMountReady);
    this.reactMountReady = null;
  },
};


Object.assign(ReactReconcileTransaction.prototype, Transaction, Mixin);

PooledClass.addPoolingTo(ReactReconcileTransaction);

var TRANSACTION_WRAPPERS = [
  SELECTION_RESTORATION,
  EVENT_SUPPRESSION,
  ON_DOM_READY_QUEUEING,
];

var SELECTION_RESTORATION = {
  /**
   * @return {Selection} Selection information.
   */
  initialize: ReactInputSelection.getSelectionInformation,
  /**
   * @param {Selection} sel Selection information returned from `initialize`.
   */
  close: ReactInputSelection.restoreSelection,
};

/**
 * Suppresses events (blur/focus) that could be inadvertently dispatched due to
 * high level DOM manipulations (like temporarily removing a text input from the
 * DOM).
 */
var EVENT_SUPPRESSION = {
  /**
   * @return {boolean} The enabled status of `ReactBrowserEventEmitter` before
   * the reconciliation.
   */
  initialize: function() {
    var currentlyEnabled = ReactBrowserEventEmitter.isEnabled();
    ReactBrowserEventEmitter.setEnabled(false);
    return currentlyEnabled;
  },

  /**
   * @param {boolean} previouslyEnabled Enabled status of
   *   `ReactBrowserEventEmitter` before the reconciliation occurred. `close`
   *   restores the previous value.
   */
  close: function(previouslyEnabled) {
    ReactBrowserEventEmitter.setEnabled(previouslyEnabled);
  },
};

/**
 * Provides a queue for collecting `componentDidMount` and
 * `componentDidUpdate` callbacks during the transaction.
 */
var ON_DOM_READY_QUEUEING = {
  /**
   * Initializes the internal `onDOMReady` queue.
   */
  initialize: function() {
    this.reactMountReady.reset();
  },

  /**
   * After DOM is flushed, invoke all registered `onDOMReady` callbacks.
   */
  close: function() {
    this.reactMountReady.notifyAll();
  },
};

这三个 wrapper 的作用注释都讲得很清楚了,不再赘述。值得一提的是这里perform的 callback 是ReactUpdatesFlushTransaction透传过来的ReactUpdate.runBatchedUpdates

目前为止的调用关系如下:

clipboard.png

clipboard.png

  • 四、总结

到此为止,transaction 相关的内容就讲完了,下一篇开始介绍真正的更新操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章