Component and other handler deregisterations

This commit is contained in:
Mo Bitar
2020-03-24 12:12:07 -05:00
parent ee7cb1fce6
commit 7aa38cbdb8
11 changed files with 476 additions and 310 deletions

View File

@@ -1,8 +1,7 @@
/* eslint-disable camelcase */
// An interface used by the Desktop app to interact with SN
import pull from 'lodash/pull';
import { isDesktopApplication } from '@/utils';
import { EncryptionIntents, ApplicationService, ApplicationEvents } from 'snjs';
import { EncryptionIntents, ApplicationService, ApplicationEvents, removeFromArray } from 'snjs';
const COMPONENT_DATA_KEY_INSTALL_ERROR = 'installError';
const COMPONENT_CONTENT_KEY_PACKAGE_INFO = 'package_info';
@@ -89,7 +88,9 @@ export class DesktopManager extends ApplicationService {
callback: callback
};
this.updateObservers.push(observer);
return observer;
return () => {
removeFromArray(this.updateObservers, observer);
};
}
searchText(text) {
@@ -106,10 +107,6 @@ export class DesktopManager extends ApplicationService {
}
}
deregisterUpdateObserver(observer) {
pull(this.updateObservers, observer);
}
// Pass null to cancel search
desktop_setSearchHandler(handler) {
this.searchHandler = handler;
@@ -161,7 +158,7 @@ export class DesktopManager extends ApplicationService {
}
desktop_deregisterComponentActivationObserver(observer) {
pull(this.componentActivationObservers, observer);
removeFromArray(this.componentActivationObservers, observer);
}
/* Notify observers that a component has been registered/activated */

View File

@@ -26,6 +26,10 @@ export class ThemeManager extends ApplicationService {
this.unsubState();
this.unsubState = null;
this.activeThemes.length = 0;
this.unregisterDesktop();
this.unregisterComponent();
this.unregisterDesktop = null;
this.unregisterComponent = null;
super.deinit();
}
@@ -38,13 +42,7 @@ export class ThemeManager extends ApplicationService {
}
}
onAppEvent(eventName) {
super.onAppEvent(eventName);
if (eventName === ApplicationEvents.SignedOut) {
this.deactivateAllThemes();
}
}
/** @access private */
async activateCachedThemes() {
const cachedThemes = await this.getCachedThemes();
const writeToCache = false;
@@ -53,8 +51,9 @@ export class ThemeManager extends ApplicationService {
}
}
/** @access private */
registerObservers() {
this.application.getDesktopService().registerUpdateObserver((component) => {
this.unregisterDesktop = this.application.getDesktopService().registerUpdateObserver((component) => {
if (component.active && component.isTheme()) {
this.deactivateTheme(component);
setTimeout(() => {
@@ -63,7 +62,7 @@ export class ThemeManager extends ApplicationService {
}
});
this.application.componentManager.registerHandler({
this.unregisterComponent = this.application.componentManager.registerHandler({
identifier: 'themeManager',
areas: ['themes'],
activationHandler: (component) => {
@@ -76,10 +75,7 @@ export class ThemeManager extends ApplicationService {
});
}
hasActiveTheme() {
return this.application.componentManager.getActiveThemes().length > 0;
}
/** @access public */
deactivateAllThemes() {
const activeThemes = this.application.componentManager.getActiveThemes();
for (const theme of activeThemes) {
@@ -92,6 +88,7 @@ export class ThemeManager extends ApplicationService {
this.decacheThemes();
}
/** @access private */
activateTheme(theme, writeToCache = true) {
if (this.activeThemes.find((t) => t.uuid === theme.uuid)) {
return;
@@ -110,6 +107,7 @@ export class ThemeManager extends ApplicationService {
}
}
/** @access private */
deactivateTheme(theme) {
const element = document.getElementById(theme.uuid);
if (element) {
@@ -120,6 +118,7 @@ export class ThemeManager extends ApplicationService {
this.cacheThemes();
}
/** @access private */
async cacheThemes() {
const mapped = await Promise.all(this.activeThemes.map(async (theme) => {
const payload = theme.payloadRepresentation();
@@ -136,6 +135,7 @@ export class ThemeManager extends ApplicationService {
);
}
/** @access private */
async decacheThemes() {
return this.application.removeValue(
CACHED_THEMES_KEY,
@@ -143,6 +143,7 @@ export class ThemeManager extends ApplicationService {
);
}
/** @access private */
async getCachedThemes() {
const cachedThemes = await this.application.getValue(
CACHED_THEMES_KEY,