add default sync sheme

This commit is contained in:
Arthur Lu 2023-07-12 22:46:59 +00:00
parent d25ce49ede
commit 995513c9b8
2 changed files with 15 additions and 4 deletions

View File

@ -1,9 +1,20 @@
import { requestAPI } from "./utils.js";
export async function setupClientSync (scheme, rate, callback) {
export async function setupClientSync (callback) {
let scheme = localStorage.getItem("sync-scheme");
let rate = Number(localStorage.getItem("sync-rate"));
if (!scheme) {
scheme = "always";
localStorage.setItem("sync-scheme", "always");
}
if (!rate) {
rate = "5";
localStorage.setItem("sync-rate", "5")
}
if (scheme === "always") {
callback();
window.setInterval(callback, rate);
window.setInterval(callback, rate * 1000);
}
else if (scheme === "hash") {
const newHash = (await requestAPI("/sync/hash")).data;
@ -15,7 +26,7 @@ export async function setupClientSync (scheme, rate, callback) {
localStorage.setItem("sync-current-hash", newHash);
callback();
}
}, rate);
}, rate * 1000);
}
else if (scheme === "interrupt") {

View File

@ -15,7 +15,7 @@ async function init () {
const addInstanceBtn = document.querySelector("#instance-add");
addInstanceBtn.addEventListener("click", handleInstanceAdd);
setupClientSync(localStorage.getItem("sync-scheme"), Number(localStorage.getItem("sync-rate")) * 1000, populateInstances);
setupClientSync(populateInstances);
}
async function populateInstances () {