Skip to content

Commit

Permalink
Wait for queue invocation to be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Dec 31, 2024
1 parent 8b47148 commit 399a17b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
38 changes: 10 additions & 28 deletions frontend/controller/serviceworkers/sw-primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ sbp('sbp/selectors/register', {
'appLogs/save': () => sbp('swLogs/save')
})

sbp('gi.periodicNotifications/importNotifications', periodicNotificationEntries)

// Set up periodic notifications on the `CHELONIA_RESET` event. We do this here,
// before calling `setupRootState`, so that the `CHELONIA_RESET` it will trigger
// will set up periodic notifications.
sbp('okTurtles.events/on', CHELONIA_RESET, () => {
sbp('gi.periodicNotifications/clearStatesAndStopTimers')
sbp('gi.periodicNotifications/init')
})

sbp('okTurtles.data/set', 'API_URL', self.location.origin)
setupRootState()
const setupPromise = setupChelonia()
Expand Down Expand Up @@ -441,31 +451,3 @@ sbp('okTurtles.events/on', NOTIFICATION_EMITTED, (notification) => {
console.error('Error displaying native notification', e)
})
})

function getPendingQueuedInvocationsCount (): number {
return Object.entries(sbp('okTurtles.eventQueue/queuedInvocations'))
.flatMap(([, list]) => list).length
}

function initOrResetPeriodicNotifications () {
const x = () => {
sbp('gi.periodicNotifications/clearStatesAndStopTimers')
sbp('gi.periodicNotifications/init')
}

if (getPendingQueuedInvocationsCount() > 0) {
setTimeout(initOrResetPeriodicNotifications, 1000)
return
}

x()
}

sbp('gi.periodicNotifications/importNotifications', periodicNotificationEntries)

sbp('gi.periodicNotifications/clearStatesAndStopTimers')
setupPromise.finally(() => initOrResetPeriodicNotifications())

sbp('okTurtles.events/on', CHELONIA_RESET, () => {
initOrResetPeriodicNotifications()
})
19 changes: 15 additions & 4 deletions frontend/model/notifications/periodicNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,26 @@ async function runNotificationListRecursive () {
if (ephemeralNotificationState.clearTimeout) return
// Prevent accidentally calling `runNotificationListRecursive` while it's
// still running
const noop = () => {}
// We set `clearTimeout` to a local value to stop processing notifications
// when clearing stop notifications, to avoid calling
// `runNotificationListRecursive` while it's running and to be able to compare
// it to the local value later to replace `clearTimeout` later on in this
// function
let aborted: boolean = false
const noop = () => { aborted = true }
ephemeralNotificationState.clearTimeout = noop

/* await Promise.all(
Object.keys(sbp('okTurtles.eventQueue/queuedInvocations')).map(queue => sbp('okTurtles.eventQueue/queueEvent', noop))
) */
// If there are any queued invocations, wait until they're done
await Promise.all(
Object.entries(sbp('okTurtles.eventQueue/queuedInvocations'))
.map(([queue, invocations]) => {
return !!invocations.length && sbp('okTurtles.eventQueue/queueEvent', queue, () => {}).catch(() => {})
})
)

// Check if enough time has passed since the last run
for (const entry of ephemeralNotificationState.notifications) {
if (aborted) break
const stateKey = entry.stateKey
const lastRun = lastRunMap[stateKey] || 0
// Use the delay from the notification entry
Expand Down

0 comments on commit 399a17b

Please sign in to comment.