Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
  • Loading branch information
t3chguy committed Jan 14, 2025
1 parent 01527cd commit 97fca2d
Showing 1 changed file with 25 additions and 44 deletions.
69 changes: 25 additions & 44 deletions src/utils/StorageAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ async function idbInit(): Promise<void> {
});
}

async function idbTransaction(
table: string,
mode: IDBTransactionMode,
fn: (objectStore: IDBObjectStore) => IDBRequest<any>,
): Promise<any> {
if (!idb) {
await idbInit();
}
return new Promise((resolve, reject) => {
const txn = idb!.transaction([table], mode);
txn.onerror = reject;

const objectStore = txn.objectStore(table);
const request = fn(objectStore);
request.onerror = reject;
request.onsuccess = (): void => {
resolve(request.result);
};
});
}

/**
* Loads an item from an IndexedDB table within the underlying `matrix-react-sdk` database.
*
Expand All @@ -57,17 +78,7 @@ export async function idbLoad(table: string, key: string | string[]): Promise<an
if (!idb) {
await idbInit();
}
return new Promise((resolve, reject) => {
const txn = idb!.transaction([table], "readonly");
txn.onerror = reject;

const objectStore = txn.objectStore(table);
const request = objectStore.get(key);
request.onerror = reject;
request.onsuccess = (event): void => {
resolve(request.result);
};
});
return idbTransaction(table, "readonly", (objectStore) => objectStore.get(key));
}

/**
Expand All @@ -84,17 +95,7 @@ export async function idbSave(table: string, key: string | string[], data: any):
if (!idb) {
await idbInit();
}
return new Promise((resolve, reject) => {
const txn = idb!.transaction([table], "readwrite");
txn.onerror = reject;

const objectStore = txn.objectStore(table);
const request = objectStore.put(data, key);
request.onerror = reject;
request.onsuccess = (event): void => {
resolve();
};
});
return idbTransaction(table, "readwrite", (objectStore) => objectStore.put(data, key));
}

/**
Expand All @@ -110,17 +111,7 @@ export async function idbDelete(table: string, key: string | string[]): Promise<
if (!idb) {
await idbInit();
}
return new Promise((resolve, reject) => {
const txn = idb!.transaction([table], "readwrite");
txn.onerror = reject;

const objectStore = txn.objectStore(table);
const request = objectStore.delete(key);
request.onerror = reject;
request.onsuccess = (): void => {
resolve();
};
});
return idbTransaction(table, "readwrite", (objectStore) => objectStore.delete(key));
}

/**
Expand All @@ -135,15 +126,5 @@ export async function idbClear(table: string): Promise<void> {
if (!idb) {
await idbInit();
}
return new Promise((resolve, reject) => {
const txn = idb!.transaction([table], "readwrite");
txn.onerror = reject;

const objectStore = txn.objectStore(table);
const request = objectStore.clear();
request.onerror = reject;
request.onsuccess = (): void => {
resolve();
};
});
return idbTransaction(table, "readwrite", (objectStore) => objectStore.clear());
}

0 comments on commit 97fca2d

Please sign in to comment.