Skip to content

Commit

Permalink
feat(datastores): allow listenQuery to have separate listen and fetch…
Browse files Browse the repository at this point in the history
… queries

Listeners go into a mode where if the expression cannot be parsed to an optimizible filter,
it will emit events for every document. This can cause quite significantly more refetches
to be performed compared to the intended listener that was set up
  • Loading branch information
rexxars committed Aug 14, 2022
1 parent 93081f5 commit 9c37c86
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/sanity/src/datastores/document/document-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ function getIdPairFromPublished(publishedId: string): IdPair {
export interface DocumentStore {
checkoutPair: (idPair: IdPair) => Pair
initialValue: (opts: InitialValueOptions) => Observable<InitialValueMsg>
listenQuery: (query: string, params: QueryParams, options: ListenQueryOptions) => Observable<any>
listenQuery: (
query: string | {fetch: string; listen: string},
params: QueryParams,
options: ListenQueryOptions
) => Observable<any>
resolveTypeForDocument: (id: string, specifiedType?: string) => Observable<string>

pair: {
Expand Down
9 changes: 6 additions & 3 deletions packages/sanity/src/datastores/document/listenQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ function isWelcomeEvent(
// todo: optimize by patching collection in-place
export const listenQuery = (
client: SanityClient,
query: string,
query: string | {fetch: string; listen: string},
params: ListenQueryParams = {},
options: ListenQueryOptions = {}
) => {
const fetchOnce$ = fetch(client, query, params, options)
const fetchQuery = typeof query === 'string' ? query : query.fetch
const listenerQuery = typeof query === 'string' ? query : query.listen

const fetchOnce$ = fetch(client, fetchQuery, params, options)

const events$ = listen(client, query, params, options).pipe(
const events$ = listen(client, listenerQuery, params, options).pipe(
mergeMap((ev, i) => {
const isFirst = i === 0
if (isFirst && !isWelcomeEvent(ev)) {
Expand Down

0 comments on commit 9c37c86

Please sign in to comment.