all files / vmodel/ Action.js

100% Statements 60/60
93.33% Branches 28/30
100% Functions 11/11
100% Lines 60/60
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                              604× 4609× 4175×       604× 604× 604× 604× 604× 604× 604× 604×   604× 559×     604× 32×     604×   604× 170×         989× 989× 989×                     938×   938× 73×     938× 938× 73×     938×             768× 768×         478× 478× 478× 478× 430×   478×     339× 334× 334× 334× 334×     334×           31× 31× 32×               398×             31× 31× 31× 16×   31× 901×                                                                
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.oldValue = null
        // 表达式初始值 & 提取依赖
    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() {
        var v = this.value
        return this.oldValue = v && v.$events ? v.$model : v
    },
 
 
    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]
        }
    }
 
}
 
 
 
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
}