diff --git a/packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts b/packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts index 98c0aa3340a..19e9975cfe8 100644 --- a/packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts +++ b/packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts @@ -2,7 +2,7 @@ import {type Action, type SanityClient} from '@sanity/client' import {type Mutation} from '@sanity/mutator' import {type SanityDocument} from '@sanity/types' import {omit} from 'lodash' -import {EMPTY, from, merge, type Observable, Subject} from 'rxjs' +import {EMPTY, from, merge, type Observable} from 'rxjs' import {filter, map, mergeMap, share, take, tap} from 'rxjs/operators' import { @@ -65,7 +65,6 @@ export interface Pair { transactionsPendingEvents$: Observable published: DocumentVersion draft: DocumentVersion - complete: () => void } function setVersion(version: 'draft' | 'published') { @@ -204,10 +203,7 @@ export function checkoutPair( ): Pair { const {publishedId, draftId} = idPair - const listenerEventsConnector = new Subject() - const listenerEvents$ = getPairListener(client, idPair, pairListenerOptions).pipe( - share({connector: () => listenerEventsConnector}), - ) + const listenerEvents$ = getPairListener(client, idPair, pairListenerOptions).pipe(share()) const reconnect$ = listenerEvents$.pipe( filter((ev) => ev.type === 'reconnect'), @@ -255,6 +251,5 @@ export function checkoutPair( consistency$: published.consistency$, remoteSnapshot$: published.remoteSnapshot$.pipe(map(setVersion('published'))), }, - complete: () => listenerEventsConnector.complete(), } } diff --git a/packages/sanity/src/core/store/_legacy/document/document-pair/memoizedPair.ts b/packages/sanity/src/core/store/_legacy/document/document-pair/memoizedPair.ts index d383dda5900..dfeb7cf8d25 100644 --- a/packages/sanity/src/core/store/_legacy/document/document-pair/memoizedPair.ts +++ b/packages/sanity/src/core/store/_legacy/document/document-pair/memoizedPair.ts @@ -1,5 +1,6 @@ import {type SanityClient} from '@sanity/client' -import {Observable, ReplaySubject, share, timer} from 'rxjs' +import {EMPTY, merge, Observable, of, ReplaySubject, share, timer} from 'rxjs' +import {mergeMap} from 'rxjs/operators' import {type PairListenerOptions} from '../getPairListener' import {type IdPair} from '../types' @@ -26,9 +27,13 @@ export const memoizedPair: ( ): Observable => { return new Observable((subscriber) => { const pair = checkoutPair(client, idPair, serverActionsEnabled, pairListenerOptions) - subscriber.next(pair) - - return pair.complete + return merge( + of(pair), + // merge in draft events and published events to makes sure they receive + // the events they need for as long as the pair is subscribed to + pair.draft.events.pipe(mergeMap(() => EMPTY)), + pair.published.events.pipe(mergeMap(() => EMPTY)), + ).subscribe(subscriber) }).pipe( share({ connector: () => new ReplaySubject(1),