UNPKG

1.41 kBJavaScriptView Raw
1/* @flow */
2
3/**
4 * Runtime helper for rendering static trees.
5 */
6export function renderStatic (
7 index: number,
8 isInFor: boolean
9): VNode | Array<VNode> {
10 const cached = this._staticTrees || (this._staticTrees = [])
11 let tree = cached[index]
12 // if has already-rendered static tree and not inside v-for,
13 // we can reuse the same tree.
14 if (tree && !isInFor) {
15 return tree
16 }
17 // otherwise, render a fresh tree.
18 tree = cached[index] = this.$options.staticRenderFns[index].call(
19 this._renderProxy,
20 null,
21 this // for render fns generated for functional component templates
22 )
23 markStatic(tree, `__static__${index}`, false)
24 return tree
25}
26
27/**
28 * Runtime helper for v-once.
29 * Effectively it means marking the node as static with a unique key.
30 */
31export function markOnce (
32 tree: VNode | Array<VNode>,
33 index: number,
34 key: string
35) {
36 markStatic(tree, `__once__${index}${key ? `_${key}` : ``}`, true)
37 return tree
38}
39
40function markStatic (
41 tree: VNode | Array<VNode>,
42 key: string,
43 isOnce: boolean
44) {
45 if (Array.isArray(tree)) {
46 for (let i = 0; i < tree.length; i++) {
47 if (tree[i] && typeof tree[i] !== 'string') {
48 markStaticNode(tree[i], `${key}_${i}`, isOnce)
49 }
50 }
51 } else {
52 markStaticNode(tree, key, isOnce)
53 }
54}
55
56function markStaticNode (node, key, isOnce) {
57 node.isStatic = true
58 node.key = key
59 node.isOnce = isOnce
60}