Notify other components of change

This commit is contained in:
Mo Bitar
2018-06-21 23:33:34 -05:00
parent c67167af97
commit 9a4e8ac964
2 changed files with 25 additions and 11 deletions

View File

@@ -49,15 +49,19 @@ class ComponentManager {
this.handleMessage(this.componentForSessionKey(event.data.sessionKey), event.data); this.handleMessage(this.componentForSessionKey(event.data.sessionKey), event.data);
}.bind(this), false); }.bind(this), false);
this.modelManager.addItemSyncObserver("component-manager", "*", (allItems, validItems, deletedItems, source) => { this.modelManager.addItemSyncObserver("component-manager", "*", (allItems, validItems, deletedItems, source, sourceKey) => {
/* If the source of these new or updated items is from a Component itself saving items, we don't need to notify /* If the source of these new or updated items is from a Component itself saving items, we don't need to notify
components again of the same item. Regarding notifying other components than the issuing component, other mapping sources components again of the same item. Regarding notifying other components than the issuing component, other mapping sources
will take care of that, like ModelManager.MappingSourceRemoteSaved will take care of that, like ModelManager.MappingSourceRemoteSaved
Update: We will now check sourceKey to determine whether the incoming change should be sent to
a component. If sourceKey == component.uuid, it will be skipped. This way, if one component triggers a change,
it's sent to other components.
*/ */
if(source == ModelManager.MappingSourceComponentRetrieved) { // if(source == ModelManager.MappingSourceComponentRetrieved) {
return; // return;
} // }
var syncedComponents = allItems.filter(function(item) { var syncedComponents = allItems.filter(function(item) {
return item.content_type === "SN|Component" || item.content_type == "SN|Theme" return item.content_type === "SN|Component" || item.content_type == "SN|Theme"
@@ -81,6 +85,11 @@ class ComponentManager {
} }
for(let observer of this.streamObservers) { for(let observer of this.streamObservers) {
if(sourceKey && sourceKey == observer.component.uuid) {
// Don't notify source of change, as it is the originator, doesn't need duplicate event.
continue;
}
var relevantItems = allItems.filter(function(item){ var relevantItems = allItems.filter(function(item){
return observer.contentTypes.indexOf(item.content_type) !== -1; return observer.contentTypes.indexOf(item.content_type) !== -1;
}) })
@@ -108,6 +117,11 @@ class ComponentManager {
]; ];
for(let observer of this.contextStreamObservers) { for(let observer of this.contextStreamObservers) {
if(sourceKey && sourceKey == observer.component.uuid) {
// Don't notify source of change, as it is the originator, doesn't need duplicate event.
continue;
}
for(let handler of this.handlers) { for(let handler of this.handlers) {
if(!handler.areas.includes(observer.component.area) && !handler.areas.includes("*")) { if(!handler.areas.includes(observer.component.area) && !handler.areas.includes("*")) {
continue; continue;
@@ -454,7 +468,7 @@ class ComponentManager {
We map the items here because modelManager is what updates the UI. If you were to instead get the items directly, We map the items here because modelManager is what updates the UI. If you were to instead get the items directly,
this would update them server side via sync, but would never make its way back to the UI. this would update them server side via sync, but would never make its way back to the UI.
*/ */
var localItems = this.modelManager.mapResponseItemsToLocalModels(responseItems, ModelManager.MappingSourceComponentRetrieved); var localItems = this.modelManager.mapResponseItemsToLocalModels(responseItems, ModelManager.MappingSourceComponentRetrieved, component.uuid);
for(var item of localItems) { for(var item of localItems) {
var responseItem = _.find(responseItems, {uuid: item.uuid}); var responseItem = _.find(responseItems, {uuid: item.uuid});

View File

@@ -125,11 +125,11 @@ class ModelManager {
this.notifySyncObserversOfModels(items, ModelManager.MappingSourceLocalSaved); this.notifySyncObserversOfModels(items, ModelManager.MappingSourceLocalSaved);
} }
mapResponseItemsToLocalModels(items, source) { mapResponseItemsToLocalModels(items, source, sourceKey) {
return this.mapResponseItemsToLocalModelsOmittingFields(items, null, source); return this.mapResponseItemsToLocalModelsOmittingFields(items, null, source, sourceKey);
} }
mapResponseItemsToLocalModelsOmittingFields(items, omitFields, source) { mapResponseItemsToLocalModelsOmittingFields(items, omitFields, source, sourceKey) {
var models = [], processedObjects = [], modelsToNotifyObserversOf = []; var models = [], processedObjects = [], modelsToNotifyObserversOf = [];
// first loop should add and process items // first loop should add and process items
@@ -202,13 +202,13 @@ class ModelManager {
} }
} }
this.notifySyncObserversOfModels(modelsToNotifyObserversOf, source); this.notifySyncObserversOfModels(modelsToNotifyObserversOf, source, sourceKey);
return models; return models;
} }
/* Note that this function is public, and can also be called manually (desktopManager uses it) */ /* Note that this function is public, and can also be called manually (desktopManager uses it) */
notifySyncObserversOfModels(models, source) { notifySyncObserversOfModels(models, source, sourceKey) {
for(var observer of this.itemSyncObservers) { for(var observer of this.itemSyncObservers) {
var allRelevantItems = observer.type == "*" ? models : models.filter(function(item){return item.content_type == observer.type}); var allRelevantItems = observer.type == "*" ? models : models.filter(function(item){return item.content_type == observer.type});
var validItems = [], deletedItems = []; var validItems = [], deletedItems = [];
@@ -221,7 +221,7 @@ class ModelManager {
} }
if(allRelevantItems.length > 0) { if(allRelevantItems.length > 0) {
observer.callback(allRelevantItems, validItems, deletedItems, source); observer.callback(allRelevantItems, validItems, deletedItems, source, sourceKey);
} }
} }
} }