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 committed Oct 1, 2024
1 parent 1b52fad commit e1fda2c
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 204 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 {type DocumentsStorage} from '../documentsStorage'
import {createSWR} from '../../../../util/rxSwr'
import {type IdPair, type PendingMutationsEvent} from '../types'
import {memoize} from '../utils/createMemoizer'
import {memoizeKeyGen} from './memoizeKeyGen'
Expand All @@ -14,6 +14,8 @@ interface TransactionSyncLockState {
enabled: boolean
}

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

/**
* @hidden
* @beta */
Expand All @@ -36,59 +38,41 @@ export const editState = memoize(
client: SanityClient
schema: Schema
serverActionsEnabled: Observable<boolean>
storage: DocumentsStorage
},
idPair: IdPair,
typeName: string,
): Observable<EditStateFor> => {
const liveEdit = isLiveEditEnabled(ctx.schema, typeName)
const storage = ctx.storage

return defer(() => {
return of({
draft: storage.get(idPair.draftId) || null,
published: storage.get(idPair.publishedId) || null,
})
}).pipe(
switchMap((cachePair) => {
return snapshotPair(ctx.client, idPair, typeName, ctx.serverActionsEnabled).pipe(
switchMap((versions) =>
combineLatest([
versions.draft.snapshots$,
versions.published.snapshots$,
versions.transactionsPendingEvents$.pipe(
// eslint-disable-next-line max-nested-callbacks
map((ev: PendingMutationsEvent) => (ev.phase === 'begin' ? LOCKED : NOT_LOCKED)),
startWith(NOT_LOCKED),
),
]),
return snapshotPair(ctx.client, idPair, typeName, ctx.serverActionsEnabled).pipe(
switchMap((versions) =>
combineLatest([
versions.draft.snapshots$,
versions.published.snapshots$,
versions.transactionsPendingEvents$.pipe(
map((ev: PendingMutationsEvent) => (ev.phase === 'begin' ? LOCKED : NOT_LOCKED)),
startWith(NOT_LOCKED),
),
tap(([draftSnapshot, publishedSnapshot]) => {
if (draftSnapshot) {
storage.save(idPair.draftId, draftSnapshot)
} else if (publishedSnapshot) {
storage.save(idPair.publishedId, publishedSnapshot)
}
}),
map(([draftSnapshot, publishedSnapshot, transactionSyncLock]) => ({
id: idPair.publishedId,
type: typeName,
draft: draftSnapshot,
published: publishedSnapshot,
liveEdit,
ready: true,
transactionSyncLock,
})),
startWith({
id: idPair.publishedId,
type: typeName,
draft: cachePair.draft,
published: cachePair.published,
liveEdit,
ready: false,
transactionSyncLock: null,
}),
)
]),
),
swr(`${idPair.publishedId}-${idPair.draftId}`),
map(({value: [draftSnapshot, publishedSnapshot, transactionSyncLock], fromCache}) => ({
id: idPair.publishedId,
type: typeName,
draft: draftSnapshot,
published: publishedSnapshot,
liveEdit,
ready: !fromCache,
transactionSyncLock: fromCache ? null : transactionSyncLock,
})),
startWith({
id: idPair.publishedId,
type: typeName,
draft: null,
published: null,
liveEdit,
ready: false,
transactionSyncLock: null,
}),
publishReplay(1),
refCount(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {type SourceClientOptions} from '../../../../config'
import {type LocaleSource} from '../../../../i18n'
import {type DraftsModelDocumentAvailability} from '../../../../preview'
import {validateDocumentWithReferences, type ValidationStatus} from '../../../../validation'
import {type DocumentsStorage} from '../documentsStorage'
import {type IdPair} from '../types'
import {memoize} from '../utils/createMemoizer'
import {editState} from './editState'
Expand All @@ -32,7 +31,6 @@ export const validation = memoize(
schema: Schema
i18n: LocaleSource
serverActionsEnabled: Observable<boolean>
storage: DocumentsStorage
},
{draftId, publishedId}: IdPair,
typeName: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
} from './document-pair/operationEvents'
import {type OperationsAPI} from './document-pair/operations'
import {validation} from './document-pair/validation'
import {createDocumentsStorage} from './documentsStorage'
import {getInitialValueStream, type InitialValueMsg, type InitialValueOptions} from './initialValue'
import {listenQuery, type ListenQueryOptions} from './listenQuery'
import {resolveTypeForDocument} from './resolveTypeForDocument'
import {type IdPair} from './types'

/**
* @hidden
* @beta */
Expand Down Expand Up @@ -104,7 +104,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 @@ -113,7 +113,6 @@ export function createDocumentStore({
schema,
i18n,
serverActionsEnabled,
storage,
}

return {
Expand Down Expand Up @@ -157,10 +156,7 @@ export function createDocumentStore({
return editOperations(ctx, getIdPairFromPublished(publishedId), type)
},
editState(publishedId, type) {
const idPair = getIdPairFromPublished(publishedId)

const edit = editState(ctx, idPair, type)
return edit
return editState(ctx, getIdPairFromPublished(publishedId), type)
},
operationEvents(publishedId, type) {
return operationEvents({
Expand All @@ -182,8 +178,7 @@ export function createDocumentStore({
)
},
validation(publishedId, type) {
const idPair = getIdPairFromPublished(publishedId)
return validation(ctx, idPair, type)
return validation(ctx, getIdPairFromPublished(publishedId), type)
},
},
}
Expand Down

This file was deleted.

Loading

0 comments on commit e1fda2c

Please sign in to comment.