diff --git a/src/public/javascripts/services/load_results.js b/src/public/javascripts/services/load_results.js new file mode 100644 index 000000000..ef0053194 --- /dev/null +++ b/src/public/javascripts/services/load_results.js @@ -0,0 +1,29 @@ +export class LoadResults { + constructor() { + this.noteIdToSync = {}; + this.sourceIdToNoteIds = {}; + } + + add(noteId, sourceId) { + this.noteIdToSync[noteId] = this.noteIdToSync[noteId] || []; + + if (!this.noteIdToSync[noteId].includes(sourceId)) { + this.noteIdToSync[noteId].push(sourceId); + } + + this.sourceIdToNoteIds[sourceId] = this.sourceIdToNoteIds[sourceId] || []; + + if (!this.sourceIdToNoteIds[sourceId]) { + this.sourceIdToNoteIds[sourceId].push(noteId); + } + } + + getNoteIds() { + return Object.keys(this.noteIdToSync); + } + + isNoteReloaded(noteId, sourceId) { + const sourceIds = this.noteIdToSync[noteId]; + return sourceIds && !!sourceIds.find(sId => sId !== sourceId); + } +} \ No newline at end of file diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js index d88cb2025..8586506a5 100644 --- a/src/public/javascripts/services/tree_cache.js +++ b/src/public/javascripts/services/tree_cache.js @@ -2,6 +2,7 @@ import Branch from "../entities/branch.js"; import NoteShort from "../entities/note_short.js"; import Attribute from "../entities/attribute.js"; import server from "./server.js"; +import {LoadResults} from "./load_results.js"; /** * TreeCache keeps a read only cache of note tree structure in frontend's memory. @@ -158,9 +159,6 @@ class TreeCache { this.addResp([note], branches, []); } } - - const appContext = (await import('./app_context.js')).default; - appContext.trigger('notesReloaded', {noteIds}) } /** @return {Promise} */ @@ -231,26 +229,31 @@ class TreeCache { } async processSyncRows(syncRows) { - const noteIdsToRefresh = new Set(); + const loadResults = new LoadResults(); syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => { const branch = this.branches[sync.entityId]; // we assume that the cache contains the old branch state and we add also the old parentNoteId // so that the old parent can also be updated - noteIdsToRefresh.add(branch.parentNoteId); + loadResults.add(branch.parentNoteId, sync.sourceId); // this should then contain new parentNoteId for which we should also update the cache - noteIdsToRefresh.add(sync.parentNoteId); + loadResults.add(sync.parentNoteId, sync.sourceId); + + loadResults.add(sync.noteId, sync.sourceId); }); - syncRows.filter(sync => sync.entityName === 'notes').forEach(sync => noteIdsToRefresh.add(sync.entityId)); + syncRows.filter(sync => sync.entityName === 'notes').forEach(sync => loadResults.add(sync.entityId, sync.sourceId)); - syncRows.filter(sync => sync.entityName === 'note_reordering').forEach(sync => noteIdsToRefresh.add(sync.entityId)); + syncRows.filter(sync => sync.entityName === 'note_reordering').forEach(sync => loadResults.add(sync.entityId, sync.sourceId)); // missing reloading the relation target note - syncRows.filter(sync => sync.entityName === 'attributes').forEach(sync => noteIdsToRefresh.add(sync.noteId)); + syncRows.filter(sync => sync.entityName === 'attributes').forEach(sync => loadResults.add(sync.noteId, sync.sourceId)); - await this.reloadNotes(Array.from(noteIdsToRefresh)); + await this.reloadNotes(loadResults.getNoteIds()); + + const appContext = (await import('./app_context.js')).default; + appContext.trigger('notesReloaded', {loadResults}); } } diff --git a/src/public/javascripts/widgets/note_detail.js b/src/public/javascripts/widgets/note_detail.js index 1b0eacf9b..023379f56 100644 --- a/src/public/javascripts/widgets/note_detail.js +++ b/src/public/javascripts/widgets/note_detail.js @@ -219,4 +219,10 @@ export default class NoteDetailWidget extends TabAwareWidget { protectedSessionStartedListener() { this.refresh(); } + + notesReloadedListener({loadResults}) { + if (loadResults.isNoteReloaded(this.noteId, this.componentId)) { + this.refresh(); + } + } } \ No newline at end of file diff --git a/src/public/javascripts/widgets/tab_aware_widget.js b/src/public/javascripts/widgets/tab_aware_widget.js index d621eccb6..6bba19c58 100644 --- a/src/public/javascripts/widgets/tab_aware_widget.js +++ b/src/public/javascripts/widgets/tab_aware_widget.js @@ -13,7 +13,15 @@ export default class TabAwareWidget extends BasicWidget { } isNote(noteId) { - return this.tabContext && this.tabContext.note && this.tabContext.note.noteId === noteId; + return this.noteId === noteId; + } + + get note() { + return this.tabContext && this.tabContext.note; + } + + get noteId() { + return this.note && this.note.noteId; } tabNoteSwitchedListener({tabId}) {