Skip to content

Commit

Permalink
fix(core): clean up edit state listeners (#5911)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler authored Mar 6, 2024
1 parent b625213 commit 4acc11e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
7 changes: 4 additions & 3 deletions packages/sanity/src/core/hooks/useEditState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {useMemoObservable} from 'react-rx'
import {merge, timer} from 'rxjs'
import {debounce, share, skip, take} from 'rxjs/operators'
import {debounce, merge, share, skip, take, timer} from 'rxjs'

import {type EditStateFor, useDocumentStore} from '../store'

Expand All @@ -13,8 +12,9 @@ export function useEditState(
const documentStore = useDocumentStore()

return useMemoObservable(() => {
const base = documentStore.pair.editState(publishedDocId, docTypeName).pipe(share())
if (priority === 'low') {
const base = documentStore.pair.editState(publishedDocId, docTypeName).pipe(share())

return merge(
base.pipe(take(1)),
base.pipe(
Expand All @@ -23,6 +23,7 @@ export function useEditState(
),
)
}

return documentStore.pair.editState(publishedDocId, docTypeName)
}, [documentStore.pair, publishedDocId, docTypeName, priority]) as EditStateFor
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {type SanityClient} from '@sanity/client'
import {type Mutation} from '@sanity/mutator'
import {type SanityDocument} from '@sanity/types'
import {EMPTY, from, merge, type Observable} from 'rxjs'
import {EMPTY, from, merge, type Observable, Subject} from 'rxjs'
import {filter, map, mergeMap, mergeMapTo, share, tap} from 'rxjs/operators'

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

function setVersion<T>(version: 'draft' | 'published') {
Expand Down Expand Up @@ -105,7 +106,10 @@ function submitCommitRequest(client: SanityClient, request: CommitRequest) {
export function checkoutPair(client: SanityClient, idPair: IdPair): Pair {
const {publishedId, draftId} = idPair

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

const reconnect$ = listenerEvents$.pipe(
filter((ev) => ev.type === 'reconnect'),
Expand Down Expand Up @@ -146,5 +150,6 @@ export function checkoutPair(client: SanityClient, idPair: IdPair): Pair {
consistency$: published.consistency$,
remoteSnapshot$: published.remoteSnapshot$.pipe(map(setVersion('published'))),
},
complete: () => listenerEventsConnector.complete(),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export const memoizedPair: (
) => Observable<Pair> = memoize(
(client: SanityClient, idPair: IdPair, _typeName: string): Observable<Pair> => {
return new Observable<Pair>((subscriber) => {
subscriber.next(checkoutPair(client, idPair))
const pair = checkoutPair(client, idPair)
subscriber.next(pair)

return pair.complete
}).pipe(publishReplay(1), refCount())
},
memoizeKeyGen,
Expand Down

0 comments on commit 4acc11e

Please sign in to comment.