Skip to content

Commit

Permalink
fix: handle edge case where document may have been deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Oct 6, 2024
1 parent 5b8b28b commit bbcb5ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,11 @@ export function getPairListener(
return merge(draftEvents$, publishedEvents$).pipe(
catchError((err, caught$) => {
if (err instanceof OutOfSyncError) {
options?.onSyncErrorRecovery(err)

if (typeof options?.onSyncErrorRecovery === 'function') {
options?.onSyncErrorRecovery(err)
} else {
console.error(err)
}
// this will retry immediately
return caught$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ export function sequentializeListenerEvents(options?: {
}

if (event.type === 'mutation') {
// Note: the buffer may have several holes in it (worst case, and quite unlikely, but still),
// we need to consider all possible chains
// toOrderedChains will return all detected chains and each chain will be orderered
// Once we have a list of chains, we can discard any chain that ends at the current revision
// Note: the buffer may have multiple holes in it (this is a worst case scenario, and probably not likely, but still),
// so we need to consider all possible chains
// `toOrderedChains` will return all detected chains and each of the returned chains will be orderered
// Once we have a list of chains, we can then discard any chain that leads up to the current revision
// since they are already applied on the document
const orderedChains = toOrderedChains(state.buffer.concat(event)).map((chain) => {
// in case the chain leads up to the current revision
Expand All @@ -111,10 +111,14 @@ export function sequentializeListenerEvents(options?: {
if (applicableChains.length > 1) {
throw new Error('Expected at most one applicable chain')
}
if (applicableChains.length > 0) {
if (applicableChains.length > 0 && applicableChains[0].length > 0) {
// we now have a continuous chain that can apply on the base revision
// Move current base revision to the last mutation event in the applicable chain
const nextBaseRevision = applicableChains[0].at(-1)?.resultRev
const lastMutation = applicableChains[0].at(-1)!
const nextBaseRevision =
// special case: if the mutation deletes the document it technically has no revision, despite
// resultRev pointing at a transaction id.
lastMutation.transition === 'disappear' ? undefined : lastMutation?.resultRev
return {
base: {revision: nextBaseRevision},
emitEvents: applicableChains[0],
Expand Down

0 comments on commit bbcb5ae

Please sign in to comment.