Skip to content

Commit

Permalink
feat(weex): WIP fix flow + handle errors in recycle-list template render
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 19, 2017
1 parent 801f793 commit 5c2ce00
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
4 changes: 0 additions & 4 deletions flow/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ declare type CompilerOptions = {
shouldDecodeNewlinesForHref?: boolean;
optimize?: boolean;

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

// for ssr optimization compiler
scopeId?: string;

Expand All @@ -37,7 +34,6 @@ declare type CompilerOptions = {
declare type CompiledResult = {
ast: ?ASTElement;
render: string;
'@render'?: string;
staticRenderFns: Array<string>;
stringRenderFns?: Array<string>;
errors?: Array<string>;
Expand Down
15 changes: 12 additions & 3 deletions src/platforms/weex/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ import {
getTagNamespace
} from '../util/index'

export const baseOptions: CompilerOptions = {
export type WeexCompilerOptions = CompilerOptions & {
// whether to compile special template for <recycle-list>
recyclable?: boolean;
};

export type WeexCompiledResult = CompiledResult & {
'@render'?: string;
};

export const baseOptions: WeexCompilerOptions = {
modules,
directives,
isUnaryTag,
Expand All @@ -31,8 +40,8 @@ const compiler = createCompiler(baseOptions)

export function compile (
template: string,
options?: CompilerOptions
): CompiledResult {
options?: WeexCompilerOptions
): WeexCompiledResult {
let generateAltRender = false
if (options && options.recyclable === true) {
generateAltRender = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import { addAttr } from 'compiler/helpers'
import { RECYCLE_LIST_MARKER } from 'weex/util/index'
import type { WeexCompilerOptions } from 'weex/compiler/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: CompilerOptions) {
export function postTransformComponent (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)
}
Expand Down
9 changes: 5 additions & 4 deletions src/platforms/weex/compiler/modules/recycle-list/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */

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

let currentRecycleList = null

function shouldCompile (el: ASTElement, options: CompilerOptions) {
function shouldCompile (el: ASTElement, options: WeexCompilerOptions) {
return options.recyclable ||
(currentRecycleList && el !== currentRecycleList)
}

function preTransformNode (el: ASTElement, options: CompilerOptions) {
function preTransformNode (el: ASTElement, options: WeexCompilerOptions) {
if (el.tag === 'recycle-list') {
currentRecycleList = el
}
Expand All @@ -25,13 +26,13 @@ function preTransformNode (el: ASTElement, options: CompilerOptions) {
}
}

function transformNode (el: ASTElement, options: CompilerOptions) {
function transformNode (el: ASTElement, options: WeexCompilerOptions) {
if (shouldCompile(el, options)) {
// do nothing yet
}
}

function postTransformNode (el: ASTElement, options: CompilerOptions) {
function postTransformNode (el: ASTElement, options: WeexCompilerOptions) {
if (shouldCompile(el, options)) {
postTransformComponent(el, options)
// <text>: transform children text into value attr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
/* @flow */

import { warn } from 'core/util/debug'
import { handleError } from 'core/util/error'
import { RECYCLE_LIST_MARKER } from 'weex/util/index'
import { createComponentInstanceForVnode } from 'core/vdom/create-component'

export function isRecyclableComponent (vnode: VNodeWithData): boolean {
return vnode.data.attrs && (RECYCLE_LIST_MARKER in vnode.data.attrs)
return vnode.data.attrs
? (RECYCLE_LIST_MARKER in vnode.data.attrs)
: false
}

export function renderRecyclableComponentTemplate (vnode: VNodeWithData): VNode {
export function renderRecyclableComponentTemplate (vnode: MountedComponentVNode): VNode {
// TODO:
// 1. adding @isComponentRoot / @componentProps to the root node
// 2. proper error handling
// adding @isComponentRoot / @componentProps to the root node

// $flow-disable-line
delete vnode.data.attrs[RECYCLE_LIST_MARKER]
const instance = createComponentInstanceForVnode(vnode)
return instance.$options['@render'].call(instance)
const vm = createComponentInstanceForVnode(vnode)
const render = (vm.$options: any)['@render']
if (render) {
try {
return render.call(vm)
} catch (err) {
handleError(err, vm, `@render`)
}
} else {
warn(
`@render function not defined on component used in <recycle-list>. ` +
`Make sure to declare \`recyclable="true"\` on the component's template.`,
vm
)
}
}

0 comments on commit 5c2ce00

Please sign in to comment.