Skip to content

Commit

Permalink
Read storage on window.onstorage (#7137)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Dec 17, 2024
1 parent c9e4297 commit 20d4266
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/state/persisted/index.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const _emitter = new EventEmitter()

export async function init() {
broadcast.onmessage = onBroadcastMessage
window.onstorage = onStorage
const stored = readFromStorage()
if (stored) {
_state = stored
Expand Down Expand Up @@ -90,6 +91,17 @@ export async function 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) {
if (
typeof data === 'object' &&
Expand Down

0 comments on commit 20d4266

Please sign in to comment.