Skip to content

Commit

Permalink
fix(presentation): use live content API for loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jan 29, 2025
1 parent ca0adf9 commit 91cd5cb
Show file tree
Hide file tree
Showing 15 changed files with 739 additions and 1,011 deletions.
6 changes: 0 additions & 6 deletions packages/sanity/package.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import {defineConfig} from '@sanity/pkg-utils'
export default defineConfig({
...baseConfig,

define: {
...baseConfig.define,
PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS:
process.env.PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS === 'true',
},

// Build unexposed bundles for scripts that need to be spawned/used in workers
bundles: [
{
Expand Down
1 change: 0 additions & 1 deletion packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@
"lodash": "^4.17.21",
"log-symbols": "^2.2.0",
"mendoza": "^3.0.0",
"mnemonist": "0.39.8",
"module-alias": "^2.2.2",
"nano-pubsub": "^3.0.0",
"nanoid": "^3.1.30",
Expand Down
31 changes: 10 additions & 21 deletions packages/sanity/src/presentation/PresentationTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {type RouterContextValue, useRouter} from 'sanity/router'
import {styled} from 'styled-components'
import {useEffectEvent} from 'use-effect-event'

import {DEFAULT_TOOL_NAME, EDIT_INTENT_MODE, LIVE_DRAFT_EVENTS_ENABLED} from './constants'
import {DEFAULT_TOOL_NAME, EDIT_INTENT_MODE} from './constants'
import PostMessageFeatures from './features/PostMessageFeatures'
import {debounce} from './lib/debounce'
import {SharedStateProvider} from './overlays/SharedStateProvider'
Expand Down Expand Up @@ -70,7 +70,6 @@ import {usePopups} from './usePopups'
import {usePreviewUrl} from './usePreviewUrl'
import {useStatus} from './useStatus'

const LoaderQueries = lazy(() => import('./loader/LoaderQueries'))
const LiveQueries = lazy(() => import('./loader/LiveQueries'))
const PostMessageDocuments = lazy(() => import('./overlays/PostMessageDocuments'))
const PostMessageRefreshMutations = lazy(() => import('./editor/PostMessageRefreshMutations'))
Expand Down Expand Up @@ -582,25 +581,15 @@ export default function PresentationTool(props: {
</PresentationNavigateProvider>
</PresentationProvider>
<Suspense>
{controller &&
(LIVE_DRAFT_EVENTS_ENABLED ? (
<LiveQueries
controller={controller}
perspective={perspective}
liveDocument={displayedDocument}
onDocumentsOnPage={setDocumentsOnPage}
onLoadersConnection={setLoadersConnection}
/>
) : (
<LoaderQueries
controller={controller}
perspective={perspective}
liveDocument={displayedDocument}
onDocumentsOnPage={setDocumentsOnPage}
onLoadersConnection={setLoadersConnection}
documentsOnPage={documentsOnPage}
/>
))}
{controller && (
<LiveQueries
controller={controller}
perspective={perspective}
liveDocument={displayedDocument}
onDocumentsOnPage={setDocumentsOnPage}
onLoadersConnection={setLoadersConnection}
/>
)}
{visualEditingComlink && params.id && params.type && (
<PostMessageRefreshMutations
comlink={visualEditingComlink}
Expand Down
17 changes: 2 additions & 15 deletions packages/sanity/src/presentation/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,8 @@ export const API_VERSION = '2023-10-16'
// Heartbeats shouldn't fire on intervals that are so short it causes performance issues
export const MIN_LOADER_QUERY_LISTEN_HEARTBEAT_INTERVAL = 1000 // ms

// Batch size for fetching documents building up the cache
export const LIVE_QUERY_CACHE_BATCH_SIZE = 100

// Total cache size for documents that are live queried
export const LIVE_QUERY_CACHE_SIZE = 2048
// How often the list over loader queries should be garbage collected
export const LOADER_QUERY_GC_INTERVAL = 30_000 // ms

// The interval at which we check if existing popups have been closed
export const POPUP_CHECK_INTERVAL = 1000 // ms

declare global {
const PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS: unknown
}

// Feature flag that uses the new Live Draft Content API instead of the Listen API + Mendoza events
export const LIVE_DRAFT_EVENTS_ENABLED =
typeof PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS === 'string'
? PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS === 'true'
: false
Loading

0 comments on commit 91cd5cb

Please sign in to comment.