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 isRunning = false
7
8 schedule(transaction: OfflineTransaction): void {
9 withSyncSpan(
10 `scheduler.schedule`,
11 {
12 'transaction.id': transaction.id,
13 queueLength: this.pendingTransactions.length,
14 },
15 () => {
16 this.pendingTransactions.push(transaction)
17 // Sort by creation time to maintain FIFO order
18 this.pendingTransactions.sort(
19 (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
20 )
21 },
22 )
23 }
24
25 getNext(): OfflineTransaction | undefined {
26 return withSyncSpan(
27 `scheduler.getNext`,
28 { pendingCount: this.pendingTransactions.length },
29 (span) => {
30 if (this.isRunning || this.pendingTransactions.length === 0) {
31 span.setAttribute(`result`, `empty`)
32 return undefined
33 }
34
35 const firstTransaction = this.pendingTransactions[0]!
36
37 if (!this.isReadyToRun(firstTransaction)) {
38 span.setAttribute(`result`, `waiting_for_first`)
39 span.setAttribute(`transaction.id`, firstTransaction.id)
40 return undefined
41 }
42
43 span.setAttribute(`result`, `found`)
44 span.setAttribute(`transaction.id`, firstTransaction.id)
45 return firstTransaction
46 },
47 )
48 }
49
50 private isReadyToRun(transaction: OfflineTransaction): boolean {
51 return Date.now() >= transaction.nextAttemptAt
52 }
53
54 markStarted(_transaction: OfflineTransaction): void {
55 this.isRunning = true
56 }
57
58 markCompleted(transaction: OfflineTransaction): void {
59 this.removeTransaction(transaction)
60 this.isRunning = false
61 }
62
63 markFailed(_transaction: OfflineTransaction): void {
64 this.isRunning = false
65 }
66
67 private removeTransaction(transaction: OfflineTransaction): void {
68 const index = this.pendingTransactions.findIndex(
69 (tx) => tx.id === transaction.id,
70 )
71 if (index >= 0) {
72 this.pendingTransactions.splice(index, 1)
73 }
74 }
75
76 updateTransaction(transaction: OfflineTransaction): void {
77 const index = this.pendingTransactions.findIndex(
78 (tx) => tx.id === transaction.id,
79 )
80 if (index >= 0) {
81 this.pendingTransactions[index] = transaction
82 // Re-sort to maintain FIFO order after update
83 this.pendingTransactions.sort(
84 (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
85 )
86 }
87 }
88
89 getPendingCount(): number {
90 return this.pendingTransactions.length
91 }
92
93 getRunningCount(): number {
94 return this.isRunning ? 1 : 0
95 }
96
97 clear(): void {
98 this.pendingTransactions = []
99 this.isRunning = false
100 }
101
102 getAllPendingTransactions(): Array<OfflineTransaction> {
103 return [...this.pendingTransactions]
104 }
105
106 updateTransactions(updatedTransactions: Array<OfflineTransaction>): void {
107 for (const updatedTx of updatedTransactions) {
108 const index = this.pendingTransactions.findIndex(
109 (tx) => tx.id === updatedTx.id,
110 )
111 if (index >= 0) {
112 this.pendingTransactions[index] = updatedTx
113 }
114 }
115 // Re-sort to maintain FIFO order after updates
116 this.pendingTransactions.sort(
117 (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
118 )
119 }
120}