Open History to see the last commit
1import { withSyncSpan } from '../telemetry/tracer'
2import type { OfflineTransaction } from '../types'
3
4export class KeyScheduler {
5 private pendingTransactions: Array<OfflineTransaction> = []
6 private activeTransactionId: string | undefined
7
8 schedule(transaction: OfflineTransaction): boolean {
9 return withSyncSpan(
10 `scheduler.schedule`,
11 {
12 'transaction.id': transaction.id,
13 queueLength: this.pendingTransactions.length,
14 },
15 () => {
16 if (
17 this.pendingTransactions.some(
18 (pending) => pending.id === transaction.id,
19 )
20 ) {
21 return false
22 }
23 this.pendingTransactions.push(transaction)
24 // Sort by creation time to maintain FIFO order
25 this.pendingTransactions.sort(
26 (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
27 )
28 return true
29 },
30 )
31 }
32
33 getNext(): OfflineTransaction | undefined {
34 return withSyncSpan(
35 `scheduler.getNext`,
36 { pendingCount: this.pendingTransactions.length },
37 (span) => {
38 if (
39 this.activeTransactionId !== undefined ||
40 this.pendingTransactions.length === 0
41 ) {
42 span.setAttribute(`result`, `empty`)
43 return undefined
44 }
45
46 const firstTransaction = this.pendingTransactions[0]!
47
48 if (!this.isReadyToRun(firstTransaction)) {
49 span.setAttribute(`result`, `waiting_for_first`)
50 span.setAttribute(`transaction.id`, firstTransaction.id)
51 return undefined
52 }
53
54 span.setAttribute(`result`, `found`)
55 span.setAttribute(`transaction.id`, firstTransaction.id)
56 return firstTransaction
57 },
58 )
59 }
60
61 private isReadyToRun(transaction: OfflineTransaction): boolean {
62 return Date.now() >= transaction.nextAttemptAt
63 }
64
65 markStarted(transaction: OfflineTransaction): void {
66 this.activeTransactionId = transaction.id
67 }
68
69 markCompleted(transaction: OfflineTransaction): void {
70 this.removeTransaction(transaction)
71 this.activeTransactionId = undefined
72 }
73
74 markFailed(_transaction: OfflineTransaction): void {
75 this.activeTransactionId = undefined
76 }
77
78 private removeTransaction(transaction: OfflineTransaction): void {
79 const index = this.pendingTransactions.findIndex(
80 (tx) => tx.id === transaction.id,
81 )
82 if (index >= 0) {
83 this.pendingTransactions.splice(index, 1)
84 }
85 }
86
87 updateTransaction(transaction: OfflineTransaction): void {
88 this.updateTransactions([transaction])
89 }
90
91 getPendingCount(): number {
92 return this.pendingTransactions.length
93 }
94
95 getRunningCount(): number {
96 return this.activeTransactionId === undefined ? 0 : 1
97 }
98
99 clear(): void {
100 this.pendingTransactions = []
101 this.activeTransactionId = undefined
102 }
103
104 /** @internal Reconcile one replay snapshot without canceling issued work. */
105 removePendingTransactions(transactionIds: Iterable<string>): Array<string> {
106 const ids = new Set(transactionIds)
107 if (this.activeTransactionId !== undefined)
108 ids.delete(this.activeTransactionId)
109 this.pendingTransactions = this.pendingTransactions.filter(
110 ({ id }) => !ids.has(id),
111 )
112 return [...ids]
113 }
114
115 getAllPendingTransactions(): Array<OfflineTransaction> {
116 return [...this.pendingTransactions]
117 }
118
119 updateTransactions(updatedTransactions: Array<OfflineTransaction>): void {
120 const updatedById = new Map(
121 updatedTransactions.map((transaction) => [transaction.id, transaction]),
122 )
123 this.pendingTransactions = this.pendingTransactions.map(
124 (transaction) => updatedById.get(transaction.id) ?? transaction,
125 )
126 // Re-sort to maintain FIFO order after updates
127 this.pendingTransactions.sort(
128 (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
129 )
130 }
131}