updateStorageAtomically function infrastructure exported ✓ 90.9%
Last updated: 2026-03-05T10:53:28.866Z
Location
Metrics
LOC: 28
Complexity: 2
Params: 3
Coverage: 90.9% (10/11 lines, 0x executed)
Signature
updateStorageAtomically(
key: StorageKey,
defaultValue: T,
updater: (current: T) => T,
): : Promise<T>
Summary
Atomically reads, transforms, and writes a storage key. Uses a per-key write queue to prevent race conditions when multiple async operations modify the same key concurrently.
Tags
#@param key - Storage key to update#@param defaultValue - Default value if key does not exist yet#@param updater - Pure function that receives current value and returns the next#@returns The new value after the update
Source Code
export async function updateStorageAtomically<T>(
key: StorageKey,
defaultValue: T,
updater: (current: T) => T,
): Promise<T> {
const previous = writeQueues.get(key) ?? Promise.resolve();
let nextValue = defaultValue;
const currentWrite = previous.then(async () => {
const current = await getFromStorage<T>(key, defaultValue);
nextValue = updater(current);
await setToStorage(key, nextValue);
});
const guardedWrite = withTimeout(currentWrite, WRITE_TIMEOUT_MS).catch(
(err) => {
log.warn(`Atomic update for key "${key}" failed:`, err);
},
);
writeQueues.set(
key,
guardedWrite.then(() => {}),
);
await currentWrite;
return nextValue;
}
Dependencies (Outgoing)
| Target | Type |
|---|---|
| setToStorage | calls |
| withTimeout | calls |
| key | dynamic_call |
Impact (Incoming)
| Source | Type |
|---|---|
| getDemoFlows | uses |
| getFieldDetectionCache | uses |
| saveFieldDetectionCacheForUrl | calls |
| deleteFieldDetectionCacheForUrl | calls |
| clearFieldDetectionCache | calls |
| getSavedForms | uses |
| saveForm | calls |
| deleteForm | calls |
| setDefaultForm | calls |
| getIgnoredFields | uses |
| addIgnoredField | calls |
| removeIgnoredField | calls |
| getRules | uses |
| saveRule | calls |
| deleteRule | calls |
| getSettings | uses |
| saveSettings | calls |