From f6d1c6402f3020ed78c93560e9a1fdb1e8a3506d Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 21 Aug 2020 09:18:33 -0400 Subject: [PATCH] Add support for removal of cloud storage entries --- platform/chromium/vapi-background.js | 20 ++++++++++++++++---- src/js/cloud-ui.js | 13 +++++++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index 7a2b3c17d..6bd301ae5 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -1559,7 +1559,7 @@ vAPI.cloud = (( ) => { return chunkCount; }; - const deleteChunks = function(datakey, start) { + const deleteChunks = async function(datakey, start) { const keys = []; // No point in deleting more than: @@ -1579,6 +1579,12 @@ vAPI.cloud = (( ) => { const push = async function(details) { const { datakey, data, encode } = details; + if ( + data === undefined || + typeof data === 'string' && data === '' + ) { + return deleteChunks(datakey, 0); + } const item = { source: options.deviceName || options.defaultDeviceName, tstamp: Date.now(), @@ -1602,14 +1608,20 @@ vAPI.cloud = (( ) => { } bin[datakey + chunkCount.toString()] = ''; // Sentinel + // Remove potentially unused trailing chunks before storing the data, + // this will free storage space which could otherwise cause the push + // operation to fail. + try { + await deleteChunks(datakey, chunkCount); + } catch (reason) { + } + + // Push the data to browser-provided cloud storage. try { await webext.storage.sync.set(bin); } catch (reason) { return String(reason); } - - // Remove potentially unused trailing chunks - deleteChunks(datakey, chunkCount); }; const pull = async function(details) { diff --git a/src/js/cloud-ui.js b/src/js/cloud-ui.js index 06d42907c..103cbc24d 100644 --- a/src/js/cloud-ui.js +++ b/src/js/cloud-ui.js @@ -73,11 +73,20 @@ const fetchStorageUsed = async function() { /******************************************************************************/ const fetchCloudData = async function() { + const info = widget.querySelector('#cloudInfo'); + const entry = await vAPI.messaging.send('cloudWidget', { what: 'cloudPull', datakey: self.cloud.datakey, }); - if ( entry instanceof Object === false ) { return entry; } + + const hasData = entry instanceof Object; + if ( hasData === false ) { + uDom.nodeFromId('cloudPull').setAttribute('disabled', ''); + uDom.nodeFromId('cloudPullAndMerge').setAttribute('disabled', ''); + info.textContent = '...\n...'; + return entry; + } self.cloud.data = entry.data; @@ -96,7 +105,7 @@ const fetchCloudData = async function() { }; const time = new Date(entry.tstamp); - widget.querySelector('#cloudInfo').textContent = + info.textContent = entry.source + '\n' + time.toLocaleString('fullwide', timeOptions); };