Skip to content

Commit

Permalink
fix(core): make sure to re-emit current edit state when re-subscribin…
Browse files Browse the repository at this point in the history
…g to document validation
  • Loading branch information
bjoerge committed Sep 29, 2023
1 parent 0715a15 commit 790cf35
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ import {
groupBy,
map,
mergeMap,
publishReplay,
refCount,
scan,
share,
shareReplay,
skip,
throttleTime,
Expand Down Expand Up @@ -82,6 +79,10 @@ const DOC_UPDATE_DELAY = 200
// throttle delay for referenced document updates (i.e. time between responding to changes in referenced documents)
const REF_UPDATE_DELAY = 1000

function shareLatestWithRefCount<T>() {
return shareReplay<T>({bufferSize: 1, refCount: true})
}

/** @internal */
export const validation = memoize(
(
Expand All @@ -105,7 +106,7 @@ export const validation = memoize(
// so only pass on documents if _other_ attributes changes
return shallowEquals(omit(prev, '_rev', '_updatedAt'), omit(next, '_rev', '_updatedAt'))
}),
share(),
shareLatestWithRefCount(),
)

const referenceIds$ = document$.pipe(
Expand All @@ -115,11 +116,7 @@ export const validation = memoize(

// Note: we only use this to trigger a re-run of validation when a referenced document is published/unpublished
const referenceExistence$ = referenceIds$.pipe(
groupBy(
(id) => id,
undefined,
() => timer(1000 * 60 * 30),
),
groupBy((id) => id, {duration: () => timer(1000 * 60 * 30)}),
mergeMap((id$) =>
id$.pipe(
distinct(),
Expand All @@ -140,7 +137,7 @@ export const validation = memoize(
return {...acc, [id]: result}
}, {}),
distinctUntilChanged(shallowEquals),
shareReplay({refCount: true, bufferSize: 1}),
shareLatestWithRefCount(),
)

// Provided to individual validation functions to support using existence of a weakly referenced document
Expand Down Expand Up @@ -180,8 +177,7 @@ export const validation = memoize(
})
}),
scan((acc, next) => ({...acc, ...next}), INITIAL_VALIDATION_STATUS),
publishReplay(1),
refCount(),
shareLatestWithRefCount(),
)
},
(ctx, idPair, typeName) => {
Expand Down

0 comments on commit 790cf35

Please sign in to comment.