Skip to content

Commit 27feda0

Browse files
authored
fix(sanity): fix race condition introduced by #8120 (#8211)
1 parent 1e12bc9 commit 27feda0

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts

-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export interface Pair {
6565
transactionsPendingEvents$: Observable<PendingMutationsEvent>
6666
published: DocumentVersion
6767
draft: DocumentVersion
68-
_keepalive: Observable<never>
6968
}
7069

7170
function setVersion<T>(version: 'draft' | 'published') {
@@ -252,8 +251,5 @@ export function checkoutPair(
252251
consistency$: published.consistency$,
253252
remoteSnapshot$: published.remoteSnapshot$.pipe(map(setVersion('published'))),
254253
},
255-
// Use this to keep the mutation pipeline active.
256-
// It won't ever emit any events, but it will prevent the eventsource connection from completing for as long as it is subscribed to
257-
_keepalive: merge(listenerEvents$, commits$).pipe(mergeMap(() => EMPTY)),
258254
}
259255
}

packages/sanity/src/core/store/_legacy/document/document-pair/memoizedPair.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {type SanityClient} from '@sanity/client'
2-
import {merge, type Observable, of, ReplaySubject, share, timer} from 'rxjs'
2+
import {EMPTY, merge, Observable, of, ReplaySubject, share, timer} from 'rxjs'
3+
import {mergeMap} from 'rxjs/operators'
34

45
import {type PairListenerOptions} from '../getPairListener'
56
import {type IdPair} from '../types'
@@ -24,12 +25,16 @@ export const memoizedPair: (
2425
serverActionsEnabled: Observable<boolean>,
2526
pairListenerOptions?: PairListenerOptions,
2627
): Observable<Pair> => {
27-
const pair = checkoutPair(client, idPair, serverActionsEnabled, pairListenerOptions)
28-
return merge(
29-
of(pair),
30-
// makes sure the pair listener is kept alive for as long as there are subscribers
31-
pair._keepalive,
32-
).pipe(
28+
return new Observable<Pair>((subscriber) => {
29+
const pair = checkoutPair(client, idPair, serverActionsEnabled, pairListenerOptions)
30+
return merge(
31+
of(pair),
32+
// merge in draft events and published events to makes sure they receive
33+
// the events they need for as long as the pair is subscribed to
34+
pair.draft.events.pipe(mergeMap(() => EMPTY)),
35+
pair.published.events.pipe(mergeMap(() => EMPTY)),
36+
).subscribe(subscriber)
37+
}).pipe(
3338
share({
3439
connector: () => new ReplaySubject(1),
3540
resetOnComplete: true,

0 commit comments

Comments
 (0)