all files / vmodel/ Action.js

100% Statements 73/73
94.44% Branches 34/36
100% Functions 12/12
100% Lines 73/73
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                              641× 4882× 4419×       641× 641× 641× 641× 641× 641× 641× 641×   641× 592×     641× 33×     641×   641× 178×         1049× 1049× 1049×                     993×   993× 80×     993× 993× 80×     993×             815×         507× 507× 507× 507× 505×   507×     356× 351× 351× 351× 351×     351×           31× 31× 32×               432×             31× 31× 31× 16×   31× 899×           956× 142× 68× 74× 13× 13× 29×   13×   61× 61× 112×   61×     814×                                                          
import {
    avalon
} from '../seed/core'
 
import {
    runActions,
    collectDeps
} from './transaction'
 
import {
    createGetter,
    createSetter
} from "../parser/index"
 
export var actionUUID = 1
    //需要重构
export function Action(vm, options, callback) {
    for (var i in options) {
        if (protectedMenbers[i] !== 1) {
            this[i] = options[i]
        }
    }
 
    this.vm = vm
    this.observers = []
    this.callback = callback
    this.uuid = ++actionUUID
    this.ids = ''
    this.mapIDs = {} //这个用于去重
    this.isAction = true
    var expr = this.expr
        // 缓存取值函数
    if (typeof this.getter !== 'function') {
        this.getter = createGetter(expr, this.type)
    }
    // 缓存设值函数(双向数据绑定)
    if (this.type === 'duplex') {
        this.setter = createSetter(expr, this.type)
    }
    // 缓存表达式旧值
    this.value = NaN
        // 表达式初始值 & 提取依赖
    if (!(this.node)) {
        this.value = this.get()
    }
}
 
Action.prototype = {
    getValue() {
        var scope = this.vm
        try {
            return this.getter.call(scope, scope)
        } catch (e) {
            avalon.log(this.getter + ' exec error')
        }
    },
 
    setValue(value) {
        var scope = this.vm
        Eif (this.setter) {
            this.setter.call(scope, scope, value)
        }
    },
 
    // get --> getValue --> getter
    get(fn) {
        var name = 'action track ' + this.type
 
        if (this.deep) {
            avalon.deepCollect = true
        }
 
        var value = collectDeps(this, this.getValue)
        if (this.deep && avalon.deepCollect) {
            avalon.deepCollect = false
        }
 
        return value
    },
 
    /**
     * 在更新视图前保存原有的value
     */
    beforeUpdate() {
        return this.oldValue = getPlainObject(this.value)
    },
 
 
    update(args, uuid) {
        var oldVal = this.beforeUpdate()
        var newVal = this.value = this.get()
        var callback = this.callback
        if (callback && this.diff(newVal, oldVal, args)) {
            callback.call(this.vm, this.value, oldVal, this.expr)
        }
        this._isScheduled = false
    },
    schedule() {
        if (!this._isScheduled) {
            this._isScheduled = true
            Eif (!avalon.uniqActions[this.uuid]) {
                avalon.uniqActions[this.uuid] = 1
                avalon.pendingActions.push(this)
            }
 
            runActions() //这里会还原_isScheduled
 
 
        }
    },
    removeDepends() {
        var self = this
        this.observers.forEach(function(depend) {
            avalon.Array.remove(depend.observers, self)
        })
    },
 
    /**
     * 比较两个计算值是否,一致,在for, class等能复杂数据类型的指令中,它们会重写diff复法
     */
    diff(a, b) {
        return a !== b
    },
 
    /**
     * 销毁指令
     */
    dispose() {
        this.value = null
        this.removeDepends()
        if (this.beforeDispose) {
            this.beforeDispose()
        }
        for (var i in this) {
            delete this[i]
        }
    }
 
}
 
function getPlainObject(v) {
    if (v && typeof v === 'object') {
        if (v && v.$events) {
            return v.$model
        } else if (Array.isArray(v)) {
            let ret = []
            for (let i = 0, n = v.length; i < n; i++) {
                ret.push(getPlainObject(v[i]))
            }
            return ret
        } else {
            let ret = {}
            for (let i in v) {
                ret[i] = getPlainObject(v[i])
            }
            return ret
        }
    } else {
        return v
    }
}
 
export var protectedMenbers = {
    vm: 1,
    callback: 1,
 
    observers: 1,
    oldValue: 1,
    value: 1,
    getValue: 1,
    setValue: 1,
    get: 1,
 
    removeDepends: 1,
    beforeUpdate: 1,
    update: 1,
    //diff
    //getter
    //setter
    //expr
    //vdom
    //type: "for"
    //name: "ms-for"
    //attrName: ":for"
    //param: "click"
    //beforeDispose
    dispose: 1
}