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

KeyScheduler.ts

102 lines3.0 KB

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