MCPcopy Create free account
hub / github.com/Maksandre/w3wallets / waitForStorageStable

Function waitForStorageStable

packages/w3wallets/src/cache/buildCache.ts:27–76  ·  view source on GitHub ↗

* Poll chrome.storage.local via a helper page until the key count stops changing. * Waits for at least 1 key to appear, then requires the count to remain the same * for STABLE_CHECKS_REQUIRED consecutive polls before returning.

(
  helperPage: Page,
  helperUrl: string,
)

Source from the content-addressed store, hash-verified

25 * for {@link STABLE_CHECKS_REQUIRED} consecutive polls before returning.
26 */
27async function waitForStorageStable(
28 helperPage: Page,
29 helperUrl: string,
30): Promise<number | null> {
31 const TIMEOUT = STORAGE_STABILIZE_TIMEOUT;
32 const POLL_INTERVAL = STORAGE_POLL_INTERVAL;
33 const STABLE_CHECKS_REQUIRED = STORAGE_STABLE_CHECKS;
34 const start = Date.now();
35 let lastKeyCount = -1;
36 let stableCount = 0;
37
38 while (Date.now() - start < TIMEOUT) {
39 await sleep(POLL_INTERVAL);
40
41 await helperPage.goto(helperUrl);
42 await helperPage.waitForFunction(
43 () => document.title.startsWith("done:"),
44 null,
45 { timeout: HELPER_PAGE_TIMEOUT },
46 );
47
48 const title = await helperPage.title();
49 const keyCount = parseInt(title.split(":")[1]!, 10);
50
51 // Skip until extension has written at least one key
52 if (keyCount === 0) continue;
53
54 if (keyCount === lastKeyCount) {
55 stableCount++;
56 } else {
57 stableCount = 1;
58 lastKeyCount = keyCount;
59 }
60 debug(
61 `Storage poll: ${keyCount} keys (stable ${stableCount}/${STABLE_CHECKS_REQUIRED})`,
62 );
63
64 if (stableCount >= STABLE_CHECKS_REQUIRED) {
65 console.log(` Storage stabilized at ${keyCount} keys`);
66 return keyCount;
67 }
68
69 console.log(
70 ` Waiting for storage to stabilize: ${keyCount} keys (${stableCount}/${STABLE_CHECKS_REQUIRED})`,
71 );
72 }
73
74 console.log(` Storage stabilization timed out after ${TIMEOUT / 1000}s`);
75 return null;
76}
77
78export interface BuildOptions {
79 force?: boolean;

Callers 1

buildCacheForSetupFunction · 0.85

Calls 3

sleepFunction · 0.90
debugFunction · 0.90
gotoMethod · 0.45

Tested by

no test coverage detected