From 93e120edb550bffda3e8647717b18e88f7fccb44 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 12 Jul 2023 22:46:59 +0000 Subject: [PATCH] add default sync sheme --- scripts/clientsync.js | 17 ++++++++++++++--- scripts/index.js | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/scripts/clientsync.js b/scripts/clientsync.js index 6d4bf69..253dd17 100644 --- a/scripts/clientsync.js +++ b/scripts/clientsync.js @@ -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") { diff --git a/scripts/index.js b/scripts/index.js index 18a01b6..4646407 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -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 () {