Skip to content

Commit

Permalink
chore(core): update editState to use createSWR cache
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin authored and bjoerge committed Oct 8, 2024
1 parent 15eba8e commit dba471c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {type SanityClient} from '@sanity/client'
import {type SanityDocument, type Schema} from '@sanity/types'
import {combineLatest, defer, type Observable, of} from 'rxjs'
import {map, publishReplay, refCount, startWith, switchMap, tap} from 'rxjs/operators'
import {combineLatest, type Observable} from 'rxjs'
import {map, publishReplay, refCount, startWith, switchMap} from 'rxjs/operators'

import {createSWR} from '../../../../util/rxSwr'
import {type PairListenerOptions} from '../getPairListener'
import {type DocumentsStorage} from '../documentsStorage'
import {type IdPair, type PendingMutationsEvent} from '../types'
import {memoize} from '../utils/createMemoizer'
import {memoizeKeyGen} from './memoizeKeyGen'
Expand All @@ -15,6 +15,8 @@ interface TransactionSyncLockState {
enabled: boolean
}

const swr = createSWR<[SanityDocument, SanityDocument, TransactionSyncLockState]>({maxSize: 50})

/**
* @hidden
* @beta */
Expand Down Expand Up @@ -43,6 +45,7 @@ export const editState = memoize(
typeName: string,
): Observable<EditStateFor> => {
const liveEdit = isLiveEditEnabled(ctx.schema, typeName)

return snapshotPair(
ctx.client,
idPair,
Expand All @@ -60,14 +63,15 @@ export const editState = memoize(
),
]),
),
map(([draftSnapshot, publishedSnapshot, transactionSyncLock]) => ({
swr(`${idPair.publishedId}-${idPair.draftId}`),
map(({value: [draftSnapshot, publishedSnapshot, transactionSyncLock], fromCache}) => ({
id: idPair.publishedId,
type: typeName,
draft: draftSnapshot,
published: publishedSnapshot,
liveEdit,
ready: true,
transactionSyncLock,
ready: !fromCache,
transactionSyncLock: fromCache ? null : transactionSyncLock,
})),
startWith({
id: idPair.publishedId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {getInitialValueStream, type InitialValueMsg, type InitialValueOptions} f
import {listenQuery, type ListenQueryOptions} from './listenQuery'
import {resolveTypeForDocument} from './resolveTypeForDocument'
import {type IdPair} from './types'

/**
* @hidden
* @beta */
Expand Down Expand Up @@ -108,7 +109,7 @@ export function createDocumentStore({
// internal operations, and a `getClient` method that we expose to user-land
// for things like validations
const client = getClient(DEFAULT_STUDIO_CLIENT_OPTIONS)
const storage = createDocumentsStorage()

const ctx = {
client,
getClient,
Expand All @@ -118,7 +119,6 @@ export function createDocumentStore({
i18n,
serverActionsEnabled,
pairListenerOptions,
storage,
}

return {
Expand Down

This file was deleted.

0 comments on commit dba471c

Please sign in to comment.