Skip to content

Commit

Permalink
fix: optimistically set the mutator actor
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Dec 20, 2024
1 parent 37f7786 commit 5347ac0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/visual-editing/src/optimistic/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function isEmptyActor(actor: MutatorActor | EmptyActor): actor is EmptyAc
return actor === emptyActor
}

export function setActor(nextActor: MutatorActor): void {
export function setActor(nextActor: MutatorActor | EmptyActor): void {
actor = nextActor
for (const onActorChange of listeners) {
onActorChange()
Expand Down
11 changes: 8 additions & 3 deletions packages/visual-editing/src/ui/useDatasetMutator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useEffect, useState} from 'react'
import {createActor} from 'xstate'
import {setActor, type MutatorActor} from '../optimistic/context'
import {emptyActor, setActor, type MutatorActor} from '../optimistic/context'
import {createSharedListener} from '../optimistic/state/createSharedListener'
import {createDatasetMutator} from '../optimistic/state/datasetMutator'
import type {VisualEditingNode} from '../types'
Expand All @@ -25,6 +25,8 @@ export function useDatasetMutator(

setMutator(mutator)
mutator.start()
// Optimistically set the mutator
setActor(mutator)

return () => {
mutator.stop()
Expand All @@ -43,11 +45,14 @@ export function useDatasetMutator(
suppressWarnings: true,
})
.then((data) => {
if (data.features['optimistic']) {
setActor(mutator)
if (featuresFetch.signal.aborted) return
if (!data.features['optimistic']) {
setActor(emptyActor)
}
})
.catch(() => {
if (featuresFetch.signal.aborted) return
setActor(emptyActor)
// eslint-disable-next-line no-console
console.warn(
'[@sanity/visual-editing] Package version mismatch detected: Please update your Sanity studio to prevent potential compatibility issues.',
Expand Down

0 comments on commit 5347ac0

Please sign in to comment.