widgets can now listen to sync data changes

This commit is contained in:
zadam 2019-08-06 22:39:27 +02:00
parent fcd87b3e2d
commit bfbc124e78
8 changed files with 60 additions and 9 deletions

View file

@ -3,7 +3,8 @@ import infoService from "./info.js";
const $outstandingSyncsCount = $("#outstanding-syncs-count");
const syncMessageHandlers = [];
const allSyncMessageHandlers = [];
const outsideSyncMessageHandlers = [];
const messageHandlers = [];
let ws;
@ -26,8 +27,12 @@ function subscribeToMessages(messageHandler) {
messageHandlers.push(messageHandler);
}
function subscribeToSyncMessages(messageHandler) {
syncMessageHandlers.push(messageHandler);
function subscribeToOutsideSyncMessages(messageHandler) {
outsideSyncMessageHandlers.push(messageHandler);
}
function subscribeToAllSyncMessages(messageHandler) {
allSyncMessageHandlers.push(messageHandler);
}
function handleMessage(event) {
@ -46,9 +51,13 @@ function handleMessage(event) {
lastSyncId = message.data[message.data.length - 1].id;
}
for (const syncMessageHandler of allSyncMessageHandlers) {
syncMessageHandler(message.data);
}
const syncData = message.data.filter(sync => sync.sourceId !== glob.sourceId);
for (const syncMessageHandler of syncMessageHandlers) {
for (const syncMessageHandler of outsideSyncMessageHandlers) {
syncMessageHandler(syncData);
}
@ -102,5 +111,6 @@ setTimeout(() => {
export default {
logError,
subscribeToMessages,
subscribeToSyncMessages
subscribeToAllSyncMessages,
subscribeToOutsideSyncMessages
};

View file

@ -357,13 +357,14 @@ function fireDetailLoaded() {
detailLoadedListeners = [];
}
messagingService.subscribeToSyncMessages(syncData => {
messagingService.subscribeToOutsideSyncMessages(syncData => {
const noteIdsToRefresh = new Set();
syncData
.filter(sync => sync.entityName === 'notes')
.forEach(sync => noteIdsToRefresh.add(sync.entityId));
// we need to reload because of promoted attributes
syncData
.filter(sync => sync.entityName === 'attributes')
.forEach(sync => noteIdsToRefresh.add(sync.noteId));
@ -373,6 +374,12 @@ messagingService.subscribeToSyncMessages(syncData => {
}
});
messagingService.subscribeToAllSyncMessages(syncData => {
for (const tc of tabContexts) {
tc.syncDataReceived(syncData);
}
});
$tabContentsContainer.on("dragover", e => e.preventDefault());
$tabContentsContainer.on("dragleave", e => e.preventDefault());

View file

@ -27,6 +27,7 @@ class Sidebar {
*/
constructor(ctx) {
this.ctx = ctx;
this.widgets = [];
this.$sidebar = ctx.$tabContent.find(".note-detail-sidebar");
this.$widgets = this.$sidebar.find(".note-detail-widgets");
this.$showSideBarButton = this.ctx.$tabContent.find(".show-sidebar-button");
@ -46,6 +47,7 @@ class Sidebar {
}
async noteLoaded() {
this.widgets = [];
this.$widgets.empty();
const widgetClasses = [AttributesWidget, LinkMapWidget, NoteRevisionsWidget, NoteInfoWidget];
@ -54,6 +56,8 @@ class Sidebar {
const $widget = this.createWidgetElement();
const attributesWidget = new widgetClass(this.ctx, $widget);
this.widgets.push(attributesWidget);
attributesWidget.renderBody(); // let it run in parallel
this.$widgets.append($widget);
@ -69,6 +73,14 @@ class Sidebar {
return $widget;
}
syncDataReceived(syncData) {
for (const widget of this.widgets) {
if (widget.syncDataReceived) {
widget.syncDataReceived(syncData);
}
}
}
}
export default Sidebar;

View file

@ -244,7 +244,10 @@ class TabContext {
treeService.setNoteTitle(this.note.noteId, this.note.title);
await server.put('notes/' + this.note.noteId, this.note.dto);
const resp = await server.put('notes/' + this.note.noteId, this.note.dto);
this.note.dateModified = resp.dateModified;
this.note.utcDateModified = resp.utcDateModified;
if (this.note.isProtected) {
protectedSessionHolder.touchProtectedSession();
@ -352,6 +355,12 @@ class TabContext {
this.$tabContent.find('.aa-input').autocomplete('close');
}
}
syncDataReceived(syncData) {
if (this.sidebar) {
this.sidebar.syncDataReceived(syncData);
}
}
}
export default TabContext;

View file

@ -759,7 +759,7 @@ messagingService.subscribeToMessages(message => {
}
});
messagingService.subscribeToSyncMessages(syncData => {
messagingService.subscribeToOutsideSyncMessages(syncData => {
if (syncData.some(sync => sync.entityName === 'branches')
|| syncData.some(sync => sync.entityName === 'notes')) {

View file

@ -54,6 +54,14 @@ class NoteInfoWidget {
$type.text(note.type);
$mime.text(note.mime);
}
syncDataReceived(syncData) {
if (syncData.find(sd => sd.entityName === 'notes' && sd.entityId === this.ctx.note.noteId)) {
console.log("Re-rendering note info");
this.renderBody();
}
}
}
export default NoteInfoWidget;

View file

@ -68,7 +68,7 @@ async function updateNote(req) {
const note = req.body;
const noteId = req.params.noteId;
await noteService.updateNote(noteId, note);
return await noteService.updateNote(noteId, note);
}
async function deleteNote(req) {

View file

@ -371,6 +371,11 @@ async function updateNote(noteId, noteUpdates) {
}
await protectNoteRevisions(note);
return {
dateModified: note.dateModified,
utcDateModified: note.utcDateModified
};
}
/** @return {boolean} - true if note has been deleted, false otherwise */