@@ -2730,6 +2730,53 @@ function renderToStaticNodeStream(element, cache, streamingStart, memLife=0) {
2730
2730
return new ReactMarkupReadableStream ( element , true , cache , streamingStart , memLife ) ;
2731
2731
}
2732
2732
2733
+ function createCacheStream ( cache , streamingStart , memLife = 0 ) {
2734
+ const bufferedChunks = [ ] ;
2735
+ return new Transform ( {
2736
+ // transform() is called with each chunk of data
2737
+ transform ( data , enc , cb ) {
2738
+ // We store the chunk of data (which is a Buffer) in memory
2739
+ bufferedChunks . push ( data ) ;
2740
+ // Then pass the data unchanged onwards to the next stream
2741
+ cb ( null , data ) ;
2742
+ } ,
2743
+
2744
+ // flush() is called when everything is done
2745
+ flush ( cb ) {
2746
+ // We concatenate all the buffered chunks of HTML to get the full HTML, then cache it at "key"
2747
+ let html = bufferedChunks . join ( "" ) ;
2748
+ delete streamingStart . sliceStartCount ;
2749
+
2750
+ for ( let component in streamingStart ) {
2751
+ let tagStack = [ ] ;
2752
+ let tagStart ;
2753
+ let tagEnd ;
2754
+
2755
+ do {
2756
+ if ( ! tagStart ) tagStart = streamingStart [ component ] ;
2757
+ else tagStart = ( html [ tagEnd ] === '<' ) ? tagEnd : html . indexOf ( '<' , tagEnd ) ;
2758
+ tagEnd = html . indexOf ( '>' , tagStart ) + 1 ;
2759
+ // Skip stack logic for void/self-closing elements and HTML comments
2760
+ if ( html [ tagEnd - 2 ] !== '/' && html [ tagStart + 1 ] !== '!' ) {
2761
+ // Push opening tags onto stack; pop closing tags off of stack
2762
+ if ( html [ tagStart + 1 ] !== '/' ) tagStack . push ( html . slice ( tagStart , tagEnd ) ) ;
2763
+ else tagStack . pop ( ) ;
2764
+ }
2765
+ } while ( tagStack . length !== 0 ) ;
2766
+ // cache component by slicing 'html'
2767
+ if ( memLife ) {
2768
+ cache . set ( component , html . slice ( streamingStart [ component ] , tagEnd ) , memLife , ( err ) => {
2769
+ if ( err ) console . log ( err )
2770
+ } ) ;
2771
+ } else {
2772
+ cache . set ( component , html . slice ( streamingStart [ component ] , tagEnd ) ) ;
2773
+ }
2774
+ }
2775
+ cb ( ) ;
2776
+ }
2777
+ } ) ;
2778
+ } ;
2779
+
2733
2780
class ComponentCache {
2734
2781
constructor ( config = { } ) {
2735
2782
@@ -2765,6 +2812,7 @@ var ReactDOMServerNode = {
2765
2812
renderToNodeStream : renderToNodeStream ,
2766
2813
renderToStaticNodeStream : renderToStaticNodeStream ,
2767
2814
ComponentCache : ComponentCache ,
2815
+ createCacheStream : createCacheStream ,
2768
2816
version : ReactVersion
2769
2817
} ;
2770
2818
0 commit comments