let apiUrl: string = `${location.protocol}//${location.hostname}:8856/`; class Favorites { private data: string[] = []; constructor() { this.data = JSON.parse(localStorage.getItem("settings.favorites")) || []; this.sync(); } public async sync(): Promise { localStorage.setItem("settings.favorites", JSON.stringify(this.data)); } public async clear(): Promise { this.data = []; this.sync(); } public async pop(): Promise { let item: any = this.data.pop(); this.sync(); return item; } public async push(toadd): Promise { this.data.push(toadd); this.sync(); } public async delete(d): Promise { this.data = this.data.filter((v) => { return v != d }); this.sync(); } public async at(d): Promise { return this.data.at(d); } public async contains(d): Promise { return this.data.includes(d); } public async shuffle(): Promise { let data: string[] = this.data; for (let i = 0; i < await this.data.length; i++) { data.sort(() => { return Math.random() - 0.5 }); } return data; } } class Blacklist { private data: string[] = []; constructor() { this.data = JSON.parse(localStorage.getItem("settings.blacklist")) || []; this.sync(); } public async sync(): Promise { localStorage.setItem("settings.blacklist", JSON.stringify(this.data)); } public async clear(): Promise { this.data = []; this.sync(); } public async pop(): Promise { let item: any = this.data.pop(); this.sync(); return item; } public async push(toadd): Promise { this.data.push(toadd); this.sync(); } public async delete(d): Promise { this.data = this.data.filter((v) => { return v != d }); this.sync(); } public async at(d): Promise { return this.data.at(d); } public async contains(d): Promise { return this.data.includes(d); } } class Cache { private cache: string[]; private checksum: string; constructor() {} async load(): Promise { if (!localStorage.getItem("cache") || !localStorage.getItem("cache.checksum")) { await this.sync(); } else { if (!await this.validate()) { await this.sync(); } else { this.cache = JSON.parse(localStorage.getItem("cache")); this.checksum = localStorage.getItem("cache.checksum"); } } } async sync(): Promise { try { let sync = await fetch(apiUrl + "sync"); let data = await sync.json(); if (!sync.ok) throw Error(sync.statusText); this.checksum = data.checksum; this.cache = data.files; await this.update(); } catch (e) { throw e; } } private async update(): Promise { localStorage.setItem("cache", JSON.stringify(this.cache)); localStorage.setItem("cache.checksum", this.checksum); } private async validate(): Promise { try { let sync = await fetch(apiUrl + "sync", { body: this.checksum, method: "POST", }); if (!sync.ok) throw Error(sync.statusText); return (await sync.json()).valid; } catch(e) { throw e; } } public async at(d): Promise { return this.cache.at(d); } public async shuffle(): Promise { let data: string[] = this.cache; for (let i = 0; i < await this.length(); i++) { data.sort(() => { return Math.random() - 0.5 }); } return data; } public async length(): Promise { return this.cache.length; } } class Config { private data = {}; private defaultConfig = { "test": 123, } constructor() { if (!localStorage.getItem("config")) localStorage.setItem("config", JSON.stringify(this.defaultConfig)); this.data = JSON.parse(localStorage.getItem("config")); this.verify(); this.sync(); } private verify() { let keys = this.keys(); for (let k in this.defaultConfig) if (!keys[k]) this.data[k] = this.defaultConfig[k]; } public sync() { localStorage.setItem("config", JSON.stringify(this.data)); } public set(vr, vl) { this.data[vr] = vl; } public get(vr) { return this.data[vr]; } public keys() { return Object.keys(this.data); } } export default class Settings { favorites: Favorites; blacklist: Blacklist; cache: Cache; config: Config; constructor() { this.favorites = new Favorites(); this.blacklist = new Blacklist(); this.cache = new Cache(); this.config = new Config(); } } export { apiUrl };