Skip to content

Commit

Permalink
feat(weex): support batch update styles and attributes (#7046)
Browse files Browse the repository at this point in the history
* feat(weex): support batch update styles

* feat(weex): support batch update attributes
  • Loading branch information
Hanks10100 authored and yyx990803 committed Nov 13, 2017
1 parent c2b1cfe commit 7cf188e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/platforms/weex/runtime/modules/attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@ function updateAttrs (oldVnode: VNodeWithData, vnode: VNodeWithData) {
attrs = vnode.data.attrs = extend({}, attrs)
}

const supportBatchUpdate = typeof elm.setAttrs === 'function'
const batchedAttrs = {}
for (key in attrs) {
cur = attrs[key]
old = oldAttrs[key]
if (old !== cur) {
elm.setAttr(key, cur)
supportBatchUpdate
? (batchedAttrs[key] = cur)
: elm.setAttr(key, cur)
}
}
for (key in oldAttrs) {
if (attrs[key] == null) {
elm.setAttr(key)
supportBatchUpdate
? (batchedAttrs[key] = undefined)
: elm.setAttr(key)
}
}
if (supportBatchUpdate) {
elm.setAttrs(batchedAttrs)
}
}

export default {
Expand Down
8 changes: 6 additions & 2 deletions src/platforms/weex/runtime/modules/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ function updateClass (oldVnode: VNodeWithData, vnode: VNodeWithData) {
}

const style = getStyle(oldClassList, classList, ctx)
for (const key in style) {
el.setStyle(key, style[key])
if (typeof el.setStyles === 'function') {
el.setStyles(style)
} else {
for (const key in style) {
el.setStyle(key, style[key])
}
}
}

Expand Down
22 changes: 19 additions & 3 deletions src/platforms/weex/runtime/modules/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ function createStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
}
const elm = vnode.elm
const staticStyle = vnode.data.staticStyle
const supportBatchUpdate = typeof elm.setStyles === 'function'
const batchedStyles = {}
for (const name in staticStyle) {
if (staticStyle[name]) {
elm.setStyle(normalize(name), staticStyle[name])
supportBatchUpdate
? (batchedStyles[normalize(name)] = staticStyle[name])
: elm.setStyle(normalize(name), staticStyle[name])
}
}
if (supportBatchUpdate) {
elm.setStyles(batchedStyles)
}
updateStyle(oldVnode, vnode)
}

Expand All @@ -41,14 +48,23 @@ function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
style = vnode.data.style = extend({}, style)
}

const supportBatchUpdate = typeof elm.setStyles === 'function'
const batchedStyles = {}
for (name in oldStyle) {
if (!style[name]) {
elm.setStyle(normalize(name), '')
supportBatchUpdate
? (batchedStyles[normalize(name)] = '')
: elm.setStyle(normalize(name), '')
}
}
for (name in style) {
cur = style[name]
elm.setStyle(normalize(name), cur)
supportBatchUpdate
? (batchedStyles[normalize(name)] = cur)
: elm.setStyle(normalize(name), cur)
}
if (supportBatchUpdate) {
elm.setStyles(batchedStyles)
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/platforms/weex/runtime/modules/transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ function enter (_, vnode) {
beforeEnterHook && beforeEnterHook(el)

if (startState) {
for (const key in startState) {
el.setStyle(key, startState[key])
if (typeof el.setStyles === 'function') {
el.setStyles(startState)
} else {
for (const key in startState) {
el.setStyle(key, startState[key])
}
}
}

Expand Down

0 comments on commit 7cf188e

Please sign in to comment.