Skip to content

Commit

Permalink
fix: deregister stream observers on editor deinit
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed Apr 21, 2020
1 parent 84de393 commit f9733f8
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 247 deletions.
21 changes: 14 additions & 7 deletions app/assets/javascripts/services/preferencesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
export class PreferencesManager extends ApplicationService {

private userPreferences!: SNUserPrefs
private loadingPrefs = false;

/** @override */
async onAppLaunch() {
super.onAppLaunch();
this.streamPreferences();
this.loadSingleton();
}

get webApplication() {
Expand All @@ -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 predicate = new SNPredicate('content_type', '=', contentType);
const previousRef = this.userPreferences;
this.userPreferences = (await this.application!.singletonManager!.findOrCreateSingleton(
predicate,
contentType,
FillItemContent({})
)) as SNUserPrefs;
this.preferencesDidChange();
}

preferencesDidChange() {
this.webApplication.getAppState().setUserPreferences(this.userPreferences);
this.loadingPrefs = false;
const didChange = !previousRef || (
this.userPreferences.lastSyncBegan?.getTime() !== previousRef?.lastSyncBegan?.getTime()
)
if (didChange) {
this.webApplication.getAppState().setUserPreferences(this.userPreferences);
}
}

syncUserPreferences() {
Expand Down
18 changes: 9 additions & 9 deletions app/assets/javascripts/ui_models/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
/** Update our note object reference whenever it changes */
const matchingNote = notes.find((item) => {
Expand Down Expand Up @@ -63,15 +72,6 @@ export class Editor {
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.
*/
Expand Down
21 changes: 14 additions & 7 deletions app/assets/javascripts/views/editor/editor_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
private removeTrashKeyObserver?: any
private removeDeleteKeyObserver?: any
private removeTabObserver?: any
private removeComponentObserver: any
private removeComponentGroupObserver: any

private removeTagsObserver!: () => void
private removeComponentsObserver!: () => void

prefKeyMonospace: string
prefKeySpellcheck: string
Expand Down Expand Up @@ -140,8 +143,12 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
}

deinit() {
this.removeComponentObserver();
this.removeComponentObserver = undefined;
this.removeTagsObserver();
this.removeComponentsObserver();
(this.removeTagsObserver as any) = undefined;
(this.removeComponentsObserver as any) = undefined;
this.removeComponentGroupObserver();
this.removeComponentGroupObserver = undefined;
this.removeAltKeyObserver();
this.removeAltKeyObserver = undefined;
this.removeTrashKeyObserver();
Expand Down Expand Up @@ -183,7 +190,7 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
this.reloadTagsString();
}
});
this.removeComponentObserver = this.componentGroup.addChangeObserver(() => {
this.removeComponentGroupObserver = this.componentGroup.addChangeObserver(() => {
this.setEditorState({
activeEditorComponent: this.componentGroup.activeComponentForArea(ComponentArea.Editor),
activeTagsComponent: this.componentGroup.activeComponentForArea(ComponentArea.NoteTags),
Expand Down Expand Up @@ -336,7 +343,7 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
}

streamItems() {
this.application.streamItems(
this.removeTagsObserver = this.application.streamItems(
ContentType.Tag,
(items) => {
if (!this.note) {
Expand All @@ -355,15 +362,15 @@ class EditorViewCtrl extends PureViewCtrl implements EditorViewScope {
}
);

this.application.streamItems(
this.removeComponentsObserver = this.application.streamItems(
ContentType.Component,
async (items) => {
const components = items as SNComponent[];
if (!this.note) {
return;
}
/** 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 */
const editors = components.filter((component) => {
return component.isEditor();
Expand Down
28 changes: 19 additions & 9 deletions app/assets/javascripts/views/notes/notes_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ class NotesViewCtrl extends PureViewCtrl {

$onInit() {
super.$onInit();
angular.element(document).ready(() => {
this.reloadPreferences();
});
this.panelPuppet = {
onReady: () => this.reloadPreferences()
onReady: () => this.reloadPanelWidth()
};
this.unsubEditorChange = this.application.editorGroup.addChangeObserver(() => {
this.setNotesState({
Expand Down Expand Up @@ -407,9 +404,27 @@ class NotesViewCtrl extends PureViewCtrl {
WebPrefKey.NotesHideDate,
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({
...viewOptions
});
this.reloadPanelWidth();
if (displayOptionsChanged) {
this.reloadNotesDisplayOptions();
}
await this.reloadNotes();
if (prevSortValue && prevSortValue !== sortBy) {
this.selectFirstNote();
}
}

reloadPanelWidth() {
const width = this.application!.getPrefsService().getValue(
WebPrefKey.NotesPanelWidth
);
Expand All @@ -422,11 +437,6 @@ class NotesViewCtrl extends PureViewCtrl {
);
}
}
this.reloadNotesDisplayOptions();
await this.reloadNotes();
if (prevSortValue && prevSortValue !== sortBy) {
this.selectFirstNote();
}
}

onPanelResize(
Expand Down
5 changes: 4 additions & 1 deletion app/assets/javascripts/views/tags/tags_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TagsViewCtrl extends PureViewCtrl {
private editingOriginalName?: string
formData: { tagTitle?: string } = {}
titles: Partial<Record<UuidString, string>> = {}
private removeTagsObserver!: () => void

/* @ngInject */
constructor(
Expand All @@ -57,6 +58,8 @@ class TagsViewCtrl extends PureViewCtrl {
}

deinit() {
this.removeTagsObserver();
(this.removeTagsObserver as any) = undefined;
this.unregisterComponent();
this.unregisterComponent = undefined;
super.deinit();
Expand Down Expand Up @@ -112,7 +115,7 @@ class TagsViewCtrl extends PureViewCtrl {
}

beginStreamingItems() {
this.application.streamItems(
this.removeTagsObserver = this.application.streamItems(
ContentType.Tag,
async (items) => {
await this.setTagState({
Expand Down
470 changes: 257 additions & 213 deletions dist/javascripts/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/javascripts/app.js.map

Large diffs are not rendered by default.

0 comments on commit f9733f8

Please sign in to comment.