optimize sync & add accept popup
This commit is contained in:
parent
c9f604863a
commit
f0e097fa76
10 changed files with 305 additions and 61 deletions
|
|
@ -1370,7 +1370,12 @@ async function runLanSync(device, code, categories, force) {
|
|||
openLanSyncModal({ ...device, code: '' }, 'lansync.badCode')
|
||||
return
|
||||
}
|
||||
const message = err === 'busy' ? t('lansync.busy') : err === 'offline' ? t('lansync.offline') : (err || t('lansync.unknownError'))
|
||||
const message =
|
||||
err === 'busy' ? t('lansync.busy')
|
||||
: err === 'offline' ? t('lansync.offline')
|
||||
: err === 'rejected' ? t('lansync.rejected')
|
||||
: err === 'incoming-timeout' ? t('lansync.incomingTimedOut')
|
||||
: (err || t('lansync.unknownError'))
|
||||
toast(t('lansync.failed', message), 'error')
|
||||
}
|
||||
|
||||
|
|
@ -1389,6 +1394,64 @@ const RECEIVE_STALL_MS = 6 * 1000
|
|||
// phases whose wording differs on this side (everything else is neutral:
|
||||
// "Comparing X", "Updating the library", "Finishing up")
|
||||
const RECV_PHASE_KEYS = { copy: 'lansync.recvPhase.copy' }
|
||||
// A device wants to push to us: the popup asks for consent before anything
|
||||
// moves. One pending request at a time (the server enforces that too).
|
||||
let incomingRequest = null
|
||||
let incomingTimer = null
|
||||
|
||||
function onLanIncoming(info) {
|
||||
if (!info || !info.requestId) return
|
||||
if (state.lanPushing || receiving) {
|
||||
// our own transfer owns the modal slot - answer so the sender fails
|
||||
// fast instead of waiting out its timeout
|
||||
window.plwl.lansyncRespondIncoming(info.requestId, false)
|
||||
return
|
||||
}
|
||||
incomingRequest = info
|
||||
const name = info.name || info.deviceId || t('lansync.unknownDevice')
|
||||
const totals = info.totals || {}
|
||||
const parts = []
|
||||
if (totals.apps > 0) parts.push(t('lansync.statsApps', totals.apps))
|
||||
if (totals.wines > 0) parts.push(t('lansync.statsWines', totals.wines))
|
||||
if (totals.prefixes > 0) parts.push(t('lansync.statsPrefixes', totals.prefixes))
|
||||
const modal = showModal(`
|
||||
<h2 id="ls-incoming-title"></h2>
|
||||
<p id="ls-incoming-text"></p>
|
||||
<p class="subtitle" id="ls-incoming-what"></p>
|
||||
<p class="subtitle sync-warning" id="ls-incoming-warning"></p>
|
||||
<div class="modal-actions">
|
||||
<button class="button" id="ls-incoming-decline"></button>
|
||||
<button class="button primary" id="ls-incoming-accept"></button>
|
||||
</div>
|
||||
`, { locked: true })
|
||||
modal.querySelector('#ls-incoming-title').textContent = t('lansync.incomingTitle', name)
|
||||
modal.querySelector('#ls-incoming-text').textContent = t('lansync.incomingText', name)
|
||||
modal.querySelector('#ls-incoming-what').textContent = parts.length ? parts.join(' · ') : t('lansync.incomingNothing')
|
||||
modal.querySelector('#ls-incoming-warning').textContent = t('lansync.mirrorWarning')
|
||||
modal.querySelector('#ls-incoming-decline').textContent = t('lansync.decline')
|
||||
modal.querySelector('#ls-incoming-accept').textContent = t('lansync.accept')
|
||||
const answer = accept => {
|
||||
if (!incomingRequest) return
|
||||
const requestId = incomingRequest.requestId
|
||||
incomingRequest = null
|
||||
if (incomingTimer) {
|
||||
clearTimeout(incomingTimer)
|
||||
incomingTimer = null
|
||||
}
|
||||
closeModal()
|
||||
window.plwl.lansyncRespondIncoming(requestId, accept)
|
||||
}
|
||||
modal.querySelector('#ls-incoming-accept').onclick = () => answer(true)
|
||||
modal.querySelector('#ls-incoming-decline').onclick = () => answer(false)
|
||||
// mirror the server's own expiry so the popup never outlives its request
|
||||
incomingTimer = setTimeout(() => {
|
||||
incomingTimer = null
|
||||
incomingRequest = null
|
||||
closeModal()
|
||||
toast(t('lansync.incomingTimedOut'), 'error')
|
||||
}, 60000)
|
||||
}
|
||||
|
||||
let receiving = false
|
||||
let receiveClosed = false
|
||||
let receiveWatchdog = null
|
||||
|
|
@ -1521,6 +1584,7 @@ function wireLanSync() {
|
|||
if (state.currentView === 'lansync') renderLanDevices(devices)
|
||||
})
|
||||
window.plwl.onLanSyncReceiving(info => onLanReceiving(info))
|
||||
window.plwl.onLanSyncIncoming(info => onLanIncoming(info))
|
||||
window.plwl.onLanSyncApplied(async info => {
|
||||
toast(t('lansync.applied', info.from || '', info.files || 0, formatBytes(info.bytes || 0)), 'success')
|
||||
await reloadLocalData()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue