Răsfoiți Sursa

optimize update order

Evan You 10 ani în urmă
părinte
comite
7fae1ba7ba
1 a modificat fișierele cu 15 adăugiri și 0 ștergeri
  1. 15 0
      src/runtime/observer/batcher.js

+ 15 - 0
src/runtime/observer/batcher.js

@@ -36,12 +36,25 @@ function resetBatcherState () {
  */
 
 function flushBatcherQueue () {
+  queue.sort(queueSorter)
   runBatcherQueue(queue)
   internalQueueDepleted = true
   runBatcherQueue(userQueue)
   resetBatcherState()
 }
 
+/**
+ * Sort queue before flush.
+ * This ensures components are updated from parent to child
+ * so there will be no duplicate updates, e.g. a child was
+ * pushed into the queue first and then its parent's props
+ * changed.
+ */
+
+function queueSorter (a, b) {
+  return a.id - b.id
+}
+
 /**
  * Run the watchers in a single queue.
  *
@@ -55,6 +68,7 @@ function runBatcherQueue (queue) {
     var watcher = queue[queueIndex]
     var id = watcher.id
     has[id] = null
+    console.log('running: ' + id)
     watcher.run()
     // in dev build, check and stop circular updates.
     if (process.env.NODE_ENV !== 'production' && has[id] != null) {
@@ -85,6 +99,7 @@ function runBatcherQueue (queue) {
 export function pushWatcher (watcher) {
   var id = watcher.id
   if (has[id] == null) {
+    console.log('push: ' + id)
     if (internalQueueDepleted && !watcher.user) {
       // an internal watcher triggered by a user watcher...
       // let's run it immediately after current user watcher is done.