Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): make sure to re-emit current edit state when re-subscribing to document validation #4954

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the line that fixes the issue. Since we were using share, re-subscribing to this observable didn't publish the current edit state of the document, leaving the validation in a pending state

)

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