Skip to content

Commit

Permalink
refactor: extract remove util
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 18, 2020
1 parent fd03149 commit 583f946
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
9 changes: 3 additions & 6 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
isFunction,
isString,
hasChanged,
NOOP
NOOP,
remove
} from '@vue/shared'
import {
currentInstance,
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
7 changes: 7 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 583f946

Please sign in to comment.