File tree Expand file tree Collapse file tree 3 files changed +41
-45
lines changed Expand file tree Collapse file tree 3 files changed +41
-45
lines changed Original file line number Diff line number Diff line change 33import RenderStream from './render-stream'
44import { createRenderFunction } from './render'
55import { warn } from 'core/util/debug'
6-
7- export const MAX_STACK_DEPTH = 1000
6+ import { createWriteFunction } from './write'
87
98export function createRenderer ( {
109 modules = [ ] ,
@@ -35,28 +34,9 @@ export function createRenderer ({
3534 done : ( err : ?Error , res : ?string ) = > any
3635 ) : void {
3736 let result = ''
38- let stackDepth = 0
39-
40- const write = ( text : string , next : Function ) = > {
41- if ( write . caching && text ) {
42- write . buffer += text
43- }
37+ const write = createWriteFunction ( text => {
4438 result += text
45- if ( stackDepth >= MAX_STACK_DEPTH ) {
46- process . nextTick ( ( ) => {
47- try { next ( ) } catch ( e ) {
48- done ( e )
49- }
50- } )
51- } else {
52- stackDepth ++
53- next ( )
54- stackDepth --
55- }
56- }
57- write . caching = false
58- write . buffer = ''
59-
39+ } , done )
6040 try {
6141 render ( component , write , ( ) => {
6242 done ( null , result )
Original file line number Diff line number Diff line change 11/* @flow */
22
33import stream from 'stream'
4- import { MAX_STACK_DEPTH } from './create-renderer '
4+ import { createWriteFunction } from './write '
55
66/**
77 * Original RenderStream implmentation by Sasha Aickin (@aickin)
@@ -25,33 +25,17 @@ export default class RenderStream extends stream.Readable {
2525 this . expectedSize = 0
2626 this . stackDepth = 0
2727
28- const write = this . write = ( text : string , next : Function ) => {
29- if ( write . caching && text ) {
30- write . buffer += text
31- }
28+ this . write = createWriteFunction ( ( text , next ) => {
3229 const n = this . expectedSize
3330 this . buffer += text
3431 if ( this . buffer . length >= n ) {
3532 this . next = next
3633 this . pushBySize ( n )
37- } else {
38- // continue rendering until we have enough text to call this.push().
39- // sometimes do this as process.nextTick to get out of stack overflows.
40- if ( this . stackDepth >= MAX_STACK_DEPTH ) {
41- process . nextTick ( ( ) => {
42- try { next ( ) } catch ( e ) {
43- this . emit ( 'error' , e )
44- }
45- } )
46- } else {
47- this . stackDepth ++
48- next ( )
49- this . stackDepth --
50- }
34+ return true // we will decide when to call next
5135 }
52- }
53- write . caching = false
54- write . buffer = ''
36+ } , err => {
37+ this . emit ( 'error' , err )
38+ } )
5539
5640 this . end = ( ) => {
5741 // the rendering is finished; we should push out the last of the buffer.
Original file line number Diff line number Diff line change 1+ /* @flow */
2+
3+ const MAX_STACK_DEPTH = 1000
4+
5+ export function createWriteFunction (
6+ write : Function ,
7+ onError : Function
8+ ) : Function {
9+ let stackDepth = 0
10+ const cachedWrite = ( text , next ) => {
11+ if ( text && cachedWrite . caching ) {
12+ cachedWrite . buffer += text
13+ }
14+ const waitForNext = write ( text , next )
15+ if ( ! waitForNext ) {
16+ if ( stackDepth >= MAX_STACK_DEPTH ) {
17+ process . nextTick ( ( ) => {
18+ try { next ( ) } catch ( e ) {
19+ onError ( e )
20+ }
21+ } )
22+ } else {
23+ stackDepth ++
24+ next ( )
25+ stackDepth --
26+ }
27+ }
28+ }
29+ cachedWrite . caching = false
30+ cachedWrite . buffer = ''
31+ return cachedWrite
32+ }
You can’t perform that action at this time.
0 commit comments