]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: adjust lifecycle
authorEvan You <yyx990803@gmail.com>
Tue, 28 May 2019 11:59:54 +0000 (19:59 +0800)
committerEvan You <yyx990803@gmail.com>
Tue, 28 May 2019 11:59:54 +0000 (19:59 +0800)
packages/runtime-core/src/componentLifecycle.ts
packages/runtime-core/src/createRenderer.ts
packages/runtime-core/src/scheduler.ts

index 29827a5e0923a30cc7c0ee86f570e71e7ae7024d..2b0ed9d1d036754366bfa3a2ab6f9f72a178690a 100644 (file)
@@ -7,6 +7,7 @@ function injectHook(
 ) {
   if (target) {
     const existing = target[name]
+    // TODO inject a error-handling wrapped version of the hook
     if (existing !== null) {
       existing.push(hook)
     } else {
index 2b381e431ea429bbc3a07ee8a4b1f359cf497dc9..3ec66d67eee5790a734e75064a093ec774277c0f 100644 (file)
@@ -1,5 +1,5 @@
 // TODO:
-// - lifecycle / refs
+// - refs
 // - slots
 // - keep alive
 // - app context
@@ -28,17 +28,18 @@ import {
   shouldUpdateComponent,
   createComponentInstance
 } from './component'
-import {
-  queueJob,
-  queuePostFlushCb,
-  flushPostFlushCbs,
-  queueReversePostFlushCb
-} from './scheduler'
+import { queueJob, queuePostFlushCb, flushPostFlushCbs } from './scheduler'
 
 function isSameType(n1: VNode, n2: VNode): boolean {
   return n1.type === n2.type && n1.key === n2.key
 }
 
+function invokeHooks(hooks: Function[]) {
+  for (let i = 0; i < hooks.length; i++) {
+    hooks[i]()
+  }
+}
+
 export type HostNode = any
 
 export interface RendererOptions {
@@ -364,6 +365,9 @@ export function createRenderer(options: RendererOptions) {
           // initial mount
           instance.vnode = vnode
           const subTree = (instance.subTree = renderComponentRoot(instance))
+          if (instance.bm !== null) {
+            invokeHooks(instance.bm)
+          }
           patch(null, subTree, container, anchor)
           vnode.el = subTree.el
           // mounted hook
@@ -391,8 +395,7 @@ export function createRenderer(options: RendererOptions) {
           }
           // upated hook
           if (instance.u !== null) {
-            // updated hooks are queued top-down, but should be fired bottom up
-            queueReversePostFlushCb(instance.u)
+            queuePostFlushCb(instance.u)
           }
         }
       },
index bf37774c35a45f802325ef81104ee239d6f60954..978e53203ea51c772599ee631d5e87a33266ee13 100644 (file)
@@ -1,6 +1,5 @@
 const queue: Function[] = []
 const postFlushCbs: Function[] = []
-const reversePostFlushCbs: Function[] = []
 const p = Promise.resolve()
 
 let isFlushing = false
@@ -20,32 +19,16 @@ export function queueJob(job: () => void, onError?: (err: Error) => void) {
 }
 
 export function queuePostFlushCb(cb: Function | Function[]) {
-  queuePostCb(cb, postFlushCbs)
-}
-
-export function queueReversePostFlushCb(cb: Function | Function[]) {
-  queuePostCb(cb, reversePostFlushCbs)
-}
-
-function queuePostCb(cb: Function | Function[], queue: Function[]) {
   if (Array.isArray(cb)) {
-    queue.push.apply(postFlushCbs, cb)
+    postFlushCbs.push.apply(postFlushCbs, cb)
   } else {
-    queue.push(cb)
+    postFlushCbs.push(cb)
   }
 }
 
 const dedupe = (cbs: Function[]): Function[] => Array.from(new Set(cbs))
 
 export function flushPostFlushCbs() {
-  if (reversePostFlushCbs.length) {
-    const cbs = dedupe(reversePostFlushCbs)
-    reversePostFlushCbs.length = 0
-    let i = cbs.length
-    while (i--) {
-      cbs[i]()
-    }
-  }
   if (postFlushCbs.length) {
     const cbs = dedupe(postFlushCbs)
     postFlushCbs.length = 0