Skip to content

Commit

Permalink
fix: issue with deactivate all themes on signout (#804)
Browse files Browse the repository at this point in the history
* fix: issue with deactivate all themes on signout

* fix: remove redundant caching
  • Loading branch information
moughxyz authored Jan 7, 2022
1 parent 561ebca commit 713be82
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions app/assets/javascripts/services/themeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ContentType,
UuidString,
FeatureStatus,
PayloadSource,
} from '@standardnotes/snjs';

const CACHED_THEMES_KEY = 'cachedThemes';
Expand All @@ -22,6 +23,11 @@ export class ThemeManager extends ApplicationService {
super.onAppEvent(event);
if (event === ApplicationEvent.SignedOut) {
this.deactivateAllThemes();
this.activeThemes = [];
this.application?.removeValue(
CACHED_THEMES_KEY,
StorageValueModes.Nonwrapped
);
} else if (event === ApplicationEvent.StorageReady) {
await this.activateCachedThemes();
} else if (event === ApplicationEvent.FeaturesUpdated) {
Expand All @@ -34,7 +40,7 @@ export class ThemeManager extends ApplicationService {
}

deinit() {
this.clearAppThemeState();
this.deactivateAllThemes();
this.activeThemes.length = 0;
this.unregisterDesktop();
this.unregisterStream();
Expand All @@ -43,7 +49,8 @@ export class ThemeManager extends ApplicationService {
super.deinit();
}

reloadThemeStatus(): void {
private reloadThemeStatus(): void {
let hasChange = false;
for (const themeUuid of this.activeThemes) {
const theme = this.application.findItem(themeUuid) as SNTheme;
if (
Expand All @@ -52,8 +59,13 @@ export class ThemeManager extends ApplicationService {
FeatureStatus.Entitled
) {
this.deactivateTheme(themeUuid);
hasChange = true;
}
}

if (hasChange) {
this.cacheThemeState();
}
}

/** @override */
Expand All @@ -64,9 +76,8 @@ export class ThemeManager extends ApplicationService {

private async activateCachedThemes() {
const cachedThemes = await this.getCachedThemes();
const writeToCache = false;
for (const theme of cachedThemes) {
this.activateTheme(theme, writeToCache);
this.activateTheme(theme);
}
}

Expand All @@ -78,40 +89,37 @@ export class ThemeManager extends ApplicationService {
this.deactivateTheme(component.uuid);
setTimeout(() => {
this.activateTheme(component as SNTheme);
this.cacheThemeState();
}, 10);
}
});

this.unregisterStream = this.application.streamItems(
ContentType.Theme,
() => {
const themes = this.application.getDisplayableItems(
ContentType.Theme
) as SNTheme[];
(items, source) => {
const themes = items as SNTheme[];
for (const theme of themes) {
if (theme.active) {
this.activateTheme(theme);
} else {
this.deactivateTheme(theme.uuid);
}
}
if (source !== PayloadSource.LocalRetrieved) {
this.cacheThemeState();
}
}
);
}

private clearAppThemeState() {
for (const uuid of this.activeThemes) {
this.deactivateTheme(uuid, false);
}
}

private deactivateAllThemes() {
this.clearAppThemeState();
this.activeThemes = [];
this.decacheThemes();
const activeThemes = this.activeThemes.slice();
for (const uuid of activeThemes) {
this.deactivateTheme(uuid);
}
}

private activateTheme(theme: SNTheme, writeToCache = true) {
private activateTheme(theme: SNTheme) {
if (this.activeThemes.find((uuid) => uuid === theme.uuid)) {
return;
}
Expand All @@ -128,24 +136,19 @@ export class ThemeManager extends ApplicationService {
link.media = 'screen,print';
link.id = theme.uuid;
document.getElementsByTagName('head')[0].appendChild(link);
if (writeToCache) {
this.cacheThemes();
}
}

private deactivateTheme(uuid: string, recache = true) {
private deactivateTheme(uuid: string) {
const element = document.getElementById(uuid) as HTMLLinkElement;
if (element) {
element.disabled = true;
element.parentNode!.removeChild(element);
element.parentNode?.removeChild(element);
}

removeFromArray(this.activeThemes, uuid);
if (recache) {
this.cacheThemes();
}
}

private async cacheThemes() {
private async cacheThemeState() {
const themes = this.application.getAll(this.activeThemes) as SNTheme[];
const mapped = await Promise.all(
themes.map(async (theme) => {
Expand All @@ -165,15 +168,6 @@ export class ThemeManager extends ApplicationService {
);
}

private async decacheThemes() {
if (this.application) {
return this.application.removeValue(
CACHED_THEMES_KEY,
StorageValueModes.Nonwrapped
);
}
}

private async getCachedThemes() {
const cachedThemes = (await this.application.getValue(
CACHED_THEMES_KEY,
Expand Down

0 comments on commit 713be82

Please sign in to comment.