Skip to content

Commit

Permalink
fix(core): 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 7, 2024
1 parent dd7563c commit dba2b56
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,12 @@ export function getPairListener(
return merge(draftEvents$, publishedEvents$).pipe(
catchError((err, caught$) => {
if (err instanceof OutOfSyncError) {
options?.onSyncErrorRecovery(err)

debug('Recovering from OutOfSyncError: %s', OutOfSyncError.name)
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 @@ -66,10 +66,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 @@ -89,10 +89,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 dba2b56

Please sign in to comment.