-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor BaseStore localStorage usage to fallback to an offscreen loc…
…alStorage solution if global.localStorage does not exists
- Loading branch information
Showing
6 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import browser from 'webextension-polyfill'; | ||
import { awaitOffscreenDocumentCreation } from '../../offscreen'; | ||
import { | ||
OffscreenCommunicationTarget, | ||
} from '../../../../shared/constants/offscreen-communication'; | ||
|
||
const offscreenSupported = browser.offscreen; | ||
|
||
const browserRuntimeSendMessageToAwait = async (params) => { | ||
await awaitOffscreenDocumentCreation(); | ||
return new Promise((resolve, reject) => { | ||
browser.runtime.sendMessage( | ||
params, | ||
(response) => { | ||
if (response.error) { | ||
reject(response.error); | ||
} | ||
|
||
resolve(response.value); | ||
} | ||
); | ||
}) | ||
} | ||
|
||
const localStorageWithOffscreen = { | ||
getItem: (key) => { | ||
if (offscreenSupported) { | ||
await awaitOffscreenDocumentCreation(); | ||
return await browserRuntimeSendMessageToAwait({ | ||
target: OffscreenCommunicationTarget.localStorageOffscreen, | ||
action: 'getItem', | ||
key, | ||
}); | ||
} else { | ||
return window.localStorage.getItem(key); | ||
} | ||
}, | ||
setItem: (key, value) => { | ||
if (offscreenSupported) { | ||
await awaitOffscreenDocumentCreation(); | ||
return await browserRuntimeSendMessageToAwait({ | ||
target: OffscreenCommunicationTarget.localStorageOffscreen, | ||
action: 'setItem', | ||
key, | ||
value, | ||
}); | ||
} else { | ||
return window.localStorage.setItem(key, value); | ||
} | ||
}, | ||
removeItem: (key) => { | ||
if (offscreenSupported) { | ||
await awaitOffscreenDocumentCreation(); | ||
return await browserRuntimeSendMessageToAwait({ | ||
target: OffscreenCommunicationTarget.localStorageOffscreen, | ||
action: 'removeItem', | ||
key, | ||
}); | ||
} else { | ||
return window.localStorage.removeItem(key); | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
OffscreenCommunicationEvents, | ||
OffscreenCommunicationTarget, | ||
} from '../../shared/constants/offscreen-communication'; | ||
import { isObject } from '@metamask/utils'; | ||
|
||
export function setupLocalStorageMessageListeners() { | ||
chrome.runtime.onMessage.addListener( | ||
( | ||
msg: { | ||
target: string; | ||
action: string; | ||
key: string; | ||
value: string; | ||
}, | ||
_sender, | ||
sendResponse, | ||
) => { | ||
if ( | ||
message && | ||
isObject(message) && | ||
message.action && | ||
message.key && | ||
message.target === OffscreenCommunicationTarget.localStorageOffscreen | ||
) { | ||
if (message.action === 'getItem') { | ||
const storedValue = window.localStorage.getItem(key); | ||
chrome.runtime.sendMessage({ | ||
target: OffscreenCommunicationTarget.extensionLocalStorage, | ||
value: storedValue, | ||
}); | ||
} else if (message.action === 'setItem') { | ||
window.localStorage.getItem(key, value); | ||
} else if (message.action === 'removeItem') { | ||
window.localStorage.removeItem(key); | ||
} | ||
} | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters