Skip to content

Commit

Permalink
helperify core vdom helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 5, 2017
1 parent 9467942 commit c5808dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/core/vdom/helpers/merge-hook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import { remove } from 'shared/util'
import { createFnInvoker } from './update-listeners'
import { remove, isDef, isUndef, isTrue } from 'shared/util'

export function mergeVNodeHook (def: Object, hookKey: string, hook: Function) {
let invoker
Expand All @@ -14,12 +14,12 @@ export function mergeVNodeHook (def: Object, hookKey: string, hook: Function) {
remove(invoker.fns, wrappedHook)
}

if (!oldHook) {
if (isUndef(oldHook)) {
// no existing hook
invoker = createFnInvoker([wrappedHook])
} else {
/* istanbul ignore if */
if (oldHook.fns && oldHook.merged) {
if (isDef(oldHook.fns) && isTrue(oldHook.merged)) {
// already a merged invoker
invoker = oldHook
invoker.fns.push(wrappedHook)
Expand Down
14 changes: 7 additions & 7 deletions src/core/vdom/helpers/normalize-children.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import { isPrimitive } from 'core/util/index'
import VNode, { createTextVNode } from 'core/vdom/vnode'
import { isDef, isUndef, isPrimitive } from 'shared/util'

// The template compiler attempts to minimize the need for normalization by
// statically analyzing the template at compile time.
Expand Down Expand Up @@ -41,25 +41,25 @@ function normalizeArrayChildren (children: any, nestedIndex?: string): Array<VNo
let i, c, last
for (i = 0; i < children.length; i++) {
c = children[i]
if (c == null || typeof c === 'boolean') continue
if (isUndef(c) || typeof c === 'boolean') continue
last = res[res.length - 1]
// nested
if (Array.isArray(c)) {
res.push.apply(res, normalizeArrayChildren(c, `${nestedIndex || ''}_${i}`))
} else if (isPrimitive(c)) {
if (last && last.text) {
last.text += String(c)
if (isDef(last) && isDef(last.text)) {
(last: any).text += String(c)
} else if (c !== '') {
// convert primitive to vnode
res.push(createTextVNode(c))
}
} else {
if (c.text && last && last.text) {
if (isDef(c.text) && isDef(last) && isDef(last.text)) {
res[res.length - 1] = createTextVNode(last.text + c.text)
} else {
// default key for nested array children (likely generated by v-for)
if (c.tag && c.key == null && nestedIndex != null) {
c.key = `__vlist${nestedIndex}_${i}__`
if (isDef(c.tag) && isUndef(c.key) && isDef(nestedIndex)) {
c.key = `__vlist${(nestedIndex: any)}_${i}__`
}
res.push(c)
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/vdom/helpers/update-listeners.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import { cached } from 'shared/util'
import { warn } from 'core/util/index'
import { cached, isUndef } from 'shared/util'

const normalizeEvent = cached((name: string): {
name: string,
Expand Down Expand Up @@ -51,13 +51,13 @@ export function updateListeners (
cur = on[name]
old = oldOn[name]
event = normalizeEvent(name)
if (!cur) {
if (isUndef(cur)) {
process.env.NODE_ENV !== 'production' && warn(
`Invalid handler for event "${event.name}": got ` + String(cur),
vm
)
} else if (!old) {
if (!cur.fns) {
} else if (isUndef(old)) {
if (isUndef(cur.fns)) {
cur = on[name] = createFnInvoker(cur)
}
add(event.name, cur, event.once, event.capture, event.passive)
Expand All @@ -67,7 +67,7 @@ export function updateListeners (
}
}
for (name in oldOn) {
if (!on[name]) {
if (isUndef(on[name])) {
event = normalizeEvent(name)
remove(event.name, oldOn[name], event.capture)
}
Expand Down

0 comments on commit c5808dc

Please sign in to comment.