# all files / src/ transaction.js

15% Statements 3/20
0% Branches 0/2
0% Functions 0/4
15% Lines 3/20

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47

    1×
    1×

    1×

     import {
        options
    } from './util'
    var queue = []
    var callbacks = []

    export var transaction = {
        isInTransation: false,
        queueCallback: function (obj) {
            //它们是保证在ComponentDidUpdate后执行
            callbacks.push(obj)
        },
        queueComponent: function (instance) {
            queue.push(instance)
        },
        dequeue: function (recursion) {

            this.isInTransation = true
            var globalBatchNumber = options.updateBatchNumber
            var renderQueue = queue
            queue = []
            var processingCallbacks = callbacks
            callbacks = []
            var refreshComponent = options.immune.refreshComponent
            for (var i = 0, n = renderQueue.length; i < n; i++) {
                var inst = renderQueue[i]
                try {
                    if (inst._updateBatchNumber === globalBatchNumber) {
                        refreshComponent(inst)
                    }
                } catch (e) {
                    /* istanbul ignore next */
                    console.warn(e)
                }
            }
            this.isInTransation = false
            processingCallbacks.forEach(function (request) {
                request
                    .cb
                    .call(request.instance)
            })
            /* istanbul ignore next */
            if (queue.length) {
                this.dequeue() //用于递归调用自身)
            }
        }
    }