@@ -4,6 +4,7 @@ import { DataSnapshot } from "../api/DataSnapshot";
4
4
import { Path } from "./util/Path" ;
5
5
import { Tree } from "./util/Tree" ;
6
6
import { PRIORITY_INDEX } from "./snap/indexes/PriorityIndex" ;
7
+ import { Node } from "./snap/Node" ;
7
8
import {
8
9
LUIDGenerator ,
9
10
warn ,
@@ -94,6 +95,22 @@ declare module './Repo' {
94
95
}
95
96
}
96
97
98
+ type Transaction = {
99
+ path : Path ,
100
+ update : Function ,
101
+ onComplete : Function ,
102
+ status : number ,
103
+ order : number ,
104
+ applyLocally : boolean ,
105
+ retryCount : number ,
106
+ unwatcher : Function ,
107
+ abortReason : any ,
108
+ currentWriteId : any ,
109
+ currentInputSnapshot : any ,
110
+ currentOutputSnapshotRaw : any ,
111
+ currentOutputSnapshotResolved : any
112
+ }
113
+
97
114
/**
98
115
* Creates a new transaction, adds it to the transactions we're tracking, and sends it to the server if possible.
99
116
*
@@ -102,7 +119,10 @@ declare module './Repo' {
102
119
* @param {?function(?Error, boolean, ?DataSnapshot) } onComplete Completion callback.
103
120
* @param {boolean } applyLocally Whether or not to make intermediate results visible
104
121
*/
105
- ( Repo . prototype as any ) . startTransaction = function ( path , transactionUpdate , onComplete , applyLocally ) {
122
+ ( Repo . prototype as any ) . startTransaction = function ( path : Path ,
123
+ transactionUpdate : ( ) => any ,
124
+ onComplete : ( Error , boolean , DataSnapshot ) => any ,
125
+ applyLocally : boolean ) {
106
126
this . log_ ( 'transaction on ' + path ) ;
107
127
108
128
// Add a watch to make sure we get server updates.
@@ -112,7 +132,7 @@ declare module './Repo' {
112
132
var unwatcher = function ( ) { watchRef . off ( 'value' , valueCallback ) ; } ;
113
133
114
134
// Initialize transaction.
115
- var transaction = /** @type { Transaction } */ ( {
135
+ var transaction : Transaction = {
116
136
path : path ,
117
137
update : transactionUpdate ,
118
138
onComplete : onComplete ,
@@ -142,7 +162,7 @@ declare module './Repo' {
142
162
currentOutputSnapshotRaw : null ,
143
163
144
164
currentOutputSnapshotResolved : null
145
- } ) ;
165
+ } ;
146
166
147
167
148
168
// Run transaction initially.
@@ -156,9 +176,8 @@ declare module './Repo' {
156
176
transaction . currentOutputSnapshotResolved = null ;
157
177
if ( transaction . onComplete ) {
158
178
// We just set the input snapshot, so this cast should be safe
159
- var snapshot = new DataSnapshot ( /** @type {!Node } */ ( transaction . currentInputSnapshot ) ,
160
- new Reference ( this , transaction . path ) , PRIORITY_INDEX ) ;
161
- transaction . onComplete ( /*error=*/ null , /*committed=*/ false , snapshot ) ;
179
+ var snapshot = new DataSnapshot ( transaction . currentInputSnapshot , new Reference ( this , transaction . path ) , PRIORITY_INDEX ) ;
180
+ transaction . onComplete ( null , false , snapshot ) ;
162
181
}
163
182
} else {
164
183
validateFirebaseData ( 'transaction failed: Data returned ' , newVal , transaction . path ) ;
@@ -192,8 +211,7 @@ declare module './Repo' {
192
211
transaction . currentOutputSnapshotResolved = newNode ;
193
212
transaction . currentWriteId = this . getNextWriteId_ ( ) ;
194
213
195
- var events = this . serverSyncTree_ . applyUserOverwrite ( path , newNode , transaction . currentWriteId ,
196
- transaction . applyLocally ) ;
214
+ var events = this . serverSyncTree_ . applyUserOverwrite ( path , newNode , transaction . currentWriteId , transaction . applyLocally ) ;
197
215
this . eventQueue_ . raiseEventsForChangedPath ( path , events ) ;
198
216
199
217
this . sendReadyTransactions_ ( ) ;
@@ -206,7 +224,7 @@ declare module './Repo' {
206
224
* @return {Node }
207
225
* @private
208
226
*/
209
- ( Repo . prototype as any ) . getLatestState_ = function ( path , excludeSets ) {
227
+ ( Repo . prototype as any ) . getLatestState_ = function ( path : Path , excludeSets : [ number ] ) : Node {
210
228
return this . serverSyncTree_ . calcCompleteEventCache ( path , excludeSets ) || ChildrenNode . EMPTY_NODE ;
211
229
} ;
212
230
@@ -221,11 +239,11 @@ declare module './Repo' {
221
239
* @param {Tree.<Array.<Transaction>>= } opt_node transactionQueueTree node to start at.
222
240
* @private
223
241
*/
224
- ( Repo . prototype as any ) . sendReadyTransactions_ = function ( opt_node ) {
225
- var node = /** @type {!Tree.<Array.<!Transaction>> } */ ( opt_node || this . transactionQueueTree_ ) ;
242
+ ( Repo . prototype as any ) . sendReadyTransactions_ = function ( node ? ) {
243
+ var node = /** @type {!Tree.<Array.<!Transaction>> } */ ( node || this . transactionQueueTree_ ) ;
226
244
227
245
// Before recursing, make sure any completed transactions are removed.
228
- if ( ! opt_node ) {
246
+ if ( ! node ) {
229
247
this . pruneCompletedTransactionsBelowNode_ ( node ) ;
230
248
}
231
249
@@ -257,7 +275,7 @@ declare module './Repo' {
257
275
* @param {!Array.<Transaction> } queue Queue of transactions under the specified location.
258
276
* @private
259
277
*/
260
- ( Repo . prototype as any ) . sendTransactionQueue_ = function ( path , queue ) {
278
+ ( Repo . prototype as any ) . sendTransactionQueue_ = function ( path : Path , queue : Array < Transaction > ) {
261
279
// Mark transactions as sent and increment retry count!
262
280
var setsToIgnore = queue . map ( function ( txn ) { return txn . currentWriteId ; } ) ;
263
281
var latestState = this . getLatestState_ ( path , setsToIgnore ) ;
@@ -345,7 +363,7 @@ declare module './Repo' {
345
363
* @return {!Path } The rootmost path that was affected by rerunning transactions.
346
364
* @private
347
365
*/
348
- ( Repo . prototype as any ) . rerunTransactions_ = function ( changedPath ) {
366
+ ( Repo . prototype as any ) . rerunTransactions_ = function ( changedPath : Path ) {
349
367
var rootMostTransactionNode = this . getAncestorTransactionNode_ ( changedPath ) ;
350
368
var path = rootMostTransactionNode . path ( ) ;
351
369
@@ -363,7 +381,7 @@ declare module './Repo' {
363
381
* @param {!Path } path The path the queue is for.
364
382
* @private
365
383
*/
366
- ( Repo . prototype as any ) . rerunTransactionQueue_ = function ( queue , path ) {
384
+ ( Repo . prototype as any ) . rerunTransactionQueue_ = function ( queue : Array < Transaction > , path : Path ) {
367
385
if ( queue . length === 0 ) {
368
386
return ; // Nothing to do!
369
387
}
@@ -413,7 +431,7 @@ declare module './Repo' {
413
431
transaction . currentOutputSnapshotResolved = newNodeResolved ;
414
432
transaction . currentWriteId = this . getNextWriteId_ ( ) ;
415
433
// Mutates setsToIgnore in place
416
- setsToIgnore . splice ( setsToIgnore . indexOf ( oldWriteId ) ) ;
434
+ setsToIgnore . splice ( setsToIgnore . indexOf ( oldWriteId ) , 1 ) ;
417
435
events = events . concat (
418
436
this . serverSyncTree_ . applyUserOverwrite ( transaction . path , newNodeResolved , transaction . currentWriteId ,
419
437
transaction . applyLocally )
@@ -473,7 +491,7 @@ declare module './Repo' {
473
491
* @return {!Tree.<Array.<!Transaction>> } The rootmost node with a transaction.
474
492
* @private
475
493
*/
476
- ( Repo . prototype as any ) . getAncestorTransactionNode_ = function ( path ) {
494
+ ( Repo . prototype as any ) . getAncestorTransactionNode_ = function ( path : Path ) : Tree {
477
495
var front ;
478
496
479
497
// Start at the root and walk deeper into the tree towards path until we find a node with pending transactions.
@@ -494,7 +512,7 @@ declare module './Repo' {
494
512
* @return {Array.<Transaction> } The generated queue.
495
513
* @private
496
514
*/
497
- ( Repo . prototype as any ) . buildTransactionQueue_ = function ( transactionNode ) {
515
+ ( Repo . prototype as any ) . buildTransactionQueue_ = function ( transactionNode : Tree ) : Array < Transaction > {
498
516
// Walk any child transaction queues and aggregate them into a single queue.
499
517
var transactionQueue = [ ] ;
500
518
this . aggregateTransactionQueuesForNode_ ( transactionNode , transactionQueue ) ;
@@ -510,7 +528,7 @@ declare module './Repo' {
510
528
* @param {Array.<Transaction> } queue
511
529
* @private
512
530
*/
513
- ( Repo . prototype as any ) . aggregateTransactionQueuesForNode_ = function ( node , queue ) {
531
+ ( Repo . prototype as any ) . aggregateTransactionQueuesForNode_ = function ( node : Tree , queue : Array < Transaction > ) {
514
532
var nodeQueue = node . getValue ( ) ;
515
533
if ( nodeQueue !== null ) {
516
534
for ( var i = 0 ; i < nodeQueue . length ; i ++ ) {
@@ -531,7 +549,7 @@ declare module './Repo' {
531
549
* @param {!Tree.<Array.<!Transaction>> } node
532
550
* @private
533
551
*/
534
- ( Repo . prototype as any ) . pruneCompletedTransactionsBelowNode_ = function ( node ) {
552
+ ( Repo . prototype as any ) . pruneCompletedTransactionsBelowNode_ = function ( node : Tree ) {
535
553
var queue = node . getValue ( ) ;
536
554
if ( queue ) {
537
555
var to = 0 ;
@@ -560,7 +578,7 @@ declare module './Repo' {
560
578
* @return {!Path }
561
579
* @private
562
580
*/
563
- ( Repo . prototype as any ) . abortTransactions_ = function ( path ) {
581
+ ( Repo . prototype as any ) . abortTransactions_ = function ( path : Path ) {
564
582
var affectedPath = this . getAncestorTransactionNode_ ( path ) . path ( ) ;
565
583
566
584
var transactionNode = this . transactionQueueTree_ . subTree ( path ) ;
@@ -586,7 +604,7 @@ declare module './Repo' {
586
604
* @param {!Tree.<Array.<Transaction>> } node Node to abort transactions for.
587
605
* @private
588
606
*/
589
- ( Repo . prototype as any ) . abortTransactionsOnNode_ = function ( node ) {
607
+ ( Repo . prototype as any ) . abortTransactionsOnNode_ = function ( node : Tree ) {
590
608
var queue = node . getValue ( ) ;
591
609
if ( queue !== null ) {
592
610
0 commit comments