mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
refactor(durable): simplify Node SQLite queue and bindings
This commit is contained in:
parent
cd0ba2fabc
commit
77ac62f22a
1 changed files with 7 additions and 9 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { AsyncLocalStorage } from "node:async_hooks";
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import { dirname } from "node:path";
|
||||
import type { SQLInputValue, StatementSync } from "node:sqlite";
|
||||
import type { StatementSync } from "node:sqlite";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import type { SqliteDatabase, SqliteExecutor, SqliteValue } from "./database.ts";
|
||||
import { SqliteStorage } from "./storage.ts";
|
||||
|
|
@ -24,15 +24,13 @@ class SerialOperationQueue {
|
|||
|
||||
async run<T>(operation: () => T | Promise<T>): Promise<T> {
|
||||
const previous = this.tail;
|
||||
let release: () => void;
|
||||
this.tail = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const { promise, resolve: release } = Promise.withResolvers<void>();
|
||||
this.tail = promise;
|
||||
await previous;
|
||||
try {
|
||||
return await operation();
|
||||
} finally {
|
||||
release!();
|
||||
release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -58,16 +56,16 @@ abstract class NodeSqliteExecutor implements SqliteExecutor {
|
|||
|
||||
run(sql: string, ...params: SqliteValue[]): Promise<void> {
|
||||
return this.runOperation(() => {
|
||||
this.statement(sql).run(...(params as SQLInputValue[]));
|
||||
this.statement(sql).run(...params);
|
||||
});
|
||||
}
|
||||
|
||||
get<T extends object>(sql: string, ...params: SqliteValue[]): Promise<T | undefined> {
|
||||
return this.runOperation(() => this.statement(sql).get(...(params as SQLInputValue[])) as T | undefined);
|
||||
return this.runOperation(() => this.statement(sql).get(...params) as T | undefined);
|
||||
}
|
||||
|
||||
all<T extends object>(sql: string, ...params: SqliteValue[]): Promise<T[]> {
|
||||
return this.runOperation(() => this.statement(sql).all(...(params as SQLInputValue[])) as T[]);
|
||||
return this.runOperation(() => this.statement(sql).all(...params) as T[]);
|
||||
}
|
||||
|
||||
protected abstract runOperation<T>(operation: () => T): Promise<T>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue