Refactor tab activation handling in saveDb function

This commit is contained in:
kwaroran
2024-04-08 23:46:34 +09:00
parent 8d094d5b66
commit 653fb6fa9f

View File

@@ -261,7 +261,8 @@ export async function saveDb(){
}) })
let gotChannel = false let gotChannel = false
const sessionID = v4() const sessionID = v4()
const channel = new BroadcastChannel('risu-save-channel') const channel = BroadcastChannel ? (new BroadcastChannel('risu-active')): null
if(channel){
channel.onmessage = async (ev) => { channel.onmessage = async (ev) => {
if(ev.data === sessionID){ if(ev.data === sessionID){
return return
@@ -272,6 +273,7 @@ export async function saveDb(){
gotChannel = false gotChannel = false
} }
} }
}
let savetrys = 0 let savetrys = 0
while(true){ while(true){
try { try {
@@ -281,7 +283,9 @@ export async function saveDb(){
await sleep(1000) await sleep(1000)
continue continue
} }
if(channel){
channel.postMessage(sessionID) channel.postMessage(sessionID)
}
changed = false changed = false
let db = get(DataBase) let db = get(DataBase)
db.saveTime = Math.floor(Date.now() / 1000) db.saveTime = Math.floor(Date.now() / 1000)