Skip to content

Commit

Permalink
fix: reopen indexedDB if global state resets
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiVandivier committed Aug 23, 2021
1 parent 194262e commit 0aded68
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 6 additions & 2 deletions pwa/src/service-worker/recording-mode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { swMsgs } from '../lib/constants'
import { SECTIONS_STORE } from '../lib/sections-db'
import { openSectionsDB, SECTIONS_STORE } from '../lib/sections-db'

// Triggered on 'START_RECORDING' message
export function startRecording(event) {
Expand Down Expand Up @@ -109,6 +109,7 @@ function stopRecording(error, clientId) {
// In case of error, notify client and remove recording
if (error) {
self.clients.get(clientId).then(client => {
// todo: use plain object instead of Error for firefox compatibility
client.postMessage({
type: swMsgs.recordingError,
payload: {
Expand Down Expand Up @@ -176,13 +177,16 @@ function startConfirmationTimeout(clientId) {
}

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

// If global state has reset, reopen IndexedDB
if (self.dbPromise === undefined) {
self.dbPromise = openSectionsDB()
}
// Add content to DB
const db = await self.dbPromise
db.put(SECTIONS_STORE, {
Expand Down
5 changes: 1 addition & 4 deletions pwa/src/service-worker/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ export function setUpServiceWorker() {
// TODO: control with env var
self.__WB_DISABLE_DEV_LOGS = true

// Globals
// Globals (Note: global state resets each time SW goes idle)

// Will be populated upon activation with a promise that accesses the
// recorded sections IndexedDB using the `idb` library - see `createDB()`
self.dbPromise
// Tracks recording states for multiple clients to handle multiple windows
// recording simultaneously
self.clientRecordingStates = {}
Expand Down

0 comments on commit 0aded68

Please sign in to comment.