7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule Transaction
10
+ * @flow
11
*/
12
13
'use strict';
14
15
var invariant = require('invariant');
16
17
+var OBSERVED_ERROR = {};
18
+
19
/**
20
* `Transaction` creates a black box that is able to wrap any method such that
21
* certain invariants are maintained before and after the method is invoked
77
*
78
* @class Transaction
79
*/
77
-var Mixin = {
80
+var TransactionImpl = {
81
/**
82
* Sets up this instance so that it is prepared for collecting metrics. Does
83
* so such that this setup method may be used on an instance that is already
85
* That can be useful if you decide to make your subclass of this mixin a
86
* "PooledClass".
87
*/
85
- reinitializeTransaction: function() {
88
+ reinitializeTransaction: function(): void {
89
this.transactionWrappers = this.getTransactionWrappers();
90
if (this.wrapperInitData) {
91
this.wrapperInitData.length = 0;
103
*/
104
getTransactionWrappers: null,
105
103
- isInTransaction: function() {
106
+ isInTransaction: function(): bool {
107
return !!this._isInTransaction;
108
},
109
124
*
125
* @return {*} Return value from `method`.
126
*/
124
- perform: function(method, scope, a, b, c, d, e, f) {
127
+ perform: function<
128
+ A, B, C, D, E, F, G,
129
+ T: (a: A, b: B, c: C, d: D, e: E, f: F) => G // eslint-disable-line space-before-function-paren
130
+ >(
131
+ method: T, scope: any,
132
+ a: A, b: B, c: C, d: D, e: E, f: F,
133
+ ): G {
134
invariant(
135
!this.isInTransaction(),
136
'Transaction.perform(...): Cannot initialize a transaction when there ' +
169
return ret;
170
},
171
163
- initializeAll: function(startIndex) {
172
+ initializeAll: function(startIndex: number): void {
173
var transactionWrappers = this.transactionWrappers;
174
for (var i = startIndex; i < transactionWrappers.length; i++) {
175
var wrapper = transactionWrappers[i];
178
// OBSERVED_ERROR state before overwriting it with the real return value
179
// of initialize -- if it's still set to OBSERVED_ERROR in the finally
180
// block, it means wrapper.initialize threw.
172
- this.wrapperInitData[i] = Transaction.OBSERVED_ERROR;
181
+ this.wrapperInitData[i] = OBSERVED_ERROR;
182
this.wrapperInitData[i] = wrapper.initialize ?
183
wrapper.initialize.call(this) :
184
null;
185
} finally {
177
- if (this.wrapperInitData[i] === Transaction.OBSERVED_ERROR) {
186
+ if (this.wrapperInitData[i] === OBSERVED_ERROR) {
187
// The initializer for wrapper i threw an error; initialize the
188
// remaining wrappers but silence any exceptions from them to ensure
189
// that the first error is the one to bubble up.
202
* (`close`rs that correspond to initializers that failed will not be
203
* invoked).
204
*/
196
- closeAll: function(startIndex) {
205
+ closeAll: function(startIndex: number): void {
206
invariant(
207
this.isInTransaction(),
208
'Transaction.closeAll(): Cannot close transaction when none are open.'
218
// close -- if it's still set to true in the finally block, it means
219
// wrapper.close threw.
220
errorThrown = true;
212
- if (initData !== Transaction.OBSERVED_ERROR && wrapper.close) {
221
+ if (initData !== OBSERVED_ERROR && wrapper.close) {
222
wrapper.close.call(this, initData);
223
}
224
errorThrown = false;
238
},
239
};
240
232
-var Transaction = {
233
-
234
- Mixin: Mixin,
235
-
236
- /**
237
- * Token to look for to determine if an error occurred.
238
- */
239
- OBSERVED_ERROR: {},
240
-
241
-};
241
+export type Transaction = typeof TransactionImpl;
242
243
-module.exports = Transaction;
243
+module.exports = TransactionImpl;