@@ -89,31 +89,60 @@ function renderToStringStream(element, stream, options) {
8989 'renderToStringStream(): You must pass a valid ReactElement.'
9090 ) ;
9191
92+ var usingV1 = false ;
93+ // deprecation warning for version 2. The v1 API allowed you to pass in a stream and returned
94+ // a Promise of a hash ; the v2 API returns a stream with a .hash property.
95+ // v1 also allowed an options hash, which v2 will not.
96+ if ( stream ) {
97+ usingV1 = true ;
98+ console . error (
99+ "You are using v1.x of the renderToString API, which is deprecated. " +
100+ "Instead of accepting a stream parameter and returning a Promise of a hash, the API " +
101+ "now returns a stream with a hash Promise property. " +
102+ "Support for this version of the API will be removed in the 3.0.0 version of react-dom-stream. " +
103+ "Please update your code, and for more info, check out (TODO: add URL here)."
104+ ) ;
105+ } else {
106+ stream = require ( "stream" ) . PassThrough ( ) ;
107+ }
108+
92109 var bufferSize = 10000 ;
93110 if ( options && options . bufferSize ) {
111+ console . error (
112+ "The options hash and bufferSize arguments have been deprecated and will be removed in " +
113+ "the v3.0.0 of react-dom-stream. " +
114+ "Please update your code, and for more info, check out (TODO: add URL here)."
115+ ) ;
94116 bufferSize = options . bufferSize ;
95117 }
96- var transaction ;
97- try {
98- ReactUpdates . injection . injectBatchingStrategy ( ReactServerBatchingStrategy ) ;
99-
100- var id = ReactInstanceHandles . createReactRootID ( ) ;
101- transaction = ReactServerRenderingTransaction . getPooled ( false ) ;
102-
103- stream = bufferedStream ( stream , bufferSize ) ;
104- stream = hashedStream ( stream ) ;
105- var hash = transaction . perform ( function ( ) {
106- var componentInstance = instantiateReactComponent ( element , null ) ;
107- componentInstance . mountComponentAsync ( id , transaction , emptyObject , stream ) ;
108- stream . flush ( ) ;
109- return stream . hash ( ) ;
110- } , null ) ;
111- return Promise . resolve ( hash ) ;
112- } finally {
113- ReactServerRenderingTransaction . release ( transaction ) ;
114- // Revert to the DOM batching strategy since these two renderers
115- // currently share these stateful modules.
116- ReactUpdates . injection . injectBatchingStrategy ( ReactDefaultBatchingStrategy ) ;
118+ var hashPromise = new Promise ( function ( resolve , reject ) {
119+ var transaction ;
120+ try {
121+ ReactUpdates . injection . injectBatchingStrategy ( ReactServerBatchingStrategy ) ;
122+
123+ var id = ReactInstanceHandles . createReactRootID ( ) ;
124+ transaction = ReactServerRenderingTransaction . getPooled ( false ) ;
125+
126+ var wrappedStream = hashedStream ( bufferedStream ( stream , bufferSize ) ) ;
127+ transaction . perform ( function ( ) {
128+ var componentInstance = instantiateReactComponent ( element , null ) ;
129+ componentInstance . mountComponentAsync ( id , transaction , emptyObject , wrappedStream ) ;
130+ wrappedStream . flush ( ) ;
131+ resolve ( wrappedStream . hash ( ) ) ;
132+ } , null ) ;
133+ } finally {
134+ ReactServerRenderingTransaction . release ( transaction ) ;
135+ // Revert to the DOM batching strategy since these two renderers
136+ // currently share these stateful modules.
137+ ReactUpdates . injection . injectBatchingStrategy ( ReactDefaultBatchingStrategy ) ;
138+ }
139+ } ) ;
140+
141+ if ( usingV1 ) {
142+ return hashPromise ;
143+ } else {
144+ stream . hash = hashPromise ;
145+ return stream ;
117146 }
118147}
119148
@@ -129,30 +158,59 @@ function renderToStaticMarkupStream(element, stream, options) {
129158 'renderToStaticMarkupStream(): You must pass a valid ReactElement.'
130159 ) ;
131160
161+ var usingV1 = false ;
162+ // deprecation warning for version 2. The v1 API allowed you to pass in a stream and returned
163+ // a Promise of a hash ; the v2 API returns a stream with a .hash property.
164+ // v1 also allowed an options hash, which v2 will not.
165+ if ( stream ) {
166+ usingV1 = true ;
167+ console . error (
168+ "You are using v1.x of the renderToMarkupStream API, which is deprecated. " +
169+ "Instead of accepting a stream parameter and returning a Promise, the API now just returns a stream. " +
170+ "Support for this version of the API will be removed in the 3.0.0 version of react-dom-stream. " +
171+ "Please update your code, and for more info, check out (TODO: add URL here)."
172+ ) ;
173+ } else {
174+ stream = require ( "stream" ) . PassThrough ( ) ;
175+ }
176+
132177 var bufferSize = 10000 ;
133178 if ( options && options . bufferSize ) {
179+ console . error (
180+ "The options hash and bufferSize arguments have been deprecated and will be removed in " +
181+ "the v3.0.0 of react-dom-stream. " +
182+ "Please update your code, and for more info, check out (TODO: add URL here)."
183+ ) ;
134184 bufferSize = options . bufferSize ;
135185 }
136- var transaction ;
137- try {
138- ReactUpdates . injection . injectBatchingStrategy ( ReactServerBatchingStrategy ) ;
139-
140- var id = ReactInstanceHandles . createReactRootID ( ) ;
141- transaction = ReactServerRenderingTransaction . getPooled ( true ) ;
142-
143- stream = bufferedStream ( stream , bufferSize ) ;
144- transaction . perform ( function ( ) {
145- var componentInstance = instantiateReactComponent ( element , null ) ;
146- componentInstance . mountComponentAsync ( id , transaction , emptyObject , stream ) ;
147- stream . flush ( ) ;
148- } , null ) ;
149-
150- return Promise . resolve ( null ) ;
151- } finally {
152- ReactServerRenderingTransaction . release ( transaction ) ;
153- // Revert to the DOM batching strategy since these two renderers
154- // currently share these stateful modules.
155- ReactUpdates . injection . injectBatchingStrategy ( ReactDefaultBatchingStrategy ) ;
186+ var promise = new Promise ( function ( resolve , reject ) {
187+ var transaction ;
188+ try {
189+ ReactUpdates . injection . injectBatchingStrategy ( ReactServerBatchingStrategy ) ;
190+
191+ var id = ReactInstanceHandles . createReactRootID ( ) ;
192+ transaction = ReactServerRenderingTransaction . getPooled ( true ) ;
193+
194+ var wrappedStream = bufferedStream ( stream , bufferSize ) ;
195+ transaction . perform ( function ( ) {
196+ var componentInstance = instantiateReactComponent ( element , null ) ;
197+ componentInstance . mountComponentAsync ( id , transaction , emptyObject , wrappedStream ) ;
198+ wrappedStream . flush ( ) ;
199+ } , null ) ;
200+
201+ return Promise . resolve ( null ) ;
202+ } finally {
203+ ReactServerRenderingTransaction . release ( transaction ) ;
204+ // Revert to the DOM batching strategy since these two renderers
205+ // currently share these stateful modules.
206+ ReactUpdates . injection . injectBatchingStrategy ( ReactDefaultBatchingStrategy ) ;
207+ }
208+ } ) ;
209+
210+ if ( usingV1 ) {
211+ return promise ;
212+ } else {
213+ return stream ;
156214 }
157215}
158216
0 commit comments