Skip to content

Commit 198cd74

Browse files
committed
fix(themer): unset the theme for the config theme, persist the snapshot in step
The crossfader compared `resolveActiveThemeOptions`, which returns `undefined` when the Studio's configured theme is the active one, to `null`: `unset-theme` never ran, and the `undefined` that `set-theme` stored instead had `buildTheme` replace the configured theme with the stock one. The persisted snapshot is now written as the machine writes the state it restores from, rather than when the browser next idles — a snapshot that lagged behind was still the one the actor restored from after a reload, and the machine then persisted those older themes over the newer ones.
1 parent 9bffed5 commit 198cd74

1 file changed

Lines changed: 9 additions & 23 deletions

File tree

‎plugins/@sanity/themer/src/tool/ThemerLayout.tsx‎

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ function ThemerThemeCrossfader({baseOptions}: {baseOptions: BuildThemeOptions})
301301
if (switching) {
302302
addThemerTransitionType('crossfade')
303303
}
304-
if (theme === null) {
304+
if (theme === undefined) {
305305
dispatch({type: 'unset-theme'})
306306
} else {
307307
dispatch({type: 'set-theme', theme})
@@ -335,31 +335,17 @@ function RevokeImageUrls() {
335335
function PersistActorSnapshot() {
336336
const actorRef = use(ToolActorRefContext)
337337

338+
// The snapshot is written in the same change as the state the machine's
339+
// `persist` writes, and never later: a snapshot that lags behind is the one
340+
// the actor restores from on the next load, and the machine would then
341+
// persist those older themes over the newer ones that were stored
342+
// @TODO use a selector or similar to only subscribe to changes that are relevant to the persisted snapshot
343+
// @TODO use WebLocks and BroadcastChannel to ensure the snapshot is persisted in a consistent way cross tabs
338344
useEffect(() => {
339-
let cancel: any
340-
function cancelOrClear() {
341-
if ('cancelIdleCallback' in window) {
342-
cancelIdleCallback(cancel)
343-
} else {
344-
clearTimeout(cancel)
345-
}
346-
}
347-
348-
// @TODO use a selector or similar to only subscribe to changes that are relevant to the persisted snapshot
349-
// @TODO use WebLocks and BroadcastChannel to ensure the snapshot is persisted in a consistent way cross tabs
350345
const subscription = actorRef.subscribe(() => {
351-
cancelOrClear()
352-
const callback = () => {
353-
const persistedSnapshot = actorRef.getPersistedSnapshot()
354-
writePersistedSnapshot(persistedSnapshot)
355-
}
356-
cancel =
357-
'cancelIdleCallback' in window ? requestIdleCallback(callback) : setTimeout(callback, 1_000)
346+
writePersistedSnapshot(actorRef.getPersistedSnapshot())
358347
})
359-
return () => {
360-
subscription.unsubscribe()
361-
cancelOrClear()
362-
}
348+
return () => subscription.unsubscribe()
363349
}, [actorRef])
364350

365351
return null

0 commit comments

Comments
 (0)