Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: Simpler database auto-closure using using keyword #6791

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions src/telemetry/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ async function getDB() {
database = null;
});

return database;
return {db: database, [Symbol.asyncDispose]() { database?.close(); }};
}

/**
* Add a log entry to the database.
* @param entry the log entry to add
*/
export async function appendEntry(entry: LogEntry): Promise<void> {
const db = await getDB();
const {db} = await using getDB();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the "using" goes where the const is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It turns out that immediate destructuring isn't possible either so it would look like this:

using resource = await getDB();
resource.db.getAll();

I pushed a new partial commit here just for reference

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened in issue in the Typescript IDB repository: jakearchibald/idb#300

await db.add(ENTRY_OBJECT_STORE, entry);
}

Expand All @@ -189,7 +189,7 @@ function makeMatchEntry(
* Returns the number of log entries in the database.
*/
export async function count(): Promise<number> {
const db = await getDB();
const {db} = await using getDB();
return db.count(ENTRY_OBJECT_STORE);
}

Expand All @@ -207,7 +207,7 @@ export async function recreateDB(): Promise<void> {
* Clears all log entries from the database.
*/
export async function clearLogs(): Promise<void> {
const db = await getDB();
const {db} = await using getDB();
await db.clear(ENTRY_OBJECT_STORE);
}

Expand All @@ -216,7 +216,7 @@ export async function clearLogs(): Promise<void> {
* @param context the query context to clear.
*/
export async function clearLog(context: MessageContext = {}): Promise<void> {
const db = await getDB();
const {db} = await using getDB();

const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");

Expand All @@ -240,7 +240,7 @@ export async function clearLog(context: MessageContext = {}): Promise<void> {
export async function getLogEntries(
context: MessageContext = {}
): Promise<LogEntry[]> {
const db = await getDB();
const {db} = await using getDB();
const objectStore = db
.transaction(ENTRY_OBJECT_STORE, "readonly")
.objectStore(ENTRY_OBJECT_STORE);
Expand Down Expand Up @@ -470,7 +470,7 @@ export async function setLoggingConfig(config: LoggingConfig): Promise<void> {
export async function clearExtensionDebugLogs(
extensionId: UUID
): Promise<void> {
const db = await getDB();
const {db} = await using getDB();
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");
const index = tx.store.index("extensionId");
for await (const cursor of index.iterate(extensionId)) {
Expand All @@ -494,7 +494,7 @@ async function _sweepLogs(): Promise<void> {
numToDelete,
});

const db = await getDB();
const {db} = await using getDB();
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");

let deletedCount = 0;
Expand Down
12 changes: 6 additions & 6 deletions src/telemetry/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export async function addTraceEntry(record: TraceEntryData): Promise<void> {
return;
}

const db = await getDB();
const {db} = await using getDB();
const callId = objectHash(record.branches);
await db.add(ENTRY_OBJECT_STORE, { ...record, callId });
}
Expand All @@ -248,7 +248,7 @@ export async function addTraceExit(record: TraceExitData): Promise<void> {

const callId = objectHash(record.branches);

const db = await getDB();
const {db} = await using getDB();

const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");

Expand All @@ -275,15 +275,15 @@ export async function addTraceExit(record: TraceExitData): Promise<void> {
* Clear all trace records.
*/
export async function clearTraces(): Promise<void> {
const db = await getDB();
const {db} = await using getDB();
await db.clear(ENTRY_OBJECT_STORE);
}

/**
* Returns the number of trace records in the database.
*/
export async function count(): Promise<number> {
const db = await getDB();
const {db} = await using getDB();
return db.count(ENTRY_OBJECT_STORE);
}

Expand All @@ -299,7 +299,7 @@ export async function recreateDB(): Promise<void> {
export async function clearExtensionTraces(extensionId: UUID): Promise<void> {
let cnt = 0;

const db = await getDB();
const {db} = await using getDB();
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");
const index = tx.store.index("extensionId");
for await (const cursor of index.iterate(extensionId)) {
Expand All @@ -313,7 +313,7 @@ export async function clearExtensionTraces(extensionId: UUID): Promise<void> {
export async function getLatestRunByExtensionId(
extensionId: UUID
): Promise<TraceRecord[]> {
const db = await getDB();
const {db} = await using getDB();
const matches = await db
.transaction(ENTRY_OBJECT_STORE, "readonly")
.objectStore(ENTRY_OBJECT_STORE)
Expand Down
6 changes: 3 additions & 3 deletions src/tours/tourRunDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async function getDB(): Promise<IDBPDatabase<TourDB>> {
* @param run the tour run
*/
export async function recordStart(run: TourMetadata): Promise<void> {
const db = await getDB();
const {db} = await using getDB();
await db.add(ENTRY_OBJECT_STORE, {
...run,
updatedAt: new Date().toISOString(),
Expand All @@ -127,7 +127,7 @@ export async function recordEnd(
nonce: UUID,
update: TourStatus
): Promise<void> {
const db = await getDB();
const {db} = await using getDB();
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");
const value = await tx.store.get(nonce);

Expand All @@ -146,6 +146,6 @@ export async function recordEnd(
* Retrieve all tour runs.
*/
export async function getAll(): Promise<TourEntry[]> {
const db = await getDB();
const {db} = await using getDB();
return db.getAll(ENTRY_OBJECT_STORE);
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"moduleResolution": "bundler",
"resolveJsonModule": true,
"baseUrl": ".",
"lib": ["es2022", "esnext.disposable", "dom"],

// TODO: Drop these lines to make TS stricter https://github.com/pixiebrix/pixiebrix-extension/issues/775
"strictNullChecks": false,
Expand Down
Loading