From 4e8e6a6f702686ca2e4c7dbde416178f528b4b34 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Mon, 8 Apr 2024 22:00:20 +0900 Subject: [PATCH] Add alertNormalWait function to alert.ts and handle tab activation in saveDb function --- src/ts/alert.ts | 13 +++++++++++++ src/ts/storage/globalApi.ts | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/ts/alert.ts b/src/ts/alert.ts index 84526c87..4b031768 100644 --- a/src/ts/alert.ts +++ b/src/ts/alert.ts @@ -44,6 +44,19 @@ export function alertNormal(msg:string){ }) } +export async function alertNormalWait(msg:string){ + alertStore.set({ + 'type': 'normal', + 'msg': msg + }) + while(true){ + if (get(alertStore).type === 'none'){ + break + } + await sleep(10) + } +} + export async function alertLogin(){ alertStore.set({ 'type': 'login', diff --git a/src/ts/storage/globalApi.ts b/src/ts/storage/globalApi.ts index 1f6f4a25..cd82f7e0 100644 --- a/src/ts/storage/globalApi.ts +++ b/src/ts/storage/globalApi.ts @@ -11,7 +11,7 @@ import { checkUpdate } from "../update"; import { botMakerMode, selectedCharID } from "../stores"; import { Body, ResponseType, fetch as TauriFetch } from "@tauri-apps/api/http"; import { loadPlugins } from "../plugins/plugins"; -import { alertConfirm, alertError, alertNormal } from "../alert"; +import { alertConfirm, alertError, alertNormal, alertNormalWait } from "../alert"; import { checkDriverInit, syncDrive } from "../drive/drive"; import { hasher } from "../parser"; import { characterURLImport, hubURL } from "../characterCards"; @@ -258,10 +258,29 @@ export async function saveDb(){ DataBase.subscribe(() => { changed = true }) + let gotChannel = false + const sessionID = v4() + const channel = new BroadcastChannel('risu-db') + channel.onmessage = async (ev) => { + if(ev.data === sessionID){ + return + } + if(!gotChannel){ + gotChannel = true + await alertNormalWait("Current tab is inactivated since other tab is active. to activate this tab, click OK") + gotChannel = false + } + } let savetrys = 0 while(true){ try { if(changed){ + if(gotChannel){ + //Data is saved in other tab + await sleep(1000) + continue + } + channel.postMessage(sessionID) changed = false let db = get(DataBase) db.saveTime = Math.floor(Date.now() / 1000)