Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preview): allow null as valid cache/memo value for preview fields #7551

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/sanity/src/core/preview/__test__/observeFields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {describe, expect, it} from '@jest/globals'
import {firstValueFrom, of, Subject} from 'rxjs'
import {take, tap} from 'rxjs/operators'

import {type ClientLike, createObserveFields} from '../observeFields'
import {type InvalidationChannelEvent} from '../types'

describe('observeFields', () => {
it('should cache the last known value and emit sync', async () => {
const client: ClientLike = {
observable: {
fetch: (query) => {
expect(query).toEqual('[*[_id in ["foo"]][0...1]{_id,_rev,_type,bar}][0...1]')
return of([
[
// no result
],
])
},
},
withConfig: () => client,
}

const invalidationChannel = new Subject<InvalidationChannelEvent>()
const observeFields = createObserveFields({
invalidationChannel,
client,
})
const first = firstValueFrom(observeFields('foo', ['bar']).pipe(take(1)))
invalidationChannel.next({type: 'connected'})

expect(await first).toMatchInlineSnapshot(`null`)

// After we got first value from server and it turned out to be `null`, we should have `null` as the memoized sync value
let syncValue = undefined
observeFields('foo', ['bar'])
.pipe(
tap((value) => {
syncValue = value
}),
take(1),
)
.subscribe()
.unsubscribe()
expect(syncValue).toBe(null)
})
})
27 changes: 21 additions & 6 deletions packages/sanity/src/core/preview/observeFields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {type SanityClient} from '@sanity/client'
import {difference, flatten, memoize} from 'lodash'
import {
combineLatest,
Expand Down Expand Up @@ -46,14 +45,29 @@ type Cache = {
[id: string]: CachedFieldObserver[]
}

/**
* Note: this should be the minimal interface createObserveFields needs to function
* It should be kept compatible with the Sanity Client
*/
export interface ClientLike {
withConfig(config: ApiConfig): ClientLike
observable: {
fetch: (
query: string,
params: Record<string, string>,
options: {tag: string},
) => Observable<unknown>
}
}

/**
* Creates a function that allows observing individual fields on a document.
* It will automatically debounce and batch requests, and maintain an in-memory cache of the latest field values
* @param options - Options to use when creating the observer
* @internal
*/
export function createObserveFields(options: {
client: SanityClient
client: ClientLike
invalidationChannel: Observable<InvalidationChannelEvent>
}) {
const {client: currentDatasetClient, invalidationChannel} = options
Expand All @@ -63,11 +77,11 @@ export function createObserveFields(options: {
)
}

function fetchAllDocumentPathsWith(client: SanityClient) {
function fetchAllDocumentPathsWith(client: ClientLike) {
return function fetchAllDocumentPath(selections: Selection[]) {
const combinedSelections = combineSelections(selections)
return client.observable
.fetch(toQuery(combinedSelections), {}, {tag: 'preview.document-paths'} as any)
.fetch(toQuery(combinedSelections), {}, {tag: 'preview.document-paths'})
.pipe(map((result: any) => reassemble(result, combinedSelections)))
}
}
Expand Down Expand Up @@ -140,9 +154,10 @@ export function createObserveFields(options: {
fields: FieldName[],
apiConfig?: ApiConfig,
): CachedFieldObserver {
let latest: T | null = null
// Note: `undefined` means the memo has not been set, while `null` means the memo is explicitly set to null (e.g. we did fetch, but got null back)
let latest: T | undefined | null = undefined
const changes$ = merge(
defer(() => (latest === null ? EMPTY : observableOf(latest))),
defer(() => (latest === undefined ? EMPTY : observableOf(latest))),
(apiConfig
? (crossDatasetListenFields(id, fields, apiConfig) as any)
: currentDatasetListenFields(id, fields)) as Observable<T>,
Expand Down
Loading