Fix notebook sync (#4738)

* Do not keep fetching default notebook

* Fixes issue where notebooks would stop listening for remote changes.

* Fix issue with notebook merge conflicts
This commit is contained in:
Andrew Henry 2022-01-20 18:43:17 -08:00 committed by GitHub
parent 556f762d20
commit 91e909bb4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 50 deletions

View File

@ -465,23 +465,6 @@ ObjectAPI.prototype.mutate = function (domainObject, path, value) {
}
};
/**
* Updates a domain object based on its latest persisted state. Note that this will mutate the provided object.
* @param {module:openmct.DomainObject} domainObject an object to refresh from its persistence store
* @returns {Promise} the provided object, updated to reflect the latest persisted state of the object.
*/
ObjectAPI.prototype.refresh = async function (domainObject) {
const refreshedObject = await this.get(domainObject.identifier);
if (domainObject.isMutable) {
domainObject.$refresh(refreshedObject);
} else {
utils.refresh(domainObject, refreshedObject);
}
return domainObject;
};
/**
* @private
*/

View File

@ -322,7 +322,7 @@ export default {
cleanupDefaultNotebook() {
this.defaultPageId = undefined;
this.defaultSectionId = undefined;
this.removeDefaultClass(this.domainObject);
this.removeDefaultClass(this.domainObject.identifier);
clearDefaultNotebook();
},
setSectionAndPageFromUrl() {
@ -619,12 +619,8 @@ export default {
this.sectionsChanged({ sections });
},
removeDefaultClass(defaultNotebookIdentifier) {
if (!defaultNotebookIdentifier) {
return;
}
this.openmct.status.delete(defaultNotebookIdentifier);
removeDefaultClass(identifier) {
this.openmct.status.delete(identifier);
},
resetSearch() {
this.search = '';
@ -633,27 +629,24 @@ export default {
toggleNav() {
this.showNav = !this.showNav;
},
updateDefaultNotebook(notebookStorage) {
const defaultNotebook = getDefaultNotebook();
const defaultNotebookIdentifier = defaultNotebook && defaultNotebook.identifier;
const isSameNotebook = defaultNotebookIdentifier
&& this.openmct.objects.areIdsEqual(defaultNotebookIdentifier, notebookStorage.identifier);
if (!isSameNotebook) {
this.removeDefaultClass(defaultNotebookIdentifier);
updateDefaultNotebook(updatedNotebookStorageObject) {
if (!this.isDefaultNotebook()) {
const persistedNotebookStorageObject = getDefaultNotebook();
if (persistedNotebookStorageObject.identifier !== undefined) {
this.removeDefaultClass(persistedNotebookStorageObject.identifier);
}
if (!defaultNotebookIdentifier || !isSameNotebook) {
setDefaultNotebook(this.openmct, notebookStorage, this.domainObject);
setDefaultNotebook(this.openmct, updatedNotebookStorageObject, this.domainObject);
}
if (this.defaultSectionId !== notebookStorage.defaultSectionId) {
setDefaultNotebookSectionId(notebookStorage.defaultSectionId);
this.defaultSectionId = notebookStorage.defaultSectionId;
if (this.defaultSectionId !== updatedNotebookStorageObject.defaultSectionId) {
setDefaultNotebookSectionId(updatedNotebookStorageObject.defaultSectionId);
this.defaultSectionId = updatedNotebookStorageObject.defaultSectionId;
}
if (this.defaultPageId !== notebookStorage.defaultPageId) {
setDefaultNotebookPageId(notebookStorage.defaultPageId);
this.defaultPageId = notebookStorage.defaultPageId;
if (this.defaultPageId !== updatedNotebookStorageObject.defaultPageId) {
setDefaultNotebookPageId(updatedNotebookStorageObject.defaultPageId);
this.defaultPageId = updatedNotebookStorageObject.defaultPageId;
}
},
updateDefaultNotebookSection(sections, id) {
@ -671,7 +664,7 @@ export default {
if (defaultNotebookSectionId === id) {
const section = sections.find(s => s.id === id);
if (!section) {
this.removeDefaultClass(this.domainObject);
this.removeDefaultClass(this.domainObject.identifier);
clearDefaultNotebook();
return;

View File

@ -8,6 +8,7 @@ export default function (openmct) {
return apiSave(domainObject);
}
const isNewMutable = !domainObject.isMutable;
const localMutable = openmct.objects._toMutable(domainObject);
let result;
@ -20,18 +21,20 @@ export default function (openmct) {
result = Promise.reject(error);
}
} finally {
if (isNewMutable) {
openmct.objects.destroyMutable(localMutable);
}
}
return result;
};
}
function resolveConflicts(localMutable, openmct) {
const localEntries = JSON.parse(JSON.stringify(localMutable.configuration.entries));
return openmct.objects.getMutable(localMutable.identifier).then((remoteMutable) => {
const localEntries = localMutable.configuration.entries;
remoteMutable.$refresh(remoteMutable);
applyLocalEntries(remoteMutable, localEntries);
applyLocalEntries(remoteMutable, localEntries, openmct);
openmct.objects.destroyMutable(remoteMutable);
@ -39,7 +42,7 @@ function resolveConflicts(localMutable, openmct) {
});
}
function applyLocalEntries(mutable, entries) {
function applyLocalEntries(mutable, entries, openmct) {
Object.entries(entries).forEach(([sectionKey, pagesInSection]) => {
Object.entries(pagesInSection).forEach(([pageKey, localEntries]) => {
const remoteEntries = mutable.configuration.entries[sectionKey][pageKey];
@ -58,14 +61,15 @@ function applyLocalEntries(mutable, entries) {
locallyModifiedEntries.forEach((locallyModifiedEntry) => {
let mergedEntry = mergedEntries.find(entry => entry.id === locallyModifiedEntry.id);
if (mergedEntry !== undefined) {
if (mergedEntry !== undefined
&& locallyModifiedEntry.text.match(/\S/)) {
mergedEntry.text = locallyModifiedEntry.text;
shouldMutate = true;
}
});
if (shouldMutate) {
mutable.$set(`configuration.entries.${sectionKey}.${pageKey}`, mergedEntries);
openmct.objects.mutate(mutable, `configuration.entries.${sectionKey}.${pageKey}`, mergedEntries);
}
});
});

View File

@ -120,7 +120,6 @@ export function addNotebookEntry(openmct, domainObject, notebookStorage, embed =
const newEntries = addEntryIntoPage(notebookStorage, entries, entry);
addDefaultClass(domainObject, openmct);
mutateObject(openmct, domainObject, 'configuration.entries', newEntries);
return id;