Fix theme caching

This commit is contained in:
Mo Bitar
2020-03-20 17:10:53 -05:00
parent a0a2a3fc30
commit 0ec0fd4872
3 changed files with 92 additions and 99 deletions

View File

@@ -1,5 +1,10 @@
import _ from 'lodash'; import _ from 'lodash';
import { ApplicationEvents, ContentTypes, StorageValueModes, EncryptionIntents, PureService } from 'snjs'; import {
ApplicationEvents,
StorageValueModes,
EncryptionIntents,
PureService,
} from 'snjs';
import { AppStateEvents } from '@/state'; import { AppStateEvents } from '@/state';
const CACHED_THEMES_KEY = 'cachedThemes'; const CACHED_THEMES_KEY = 'cachedThemes';
@@ -22,11 +27,10 @@ export class ThemeManager extends PureService {
this.unsub = application.addEventObserver((event) => { this.unsub = application.addEventObserver((event) => {
if (event === ApplicationEvents.Started) { if (event === ApplicationEvents.Started) {
this.onAppStart(); this.onAppStart();
} else if(event === ApplicationEvents.SignedOut) { } else if (event === ApplicationEvents.SignedOut) {
this.deactivateAllThemes(); this.deactivateAllThemes();
} }
}); });
this.unsubState = appState.addObserver((eventName, data) => { this.unsubState = appState.addObserver((eventName, data) => {
if (eventName === AppStateEvents.DesktopExtsReady) { if (eventName === AppStateEvents.DesktopExtsReady) {
this.activateCachedThemes(); this.activateCachedThemes();
@@ -45,6 +49,7 @@ export class ThemeManager extends PureService {
async deinit() { async deinit() {
super.deinit(); super.deinit();
this.unsubState(); this.unsubState();
this.activeThemes = [];
} }
async activateCachedThemes() { async activateCachedThemes() {
@@ -57,7 +62,6 @@ export class ThemeManager extends PureService {
registerObservers() { registerObservers() {
this.desktopManager.registerUpdateObserver((component) => { this.desktopManager.registerUpdateObserver((component) => {
// Reload theme if active
if (component.active && component.isTheme()) { if (component.active && component.isTheme()) {
this.deactivateTheme(component); this.deactivateTheme(component);
setTimeout(() => { setTimeout(() => {
@@ -87,15 +91,16 @@ export class ThemeManager extends PureService {
const activeThemes = this.application.componentManager.getActiveThemes(); const activeThemes = this.application.componentManager.getActiveThemes();
for (const theme of activeThemes) { for (const theme of activeThemes) {
if (theme) { if (theme) {
this.application.componentManager.deactivateComponent(theme); const dontSync = true;
this.application.componentManager.deactivateComponent(theme, dontSync);
} }
} }
this.activeThemes = [];
this.decacheThemes(); this.decacheThemes();
} }
activateTheme(theme, writeToCache = true) { activateTheme(theme, writeToCache = true) {
if (_.find(this.activeThemes, { uuid: theme.uuid })) { if (this.activeThemes.find((t) => t.uuid === theme.uuid)) {
return; return;
} }
this.activeThemes.push(theme); this.activeThemes.push(theme);
@@ -118,9 +123,7 @@ export class ThemeManager extends PureService {
element.disabled = true; element.disabled = true;
element.parentNode.removeChild(element); element.parentNode.removeChild(element);
} }
_.remove(this.activeThemes, { uuid: theme.uuid }); _.remove(this.activeThemes, { uuid: theme.uuid });
this.cacheThemes(); this.cacheThemes();
} }
@@ -155,10 +158,8 @@ export class ThemeManager extends PureService {
if (cachedThemes) { if (cachedThemes) {
const themes = []; const themes = [];
for (const cachedTheme of cachedThemes) { for (const cachedTheme of cachedThemes) {
const theme = await this.application.createItem({ const payload = this.application.createPayloadFromObject(cachedTheme);
contentType: ContentTypes.Theme, const theme = this.application.createItemFromPayload(payload);
content: cachedTheme.content
});
themes.push(theme); themes.push(theme);
} }
return themes; return themes;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long