Skip to content

Commit

Permalink
Better compability with indexedDB
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Jan 30, 2025
1 parent cb138d7 commit 3067a8a
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions src/host/lcache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class LCache implements Cache {
}
}

private async resultToUint8Array(result: ArrayBuffer | Blob): Promise<Uint8Array> {
if (result instanceof Blob) {
return new Uint8Array(await result.arrayBuffer());
}
return new Uint8Array(result);
}

public close() {
if (this.db !== null) {
this.db.close();
Expand All @@ -104,8 +111,11 @@ class LCache implements Cache {
}

const transaction = this.db.transaction(this.storeName, "readwrite");
const request = transaction.objectStore(this.storeName).put(data.buffer, key);
request.onerror = () => reject;
const request = transaction.objectStore(this.storeName).put(new Blob([data.buffer]), key);
request.onerror = (e) => {
reject(new Error("Can't put key '" + key + "'"));
console.error(e);
};
request.onsuccess = () => resolve();
});
}
Expand Down Expand Up @@ -141,11 +151,11 @@ class LCache implements Cache {
}

const transaction = this.db.transaction(this.storeName, "readonly");
const request = transaction.objectStore(this.storeName).get(key) as IDBRequest<ArrayBuffer>;
const request = transaction.objectStore(this.storeName).get(key) as IDBRequest<ArrayBuffer | Blob>;
request.onerror = () => reject(new Error("Can't read value for key '" + key + "'"));
request.onsuccess = () => {
if (request.result) {
resolve(new Uint8Array(request.result));
resolve(this.resultToUint8Array(request.result));
} else {
rejectOrResolve("Result is empty for key '" + key + "', result: " + request.result);
}
Expand All @@ -161,35 +171,26 @@ class LCache implements Cache {
}

const transaction = this.db.transaction(this.storeName, "readonly");
const request = transaction.objectStore(this.storeName).index("key").getAllKeys();
request.onerror = () => reject;
const request = transaction.objectStore(this.storeName).getAllKeys();
request.onerror = reject;
request.onsuccess = (event) => {
const keys = (event.target as any).result as string[];
resolve(keys);
if (request.result) {
resolve(request.result as string[]);
} else {
resolve([]);
}
};
});
}

public forEach(each: (key: string, value: Uint8Array) => void): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (this.db === null) {
resolve();
return;
public async forEach(each: (key: string, value: Uint8Array) => void): Promise<void> {
const keys = await this.keys();
for (const key of keys) {
const value = await this.get(key);
if (value) {
each(key, value);
}

const transaction = this.db.transaction(this.storeName, "readonly");
const request = transaction.objectStore(this.storeName).openCursor();
request.onerror = () => reject;
request.onsuccess = (event) => {
const cursor = (event.target as any).result as IDBCursorWithValue;
if (cursor) {
each(cursor.key.toString(), new Uint8Array(cursor.value));
cursor.continue();
} else {
resolve();
}
};
});
}
}
}

Expand Down

0 comments on commit 3067a8a

Please sign in to comment.