From 00fb5d6512a24a2ed857ed0c01b4afd20c026417 Mon Sep 17 00:00:00 2001 From: Herman Wikner Date: Thu, 27 Oct 2022 15:15:16 +0200 Subject: [PATCH] fix(react-hooks): issue with `useEditState` low priority --- .../@sanity/react-hooks/src/useEditState.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/@sanity/react-hooks/src/useEditState.ts b/packages/@sanity/react-hooks/src/useEditState.ts index 933fe33a94d..8ad75521b12 100644 --- a/packages/@sanity/react-hooks/src/useEditState.ts +++ b/packages/@sanity/react-hooks/src/useEditState.ts @@ -4,8 +4,8 @@ import type {EditStateFor} from '@sanity/base/_internal' import documentStore from 'part:@sanity/base/datastore/document' import {useMemoObservable} from 'react-rx' -import {merge, timer} from 'rxjs' -import {debounce, share, skip, take} from 'rxjs/operators' +import {timer, of} from 'rxjs' +import {map, share, switchMap} from 'rxjs/operators' export function useEditState( publishedDocId: string, @@ -14,15 +14,17 @@ export function useEditState( ): EditStateFor { return useMemoObservable(() => { const base = documentStore.pair.editState(publishedDocId, docTypeName).pipe(share()) + if (priority === 'low') { - return merge( - base.pipe(take(1)), - base.pipe( - skip(1), - debounce(() => timer(1000)) - ) + return base.pipe( + switchMap((editState, index) => { + if (index === 0) return of(editState) + + return timer(1000).pipe(map(() => editState)) + }) ) } + return base }, [publishedDocId, docTypeName, priority]) as EditStateFor }