1818
1919"use strict" ;
2020
21- var throwIf = require ( 'throwIf' ) ;
22-
23- var DUAL_TRANSACTION = 'DUAL_TRANSACTION' ;
24- var MISSING_TRANSACTION = 'MISSING_TRANSACTION' ;
25- if ( __DEV__ ) {
26- DUAL_TRANSACTION =
27- 'Cannot initialize transaction when there is already an outstanding ' +
28- 'transaction. Common causes of this are trying to render a component ' +
29- 'when you are already rendering a component or attempting a state ' +
30- 'transition while in a render function. Another possibility is that ' +
31- 'you are rendering new content (or state transitioning) in a ' +
32- 'componentDidRender callback. If this is not the case, please report the ' +
33- 'issue immediately.' ;
34-
35- MISSING_TRANSACTION =
36- 'Cannot close transaction when there is none open.' ;
37- }
21+ var invariant = require ( 'invariant' ) ;
3822
3923/**
4024 * `Transaction` creates a black box that is able to wrap any method such that
@@ -156,26 +140,33 @@ var Mixin = {
156140 * @return Return value from `method`.
157141 */
158142 perform : function ( method , scope , a , b , c , d , e , f ) {
159- throwIf ( this . isInTransaction ( ) , DUAL_TRANSACTION ) ;
143+ invariant (
144+ ! this . isInTransaction ( ) ,
145+ 'Transaction.perform(...): Cannot initialize a transaction when there ' +
146+ 'is already an outstanding transaction.'
147+ ) ;
160148 var memberStart = Date . now ( ) ;
161- var err = null ;
149+ var errorToThrow = null ;
162150 var ret ;
163151 try {
164152 this . initializeAll ( ) ;
165153 ret = method . call ( scope , a , b , c , d , e , f ) ;
166- } catch ( ie_requires_catch ) {
167- err = ie_requires_catch ;
154+ } catch ( error ) {
155+ // IE8 requires `catch` in order to use `finally`.
156+ errorToThrow = error ;
168157 } finally {
169158 var memberEnd = Date . now ( ) ;
170159 this . methodInvocationTime += ( memberEnd - memberStart ) ;
171160 try {
172161 this . closeAll ( ) ;
173- } catch ( closeAllErr ) {
174- err = err || closeAllErr ;
162+ } catch ( closeError ) {
163+ // If `method` throws, prefer to show that stack trace over any thrown
164+ // by invoking `closeAll`.
165+ errorToThrow = errorToThrow || closeError ;
175166 }
176167 }
177- if ( err ) {
178- throw err ;
168+ if ( errorToThrow ) {
169+ throw errorToThrow ;
179170 }
180171 return ret ;
181172 } ,
@@ -184,24 +175,26 @@ var Mixin = {
184175 this . _isInTransaction = true ;
185176 var transactionWrappers = this . transactionWrappers ;
186177 var wrapperInitTimes = this . timingMetrics . wrapperInitTimes ;
187- var err = null ;
178+ var errorToThrow = null ;
188179 for ( var i = 0 ; i < transactionWrappers . length ; i ++ ) {
189180 var initStart = Date . now ( ) ;
190181 var wrapper = transactionWrappers [ i ] ;
191182 try {
192- this . wrapperInitData [ i ] =
193- wrapper . initialize ? wrapper . initialize . call ( this ) : null ;
194- } catch ( initErr ) {
195- err = err || initErr ; // Remember the first error.
183+ this . wrapperInitData [ i ] = wrapper . initialize ?
184+ wrapper . initialize . call ( this ) :
185+ null ;
186+ } catch ( initError ) {
187+ // Prefer to show the stack trace of the first error.
188+ errorToThrow = errorToThrow || initError ;
196189 this . wrapperInitData [ i ] = Transaction . OBSERVED_ERROR ;
197190 } finally {
198191 var curInitTime = wrapperInitTimes [ i ] ;
199192 var initEnd = Date . now ( ) ;
200193 wrapperInitTimes [ i ] = ( curInitTime || 0 ) + ( initEnd - initStart ) ;
201194 }
202195 }
203- if ( err ) {
204- throw err ;
196+ if ( errorToThrow ) {
197+ throw errorToThrow ;
205198 }
206199 } ,
207200
@@ -212,10 +205,13 @@ var Mixin = {
212205 * invoked).
213206 */
214207 closeAll : function ( ) {
215- throwIf ( ! this . isInTransaction ( ) , MISSING_TRANSACTION ) ;
208+ invariant (
209+ this . isInTransaction ( ) ,
210+ 'Transaction.closeAll(): Cannot close transaction when none are open.'
211+ ) ;
216212 var transactionWrappers = this . transactionWrappers ;
217213 var wrapperCloseTimes = this . timingMetrics . wrapperCloseTimes ;
218- var err = null ;
214+ var errorToThrow = null ;
219215 for ( var i = 0 ; i < transactionWrappers . length ; i ++ ) {
220216 var wrapper = transactionWrappers [ i ] ;
221217 var closeStart = Date . now ( ) ;
@@ -224,8 +220,9 @@ var Mixin = {
224220 if ( initData !== Transaction . OBSERVED_ERROR ) {
225221 wrapper . close && wrapper . close . call ( this , initData ) ;
226222 }
227- } catch ( closeErr ) {
228- err = err || closeErr ; // Remember the first error.
223+ } catch ( closeError ) {
224+ // Prefer to show the stack trace of the first error.
225+ errorToThrow = errorToThrow || closeError ;
229226 } finally {
230227 var closeEnd = Date . now ( ) ;
231228 var curCloseTime = wrapperCloseTimes [ i ] ;
@@ -234,14 +231,16 @@ var Mixin = {
234231 }
235232 this . wrapperInitData . length = 0 ;
236233 this . _isInTransaction = false ;
237- if ( err ) {
238- throw err ;
234+ if ( errorToThrow ) {
235+ throw errorToThrow ;
239236 }
240237 }
241238} ;
242239
243240var Transaction = {
241+
244242 Mixin : Mixin ,
243+
245244 /**
246245 * Token to look for to determine if an error occured.
247246 */
0 commit comments