ModelManager mapping sources

This commit is contained in:
Mo Bitar
2017-11-13 10:12:07 -06:00
parent fc06abb1fe
commit 909d66067b
5 changed files with 45 additions and 36 deletions

View File

@@ -1,6 +1,13 @@
class ModelManager {
constructor(storageManager) {
ModelManager.MappingSourceRemoteRetrieved = "MappingSourceRemoteRetrieved";
ModelManager.MappingSourceRemoteSaved = "MappingSourceRemoteSaved";
ModelManager.MappingSourceLocalRetrieved = "MappingSourceLocalRetrieved";
ModelManager.MappingSourceComponentRetrieved = "MappingSourceComponentRetrieved";
ModelManager.MappingSourceRemoteActionRetrieved = "MappingSourceRemoteActionRetrieved"; /* aciton-based Extensions like note history */
ModelManager.MappingSourceFileImport = "MappingSourceFileImport";
this.storageManager = storageManager;
this.notes = [];
this.tags = [];
@@ -96,11 +103,11 @@ class ModelManager {
return tag;
}
mapResponseItemsToLocalModels(items, options) {
return this.mapResponseItemsToLocalModelsOmittingFields(items, null, options);
mapResponseItemsToLocalModels(items, source) {
return this.mapResponseItemsToLocalModelsOmittingFields(items, null, source);
}
mapResponseItemsToLocalModelsOmittingFields(items, omitFields, options) {
mapResponseItemsToLocalModelsOmittingFields(items, omitFields, source) {
var models = [], processedObjects = [], modelsToNotifyObserversOf = [];
// first loop should add and process items
@@ -136,14 +143,6 @@ class ModelManager {
item = this.createItem(json_obj);
}
/* If content is being omitted from the json_obj, this means this is a metadata save only.
This happens by the sync manager on sync completion when processing saved items to update
their meta fields, like updated_at that comes from the server. We omit content in such a case
because content may be outdated from the time a sync begins to when it completes (user performed action in between).
So we will only ever update content from a remote source when it is retrieved by the server (serverResponse.retrieved_items)
*/
item.lastTouchSaved = omitFields && omitFields.includes("content");
this.addItem(item);
modelsToNotifyObserversOf.push(item);
@@ -159,18 +158,12 @@ class ModelManager {
}
}
/* Sometimes, a controller will want to map incoming state to the UI, but without yet notifiny all observers of an item change
Particulary, the componentManager sets dontNotifyObservers to true when it receives an update from a component and wnats
to update the remaining UI, but without receiving a (useless) syncObserverCallback immediately.
*/
if(!(options && options.dontNotifyObservers)) {
this.notifySyncObserversOfModels(modelsToNotifyObserversOf);
}
this.notifySyncObserversOfModels(modelsToNotifyObserversOf, source);
return models;
}
notifySyncObserversOfModels(models) {
notifySyncObserversOfModels(models, source) {
for(var observer of this.itemSyncObservers) {
var allRelevantItems = models.filter(function(item){return item.content_type == observer.type || observer.type == "*"});
var validItems = [], deletedItems = [];
@@ -183,7 +176,7 @@ class ModelManager {
}
if(allRelevantItems.length > 0) {
observer.callback(allRelevantItems, validItems, deletedItems);
observer.callback(allRelevantItems, validItems, deletedItems, source);
}
}
}