Przeglądaj źródła

fix(runtime-vapor): clean event handler in current effect

三咲智子 Kevin Deng 2 lat temu
rodzic
commit
11b99ba9ab
1 zmienionych plików z 19 dodań i 13 usunięć
  1. 19 13
      packages/runtime-vapor/src/dom/event.ts

+ 19 - 13
packages/runtime-vapor/src/dom/event.ts

@@ -31,22 +31,28 @@ export function on(
 ) {
   const handler: DelegatedHandler = eventHandler(handlerGetter, options)
   const cleanupMetadata = recordEventMetadata(el, event, handler)
+
+  let cleanupEvent: (() => void) | undefined
   queuePostRenderEffect(() => {
-    const cleanupEvent = addEventListener(el, event, handler, options)
+    cleanupEvent = addEventListener(el, event, handler, options)
+  })
 
-    function cleanup() {
-      cleanupMetadata()
-      cleanupEvent()
-    }
+  const scope = getCurrentScope()
+  const effect = getCurrentEffect()
+  // If we are in an effect and the effect has the same scope as
+  // the current scope, we can cleanup when the effect is disposed
+  // This solves the issue where createFor itself has an effect,
+  // but this effect is unrelated to its block.
+  if (effect && effect.scope === scope) {
+    onEffectCleanup(cleanup)
+  } else if (scope) {
+    onScopeDispose(cleanup)
+  }
 
-    const scope = getCurrentScope()
-    const effect = getCurrentEffect()
-    if (effect && effect.scope === scope) {
-      onEffectCleanup(cleanup)
-    } else if (scope) {
-      onScopeDispose(cleanup)
-    }
-  })
+  function cleanup() {
+    cleanupMetadata()
+    cleanupEvent && cleanupEvent()
+  }
 }
 
 export type DelegatedHandler = {