Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2616. Wait until Chelonia is ready before logging in #2620

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions frontend/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async function startApp () {
})]
).catch(e => {
console.error('[main] Error setting up service worker', e)
alert(L('Error while setting up service worker'))
alert(L('Error while setting up service worker: {err}', { err: e.message }))
window.location.reload() // try again, sometimes it fixes it
throw e
})
Expand Down Expand Up @@ -354,7 +354,20 @@ async function startApp () {
// happened (an example where things can happen this quickly is in the
// tests).
let oldIdentityContractID = null
sbp('gi.db/settings/load', SETTING_CURRENT_USER).then(async (identityContractID) => {
Promise.all([sbp('gi.db/settings/load', SETTING_CURRENT_USER), (() => {
// Wait for SW to be ready
console.debug('[app] Waiting for SW to be ready')
return Promise.race([
navigator.serviceWorker.ready,
new Promise((resolve, reject) => setTimeout(() => reject(new Error('SW ready timeout')), 10000))
]).catch(e => {
console.error('[app] Service worker failed to become ready:', e)
// Fallback behavior
this.removeLoadingAnimation()
alert(L('Error while setting up service worker: {err}', { err: e.message }))
throw e
taoeffect marked this conversation as resolved.
Show resolved Hide resolved
})
})()]).then(async ([identityContractID]) => {
oldIdentityContractID = identityContractID
if (!identityContractID || this.ephemeral.finishedLogin === 'yes') return
// Calling login could result in a prompt in case of an error; if the
Expand All @@ -366,6 +379,22 @@ async function startApp () {
await sbp('gi.app/identity/login', { identityContractID })
removeHandler()
await sbp('chelonia/contract/wait', identityContractID)
}).then(() => {
const sw = ((navigator.serviceWorker: any): ServiceWorkerContainer)
const onready = () => {
this.ephemeral.ready = true
this.removeLoadingAnimation()
setupNativeNotificationsListeners()
}
if (!sw.controller) {
const listener = (ev: Event) => {
sw.removeEventListener('controllerchange', listener, false)
onready()
}
sw.addEventListener('controllerchange', listener, false)
} else {
onready()
}
}).catch(async e => {
this.removeLoadingAnimation()
oldIdentityContractID && sbp('appLogs/clearLogs', oldIdentityContractID).catch(e => {
Expand All @@ -378,34 +407,6 @@ async function startApp () {
question: L('Error details: {reportError}', LError(e)),
primaryButton: L('Close')
})
}).finally(() => {
// Wait for SW to be ready
console.debug('[app] Waiting for SW to be ready')
const sw = ((navigator.serviceWorker: any): ServiceWorkerContainer)
Promise.race([
sw.ready,
new Promise((resolve, reject) => setTimeout(() => reject(new Error('SW ready timeout')), 10000))
]).then(() => {
const onready = () => {
this.ephemeral.ready = true
this.removeLoadingAnimation()
setupNativeNotificationsListeners()
}
if (!sw.controller) {
const listener = (ev: Event) => {
sw.removeEventListener('controllerchange', listener, false)
onready()
}
sw.addEventListener('controllerchange', listener, false)
} else {
onready()
}
}).catch(e => {
console.error('[app] Service worker failed to become ready:', e)
// Fallback behavior
this.removeLoadingAnimation()
alert(L('Error while setting up service worker'))
})
})
},
computed: {
Expand Down