]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: extract remove util
authorEvan You <yyx990803@gmail.com>
Tue, 18 Feb 2020 18:52:59 +0000 (13:52 -0500)
committerEvan You <yyx990803@gmail.com>
Tue, 18 Feb 2020 18:52:59 +0000 (13:52 -0500)
packages/runtime-core/src/apiWatch.ts
packages/runtime-core/src/components/KeepAlive.ts
packages/shared/src/index.ts

index b3c92239faf07964b7aa7f0f445d7fea38a8ca11..da3df3033f94500a3d5d9803f6dedd642f65d340 100644 (file)
@@ -14,7 +14,8 @@ import {
   isFunction,
   isString,
   hasChanged,
-  NOOP
+  NOOP,
+  remove
 } from '@vue/shared'
 import {
   currentInstance,
@@ -264,11 +265,7 @@ function doWatch(
   return () => {
     stop(runner)
     if (instance) {
-      const effects = instance.effects!
-      const index = effects.indexOf(runner)
-      if (index > -1) {
-        effects.splice(index, 1)
-      }
+      remove(instance.effects!, runner)
     }
   }
 }
index d5750d5aacfdc6ee95c4d0f2ae5770e0acd8da8e..8aae9b4efc29cdfb0c021b00e0e68e2e6a12dc0f 100644 (file)
@@ -10,7 +10,7 @@ import {
 import { VNode, cloneVNode, isVNode, VNodeProps } from '../vnode'
 import { warn } from '../warning'
 import { onBeforeUnmount, injectHook, onUnmounted } from '../apiLifecycle'
-import { isString, isArray, ShapeFlags } from '@vue/shared'
+import { isString, isArray, ShapeFlags, remove } from '@vue/shared'
 import { watch } from '../apiWatch'
 import { SuspenseBoundary } from './Suspense'
 import {
@@ -297,7 +297,6 @@ function injectToKeepAliveRoot(
 ) {
   injectHook(type, hook, keepAliveRoot, true /* prepend */)
   onUnmounted(() => {
-    const hooks = keepAliveRoot[type]!
-    hooks.splice(hooks.indexOf(hook), 1)
+    remove(keepAliveRoot[type]!, hook)
   }, target)
 }
index b08300cd94a6d387f166e6dd5116320af66a672a..d5c356140f25eb1d04c1cc3467f0fb7934dbac62 100644 (file)
@@ -36,6 +36,13 @@ export const extend = <T extends object, U extends object>(
   return a as any
 }
 
+export const remove = <T>(arr: T[], el: T) => {
+  const i = arr.indexOf(el)
+  if (i > -1) {
+    arr.splice(i, 1)
+  }
+}
+
 const hasOwnProperty = Object.prototype.hasOwnProperty
 export const hasOwn = (
   val: object,