Skip to content

Commit 801f793

Browse files
committed
feat(weex): WIP invoke recycle-list child component with backing instance
1 parent c1743a9 commit 801f793

File tree

11 files changed

+59
-17
lines changed

11 files changed

+59
-17
lines changed

flow/compiler.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ declare type CompilerOptions = {
1919
shouldDecodeTags?: boolean;
2020
shouldDecodeNewlines?: boolean;
2121
shouldDecodeNewlinesForHref?: boolean;
22+
optimize?: boolean;
2223

2324
// support <recycle-list> in weex
2425
recyclable?: boolean;

src/compiler/helpers.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ export function pluckModuleFunction<F: Function> (
1818

1919
export function addProp (el: ASTElement, name: string, value: string) {
2020
(el.props || (el.props = [])).push({ name, value })
21+
el.plain = false
2122
}
2223

2324
export function addAttr (el: ASTElement, name: string, value: any) {
2425
(el.attrs || (el.attrs = [])).push({ name, value })
26+
el.plain = false
2527
}
2628

2729
export function addDirective (
@@ -33,6 +35,7 @@ export function addDirective (
3335
modifiers: ?ASTModifiers
3436
) {
3537
(el.directives || (el.directives = [])).push({ name, rawName, value, arg, modifiers })
38+
el.plain = false
3639
}
3740

3841
export function addHandler (
@@ -105,6 +108,8 @@ export function addHandler (
105108
} else {
106109
events[name] = newHandler
107110
}
111+
112+
el.plain = false
108113
}
109114

110115
export function getBindingAttr (

src/compiler/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export const createCompiler = createCompilerCreator(function baseCompile (
1313
options: CompilerOptions
1414
): CompiledResult {
1515
const ast = parse(template.trim(), options)
16-
optimize(ast, options)
16+
if (options.optimize !== false) {
17+
optimize(ast, options)
18+
}
1719
const code = generate(ast, options)
1820
return {
1921
ast,

src/core/vdom/create-component.js

+9-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import VNode from './vnode'
44
import { resolveConstructorOptions } from 'core/instance/init'
55
import { queueActivatedComponent } from 'core/observer/scheduler'
66
import { createFunctionalComponent } from './create-functional-component'
7+
import { renderRecyclableComponentTemplate } from 'weex/runtime/recycle-list/render-component-template'
78

89
import {
910
warn,
@@ -144,21 +145,6 @@ export function createComponent (
144145

145146
data = data || {}
146147

147-
// recycle-list optimized render function for extracting cell-slot
148-
// template. This is essentially inline expanding instead of creating
149-
// an actual instance.
150-
// https://github.com/Hanks10100/weex-native-directive/tree/master/component
151-
if (__WEEX__ && data.attrs && data.attrs['@isInRecycleList']) {
152-
const altRender = Ctor.options['@render']
153-
if (altRender) {
154-
return altRender.call(
155-
context,
156-
context.$createElement,
157-
data.attrs
158-
)
159-
}
160-
}
161-
162148
// resolve constructor options in case global mixins are applied after
163149
// component constructor creation
164150
resolveConstructorOptions(Ctor)
@@ -206,6 +192,14 @@ export function createComponent (
206192
{ Ctor, propsData, listeners, tag, children },
207193
asyncFactory
208194
)
195+
196+
// Weex specific: invoke recycle-list optimized @render function for
197+
// extracting cell-slot template.
198+
// https://github.com/Hanks10100/weex-native-directive/tree/master/component
199+
if (__WEEX__ && data.attrs && ('@inRecycleList' in data.attrs)) {
200+
return renderRecyclableComponentTemplate(vnode)
201+
}
202+
209203
return vnode
210204
}
211205

src/platforms/weex/compiler/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export function compile (
4343
// generate @render function for <recycle-list>
4444
if (options && generateAltRender) {
4545
options.recyclable = true
46+
// disable static optimizations
47+
options.optimize = false
4648
const { render } = compiler.compile(template, options)
4749
result['@render'] = render
4850
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* @flow */
2+
3+
import { addAttr } from 'compiler/helpers'
4+
import { RECYCLE_LIST_MARKER } from 'weex/util/index'
5+
6+
// mark components as inside recycle-list so that we know we need to invoke
7+
// their special @render function instead of render in create-component.js
8+
export function postTransformComponent (el: ASTElement, options: CompilerOptions) {
9+
if (!options.isReservedTag(el.tag) && el.tag !== 'cell-slot') {
10+
addAttr(el, RECYCLE_LIST_MARKER, true)
11+
}
12+
}

src/platforms/weex/compiler/modules/recycle-list/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22

3+
import { postTransformComponent } from './component'
34
import { postTransformText } from './text'
45
import { preTransformVBind } from './v-bind'
56
import { preTransformVIf } from './v-if'
@@ -32,6 +33,7 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
3233

3334
function postTransformNode (el: ASTElement, options: CompilerOptions) {
3435
if (shouldCompile(el, options)) {
36+
postTransformComponent(el, options)
3537
// <text>: transform children text into value attr
3638
if (el.tag === 'text') {
3739
postTransformText(el, options)

src/platforms/weex/compiler/modules/recycle-list/text.js

-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ export function postTransformText (el: ASTElement, options: CompilerOptions) {
1919
if (el.children.length) {
2020
addAttr(el, 'value', genText(el.children[0]))
2121
el.children = []
22-
el.plain = false
2322
}
2423
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* @flow */
2+
3+
import { RECYCLE_LIST_MARKER } from 'weex/util/index'
4+
import { createComponentInstanceForVnode } from 'core/vdom/create-component'
5+
6+
export function isRecyclableComponent (vnode: VNodeWithData): boolean {
7+
return vnode.data.attrs && (RECYCLE_LIST_MARKER in vnode.data.attrs)
8+
}
9+
10+
export function renderRecyclableComponentTemplate (vnode: VNodeWithData): VNode {
11+
// TODO:
12+
// 1. adding @isComponentRoot / @componentProps to the root node
13+
// 2. proper error handling
14+
delete vnode.data.attrs[RECYCLE_LIST_MARKER]
15+
const instance = createComponentInstanceForVnode(vnode)
16+
return instance.$options['@render'].call(instance)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// import {
2+
// // id, 'lifecycle', hookname, fn
3+
// // https://github.com/Hanks10100/weex-native-directive/tree/master/component
4+
// registerComponentHook,
5+
// updateComponentData
6+
// } from '../util/index'

src/platforms/weex/util/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ declare var document: Object;
44
import { makeMap } from 'shared/util'
55
import { warn } from 'core/util/index'
66

7+
export const RECYCLE_LIST_MARKER = '@inRecycleList'
8+
79
export const isReservedTag = makeMap(
810
'template,script,style,element,content,slot,link,meta,svg,view,' +
911
'a,div,img,image,text,span,input,switch,textarea,spinner,select,' +

0 commit comments

Comments
 (0)