Skip to content

Commit

Permalink
[themes] Potentially fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Apr 12, 2024
1 parent 85b2261 commit dbfe3bf
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/lib/managers/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export async function fetchTheme(id: string, selected = false) {
// TODO: Should we prompt when the selected theme is updated?
if (selected) {
writeTheme(themes[id]);
applyTheme(themes[id], vdThemeFallback, true);
applyTheme(themes[id], vdThemeFallback);
}
}

Expand Down Expand Up @@ -233,6 +233,7 @@ let vdKey = "vd-theme";
let vdThemeFallback = "darker";
let enabled = false;
let currentTheme: Theme | null;
let storageResolved = false;

const discordThemes = new Set(["darker", "midnight", "dark", "light"]);
function isDiscordTheme(name: string) {
Expand Down Expand Up @@ -260,19 +261,22 @@ function patchColor() {
ThemeStore.addChangeListener(() => {
if (ThemeStore.theme) {
enabled = ThemeStore.theme === vdKey;
if (ThemeStore.theme !== vdKey) {
if (!enabled) {
selectTheme(null);
vdThemeFallback = ThemeStore.theme;
}
}
});

after("get", mmkvStorage, ([a], ret) => {
if (a === "SelectivelySyncedUserSettingsStore") {
storageResolved = true;
if (ret?._state?.appearance?.settings?.theme && enabled) {
vdThemeFallback = ret._state.appearance.settings.theme;
ret._state.appearance.settings.theme = vdKey;
}
} else if (a === "ThemeStore") {
storageResolved = true;
if (ret?._state?.theme && enabled) {
vdThemeFallback = ret._state.theme;
ret._state.theme = vdKey;
Expand Down Expand Up @@ -302,7 +306,6 @@ function patchColor() {
if (value._state?.theme) {
const { theme } = value._state;
if (isDiscordTheme(theme)) {
selectTheme(null);
vdThemeFallback = theme;
} else {
value._state.theme = vdThemeFallback;
Expand Down Expand Up @@ -348,14 +351,14 @@ function patchColor() {
function getDefaultFallbackTheme(fallback: string = vdThemeFallback) {
const theme = ThemeStore.theme.toLowerCase() as string;

if (theme === "darker" || theme === "midnight" || theme === "dark" || theme === "light") {
if (isDiscordTheme(theme)) {
return theme;
} else {
return fallback;
}
}

export function applyTheme(appliedTheme: Theme | null, fallbackTheme?: string, update = true) {
export function applyTheme(appliedTheme: Theme | null, fallbackTheme?: string) {
if (!fallbackTheme) fallbackTheme = getDefaultFallbackTheme();

currentTheme = appliedTheme;
Expand All @@ -376,7 +379,7 @@ export function applyTheme(appliedTheme: Theme | null, fallbackTheme?: string, u
});
}

if (update) {
if (storageResolved) {
appearanceManager.setShouldSyncAppearanceSettings(false);
appearanceManager.updateTheme(appliedTheme ? vdKey : fallbackTheme);
}
Expand All @@ -387,10 +390,10 @@ export function applyTheme(appliedTheme: Theme | null, fallbackTheme?: string, u
*/
export async function initThemes() {
const currentTheme = getThemeFromLoader();
enabled = !!currentTheme;
enabled = Boolean(currentTheme);

patchColor();
applyTheme(currentTheme, vdThemeFallback, false);
applyTheme(currentTheme, vdThemeFallback);

updateThemes().catch(e => console.error("Failed to update themes", e));
}
Expand Down

0 comments on commit dbfe3bf

Please sign in to comment.