1
1
/* @flow */
2
2
3
-
import Watcher from '../observer/watcher'
4
3
import Dep from '../observer/dep'
4
+
import Watcher from '../observer/watcher'
5
5
6
6
import {
7
7
set,
@@ -108,53 +108,62 @@ function initData (vm: Component) {
108
108
observe(data, true /* asRootData */)
109
109
}
110
110
111
+
const computedWatcherOptions = { lazy: true }
112
+
113
+
function initComputed (vm: Component, computed: Object) {
114
+
const watchers = vm._computedWatchers = Object.create(null)
115
+
116
+
for (const key in computed) {
117
+
const userDef = computed[key]
118
+
const getter = typeof userDef === 'function' ? userDef : userDef.get
119
+
// create internal watcher for the computed property.
120
+
watchers[key] = new Watcher(vm, getter, noop, computedWatcherOptions)
121
+
122
+
// component-defined computed properties are already defined on the
123
+
// component prototype. We only need to define on-the-fly computed
124
+
// properties here.
125
+
if (!(key in vm)) {
126
+
defineComputed(vm, key, userDef)
127
+
}
128
+
}
129
+
}
130
+
111
131
const computedSharedDefinition = {
112
132
enumerable: true,
113
133
configurable: true,
114
134
get: noop,
115
135
set: noop
116
136
}
117
137
118
-
function initComputed (vm: Component, computed: Object) {
119
-
for (const key in computed) {
120
-
/* istanbul ignore if */
121
-
if (process.env.NODE_ENV !== 'production' && key in vm) {
122
-
warn(
123
-
`existing instance property "${key}" will be ` +
124
-
`overwritten by a computed property with the same name.`,
125
-
vm
126
-
)
127
-
}
128
-
const userDef = computed[key]
129
-
if (typeof userDef === 'function') {
130
-
computedSharedDefinition.get = makeComputedGetter(userDef, vm)
131
-
computedSharedDefinition.set = noop
132
-
} else {
133
-
computedSharedDefinition.get = userDef.get
134
-
? userDef.cache !== false
135
-
? makeComputedGetter(userDef.get, vm)
136
-
: bind(userDef.get, vm)
137
-
: noop
138
-
computedSharedDefinition.set = userDef.set
139
-
? bind(userDef.set, vm)
140
-
: noop
141
-
}
142
-
Object.defineProperty(vm, key, computedSharedDefinition)
138
+
export function defineComputed (target: any, key: string, userDef: Object | Function) {
139
+
if (typeof userDef === 'function') {
140
+
computedSharedDefinition.get = createComputedGetter(key)
141
+
computedSharedDefinition.set = noop
142
+
} else {
143
+
computedSharedDefinition.get = userDef.get
144
+
? userDef.cache !== false
145
+
? createComputedGetter(key)
146
+
: userDef.get
147
+
: noop
148
+
computedSharedDefinition.set = userDef.set
149
+
? userDef.set
150
+
: noop
143
151
}
152
+
Object.defineProperty(target, key, computedSharedDefinition)
144
153
}
145
154
146
-
function makeComputedGetter (getter: Function, owner: Component): Function {
147
-
const watcher = new Watcher(owner, getter, noop, {
148
-
lazy: true
149
-
})
155
+
function createComputedGetter (key) {
150
156
return function computedGetter () {
151
-
if (watcher.dirty) {
152
-
watcher.evaluate()
153
-
}
154
-
if (Dep.target) {
155
-
watcher.depend()
157
+
const watcher = this._computedWatchers && this._computedWatchers[key]
158
+
if (watcher) {
159
+
if (watcher.dirty) {
160
+
watcher.evaluate()
161
+
}
162
+
if (Dep.target) {
163
+
watcher.depend()
164
+
}
165
+
return watcher.value
156
166
}
157
-
return watcher.value
158
167
}
159
168
}
160
169
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4