Add persistent storage feature
This commit is contained in:
61
src/ts/storage/persistant.ts
Normal file
61
src/ts/storage/persistant.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user