@@ -13,83 +13,76 @@ import { isTrue, isUndef } from 'shared/util'
13
13
import { createWriteFunction } from './write'
14
14
15
15
export default class RenderStream extends Readable {
16
- buffer : string
17
- render : ( write : Function , done : Function ) => void
18
- expectedSize : number
19
- write : Function
20
- //@ts -expect-error
21
- next : Function
22
- end : Function
23
- //@ts -expect-error
24
- done : boolean
16
+ private buffer : string = '' ;
17
+ private readonly renderComponent : ( write : Function , done : Function ) => void ;
18
+ private expectedSize : number = 0 ;
19
+ private nextFunction : Function | undefined ;
20
+ private readonly endFunction : Function ;
21
+ private isDone : boolean = false ;
25
22
26
- constructor ( render : Function ) {
27
- super ( )
28
- this . buffer = ''
29
- //@ts -expect-error
30
- this . render = render
31
- this . expectedSize = 0
32
-
33
- this . write = createWriteFunction (
34
- ( text , next ) => {
35
- const n = this . expectedSize
36
- this . buffer += text
37
- if ( this . buffer . length >= n ) {
38
- this . next = next
39
- this . pushBySize ( n )
40
- return true // we will decide when to call next
41
- }
42
- return false
43
- } ,
44
- err => {
45
- this . emit ( 'error' , err )
46
- }
47
- )
48
-
49
- this . end = ( ) => {
50
- this . emit ( 'beforeEnd' )
23
+ constructor ( renderComponent : ( write : Function , done : Function ) => void ) {
24
+ super ( { highWaterMark : 16 } ) ; // set highWaterMark to improve performance
25
+ this . renderComponent = renderComponent ;
26
+ this . endFunction = ( ) => {
27
+ this . emit ( 'beforeEnd' ) ;
51
28
// the rendering is finished; we should push out the last of the buffer.
52
- this . done = true
53
- this . push ( this . buffer )
54
- }
29
+ this . isDone = true ;
30
+ this . push ( this . buffer ) ;
31
+ } ;
55
32
}
56
33
57
- pushBySize ( n : number ) {
58
- const bufferToPush = this . buffer . substring ( 0 , n )
59
- this . buffer = this . buffer . substring ( n )
60
- this . push ( bufferToPush )
34
+ private pushBySize ( n : number ) : void {
35
+ const bufferToPush = this . buffer . substring ( 0 , n ) ;
36
+ this . buffer = this . buffer . substring ( n ) ;
37
+ this . push ( bufferToPush ) ;
61
38
}
62
39
63
- tryRender ( ) {
40
+ private writeFunction = createWriteFunction (
41
+ ( text , next ) => {
42
+ const n = this . expectedSize ;
43
+ this . buffer += text ;
44
+ if ( this . buffer . length >= n ) {
45
+ this . nextFunction = next ;
46
+ this . pushBySize ( n ) ;
47
+ return true ; // we will decide when to call next
48
+ }
49
+ return false ;
50
+ } ,
51
+ ( err : Error ) => {
52
+ this . emit ( 'error' , err ) ;
53
+ }
54
+ ) ;
55
+
56
+ private tryRender ( ) : void {
64
57
try {
65
- this . render ( this . write , this . end )
58
+ this . renderComponent ( this . writeFunction , this . endFunction ) ;
66
59
} catch ( e ) {
67
- this . emit ( 'error' , e )
60
+ this . emit ( 'error' , e ) ;
68
61
}
69
62
}
70
63
71
- tryNext ( ) {
64
+ private tryNext ( ) : void {
72
65
try {
73
- this . next ( )
66
+ this . nextFunction ! ( ) ;
74
67
} catch ( e ) {
75
- this . emit ( 'error' , e )
68
+ this . emit ( 'error' , e ) ;
76
69
}
77
70
}
78
71
79
- _read ( n : number ) {
72
+ _read ( n : number ) : void {
80
73
this . expectedSize = n
81
74
// it's possible that the last chunk added bumped the buffer up to > 2 * n,
82
75
// which means we will need to go through multiple read calls to drain it
83
76
// down to < n.
84
- if ( isTrue ( this . done ) ) {
77
+ if ( isTrue ( this . isDone ) ) {
85
78
this . push ( null )
86
79
return
87
80
}
88
81
if ( this . buffer . length >= n ) {
89
82
this . pushBySize ( n )
90
83
return
91
84
}
92
- if ( isUndef ( this . next ) ) {
85
+ if ( isUndef ( this . nextFunction ) ) {
93
86
// start the rendering chain.
94
87
this . tryRender ( )
95
88
} else {
0 commit comments