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 3
3
import RenderStream from './render-stream'
4
4
import { createRenderFunction } from './render'
5
5
import { warn } from 'core/util/debug'
6
-
7
- export const MAX_STACK_DEPTH = 1000
6
+ import { createWriteFunction } from './write'
8
7
9
8
export function createRenderer ( {
10
9
modules = [ ] ,
@@ -35,28 +34,9 @@ export function createRenderer ({
35
34
done : ( err : ?Error , res : ?string ) = > any
36
35
) : void {
37
36
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 => {
44
38
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 )
60
40
try {
61
41
render ( component , write , ( ) => {
62
42
done ( null , result )
Original file line number Diff line number Diff line change 1
1
/* @flow */
2
2
3
3
import stream from 'stream'
4
- import { MAX_STACK_DEPTH } from './create-renderer '
4
+ import { createWriteFunction } from './write '
5
5
6
6
/**
7
7
* Original RenderStream implmentation by Sasha Aickin (@aickin)
@@ -25,33 +25,17 @@ export default class RenderStream extends stream.Readable {
25
25
this . expectedSize = 0
26
26
this . stackDepth = 0
27
27
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 ) => {
32
29
const n = this . expectedSize
33
30
this . buffer += text
34
31
if ( this . buffer . length >= n ) {
35
32
this . next = next
36
33
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
51
35
}
52
- }
53
- write . caching = false
54
- write . buffer = ''
36
+ } , err => {
37
+ this . emit ( 'error' , err )
38
+ } )
55
39
56
40
this . end = ( ) => {
57
41
// 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