Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

KeyScheduler.ts

142 lines4.0 KB

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 const index = this.pendingTransactions.findIndex(
89 (tx) => tx.id === transaction.id,
90 )
91 if (index >= 0) {
92 this.pendingTransactions[index] = transaction
93 // Re-sort to maintain FIFO order after update
94 this.pendingTransactions.sort(
95 (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
96 )
97 }
98 }
99​
100 getPendingCount(): number {
101 return this.pendingTransactions.length
102 }
103​
104 getRunningCount(): number {
105 return this.activeTransactionId === undefined ? 0 : 1
106 }
107​
108 clear(): void {
109 this.pendingTransactions = []
110 this.activeTransactionId = undefined
111 }
112​
113 /** @internal Reconcile one replay snapshot without canceling issued work. */
114 removePendingTransactions(transactionIds: Iterable<string>): Array<string> {
115 const ids = new Set(transactionIds)
116 if (this.activeTransactionId !== undefined)
117 ids.delete(this.activeTransactionId)
118 this.pendingTransactions = this.pendingTransactions.filter(
119 ({ id }) => !ids.has(id),
120 )
121 return [...ids]
122 }
123​
124 getAllPendingTransactions(): Array<OfflineTransaction> {
125 return [...this.pendingTransactions]
126 }
127​
128 updateTransactions(updatedTransactions: Array<OfflineTransaction>): void {
129 for (const updatedTx of updatedTransactions) {
130 const index = this.pendingTransactions.findIndex(
131 (tx) => tx.id === updatedTx.id,
132 )
133 if (index >= 0) {
134 this.pendingTransactions[index] = updatedTx
135 }
136 }
137 // Re-sort to maintain FIFO order after updates
138 this.pendingTransactions.sort(
139 (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
140 )
141 }