all files / vmodel/ Mutation.js

100% Statements 47/47
93.33% Branches 28/30
100% Functions 7/7
100% Lines 47/47
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                            919× 919× 541× 541× 134×     919× 919× 919× 919×   919× 919× 919× 919×       2917× 1271× 1271× 1271× 391× 125× 396× 77×     266× 73× 111× 111×             2917×       1353× 1353×       1226×       335× 335× 335×       389× 389× 304× 53× 53× 53× 53× 52×   53×     304× 304× 304×      
import {
    transactionStart,
    transactionEnd,
    reportObserved,
    propagateChanged
} from './transaction'
import {
    avalon,
    platform
} from '../seed/core'
/**
* 
 与Computed等共享UUID
*/
export let obid = 1
export function Mutation(expr, value, vm) { //构造函数
    this.expr = expr
    if (value) {
        var childVm = platform.createProxy(value, this)
        if (childVm) {
            value = childVm
        }
    }
    this.value = value
    this.vm = vm
    try {
        vm.$mutations[expr] = this
    } catch (ignoreIE) {}
    this.uuid = ++obid
    this.updateVersion()
    this.mapIDs = {}
    this.observers = []
}
 
Mutation.prototype = {
    get() {
        if (avalon.trackingAction) {
            this.collect() //被收集
            var childOb = this.value
            if (childOb && childOb.$events) {
                if (Array.isArray(childOb)) {
                    childOb.forEach(function(item) {
                        if (item && item.$events) {
                            item.$events.__dep__.collect()
                        }
                    })
                } else if (avalon.deepCollect) {
                    for (var key in childOb) {
                        Eif (childOb.hasOwnProperty(key)) {
                            var collectIt = childOb[key]
                        }
                    }
                }
 
            }
        }
        return this.value
    },
 
    collect() {
        avalon.track(name, '被收集')
        reportObserved(this)
    },
 
    updateVersion() {
        this.version = Math.random() + Math.random()
    },
 
    notify() {
        transactionStart()
        propagateChanged(this)
        transactionEnd()
    },
 
    set(newValue) {
        var oldValue = this.value
        if (newValue !== oldValue) {
            if (avalon.isObject(newValue)) {
                var hash = oldValue && oldValue.$hashcode
                var childVM = platform.createProxy(newValue, this)
                Eif (childVM) {
                    if (hash) {
                        childVM.$hashcode = hash
                    }
                    newValue = childVM
                }
            }
            this.value = newValue
            this.updateVersion()
            this.notify()
        }
    }
}