Skip to content

Commit

Permalink
fix(core): references to documents on same release are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jan 18, 2025
1 parent 7ef3083 commit 0327eaa
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ import {
shareReplay,
skip,
throttleTime,
withLatestFrom,
} from 'rxjs/operators'
import {exhaustMapWithTrailing} from 'rxjs-exhaustmap-with-trailing'
import shallowEquals from 'shallow-equals'

import {type DocumentPreviewStore, type LocaleSource, type SourceClientOptions} from '..'
import {
type DocumentPreviewStore,
getVersionFromId,
type LocaleSource,
type SourceClientOptions,
} from '..'
import {validateDocumentObservable} from './validateDocument'

/**
Expand Down Expand Up @@ -70,8 +76,11 @@ type GetDocumentExists = NonNullable<ValidationContext['getDocumentExists']>
const listenDocumentExists = (
observeDocumentAvailability: DocumentPreviewStore['unstable_observeDocumentPairAvailability'],
id: string,
versionId: string | undefined,
): Observable<boolean> =>
observeDocumentAvailability(id).pipe(map(({published}) => published.available))
observeDocumentAvailability(id, {version: versionId}).pipe(
map(({published, version}) => published.available || version?.available || false),
)

// throttle delay for referenced document updates (i.e. time between responding to changes in referenced documents)
const REF_UPDATE_DELAY = 1000
Expand All @@ -98,14 +107,19 @@ export function validateDocumentWithReferences(
mergeMap((ids) => from(ids)),
)

const versionId$ = document$.pipe(
map((doc) => getVersionFromId(doc?._id || '')),
distinctUntilChanged(),
)
// 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, {duration: () => timer(1000 * 60 * 30)}),
mergeMap((id$) =>
id$.pipe(
distinct(),
mergeMap((id) =>
listenDocumentExists(ctx.observeDocumentPairAvailability, id).pipe(
withLatestFrom(versionId$),
mergeMap(([id, versionId]) =>
listenDocumentExists(ctx.observeDocumentPairAvailability, id, versionId).pipe(
map(
// eslint-disable-next-line max-nested-callbacks
(result) => [id, result] as const,
Expand Down

0 comments on commit 0327eaa

Please sign in to comment.