-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve color scheme store perf (#8059)
- Loading branch information
Showing
3 changed files
with
51 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {type StudioThemeColorSchemeKey} from '../theme/types' | ||
|
||
function getScheme(scheme: unknown): StudioThemeColorSchemeKey { | ||
switch (scheme) { | ||
case 'dark': | ||
case 'light': | ||
return scheme | ||
default: | ||
return 'system' | ||
} | ||
} | ||
|
||
/** @internal */ | ||
export const LOCAL_STORAGE_KEY = 'sanityStudio:ui:colorScheme' | ||
|
||
let snapshot: StudioThemeColorSchemeKey | ||
const subscribers = new Set<() => void>() | ||
|
||
/** @internal */ | ||
export const subscribe = (onStoreChange: () => void) => { | ||
if (!snapshot) { | ||
snapshot = getScheme(localStorage.getItem(LOCAL_STORAGE_KEY)) || 'system' | ||
} | ||
subscribers.add(onStoreChange) | ||
return (): void => { | ||
subscribers.delete(onStoreChange) | ||
} | ||
} | ||
/** @internal */ | ||
export function getSnapshot(): StudioThemeColorSchemeKey { | ||
return snapshot | ||
} | ||
/** @internal */ | ||
export function setSnapshot(nextScheme: StudioThemeColorSchemeKey): void { | ||
snapshot = getScheme(nextScheme) | ||
for (const subscription of subscribers) { | ||
subscription() | ||
} | ||
} |