Skip to content

Commit

Permalink
fix: catch errors in completeRecording
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiVandivier committed Aug 18, 2021
1 parent f8e1823 commit e67ae82
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions pwa/src/service-worker/recording-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,36 +176,41 @@ function startConfirmationTimeout(clientId) {
}

/** Triggered by 'COMPLETE_RECORDING' message; saves recording */
// todo: handle errors
export async function completeRecording(clientId) {
const recordingState = self.clientRecordingStates[clientId]
console.debug('[SW] Completing recording', { clientId, recordingState })
clearTimeout(recordingState.confirmationTimeout)

// Move requests from temp cache to section-<ID> cache
const sectionCache = await caches.open(recordingState.sectionId)
const tempCache = await caches.open(getCacheKey('temp', clientId))
const tempCacheItemKeys = await tempCache.keys()
tempCacheItemKeys.forEach(async request => {
const response = await tempCache.match(request)
sectionCache.put(request, response)
})

// Add content to DB
const db = await self.dbPromise
db.put(SECTIONS_STORE, {
// Note that request objects can't be stored in the IDB
// https://stackoverflow.com/questions/32880073/whats-the-best-option-for-structured-cloning-of-a-fetch-api-request-object
sectionId: recordingState.sectionId, // the key path
lastUpdated: new Date(),
// 'requests' can later hold data for normalization
requests: recordingState.fulfilledRequests,
}).catch(console.error)
try {
const recordingState = self.clientRecordingStates[clientId]
console.debug('[SW] Completing recording', { clientId, recordingState })
clearTimeout(recordingState.confirmationTimeout)

// Add content to DB
const db = await self.dbPromise
db.put(SECTIONS_STORE, {
// Note that request objects can't be stored in the IDB
// https://stackoverflow.com/questions/32880073/whats-the-best-option-for-structured-cloning-of-a-fetch-api-request-object
sectionId: recordingState.sectionId, // the key path
lastUpdated: new Date(),
// 'requests' can later hold data for normalization
requests: recordingState.fulfilledRequests,
}).catch(console.error)

// Move requests from temp cache to section-<ID> cache
const sectionCache = await caches.open(recordingState.sectionId)
const tempCache = await caches.open(getCacheKey('temp', clientId))
const tempCacheItemKeys = await tempCache.keys()
tempCacheItemKeys.forEach(async request => {
const response = await tempCache.match(request)
sectionCache.put(request, response)
})

// Clean up
removeRecording(clientId)
// Clean up
removeRecording(clientId)

// Send confirmation message to client
self.clients.get(clientId).then(client => {
client.postMessage({ type: swMsgs.recordingCompleted })
})
// Send confirmation message to client
self.clients.get(clientId).then(client => {
client.postMessage({ type: swMsgs.recordingCompleted })
})
} catch (err) {
stopRecording(err, clientId)
}
}

0 comments on commit e67ae82

Please sign in to comment.