Skip to content

Commit 5c2ce00

Browse files
committed
feat(weex): WIP fix flow + handle errors in recycle-list template render
1 parent 801f793 commit 5c2ce00

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

flow/compiler.js

-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ declare type CompilerOptions = {
2121
shouldDecodeNewlinesForHref?: boolean;
2222
optimize?: boolean;
2323

24-
// support <recycle-list> in weex
25-
recyclable?: boolean;
26-
2724
// for ssr optimization compiler
2825
scopeId?: string;
2926

@@ -37,7 +34,6 @@ declare type CompilerOptions = {
3734
declare type CompiledResult = {
3835
ast: ?ASTElement;
3936
render: string;
40-
'@render'?: string;
4137
staticRenderFns: Array<string>;
4238
stringRenderFns?: Array<string>;
4339
errors?: Array<string>;

src/platforms/weex/compiler/index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ import {
1414
getTagNamespace
1515
} from '../util/index'
1616

17-
export const baseOptions: CompilerOptions = {
17+
export type WeexCompilerOptions = CompilerOptions & {
18+
// whether to compile special template for <recycle-list>
19+
recyclable?: boolean;
20+
};
21+
22+
export type WeexCompiledResult = CompiledResult & {
23+
'@render'?: string;
24+
};
25+
26+
export const baseOptions: WeexCompilerOptions = {
1827
modules,
1928
directives,
2029
isUnaryTag,
@@ -31,8 +40,8 @@ const compiler = createCompiler(baseOptions)
3140

3241
export function compile (
3342
template: string,
34-
options?: CompilerOptions
35-
): CompiledResult {
43+
options?: WeexCompilerOptions
44+
): WeexCompiledResult {
3645
let generateAltRender = false
3746
if (options && options.recyclable === true) {
3847
generateAltRender = true

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import { addAttr } from 'compiler/helpers'
44
import { RECYCLE_LIST_MARKER } from 'weex/util/index'
5+
import type { WeexCompilerOptions } from 'weex/compiler/index'
56

67
// mark components as inside recycle-list so that we know we need to invoke
78
// their special @render function instead of render in create-component.js
8-
export function postTransformComponent (el: ASTElement, options: CompilerOptions) {
9+
export function postTransformComponent (el: ASTElement, options: WeexCompilerOptions) {
10+
// $flow-disable-line (we know isReservedTag is there)
911
if (!options.isReservedTag(el.tag) && el.tag !== 'cell-slot') {
1012
addAttr(el, RECYCLE_LIST_MARKER, true)
1113
}

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

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

3+
import type { WeexCompilerOptions } from 'weex/compiler/index'
34
import { postTransformComponent } from './component'
45
import { postTransformText } from './text'
56
import { preTransformVBind } from './v-bind'
@@ -9,12 +10,12 @@ import { postTransformVOn } from './v-on'
910

1011
let currentRecycleList = null
1112

12-
function shouldCompile (el: ASTElement, options: CompilerOptions) {
13+
function shouldCompile (el: ASTElement, options: WeexCompilerOptions) {
1314
return options.recyclable ||
1415
(currentRecycleList && el !== currentRecycleList)
1516
}
1617

17-
function preTransformNode (el: ASTElement, options: CompilerOptions) {
18+
function preTransformNode (el: ASTElement, options: WeexCompilerOptions) {
1819
if (el.tag === 'recycle-list') {
1920
currentRecycleList = el
2021
}
@@ -25,13 +26,13 @@ function preTransformNode (el: ASTElement, options: CompilerOptions) {
2526
}
2627
}
2728

28-
function transformNode (el: ASTElement, options: CompilerOptions) {
29+
function transformNode (el: ASTElement, options: WeexCompilerOptions) {
2930
if (shouldCompile(el, options)) {
3031
// do nothing yet
3132
}
3233
}
3334

34-
function postTransformNode (el: ASTElement, options: CompilerOptions) {
35+
function postTransformNode (el: ASTElement, options: WeexCompilerOptions) {
3536
if (shouldCompile(el, options)) {
3637
postTransformComponent(el, options)
3738
// <text>: transform children text into value attr
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
/* @flow */
22

3+
import { warn } from 'core/util/debug'
4+
import { handleError } from 'core/util/error'
35
import { RECYCLE_LIST_MARKER } from 'weex/util/index'
46
import { createComponentInstanceForVnode } from 'core/vdom/create-component'
57

68
export function isRecyclableComponent (vnode: VNodeWithData): boolean {
7-
return vnode.data.attrs && (RECYCLE_LIST_MARKER in vnode.data.attrs)
9+
return vnode.data.attrs
10+
? (RECYCLE_LIST_MARKER in vnode.data.attrs)
11+
: false
812
}
913

10-
export function renderRecyclableComponentTemplate (vnode: VNodeWithData): VNode {
14+
export function renderRecyclableComponentTemplate (vnode: MountedComponentVNode): VNode {
1115
// TODO:
12-
// 1. adding @isComponentRoot / @componentProps to the root node
13-
// 2. proper error handling
16+
// adding @isComponentRoot / @componentProps to the root node
17+
18+
// $flow-disable-line
1419
delete vnode.data.attrs[RECYCLE_LIST_MARKER]
15-
const instance = createComponentInstanceForVnode(vnode)
16-
return instance.$options['@render'].call(instance)
20+
const vm = createComponentInstanceForVnode(vnode)
21+
const render = (vm.$options: any)['@render']
22+
if (render) {
23+
try {
24+
return render.call(vm)
25+
} catch (err) {
26+
handleError(err, vm, `@render`)
27+
}
28+
} else {
29+
warn(
30+
`@render function not defined on component used in <recycle-list>. ` +
31+
`Make sure to declare \`recyclable="true"\` on the component's template.`,
32+
vm
33+
)
34+
}
1735
}

0 commit comments

Comments
 (0)