Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dependencies": {
"@standard-schema/spec": "^1.0.0",
"@tanstack/db-ivm": "workspace:*",
"@tanstack/pacer": "^0.1.0"
"@tanstack/pacer": "^0.16.2"
},
"devDependencies": {
"@vitest/coverage-istanbul": "^3.2.4",
Expand Down
33 changes: 18 additions & 15 deletions packages/db/src/strategies/queueStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,31 @@ import type { Transaction } from "../transactions"
* ```
*/
export function queueStrategy(options?: QueueStrategyOptions): QueueStrategy {
const queuer = new AsyncQueuer<void>({
concurrency: 1, // Process one at a time to ensure serialization
wait: options?.wait,
maxSize: options?.maxSize,
addItemsTo: options?.addItemsTo ?? `back`, // Default FIFO: add to back
getItemsFrom: options?.getItemsFrom ?? `front`, // Default FIFO: get from front
started: true, // Start processing immediately
})
const queuer = new AsyncQueuer<() => Transaction>(
async (fn) => {
const transaction = fn()
// Wait for the transaction to be persisted before processing next item
// Note: fn() already calls commit(), we just wait for it to complete
await transaction.isPersisted.promise
},
{
concurrency: 1, // Process one at a time to ensure serialization
wait: options?.wait,
maxSize: options?.maxSize,
addItemsTo: options?.addItemsTo ?? `back`, // Default FIFO: add to back
getItemsFrom: options?.getItemsFrom ?? `front`, // Default FIFO: get from front
started: true, // Start processing immediately
}
)

return {
_type: `queue`,
options,
execute: <T extends object = Record<string, unknown>>(
fn: () => Transaction<T>
) => {
// Wrap the callback in an async function that waits for persistence
queuer.addItem(async () => {
const transaction = fn()
// Wait for the transaction to be persisted before processing next item
// Note: fn() already calls commit(), we just wait for it to complete
await transaction.isPersisted.promise
})
// Add the transaction-creating function to the queue
queuer.addItem(fn as () => Transaction)
},
cleanup: () => {
queuer.stop()
Expand Down
26 changes: 20 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading