]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
chore: improve edison/fix/lazyHydrate 12975/head
authordaiwei <daiwei521@126.com>
Sun, 2 Mar 2025 14:28:36 +0000 (22:28 +0800)
committerdaiwei <daiwei521@126.com>
Sun, 2 Mar 2025 14:28:36 +0000 (22:28 +0800)
packages/runtime-core/src/apiAsyncComponent.ts
packages/runtime-core/src/hydrationStrategies.ts

index 2c0ac4b7d20a0c735b9fab7116bef5a97ee633d0..ac016e5bb65ae002d7a197227e931ebd69023b24 100644 (file)
@@ -124,15 +124,22 @@ export function defineAsyncComponent<
 
     __asyncHydrate(el, instance, hydrate) {
       if (hydrateStrategy) {
-        let teardown: (() => void) | void
-        if (resolvedComp) {
-          teardown = hydrateStrategy(hydrate, cb => forEachElement(el, cb))
-        } else {
-          teardown = hydrateStrategy(
-            () => performLoad().then(() => !instance.isUnmounted && hydrate()),
-            cb => forEachElement(el, cb),
-          )
+        const hydrateWithCallback = (postHydrate?: () => void) => {
+          if (resolvedComp) {
+            hydrate()
+            postHydrate && postHydrate()
+          } else {
+            performLoad().then(() => {
+              if (!instance.isUnmounted) {
+                hydrate()
+                postHydrate && postHydrate()
+              }
+            })
+          }
         }
+        let teardown = hydrateStrategy(hydrateWithCallback, cb =>
+          forEachElement(el, cb),
+        )
         if (teardown) {
           ;(instance.bum || (instance.bum = [])).push(teardown)
         }
index 071ac819b686e5c098fc4ff0767959de9b2c28f5..0e99d6be02612eb0e22cf24ba7b0ca894731a140 100644 (file)
@@ -18,7 +18,7 @@ const cancelIdleCallback: Window['cancelIdleCallback'] =
  *          listeners.
  */
 export type HydrationStrategy = (
-  hydrate: () => void | Promise<any>,
+  hydrate: (postHydrate?: () => void) => void,
   forEachElement: (cb: (el: Element) => any) => void,
 ) => (() => void) | void
 
@@ -29,7 +29,7 @@ export type HydrationStrategyFactory<Options> = (
 export const hydrateOnIdle: HydrationStrategyFactory<number> =
   (timeout = 10000) =>
   hydrate => {
-    const id = requestIdleCallback(hydrate, { timeout })
+    const id = requestIdleCallback(() => hydrate(), { timeout })
     return () => cancelIdleCallback(id)
   }
 
@@ -73,8 +73,9 @@ export const hydrateOnMediaQuery: HydrationStrategyFactory<string> =
       if (mql.matches) {
         hydrate()
       } else {
-        mql.addEventListener('change', hydrate, { once: true })
-        return () => mql.removeEventListener('change', hydrate)
+        const doHydrate = () => hydrate()
+        mql.addEventListener('change', doHydrate, { once: true })
+        return () => mql.removeEventListener('change', doHydrate)
       }
     }
   }
@@ -86,14 +87,14 @@ export const hydrateOnInteraction: HydrationStrategyFactory<
   (hydrate, forEach) => {
     if (isString(interactions)) interactions = [interactions]
     let hasHydrated = false
-    const doHydrate = async (e: Event) => {
+    const doHydrate = (e: Event) => {
       if (!hasHydrated) {
         hasHydrated = true
         teardown()
-        // eslint-disable-next-line no-restricted-syntax
-        await hydrate()
-        // replay event
-        e.target!.dispatchEvent(new (e.constructor as any)(e.type, e))
+        hydrate(() => {
+          // replay event
+          e.target!.dispatchEvent(new (e.constructor as any)(e.type, e))
+        })
       }
     }
     const teardown = () => {