Skip to content

Commit

Permalink
fix(sanity): fix race condition regression introduced by #8120
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Jan 8, 2025
1 parent 1e12bc9 commit ae855bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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} from 'rxjs'
import {EMPTY, from, merge, type Observable, Subject} from 'rxjs'
import {filter, map, mergeMap, share, take, tap} from 'rxjs/operators'

import {
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface Pair {
transactionsPendingEvents$: Observable<PendingMutationsEvent>
published: DocumentVersion
draft: DocumentVersion
_keepalive: Observable<never>
complete: () => void
}

function setVersion<T>(version: 'draft' | 'published') {
Expand Down Expand Up @@ -204,7 +204,10 @@ export function checkoutPair(
): Pair {
const {publishedId, draftId} = idPair

const listenerEvents$ = getPairListener(client, idPair, pairListenerOptions).pipe(share())
const listenerEventsConnector = new Subject<ListenerEvent>()
const listenerEvents$ = getPairListener(client, idPair, pairListenerOptions).pipe(
share({connector: () => listenerEventsConnector}),
)

const reconnect$ = listenerEvents$.pipe(
filter((ev) => ev.type === 'reconnect'),
Expand Down Expand Up @@ -252,8 +255,6 @@ export function checkoutPair(
consistency$: published.consistency$,
remoteSnapshot$: published.remoteSnapshot$.pipe(map(setVersion('published'))),
},
// Use this to keep the mutation pipeline active.
// It won't ever emit any events, but it will prevent the eventsource connection from completing for as long as it is subscribed to
_keepalive: merge(listenerEvents$, commits$).pipe(mergeMap(() => EMPTY)),
complete: () => listenerEventsConnector.complete(),
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {type SanityClient} from '@sanity/client'
import {merge, type Observable, of, ReplaySubject, share, timer} from 'rxjs'
import {Observable, ReplaySubject, share, timer} from 'rxjs'

import {type PairListenerOptions} from '../getPairListener'
import {type IdPair} from '../types'
Expand All @@ -24,12 +24,12 @@ export const memoizedPair: (
serverActionsEnabled: Observable<boolean>,
pairListenerOptions?: PairListenerOptions,
): Observable<Pair> => {
const pair = checkoutPair(client, idPair, serverActionsEnabled, pairListenerOptions)
return merge(
of(pair),
// makes sure the pair listener is kept alive for as long as there are subscribers
pair._keepalive,
).pipe(
return new Observable<Pair>((subscriber) => {
const pair = checkoutPair(client, idPair, serverActionsEnabled, pairListenerOptions)
subscriber.next(pair)

return pair.complete
}).pipe(
share({
connector: () => new ReplaySubject(1),
resetOnComplete: true,
Expand Down

0 comments on commit ae855bd

Please sign in to comment.