vue.js源代码core scedule.js学习笔记
vue.js源代码学习笔记corescedule.js,供大家参考,具体内容如下
/*@flow*/
importtypeWatcherfrom'./watcher'
importconfigfrom'../config'
import{callHook}from'../instance/lifecycle'
import{
warn,
nextTick,
devtools
}from'../util/index'
constqueue:Array=[]
lethas:{[key:number]:?true}={}
letcircular:{[key:number]:number}={}
letwaiting=false
letflushing=false
letindex=0
/**
*Resetthescheduler'sstate.
*/
functionresetSchedulerState(){
queue.length=0
has={}
if(process.env.NODE_ENV!=='production'){
circular={}
}
waiting=flushing=false
}
/**
*Flushbothqueuesandrunthewatchers.
*/
functionflushSchedulerQueue(){
flushing=true
letwatcher,id,vm
//Sortqueuebeforeflush.
//Thisensuresthat:
//1.Componentsareupdatedfromparenttochild.(becauseparentisalways
//createdbeforethechild)
//2.Acomponent'suserwatchersarerunbeforeitsrenderwatcher(because
//userwatchersarecreatedbeforetherenderwatcher)
//3.Ifacomponentisdestroyedduringaparentcomponent'swatcherrun,
//itswatcherscanbeskipped.
queue.sort((a,b)=>a.id-b.id)
//donotcachelengthbecausemorewatchersmightbepushed
//aswerunexistingwatchers
for(index=0;indexconfig._maxUpdateCount){
warn(
'Youmayhaveaninfiniteupdateloop'+(
watcher.user
?`inwatcherwithexpression"${watcher.expression}"`
:`inacomponentrenderfunction.`
),
watcher.vm
)
break
}
}
}
//resetschedulerbeforeupdatedhookcalled
constoldQueue=queue.slice()
resetSchedulerState()
//callupdatedhooks
index=oldQueue.length
while(index--){
watcher=oldQueue[index]
vm=watcher.vm
if(vm._watcher===watcher&&vm._isMounted){
callHook(vm,'updated')
}
}
//devtoolhook
/*istanbulignoreif*/
if(devtools&&config.devtools){
devtools.emit('flush')
}
}
/**
*Pushawatcherintothewatcherqueue.
*JobswithduplicateIDswillbeskippedunlessit's
*pushedwhenthequeueisbeingflushed.
*/
exportfunctionqueueWatcher(watcher:Watcher){
constid=watcher.id
if(has[id]==null){
has[id]=true
if(!flushing){
queue.push(watcher)
}else{
//ifalreadyflushing,splicethewatcherbasedonitsid
//ifalreadypastitsid,itwillberunnextimmediately.
leti=queue.length-1
while(i>=0&&queue[i].id>watcher.id){
i--
}
queue.splice(Math.max(i,index)+1,0,watcher)
}
//queuetheflush
if(!waiting){
waiting=true
nextTick(flushSchedulerQueue)
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。