add default sync sheme

This commit is contained in:
Arthur Lu 2023-07-12 22:46:59 +00:00
parent 0e07557375
commit 93e120edb5
2 changed files with 15 additions and 4 deletions

View File

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

View File

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