Skip to content

Commit 0b9c451

Browse files
fix: lazy-load storage cache on set() to prevent null spread (#38)
When an MV3 service worker wakes up and calls set() with an updater (e.g. requestStorage.addEvent spreading prev into a new array), the in-memory cache may still be null because _getDataFromStorage() is async. The updater then runs with prev = null and [...null] throws "TypeError: a is not iterable", aborting addEvent and leaving the approval flow in a bad state (popup opens for a request that was never persisted). Load the cache synchronously-ish (await from chrome.storage) in set() if it's still null, so every updater receives a real value. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 78b5ca0 commit 0b9c451

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

packages/storage/lib/base.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ export function createStorage<D = string>(key: string, fallback: D, config?: Sto
168168
};
169169

170170
const set = async (valueOrUpdate: ValueOrUpdate<D>) => {
171+
if (cache === null) {
172+
cache = await _getDataFromStorage();
173+
}
171174
cache = await updateCache(valueOrUpdate, cache);
172175

173176
await chrome.storage[storageType].set({ [key]: serialize(cache) });

0 commit comments

Comments
 (0)