Skip to content

Commit 5546ef2

Browse files
committed
refactor: use requestAnimationFrame to mitigate blocking of main thread
1 parent 0fe373e commit 5546ef2

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

packages/idb-cache/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
parseChunkIndexFromKey,
1212
getAllChunkKeysForBaseKey,
1313
deterministicUUID,
14+
waitForAnimationFrame,
1415
} from "./utils";
1516
import { encryptionWorkerFunction } from "./encryptionWorkerFn";
1617
import {
@@ -373,6 +374,7 @@ export class IDBCache implements AsyncStorage {
373374
if (!this.dbReadyPromise) return null;
374375
await this.ensureWorkerInitialized();
375376

377+
await waitForAnimationFrame();
376378
const db = await this.dbReadyPromise;
377379
const baseKey = await deterministicUUID(`${this.cacheKey}:${itemKey}`);
378380
const now = Date.now();
@@ -509,6 +511,7 @@ export class IDBCache implements AsyncStorage {
509511
if (!this.dbReadyPromise) return;
510512
await this.ensureWorkerInitialized();
511513

514+
await waitForAnimationFrame();
512515
const db = await this.dbReadyPromise;
513516
const baseKey = await deterministicUUID(`${this.cacheKey}:${itemKey}`);
514517
const expirationTimestamp = Date.now() + this.gcTime;

packages/idb-cache/src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import { LRUCache } from "./LRUCache";
1010

1111
const uuidCache = new LRUCache<string, string>(1000);
1212

13+
// Used to avoid main thread blocking
14+
// even though it slows operations
15+
export function waitForAnimationFrame() {
16+
return new Promise((resolve) => requestAnimationFrame(resolve));
17+
}
18+
1319
function isValidHex(hex: string): boolean {
1420
return /^[0-9a-fA-F]{128}$/.test(hex);
1521
}
@@ -55,6 +61,7 @@ export async function deterministicUUID(key: string): Promise<string> {
5561

5662
const encoder = new TextEncoder();
5763
const data = encoder.encode(key);
64+
// Usually under 1 ms; not outsourced to worker
5865
const hashBuffer = await crypto.subtle.digest("SHA-512", data);
5966
const hashHex = bufferToHex(hashBuffer);
6067

@@ -126,6 +133,7 @@ export async function openDatabase<T extends DBSchema>(
126133
dbVersion: number
127134
): Promise<import("idb").IDBPDatabase<T>> {
128135
try {
136+
await waitForAnimationFrame();
129137
return await openDB<T>(dbName, dbVersion, {
130138
upgrade(db) {
131139
createStoreWithIndexes(db, storeName);
@@ -135,6 +143,7 @@ export async function openDatabase<T extends DBSchema>(
135143
if (error instanceof DOMException && error.name === "VersionError") {
136144
console.warn(`VersionError: Deleting database ${dbName} and retrying...`);
137145
await deleteDB(dbName);
146+
await waitForAnimationFrame();
138147
return await openDB<T>(dbName, dbVersion, {
139148
upgrade(db) {
140149
createStoreWithIndexes(db, storeName);

0 commit comments

Comments
 (0)