Add persistent storage feature

This commit is contained in:
kwaroran
2024-02-13 13:20:38 +09:00
parent ad3bd9b17f
commit aa320988b0
4 changed files with 85 additions and 2 deletions

View File

@@ -0,0 +1,61 @@
import { get } from "svelte/store";
import { DataBase } from "./database";
import { alertNormal } from "../alert";
import { language } from "src/lang";
import { isNodeServer, isTauri } from "./globalApi";
async function requestPersistantStorageMain() {
if(navigator.storage && navigator.storage.persist) {
if(await navigator.storage.persisted()) {
return true;
}
//if is chromium
//@ts-ignore
const isChromium = window.chrome;
if (isChromium) {
//chromium requires notification to persist
alertNormal("For chromium based browsers, you need to allow notifications to persist data")
const status = await Notification.requestPermission()
if(status === 'granted') {
return navigator.storage.persist();
}
}
const isFirefox = navigator.userAgent.indexOf("Firefox") !== -1;
if(isFirefox) {
//firefox can just ask for persist
return navigator.storage.persist();
}
return false;
}
return false
}
export async function persistantStorageRecommended() {
const db = get(DataBase)
if(navigator.storage && navigator.storage.persist && (!isTauri) && (!isNodeServer)) {
if(await navigator.storage.persisted()) {
return false;
}
if(db.characters.length > 5){
return true;
}
}
return false;
}
export async function requestPersistantStorage() {
const status = await requestPersistantStorageMain();
if(status) {
alertNormal(language.persistentStorageSuccess)
} else {
alertNormal(language.persistentStorageFail)
}
return status;
}