Source code analysis
initComputed
InitComputed this code does a few things
- Each computed distribution watcher;
- DefineComputed processing;
- Collect all computed Watcher;
What did Watcher do
Take a look at the Watcher source constructor
- With computed new watcher, lazy is used to cache the results instead of recalculating them each time it is used.
- Save the configured getter, and store the computed-getter set by the user in watcher. Getter for later calculation.
- Watcher. Value stores the result, but with the condition that, because of lazy, the new instance is not created and the value is read immediately. So this is kind of an optimization of Vue, if you read computed again, and you start computing, instead of initializing, you start computing, and you don’t start computing, but you still compute value using the same method called watcher.get;
DefineComputed processing
- The set function defaults to an empty function. If it is set by the user, the user setting is used.
- If caching is used, the get method is wrapped in createComputedGetter, otherwise in createGetterInvoker;
- The set method and get method are encapsulated in sharedPropertyDefinition and monitored.
createComputedGetter
- Cache control is controlled by watcher.dirty, which watcher.evaluate uses to recalculate, update the cache value, and reset dirty to false to indicate that the cache has been updated
watcher.depend()
if (Dep.target) {
watcher.depend();
}
Copy the code
Watcher.prototype.depend = function() {
var i = this.deps.length;
while (i--) {
// this.deps[i].depend();
dep.addSub(Dep.target)
}
};
Copy the code
The above code has a matchmaking function:
- P refers to C, C refers to D;
- In theory, when D changes, C changes, and C notifies P to update;
- In fact, C allows D to establish a connection with P, so that D directly notifies P when it changes;
Dep. What is the target
Dep.target is the page watcher
createGetterInvoker
Execute the getter function for computed directly if there is no cache
Related interview questions
- If the value of the calculated attribute c is c = a+b, if a=1, b=2; After ab is updated to a=2 and b=1, c is recalculated or retrieved from the cache?
Vue series of courses
Vue source code analysis will be conducted in succession recently, a series of courses are as follows:
The state series
- Principle of props
- Principle of the methods
- Principle of the data
- Principles of the computed
- Watch the principle
Lifecycle series
- Life cycle principle
The event series
- Principle of the event
Render series
- Render principle
Inject/dojo.provide series
- Inject/dojo.provide principle
The plugins series
- Vue – principle of the router
- Vue Router stuff
- Vuex something you should know
- Vue source code analysis of vuex principle
- Vue custom plug-in
Component series
- Keep alive – principle
- Vue single file component
- Communication between Vue components
- Vue virtual list
Series of instructions
- Vue custom command
- Caching principles for vue source code parsing
The algorithm series
- Principle of diff
- Vue compiler source code analysis
Asynchronous tasks
- NextTick principle of vUE source code parsing
other
- Vue unit tests
- Vue multicast components
- Things you didn’t know about Vue
- Vue skills big decryption
- Interview – VUE data responsive implementation of advanced front-end