Skip to content

Commit 583f946

Browse files
committed
refactor: extract remove util
1 parent fd03149 commit 583f946

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

packages/runtime-core/src/apiWatch.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
isFunction,
1515
isString,
1616
hasChanged,
17-
NOOP
17+
NOOP,
18+
remove
1819
} from '@vue/shared'
1920
import {
2021
currentInstance,
@@ -264,11 +265,7 @@ function doWatch(
264265
return () => {
265266
stop(runner)
266267
if (instance) {
267-
const effects = instance.effects!
268-
const index = effects.indexOf(runner)
269-
if (index > -1) {
270-
effects.splice(index, 1)
271-
}
268+
remove(instance.effects!, runner)
272269
}
273270
}
274271
}

packages/runtime-core/src/components/KeepAlive.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { VNode, cloneVNode, isVNode, VNodeProps } from '../vnode'
1111
import { warn } from '../warning'
1212
import { onBeforeUnmount, injectHook, onUnmounted } from '../apiLifecycle'
13-
import { isString, isArray, ShapeFlags } from '@vue/shared'
13+
import { isString, isArray, ShapeFlags, remove } from '@vue/shared'
1414
import { watch } from '../apiWatch'
1515
import { SuspenseBoundary } from './Suspense'
1616
import {
@@ -297,7 +297,6 @@ function injectToKeepAliveRoot(
297297
) {
298298
injectHook(type, hook, keepAliveRoot, true /* prepend */)
299299
onUnmounted(() => {
300-
const hooks = keepAliveRoot[type]!
301-
hooks.splice(hooks.indexOf(hook), 1)
300+
remove(keepAliveRoot[type]!, hook)
302301
}, target)
303302
}

packages/shared/src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export const extend = <T extends object, U extends object>(
3636
return a as any
3737
}
3838

39+
export const remove = <T>(arr: T[], el: T) => {
40+
const i = arr.indexOf(el)
41+
if (i > -1) {
42+
arr.splice(i, 1)
43+
}
44+
}
45+
3946
const hasOwnProperty = Object.prototype.hasOwnProperty
4047
export const hasOwn = (
4148
val: object,

0 commit comments

Comments
 (0)