Skip to content

Commit

Permalink
feat(weex): WIP mark recycle list child component root
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 19, 2017
1 parent b06d09f commit 88f3889
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 19 deletions.
6 changes: 6 additions & 0 deletions src/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export function addAttr (el: ASTElement, name: string, value: any) {
el.plain = false
}

// add a raw attr (use this in preTransforms)
export function addRawAttr (el: ASTElement, name: string, value: any) {
el.attrsMap[name] = value
el.attrsList.push({ name, value })
}

export function addDirective (
el: ASTElement,
name: string,
Expand Down
8 changes: 6 additions & 2 deletions src/core/vdom/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import VNode from './vnode'
import { resolveConstructorOptions } from 'core/instance/init'
import { queueActivatedComponent } from 'core/observer/scheduler'
import { createFunctionalComponent } from './create-functional-component'
import { renderRecyclableComponentTemplate } from 'weex/runtime/recycle-list/render-component-template'

import {
warn,
Expand All @@ -28,6 +27,11 @@ import {
deactivateChildComponent
} from '../instance/lifecycle'

import {
isRecyclableComponent,
renderRecyclableComponentTemplate
} from 'weex/runtime/recycle-list/render-component-template'

// hooks to be invoked on component VNodes during patch
const componentVNodeHooks = {
init (
Expand Down Expand Up @@ -196,7 +200,7 @@ export function createComponent (
// Weex specific: invoke recycle-list optimized @render function for
// extracting cell-slot template.
// https://github.com/Hanks10100/weex-native-directive/tree/master/component
if (__WEEX__ && data.attrs && ('@inRecycleList' in data.attrs)) {
if (__WEEX__ && isRecyclableComponent(vnode)) {
return renderRecyclableComponentTemplate(vnode)
}
Expand Down
14 changes: 14 additions & 0 deletions src/platforms/weex/compiler/modules/recycle-list/component-root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* @flow */

import { addRawAttr } from 'compiler/helpers'

// mark component root nodes as
export function preTransformComponentRoot (
el: ASTElement,
options: WeexCompilerOptions
) {
if (!el.parent) {
// component root
addRawAttr(el, '$isComponentRoot', true)
}
}
9 changes: 6 additions & 3 deletions src/platforms/weex/compiler/modules/recycle-list/component.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* @flow */

import { addAttr } from 'compiler/helpers'
import { addRawAttr } from 'compiler/helpers'
import { RECYCLE_LIST_MARKER } from 'weex/util/index'

// mark components as inside recycle-list so that we know we need to invoke
// their special @render function instead of render in create-component.js
export function postTransformComponent (el: ASTElement, options: WeexCompilerOptions) {
export function preTransformComponent (
el: ASTElement,
options: WeexCompilerOptions
) {
// $flow-disable-line (we know isReservedTag is there)
if (!options.isReservedTag(el.tag) && el.tag !== 'cell-slot') {
addAttr(el, RECYCLE_LIST_MARKER, true)
addRawAttr(el, RECYCLE_LIST_MARKER, 'true')
}
}
6 changes: 4 additions & 2 deletions src/platforms/weex/compiler/modules/recycle-list/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import { postTransformComponent } from './component'
import { preTransformComponent } from './component'
import { preTransformComponentRoot } from './component-root'
import { postTransformText } from './text'
import { preTransformVBind } from './v-bind'
import { preTransformVIf } from './v-if'
Expand All @@ -19,6 +20,8 @@ function preTransformNode (el: ASTElement, options: WeexCompilerOptions) {
currentRecycleList = el
}
if (shouldCompile(el, options)) {
preTransformComponent(el, options)
preTransformComponentRoot(el, options)
preTransformVBind(el, options)
preTransformVIf(el, options) // also v-else-if and v-else
preTransformVFor(el, options)
Expand All @@ -33,7 +36,6 @@ function transformNode (el: ASTElement, options: WeexCompilerOptions) {

function postTransformNode (el: ASTElement, options: WeexCompilerOptions) {
if (shouldCompile(el, options)) {
postTransformComponent(el, options)
// <text>: transform children text into value attr
if (el.tag === 'text') {
postTransformText(el, options)
Expand Down
7 changes: 2 additions & 5 deletions src/platforms/weex/compiler/modules/recycle-list/v-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { camelize } from 'shared/util'
import { bindRE } from 'compiler/parser/index'
import { getAndRemoveAttr } from 'compiler/helpers'
import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers'

function parseAttrName (name: string): string {
return camelize(name.replace(bindRE, ''))
Expand All @@ -16,10 +16,7 @@ export function preTransformVBind (el: ASTElement, options: WeexCompilerOptions)
'@binding': getAndRemoveAttr(el, attr)
}
delete el.attrsMap[attr]
el.attrsMap[name] = value
el.attrsList.push({ name, value })
// addAttr(el, name, value)
// el.hasBindings = false
addRawAttr(el, name, value)
}
}
}
5 changes: 2 additions & 3 deletions src/platforms/weex/compiler/modules/recycle-list/v-for.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import { forAliasRE, forIteratorRE, stripParensRE } from 'compiler/parser/index'
import { getAndRemoveAttr } from 'compiler/helpers'
import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers'

export function preTransformVFor (el: ASTElement, options: WeexCompilerOptions) {
const exp = getAndRemoveAttr(el, 'v-for')
Expand All @@ -26,8 +26,7 @@ export function preTransformVFor (el: ASTElement, options: WeexCompilerOptions)
}
}
delete el.attrsMap['v-for']
el.attrsMap['[[repeat]]'] = desc
el.attrsList.push({ name: '[[repeat]]', value: desc })
addRawAttr(el, '[[repeat]]', desc)
} else if (process.env.NODE_ENV !== 'production' && options.warn) {
options.warn(`Invalid v-for expression: ${exp}`)
}
Expand Down
5 changes: 2 additions & 3 deletions src/platforms/weex/compiler/modules/recycle-list/v-if.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @flow */

import { getAndRemoveAttr } from 'compiler/helpers'
import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers'

function hasConditionDirective (el: ASTElement): boolean {
for (const attr in el.attrsMap) {
Expand Down Expand Up @@ -42,7 +42,6 @@ export function preTransformVIf (el: ASTElement, options: WeexCompilerOptions) {
return
}
}
el.attrsMap['[[match]]'] = exp
el.attrsList.push({ name: '[[match]]', value: exp })
addRawAttr(el, '[[match]]', exp)
}
}
2 changes: 1 addition & 1 deletion src/platforms/weex/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare var document: Object;
import { makeMap } from 'shared/util'
import { warn } from 'core/util/index'

export const RECYCLE_LIST_MARKER = '@inRecycleList'
export const RECYCLE_LIST_MARKER = '$inRecycleList'

export const isReservedTag = makeMap(
'template,script,style,element,content,slot,link,meta,svg,view,' +
Expand Down

0 comments on commit 88f3889

Please sign in to comment.