Skip to content

Commit

Permalink
feat(core): use quick-lru for documents edit state
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Sep 27, 2024
1 parent c98ed5e commit 597cbf3
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 279 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {type SanityClient} from '@sanity/client'
import {type SanityDocument, type Schema} from '@sanity/types'
import {combineLatest, defer, merge, type Observable, of} from 'rxjs'
import {finalize, map, publishReplay, refCount, startWith, switchMap, tap} from 'rxjs/operators'
import QuickLRU from 'quick-lru'
import {combineLatest, defer, type Observable, of} from 'rxjs'
import {map, publishReplay, refCount, startWith, switchMap, tap} from 'rxjs/operators'

import {type IdPair, type PendingMutationsEvent} from '../types'
import {memoize} from '../utils/createMemoizer'
import {memoizeKeyGen} from './memoizeKeyGen'
import {snapshotPair} from './snapshotPair'
import {isLiveEditEnabled} from './utils/isLiveEditEnabled'
import {getPairFromLocalStorage, savePairToLocalStorage} from './utils/localStoragePOC'

const cache = new QuickLRU<string, SanityDocument | null>({maxSize: 10})

interface TransactionSyncLockState {
enabled: boolean
Expand Down Expand Up @@ -51,66 +53,47 @@ export const editState = memoize(
transactionSyncLock: null,
}

let cachedDocumentPair: {
draft: SanityDocument | null
published: SanityDocument | null
} | null = null

function getCachedPair() {
// try first read it from memory
// if we haven't got it in memory, see if it's in localstorage
if (cachedDocumentPair) {
return cachedDocumentPair
}
return getPairFromLocalStorage(idPair)
}

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 defer(() => {
return of({
draft: cache.get(idPair.draftId) || null,
published: cache.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),
),
]),
),
]),
),
tap(([draft, published]) => {
cachedDocumentPair = {draft, published}
}),
map(([draftSnapshot, publishedSnapshot, transactionSyncLock]) => ({
id: idPair.publishedId,
type: typeName,
draft: draftSnapshot,
published: publishedSnapshot,
liveEdit,
ready: true,
transactionSyncLock,
})),
// todo: turn this into a proper operator function - It's like startWith only that it takes a function that will be invoked upon subscription
(input$) => {
return defer(() => {
const cachedPair = getCachedPair()
return merge(
cachedPair
? of({
id: idPair.publishedId,
type: typeName,
draft: cachedPair.draft,
published: cachedPair.published,
liveEdit,
ready: false,
transactionSyncLock: null,
})
: of(INITIAL),
input$,
)
})
},
finalize(() => {
savePairToLocalStorage(cachedDocumentPair)
tap(([draftSnapshot, publishedSnapshot]) => {
if (draftSnapshot) {
cache.set(idPair.draftId, draftSnapshot)
} else if (publishedSnapshot) {
cache.set(idPair.publishedId, publishedSnapshot)
}
}),
map(([draftSnapshot, publishedSnapshot, transactionSyncLock]) => ({
id: idPair.publishedId,
type: typeName,
draft: draftSnapshot,
published: publishedSnapshot,
liveEdit,
ready: true,
transactionSyncLock,
})),
startWith({
...INITIAL,
draft: cachePair.draft,
published: cachePair.published,
}),
)
}),
publishReplay(1),
refCount(),
Expand Down

This file was deleted.

Loading

0 comments on commit 597cbf3

Please sign in to comment.