fix: deregister stream observers on editor deinit

This commit is contained in:
Mo Bitar
2020-04-21 12:17:01 -05:00
parent 84de39375c
commit f9733f8dac
7 changed files with 318 additions and 247 deletions

View File

@@ -12,12 +12,12 @@ import {
export class PreferencesManager extends ApplicationService { export class PreferencesManager extends ApplicationService {
private userPreferences!: SNUserPrefs private userPreferences!: SNUserPrefs
private loadingPrefs = false;
/** @override */ /** @override */
async onAppLaunch() { async onAppLaunch() {
super.onAppLaunch(); super.onAppLaunch();
this.streamPreferences(); this.streamPreferences();
this.loadSingleton();
} }
get webApplication() { get webApplication() {
@@ -33,19 +33,26 @@ export class PreferencesManager extends ApplicationService {
); );
} }
async loadSingleton() { private async loadSingleton() {
if(this.loadingPrefs) {
return;
}
this.loadingPrefs = true;
const contentType = ContentType.UserPrefs; const contentType = ContentType.UserPrefs;
const predicate = new SNPredicate('content_type', '=', contentType); const predicate = new SNPredicate('content_type', '=', contentType);
const previousRef = this.userPreferences;
this.userPreferences = (await this.application!.singletonManager!.findOrCreateSingleton( this.userPreferences = (await this.application!.singletonManager!.findOrCreateSingleton(
predicate, predicate,
contentType, contentType,
FillItemContent({}) FillItemContent({})
)) as SNUserPrefs; )) as SNUserPrefs;
this.preferencesDidChange(); this.loadingPrefs = false;
} const didChange = !previousRef || (
this.userPreferences.lastSyncBegan?.getTime() !== previousRef?.lastSyncBegan?.getTime()
preferencesDidChange() { )
this.webApplication.getAppState().setUserPreferences(this.userPreferences); if (didChange) {
this.webApplication.getAppState().setUserPreferences(this.userPreferences);
}
} }
syncUserPreferences() { syncUserPreferences() {

View File

@@ -30,6 +30,15 @@ export class Editor {
); );
} }
deinit() {
this.removeStreamObserver();
(this.removeStreamObserver as any) = undefined;
this._onNoteChange = undefined;
(this.application as any) = undefined;
this._onNoteChange = undefined;
this._onNoteValueChange = undefined;
}
private async handleNoteStream(notes: SNNote[], source?: PayloadSource) { private async handleNoteStream(notes: SNNote[], source?: PayloadSource) {
/** Update our note object reference whenever it changes */ /** Update our note object reference whenever it changes */
const matchingNote = notes.find((item) => { const matchingNote = notes.find((item) => {
@@ -63,15 +72,6 @@ export class Editor {
this.setNote(note as SNNote); this.setNote(note as SNNote);
} }
deinit() {
this.removeStreamObserver();
(this.removeStreamObserver as any) = undefined;
this._onNoteChange = undefined;
(this.application as any) = undefined;
this._onNoteChange = undefined;
this._onNoteValueChange = undefined;
}
/** /**
* Register to be notified when the editor's note changes. * Register to be notified when the editor's note changes.
*/ */

View File

@@ -112,7 +112,10 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
private removeTrashKeyObserver?: any private removeTrashKeyObserver?: any
private removeDeleteKeyObserver?: any private removeDeleteKeyObserver?: any
private removeTabObserver?: any private removeTabObserver?: any
private removeComponentObserver: any private removeComponentGroupObserver: any
private removeTagsObserver!: () => void
private removeComponentsObserver!: () => void
prefKeyMonospace: string prefKeyMonospace: string
prefKeySpellcheck: string prefKeySpellcheck: string
@@ -140,8 +143,12 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
} }
deinit() { deinit() {
this.removeComponentObserver(); this.removeTagsObserver();
this.removeComponentObserver = undefined; this.removeComponentsObserver();
(this.removeTagsObserver as any) = undefined;
(this.removeComponentsObserver as any) = undefined;
this.removeComponentGroupObserver();
this.removeComponentGroupObserver = undefined;
this.removeAltKeyObserver(); this.removeAltKeyObserver();
this.removeAltKeyObserver = undefined; this.removeAltKeyObserver = undefined;
this.removeTrashKeyObserver(); this.removeTrashKeyObserver();
@@ -183,7 +190,7 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
this.reloadTagsString(); this.reloadTagsString();
} }
}); });
this.removeComponentObserver = this.componentGroup.addChangeObserver(() => { this.removeComponentGroupObserver = this.componentGroup.addChangeObserver(() => {
this.setEditorState({ this.setEditorState({
activeEditorComponent: this.componentGroup.activeComponentForArea(ComponentArea.Editor), activeEditorComponent: this.componentGroup.activeComponentForArea(ComponentArea.Editor),
activeTagsComponent: this.componentGroup.activeComponentForArea(ComponentArea.NoteTags), activeTagsComponent: this.componentGroup.activeComponentForArea(ComponentArea.NoteTags),
@@ -336,7 +343,7 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
} }
streamItems() { streamItems() {
this.application.streamItems( this.removeTagsObserver = this.application.streamItems(
ContentType.Tag, ContentType.Tag,
(items) => { (items) => {
if (!this.note) { if (!this.note) {
@@ -355,7 +362,7 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
} }
); );
this.application.streamItems( this.removeComponentsObserver = this.application.streamItems(
ContentType.Component, ContentType.Component,
async (items) => { async (items) => {
const components = items as SNComponent[]; const components = items as SNComponent[];
@@ -363,7 +370,7 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
return; return;
} }
/** Reload componentStack in case new ones were added or removed */ /** Reload componentStack in case new ones were added or removed */
this.reloadComponentStack(); await this.reloadComponentStack();
/** Observe editor changes to see if the current note should update its editor */ /** Observe editor changes to see if the current note should update its editor */
const editors = components.filter((component) => { const editors = components.filter((component) => {
return component.isEditor(); return component.isEditor();

View File

@@ -76,11 +76,8 @@ class NotesViewCtrl extends PureViewCtrl {
$onInit() { $onInit() {
super.$onInit(); super.$onInit();
angular.element(document).ready(() => {
this.reloadPreferences();
});
this.panelPuppet = { this.panelPuppet = {
onReady: () => this.reloadPreferences() onReady: () => this.reloadPanelWidth()
}; };
this.unsubEditorChange = this.application.editorGroup.addChangeObserver(() => { this.unsubEditorChange = this.application.editorGroup.addChangeObserver(() => {
this.setNotesState({ this.setNotesState({
@@ -407,9 +404,27 @@ class NotesViewCtrl extends PureViewCtrl {
WebPrefKey.NotesHideDate, WebPrefKey.NotesHideDate,
false false
); );
const state = this.getState();
const displayOptionsChanged = (
viewOptions.sortBy !== state.sortBy ||
viewOptions.sortReverse !== state.sortReverse ||
viewOptions.hidePinned !== state.hidePinned ||
viewOptions.showArchived !== state.showArchived
);
await this.setNotesState({ await this.setNotesState({
...viewOptions ...viewOptions
}); });
this.reloadPanelWidth();
if (displayOptionsChanged) {
this.reloadNotesDisplayOptions();
}
await this.reloadNotes();
if (prevSortValue && prevSortValue !== sortBy) {
this.selectFirstNote();
}
}
reloadPanelWidth() {
const width = this.application!.getPrefsService().getValue( const width = this.application!.getPrefsService().getValue(
WebPrefKey.NotesPanelWidth WebPrefKey.NotesPanelWidth
); );
@@ -422,11 +437,6 @@ class NotesViewCtrl extends PureViewCtrl {
); );
} }
} }
this.reloadNotesDisplayOptions();
await this.reloadNotes();
if (prevSortValue && prevSortValue !== sortBy) {
this.selectFirstNote();
}
} }
onPanelResize( onPanelResize(

View File

@@ -45,6 +45,7 @@ class TagsViewCtrl extends PureViewCtrl {
private editingOriginalName?: string private editingOriginalName?: string
formData: { tagTitle?: string } = {} formData: { tagTitle?: string } = {}
titles: Partial<Record<UuidString, string>> = {} titles: Partial<Record<UuidString, string>> = {}
private removeTagsObserver!: () => void
/* @ngInject */ /* @ngInject */
constructor( constructor(
@@ -57,6 +58,8 @@ class TagsViewCtrl extends PureViewCtrl {
} }
deinit() { deinit() {
this.removeTagsObserver();
(this.removeTagsObserver as any) = undefined;
this.unregisterComponent(); this.unregisterComponent();
this.unregisterComponent = undefined; this.unregisterComponent = undefined;
super.deinit(); super.deinit();
@@ -112,7 +115,7 @@ class TagsViewCtrl extends PureViewCtrl {
} }
beginStreamingItems() { beginStreamingItems() {
this.application.streamItems( this.removeTagsObserver = this.application.streamItems(
ContentType.Tag, ContentType.Tag,
async (items) => { async (items) => {
await this.setTagState({ await this.setTagState({

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long