From 79bc6c866b43db676d4a2aa1960c4994308d14e3 Mon Sep 17 00:00:00 2001 From: pedrobonamin Date: Tue, 1 Oct 2024 08:44:57 +0200 Subject: [PATCH] chore(core): update editState to use createSWR cache --- .../document/document-pair/editState.ts | 82 ++++++++----------- .../document/document-pair/validation.ts | 2 - .../store/_legacy/document/document-store.ts | 3 - .../_legacy/document/documentsStorage.ts | 21 ----- 4 files changed, 33 insertions(+), 75 deletions(-) delete mode 100644 packages/sanity/src/core/store/_legacy/document/documentsStorage.ts diff --git a/packages/sanity/src/core/store/_legacy/document/document-pair/editState.ts b/packages/sanity/src/core/store/_legacy/document/document-pair/editState.ts index 6bb0ac511d7..6f2774aa2ba 100644 --- a/packages/sanity/src/core/store/_legacy/document/document-pair/editState.ts +++ b/packages/sanity/src/core/store/_legacy/document/document-pair/editState.ts @@ -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' @@ -14,6 +14,8 @@ interface TransactionSyncLockState { enabled: boolean } +const swr = createSWR<[SanityDocument, SanityDocument, TransactionSyncLockState]>({maxSize: 50}) + /** * @hidden * @beta */ @@ -36,59 +38,41 @@ export const editState = memoize( client: SanityClient schema: Schema serverActionsEnabled: Observable - storage: DocumentsStorage }, idPair: IdPair, typeName: string, ): Observable => { 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) => { + return 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(), diff --git a/packages/sanity/src/core/store/_legacy/document/document-pair/validation.ts b/packages/sanity/src/core/store/_legacy/document/document-pair/validation.ts index c3db6550fc9..8cf895fe80a 100644 --- a/packages/sanity/src/core/store/_legacy/document/document-pair/validation.ts +++ b/packages/sanity/src/core/store/_legacy/document/document-pair/validation.ts @@ -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' @@ -32,7 +31,6 @@ export const validation = memoize( schema: Schema i18n: LocaleSource serverActionsEnabled: Observable - storage: DocumentsStorage }, {draftId, publishedId}: IdPair, typeName: string, diff --git a/packages/sanity/src/core/store/_legacy/document/document-store.ts b/packages/sanity/src/core/store/_legacy/document/document-store.ts index dceea95b7a4..30b3b7faec8 100644 --- a/packages/sanity/src/core/store/_legacy/document/document-store.ts +++ b/packages/sanity/src/core/store/_legacy/document/document-store.ts @@ -23,7 +23,6 @@ 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' @@ -104,7 +103,6 @@ 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, @@ -113,7 +111,6 @@ export function createDocumentStore({ schema, i18n, serverActionsEnabled, - storage, } return { diff --git a/packages/sanity/src/core/store/_legacy/document/documentsStorage.ts b/packages/sanity/src/core/store/_legacy/document/documentsStorage.ts deleted file mode 100644 index 5da6aa16c74..00000000000 --- a/packages/sanity/src/core/store/_legacy/document/documentsStorage.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {type SanityDocument} from '@sanity/types' -import QuickLRU from 'quick-lru' - -export interface DocumentsStorage { - save: (id: string, doc: SanityDocument) => void - get: (id: string) => SanityDocument | null -} - -export function createDocumentsStorage(): DocumentsStorage { - const documentsCache = new QuickLRU({ - maxSize: 50, - }) - return { - save(id, doc) { - documentsCache.set(id, doc) - }, - get(id) { - return documentsCache.get(id) || null - }, - } -}