Read storage on window.onstorage (#7137)
This commit is contained in:
@@ -24,6 +24,7 @@ const _emitter = new EventEmitter()
|
|||||||
|
|
||||||
export async function init() {
|
export async function init() {
|
||||||
broadcast.onmessage = onBroadcastMessage
|
broadcast.onmessage = onBroadcastMessage
|
||||||
|
window.onstorage = onStorage
|
||||||
const stored = readFromStorage()
|
const stored = readFromStorage()
|
||||||
if (stored) {
|
if (stored) {
|
||||||
_state = stored
|
_state = stored
|
||||||
@@ -90,6 +91,17 @@ export async function clearStorage() {
|
|||||||
}
|
}
|
||||||
clearStorage satisfies PersistedApi['clearStorage']
|
clearStorage satisfies PersistedApi['clearStorage']
|
||||||
|
|
||||||
|
function onStorage() {
|
||||||
|
const next = readFromStorage()
|
||||||
|
if (next === _state) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (next) {
|
||||||
|
_state = next
|
||||||
|
_emitter.emit('update')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function onBroadcastMessage({data}: MessageEvent) {
|
async function onBroadcastMessage({data}: MessageEvent) {
|
||||||
if (
|
if (
|
||||||
typeof data === 'object' &&
|
typeof data === 'object' &&
|
||||||
|
|||||||
Reference in New Issue
Block a user