Skip to content

Commit

Permalink
move back the call to syncContractsInOrder where it was
Browse files Browse the repository at this point in the history
  • Loading branch information
taoeffect committed Feb 21, 2025
1 parent 7f9e00d commit 4694034
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions frontend/setupChelonia.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,6 @@ const setupChelonia = async (): Promise<*> => {
saveChelonia().catch(e => {
console.error('LOGIN_COMPLETE handler: Error saving Chelonia state', e)
})

sbp('gi.db/settings/load', SETTING_CURRENT_USER).then(async (identityContractID) => {
// This loads CHELONIA_STATE when _not_ running as a service worker
const cheloniaState = await sbp('chelonia/rootState')
if (!cheloniaState || !identityContractID) return
if (cheloniaState.loggedIn?.identityContractID !== identityContractID) return
// it is important we first login before syncing any contracts here since that will load the
// state and the contract sideEffects will sometimes need that state, e.g. loggedIn.identityContractID
await sbp('chelonia/contract/sync', identityContractID).then(async () => {
const contractIDs = groupContractsByType(cheloniaState.contracts)
await syncContractsInOrder(contractIDs)
}).catch(e => {
console.error('[setupChelonia] Error syncing identity contract and groups', e)
})
})
})

sbp('okTurtles.events/on', CHELONIA_STATE_MODIFIED, () => {
Expand Down Expand Up @@ -315,6 +300,25 @@ const setupChelonia = async (): Promise<*> => {
}
}
}))

// this should be done here and not in LOGIN_COMPLETE because:
// If the SW awakens but it's not a navigation event, you'll skip the code syncing all
// contracts, which will remove all contracts from the push subscription. So, the second
// time the SW wakes up it'll have no contracts
sbp('gi.db/settings/load', SETTING_CURRENT_USER).then(async (identityContractID) => {
// This loads CHELONIA_STATE when _not_ running as a service worker
const cheloniaState = await sbp('chelonia/rootState')
if (!cheloniaState || !identityContractID) return
if (cheloniaState.loggedIn?.identityContractID !== identityContractID) return
// it is important we first login before syncing any contracts here since that will load the
// state and the contract sideEffects will sometimes need that state, e.g. loggedIn.identityContractID
await sbp('chelonia/contract/sync', identityContractID).then(async () => {
const contractIDs = groupContractsByType(cheloniaState.contracts)
await syncContractsInOrder(contractIDs)
}).catch(e => {
console.error('[setupChelonia] Error syncing identity contract and groups', e)
})
})
}

// This implements a 'singleton promise' or 'lazy intialization' of setupChelonia.
Expand Down

0 comments on commit 4694034

Please sign in to comment.