Skip to content

Commit

Permalink
fix: getStorage() not working on ios18+
Browse files Browse the repository at this point in the history
There is a bug on Safari 18+
where if storage.get() is provided null or undefined fallback value
it returns an empty object.

https://forums.developer.apple.com/forums/thread/765169

Fixes #9
  • Loading branch information
BlueGreenMagick committed Jan 9, 2025
1 parent 447fbe2 commit 627cdec
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions main/src/extension/browserApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,20 +456,21 @@ export async function currentTab(): Promise<chrome.tabs.Tab> {
return promise;
}

/** If `or` is undefined, returns `undefined` if value does not exist. */
/**
* `or = undefined` is returned if storage value is `undefined` of not set.
*/
export async function getStorage<K extends StorageKey, V = undefined>(
key: K,
or?: V,
): Promise<StorageValues[K] | V> {
const [promise, resolve] = createPromise<V>();
let req: string | Record<string, V> = key;
if (or !== undefined) {
req = {
[key]: or,
};
}
browserStorage().get(req, (obj) => {
resolve(obj[key] as V);
const [promise, resolve] = createPromise<StorageValues[K] | V>();
browserStorage().get(key, (obj) => {
const value = obj[key] as StorageValues[K] | undefined;
if (or !== undefined && value === undefined) {
resolve(or);
} else {
resolve(value as StorageValues[K]);
}
});
return promise;
}
Expand Down

0 comments on commit 627cdec

Please sign in to comment.