1
1
/* @flow */
2
2
3
3
import { escape } from 'he'
4
+ import { RenderContext } from './render-context'
4
5
import { compileToFunctions } from 'web/compiler/index'
5
6
import { createComponentInstanceForVnode } from 'core/vdom/create-component'
6
- import { noop } from 'shared/util'
7
7
8
8
let warned = Object . create ( null )
9
9
const warnOnce = msg => {
@@ -13,17 +13,6 @@ const warnOnce = msg => {
13
13
}
14
14
}
15
15
16
- const normalizeAsync = ( cache , method ) => {
17
- const fn = cache [ method ]
18
- if ( ! fn ) {
19
- return
20
- } else if ( fn . length > 1 ) {
21
- return ( key , cb ) => fn . call ( cache , key , cb )
22
- } else {
23
- return ( key , cb ) => cb ( fn . call ( cache , key ) )
24
- }
25
- }
26
-
27
16
const compilationCache = Object . create ( null )
28
17
const normalizeRender = vm => {
29
18
const { render, template } = vm . $options
@@ -202,76 +191,24 @@ function renderStartingTag (node: VNode, context) {
202
191
return markup + '>'
203
192
}
204
193
205
- const nextFactory = context => function next ( ) {
206
- const lastState = context . renderStates . pop ( )
207
- if ( ! lastState ) {
208
- context . done ( )
209
- // cleanup context, avoid leakage
210
- context = ( null : any )
211
- return
212
- }
213
- switch ( lastState . type ) {
214
- case 'Component' :
215
- context . activeInstance = lastState . prevActive
216
- next ( )
217
- break
218
- case 'Element' :
219
- const { children, total } = lastState
220
- const rendered = lastState . rendered ++
221
- if ( rendered < total ) {
222
- context . renderStates . push ( lastState )
223
- renderNode ( children [ rendered ] , false , context )
224
- } else {
225
- context . write ( lastState . endTag , next )
226
- }
227
- break
228
- case 'ComponentWithCache' :
229
- const { buffer, bufferIndex, key } = lastState
230
- const result = buffer [ bufferIndex ]
231
- context . cache . set ( key , result )
232
- if ( bufferIndex === 0 ) {
233
- // this is a top-level cached component,
234
- // exit caching mode.
235
- context . write . caching = false
236
- } else {
237
- // parent component is also being cached,
238
- // merge self into parent's result
239
- buffer [ bufferIndex - 1 ] += result
240
- }
241
- buffer . length = bufferIndex
242
- next ( )
243
- break
244
- }
245
- }
246
-
247
194
export function createRenderFunction (
248
195
modules : Array < Function > ,
249
196
directives : Object ,
250
197
isUnaryTag : Function ,
251
198
cache : any
252
199
) {
253
- if ( cache && ( ! cache . get || ! cache . set ) ) {
254
- throw new Error ( 'renderer cache must implement at least get & set.' )
255
- }
256
-
257
- const get = cache && normalizeAsync ( cache , 'get' )
258
- const has = cache && normalizeAsync ( cache , 'has' )
259
-
260
200
return function render (
261
201
component : Component ,
262
202
write : ( text : string , next : Function ) = > void ,
263
203
done : Function
264
204
) {
265
205
warned = Object . create ( null )
266
- const context = {
206
+ const context = new RenderContext ( {
267
207
activeInstance : component ,
268
- renderStates : [ ] ,
269
- next : noop , // for flow
270
- write, done,
208
+ write, done, renderNode,
271
209
isUnaryTag, modules, directives,
272
- cache, get, has
273
- }
274
- context . next = nextFactory ( context )
210
+ cache
211
+ } )
275
212
normalizeRender ( component )
276
213
renderNode ( component . _render ( ) , true , context )
277
214
}
0 commit comments