Skip to content

Commit

Permalink
fix(react-hooks): issue with useEditState low priority
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanwikner committed Oct 28, 2022
1 parent 3dbbd9e commit 00fb5d6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/@sanity/react-hooks/src/useEditState.ts
Expand Up @@ -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,
Expand All @@ -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
}

2 comments on commit 00fb5d6

@vercel
Copy link

@vercel vercel bot commented on 00fb5d6 Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

studio-workshop – ./

studio-workshop.sanity.build
studio-workshop-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 00fb5d6 Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.