Skip to content

Commit

Permalink
Better error handling. Closes #2522. (#2524)
Browse files Browse the repository at this point in the history
* Better error handling. Closes #2522.

* Comments & bugfixes
  • Loading branch information
corrideat authored Jan 21, 2025
1 parent ae22899 commit faadd19
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 3 additions & 1 deletion frontend/views/pages/PendingApproval.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default ({
let reset = false
let destroyed = false
const syncPromise = sbp('chelonia/contract/wait', this.ourIdentityContractId).then(async () => {
const syncPromise = sbp('chelonia/externalStateWait', this.ourIdentityContractId).then(async () => {
if (destroyed) return
reset = false
// We don't want to accidentally unsubscribe from the group while this
Expand Down Expand Up @@ -87,6 +87,8 @@ export default ({
sbp('chelonia/contract/currentKeyIdByName', this.ourIdentityContractId, 'csk'),
sbp('chelonia/contract/currentKeyIdByName', this.ourIdentityContractId, 'cek')
])
if (!state.groups[this.ephemeral.groupIdWhenMounted]) return
await sbp('gi.actions/group/join', {
originatingContractID: this.ourIdentityContractId,
originatingContractName: 'gi.contracts/identity',
Expand Down
31 changes: 29 additions & 2 deletions shared/domains/chelonia/localSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ export default (sbp('sbp/selectors/register', {
// 3. Each tab calls this selector once to set up event listeners on EVENT_HANDLED
// and CONTRACTS_MODIFIED, which will keep each tab's state updated every
// time Chelonia handles an event.
'chelonia/externalStateSetup': ({ stateSelector, reactiveSet = Reflect.set.bind(Reflect), reactiveDel = Reflect.deleteProperty.bind(Reflect) }: {
'chelonia/externalStateSetup': function ({ stateSelector, reactiveSet = Reflect.set.bind(Reflect), reactiveDel = Reflect.deleteProperty.bind(Reflect) }: {
stateSelector: string,
reactiveSet: Function,
reactiveDel: Function
}) => {
}) {
this.stateSelector = stateSelector
sbp('okTurtles.events/on', EVENT_HANDLED, (contractID, message) => {
// The purpose of putting things immediately into a queue is to have
// state mutations happen in a well-defined order. This is done for two
Expand Down Expand Up @@ -90,5 +91,31 @@ export default (sbp('sbp/selectors/register', {
sbp('okTurtles.events/emit', CONTRACTS_MODIFIED_READY, subscriptionSet, { added, removed })
})
})
},
// This function is similar in purpose to `chelonia/contract/wait`, except
// that it's also designed to take into account delays copying Chelonia state
// to an external state (e.g., when using `chelonia/externalStateSetup`).
'chelonia/externalStateWait': async function (contractID) {
await sbp('chelonia/contract/wait', contractID)
const { cheloniaState } = await sbp('chelonia/contract/fullState', contractID)
const localState = sbp(this.stateSelector)
// If the current 'local' state has a height higher than or equal to the
// Chelonia height, we've processed all events and don't need to wait any
// longer.
if (cheloniaState.height <= localState.contracts[contractID]?.height) return

// Otherwise, listen for `EVENT_HANDLED_READY` events till we have reached
// the necessary height.
return new Promise((resolve) => {
const removeListener = sbp('okTurtles.events/on', EVENT_HANDLED_READY, (cID) => {
if (cID !== contractID) return

const localState = sbp(this.stateSelector)
if (cheloniaState.height <= localState.contracts[contractID]?.height) {
resolve()
removeListener()
}
})
})
}
}): string[])

0 comments on commit faadd19

Please sign in to comment.