View and Fields get out of sync on realtime update
Describe the problem
At the moment when we send the realtime update force_view_refresh
the view
is updated via view/forceUpdate
and the fields
are updated via field/forceSetFields
.
Those 2 actions happen at different points in time, the view
is updated at the start and the fields
later during a callback.
realtime.registerEvent('force_view_refresh', async ({ store, app }, data) => {
const view = store.getters['view/get'](data.view_id)
if (view !== undefined) {
if (store.getters['view/getSelectedId'] === view.id) {
await store.dispatch('view/forceUpdate', {. <--- View is being updated (out of sync after this)
view,
values: data.view,
repopulate: true,
})
app.$bus.$emit('table-refresh', {
tableId: store.getters['table/getSelectedId'],
includeFieldOptions: true,
async callback() {
await store.dispatch('field/forceSetFields', { <--- Fields are being updated (in sync again)
fields: data.fields,
})
},
})
}
}
})
The issue with that, is that the view
and fields
become out of sync for the time it takes from one dispatch
to the second dispatch
.
The view
references fields
in its sort
and filters
, so when they are out of sync the view
might reference a field
that isn't in fields
yet.
This issue only becomes obvious when looking at the Public Gallery View, since if a field is hidden, it doesn't exist anymore for that view and therefore won't be in fields
and the mismatch will cause errors.
Specifically, it will cause an error in ViewSortContext
where we had to add <template v-if="field">
to make sure that if the view
and fields
are out of sync and it can't find the field, that it won't render the component fully.
Ideally we want to get rid of <template v-if="field">
and make sure that we are always in sync.
Possible Solution
In order to make sure that these values stay in sync, we need to make sure that they are updated at the same time.
This could be achieved by adding another action that does both, the update of the view
and the update of the fields
.
If that doesn't work we need to find some way to ensure that the update of fields
and view
happen at the same time.