@@ -12,6 +12,8 @@ import type {
12
12
const transactions : Array < Transaction < any > > = [ ]
13
13
let transactionStack : Array < Transaction < any > > = [ ]
14
14
15
+ let sequenceNumber = 0
16
+
15
17
export function createTransaction <
16
18
TData extends object = Record < string , unknown > ,
17
19
> ( config : TransactionConfig < TData > ) : Transaction < TData > {
@@ -54,6 +56,7 @@ export class Transaction<
54
56
public isPersisted : Deferred < Transaction < T , TOperation > >
55
57
public autoCommit : boolean
56
58
public createdAt : Date
59
+ public sequenceNumber : number
57
60
public metadata : Record < string , unknown >
58
61
public error ?: {
59
62
message : string
@@ -71,6 +74,7 @@ export class Transaction<
71
74
this . isPersisted = createDeferred < Transaction < T , TOperation > > ( )
72
75
this . autoCommit = config . autoCommit ?? true
73
76
this . createdAt = new Date ( )
77
+ this . sequenceNumber = sequenceNumber ++
74
78
this . metadata = config . metadata ?? { }
75
79
}
76
80
@@ -200,4 +204,19 @@ export class Transaction<
200
204
201
205
return this
202
206
}
207
+
208
+ /**
209
+ * Compare two transactions by their createdAt time and sequence number in order
210
+ * to sort them in the order they were created.
211
+ * @param other - The other transaction to compare to
212
+ * @returns -1 if this transaction was created before the other, 1 if it was created after, 0 if they were created at the same time
213
+ */
214
+ compareCreatedAt ( other : Transaction < any > ) : number {
215
+ const createdAtComparison =
216
+ this . createdAt . getTime ( ) - other . createdAt . getTime ( )
217
+ if ( createdAtComparison !== 0 ) {
218
+ return createdAtComparison
219
+ }
220
+ return this . sequenceNumber - other . sequenceNumber
221
+ }
203
222
}
0 commit comments