Skip to content

Commit

Permalink
fix(recording-mode): handle fulfilled requests after recording error (#…
Browse files Browse the repository at this point in the history
…642)

* chore: remove todo comment

* fix(recording-mode): return early if recording state is undefined

* fix(recording-mode): reduce unnecessary error logging
  • Loading branch information
KaiVandivier committed Sep 8, 2021
1 parent d38be02 commit 928e2cb
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pwa/src/service-worker/recording-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,21 @@ export function handleRecordedRequest({ request, event }) {
return handleRecordedResponse(request, response, event.clientId)
})
.catch(error => {
console.error(error)
stopRecording(error, event.clientId)
})
}

/** Response handler during recording mode */
function handleRecordedResponse(request, response, clientId) {
const recordingState = self.clientRecordingStates[clientId]

if (!recordingState) {
// It's likely that the recording was stopped due to an error.
// There will be plenty of error messages logged; no need for another
// one here
return response
}

// add response to temp cache - when recording is successful, move to permanent cache
const tempCacheKey = getCacheKey('temp', clientId)
addToCache(tempCacheKey, request, response)
Expand Down Expand Up @@ -103,13 +110,16 @@ function startRecordingTimeout(clientId) {
function stopRecording(error, clientId) {
const recordingState = self.clientRecordingStates[clientId]

console.debug('[SW] Stopping recording', { clientId, recordingState })
clearTimeout(recordingState?.recordingTimeout)
if (recordingState) {
console.debug('[SW] Stopping recording', { clientId, recordingState })
clearTimeout(recordingState.recordingTimeout)
}

// In case of error, notify client and remove recording
// In case of error, notify client and remove recording.
// Post message even if !recordingState to ensure client stops.
if (error) {
self.clients.get(clientId).then(client => {
// todo: use plain object instead of Error for firefox compatibility
// use plain object instead of Error for firefox compatibility
client.postMessage({
type: swMsgs.recordingError,
payload: {
Expand Down

0 comments on commit 928e2cb

Please sign in to comment.