From 0641747a78df86a76686bf6701c13c8a8467a637 Mon Sep 17 00:00:00 2001 From: I Made Setia Baruna Date: Fri, 6 Nov 2020 17:56:11 +0700 Subject: [PATCH] Add data sync for wish counter --- src/routes/settings.svelte | 15 +++++++++----- src/stores/dataSync.js | 1 + src/stores/saveManager.js | 41 +++++++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/routes/settings.svelte b/src/routes/settings.svelte index f3c6934f..b7862024 100644 --- a/src/routes/settings.svelte +++ b/src/routes/settings.svelte @@ -3,7 +3,7 @@ import Button from '../components/Button.svelte'; import Icon from '../components/Icon.svelte'; - import { driveSignedIn, driveLoading, synced } from '../stores/dataSync'; + import { driveSignedIn, driveLoading, synced, localModified, lastSyncTime } from '../stores/dataSync'; function signIn() { gapi.auth2.getAuthInstance().signIn(); @@ -12,6 +12,8 @@ function signOut() { gapi.auth2.getAuthInstance().signOut(); } + + $: isSynced = $synced && !$localModified;
@@ -33,14 +35,17 @@

Sync Status: - - {$synced ? 'Synced' : 'Syncing...'} - {#if $synced} + + {isSynced ? 'Synced' : $localModified && $synced ? 'Waiting...' : 'Syncing...'} + {#if isSynced} - {:else} + {:else if $localModified && !$synced} {/if}

+ {#if $lastSyncTime !== null} +

Last Sync: {$lastSyncTime.format('dddd, MMMM D, YYYY h:mm:ss A')}

+ {/if} {/if}
diff --git a/src/stores/dataSync.js b/src/stores/dataSync.js index 8abf1def..af2c7808 100644 --- a/src/stores/dataSync.js +++ b/src/stores/dataSync.js @@ -3,5 +3,6 @@ import { writable } from 'svelte/store'; export const driveSignedIn = writable(false); export const driveLoading = writable(true); export const lastSyncTime = writable(null); +export const localModified = writable(false); export const synced = writable(false); export const saveId = writable(''); diff --git a/src/stores/saveManager.js b/src/stores/saveManager.js index 034d67fb..9df9aa6d 100644 --- a/src/stores/saveManager.js +++ b/src/stores/saveManager.js @@ -1,7 +1,8 @@ import dayjs from 'dayjs'; import { writable } from 'svelte/store'; +import debounce from 'lodash/debounce'; -import { synced } from './dataSync'; +import { synced, saveId, localModified, lastSyncTime } from './dataSync'; export const updateTime = writable(null); export const fromRemote = writable(false); @@ -10,13 +11,43 @@ export const UPDATE_TIME_KEY = 'update-time'; let pendingQueue = []; let queueSave = true; +let saveFileId = ''; -const unsubscribe = synced.subscribe((value) => { +saveId.subscribe((val) => { + saveFileId = val; +}); + +const saveToRemote = debounce(() => { + saveData(getLocalSaveJson()); +}, 5000); + +async function saveData(data) { + console.log('saving to remote file'); + synced.set(false); + + try { + await gapi.client.request({ + path: `/upload/drive/v3/files/${saveFileId}`, + method: 'PATCH', + params: { + uploadType: 'media', + }, + body: data, + }); + synced.set(true); + localModified.set(false); + } catch (err) { + console.error(err); + } +} + +synced.subscribe((value) => { console.log('synced:', value); queueSave = !value; if (value) { flushPendingQueue(); + lastSyncTime.set(dayjs()); } }); @@ -33,6 +64,10 @@ export const getLocalSaveJson = () => { }; export const updateSave = (key, data, isFromRemote) => { + if (!isFromRemote) { + localModified.set(true); + } + if (queueSave && !isFromRemote) { pendingQueue.push({ key, data }); return; @@ -44,6 +79,7 @@ export const updateSave = (key, data, isFromRemote) => { const currentTime = dayjs().toISOString(); updateTime.set(currentTime); localStorage.setItem(UPDATE_TIME_KEY, currentTime); + saveToRemote(); } else { fromRemote.set(true); } @@ -64,5 +100,4 @@ export const flushPendingQueue = () => { pendingQueue = []; queueSave = false; - unsubscribe(); };