From 90fa384965b15121579ef1f18e7267f3148af95d Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Fri, 19 May 2017 16:46:38 +0200 Subject: [PATCH] promises are simplified --- frontend-js/src/main/js/map/data/MapModel.js | 342 +++++++++---------- 1 file changed, 166 insertions(+), 176 deletions(-) diff --git a/frontend-js/src/main/js/map/data/MapModel.js b/frontend-js/src/main/js/map/data/MapModel.js index 13178150e6..7d810f1f75 100644 --- a/frontend-js/src/main/js/map/data/MapModel.js +++ b/frontend-js/src/main/js/map/data/MapModel.js @@ -122,34 +122,31 @@ MapModel.prototype.getAliasById = function(id, complete) { if (complete) { return this.getCompleteAliasById(id); } - return new Promise(function(resolve, reject) { - if (self._aliases[id] !== undefined) { - resolve(self._aliases[id]); - } else { - self.getMissingElements({aliasIds: [id]}).then(function(){ - resolve(self._aliases[id]); - }).catch(reject); - } - }); + if (self._aliases[id] !== undefined) { + return Promise.resolve(self._aliases[id]); + } else { + return self.getMissingElements({ + aliasIds : [ id ] + }).then(function() { + return self._aliases[id]; + }); + } }; - MapModel.prototype.getCompleteAliasById = function(id) { var self = this; - return new Promise(function(resolve, reject) { - if (self._aliases[id]!==undefined && self._aliases[id].isComplete()) { - resolve(self._aliases[id]); - } else { - ServerConnector.getAliases([id]).then(function(aliases){ - if (self._aliases[id] === undefined) { - self._aliases[id] = aliases[0]; - } else { - self._aliases[id] .update(aliases[0]); - } - resolve(self._aliases[id]); - }, reject); - } - }); + if (self._aliases[id] !== undefined && self._aliases[id].isComplete()) { + return Promise.resolve(self._aliases[id]); + } else { + return ServerConnector.getAliases([ id ]).then(function(aliases) { + if (self._aliases[id] === undefined) { + self._aliases[id] = aliases[0]; + } else { + self._aliases[id].update(aliases[0]); + } + return self._aliases[id]; + }); + } }; /** @@ -164,80 +161,84 @@ MapModel.prototype.getReactionById = function(id, complete) { if (complete) { return this.getCompleteReactionById(id); } - return new Promise(function(resolve, reject) { - if (self._reactions[id] !== undefined) { - resolve(self._reactions[id]); - } else { - self.getMissingElements({reactionIds:[id]}).then(function(){ - resolve(self._reactions[id]); - }).catch(reject); - } - }); + if (self._reactions[id] !== undefined) { + return Promise.resolve(self._reactions[id]); + } else { + return self.getMissingElements({ + reactionIds : [ id ] + }).then(function() { + return self._reactions[id]; + }); + } }; MapModel.prototype.getCompleteReactionById = function(id) { var self = this; - return new Promise(function(resolve, reject) { - if (self._reactions[id] instanceof Reaction && self._reactions[id].isComplete()) { - resolve(self._reactions[id]); - } else { - var result; - return ServerConnector.getReactions([id]).then(function(reactions){ - if (self._reactions[id] === undefined) { - self._reactions[id] = reactions[0]; - } else { - self._reactions[id] .update(reactions[0]); - } - var ids =[]; - var i; - result =self._reactions[id]; - for (i=0;i<result.getReactants().length;i++) { - if (!(result.getReactants()[i] instanceof Alias)) { - if (self._aliases[result.getReactants()[i]]===undefined || !self._aliases[result.getReactants()[i]].isComplete()) { - ids.push(result.getReactants()[i]); - } + if (self._reactions[id] instanceof Reaction && self._reactions[id].isComplete()) { + return Promise.resolve(self._reactions[id]); + } else { + var result; + return ServerConnector.getReactions([ id ]).then( + function(reactions) { + if (self._reactions[id] === undefined) { + self._reactions[id] = reactions[0]; + } else { + self._reactions[id].update(reactions[0]); } - } - for (i=0;i<result.getProducts().length;i++) { - if (!(result.getProducts()[i] instanceof Alias)) { - if (self._aliases[result.getProducts()[i]]===undefined || !self._aliases[result.getProducts()[i]].isComplete()) { - ids.push(result.getProducts()[i]); + var ids = []; + var i; + result = self._reactions[id]; + for (i = 0; i < result.getReactants().length; i++) { + if (!(result.getReactants()[i] instanceof Alias)) { + if (self._aliases[result.getReactants()[i]] === undefined + || !self._aliases[result.getReactants()[i]].isComplete()) { + ids.push(result.getReactants()[i]); + } } } - } - for (i=0;i<result.getModifiers().length;i++) { - if (!(result.getModifiers()[i] instanceof Alias)) { - if (self._aliases[result.getModifiers()[i]]===undefined || !self._aliases[result.getModifiers()[i]].isComplete()) { - ids.push(result.getModifiers()[i]); + for (i = 0; i < result.getProducts().length; i++) { + if (!(result.getProducts()[i] instanceof Alias)) { + if (self._aliases[result.getProducts()[i]] === undefined + || !self._aliases[result.getProducts()[i]].isComplete()) { + ids.push(result.getProducts()[i]); + } } } - } - return self.getMissingElements({aliasIds:ids, complete : true}); - }).then(function(){ - var i; - result =self._reactions[id]; - for (i=0;i<result.getReactants().length;i++) { - if (!(result.getReactants()[i] instanceof Alias)) { - result.getReactants()[i] = self._aliases[result.getReactants()[i]]; + for (i = 0; i < result.getModifiers().length; i++) { + if (!(result.getModifiers()[i] instanceof Alias)) { + if (self._aliases[result.getModifiers()[i]] === undefined + || !self._aliases[result.getModifiers()[i]].isComplete()) { + ids.push(result.getModifiers()[i]); + } + } } + return self.getMissingElements({ + aliasIds : ids, + complete : true + }); + }).then(function() { + var i; + result = self._reactions[id]; + for (i = 0; i < result.getReactants().length; i++) { + if (!(result.getReactants()[i] instanceof Alias)) { + result.getReactants()[i] = self._aliases[result.getReactants()[i]]; } - for (i=0;i<result.getProducts().length;i++) { - if (!(result.getProducts()[i] instanceof Alias)) { - result.getProducts()[i] = self._aliases[result.getProducts()[i]]; - } + } + for (i = 0; i < result.getProducts().length; i++) { + if (!(result.getProducts()[i] instanceof Alias)) { + result.getProducts()[i] = self._aliases[result.getProducts()[i]]; } - for (i=0;i<result.getModifiers().length;i++) { - if (!(result.getModifiers()[i] instanceof Alias)) { - result.getModifiers()[i] = self._aliases[result.getModifiers()[i]]; - } + } + for (i = 0; i < result.getModifiers().length; i++) { + if (!(result.getModifiers()[i] instanceof Alias)) { + result.getModifiers()[i] = self._aliases[result.getModifiers()[i]]; } - resolve(result); - }).catch(reject); - } - }); + } + return result; + }); + } }; - MapModel.prototype.getMissingElements = function(elements) { var self = this; @@ -245,81 +246,77 @@ MapModel.prototype.getMissingElements = function(elements) { var aliasIds = []; var reactionIds = []; - var i=0; - if (elements.reactionIds!==undefined) { + var i = 0; + if (elements.reactionIds !== undefined) { reactionIds.push.apply(reactionIds, elements.reactionIds); - for (i=0;i<reactionIds.length;i++) { - this._missingReactions[reactionIds[i]]=reactionIds[i]; + for (i = 0; i < reactionIds.length; i++) { + this._missingReactions[reactionIds[i]] = reactionIds[i]; } - + } - if (elements.aliasIds!==undefined) { + if (elements.aliasIds !== undefined) { aliasIds.push.apply(aliasIds, elements.aliasIds); - for (i=0;i<aliasIds.length;i++) { - this._missingAliases[aliasIds[i]]=aliasIds[i]; - } + for (i = 0; i < aliasIds.length; i++) { + this._missingAliases[aliasIds[i]] = aliasIds[i]; + } } - for (i=0;i<layouts.length;i++) { + for (i = 0; i < layouts.length; i++) { var layout = layouts[i]; if (layout.isInitialized()) { var aliases = layout.getAliases(); - for (var j =0; j<aliases.length;j++) { + for (var j = 0; j < aliases.length; j++) { var alias = aliases[j]; - if (this._aliases[alias.getId()]===undefined && this._missingAliases[alias.getId()] === undefined) { - this._missingAliases[alias.getId()]=alias.getId(); + if (this._aliases[alias.getId()] === undefined && this._missingAliases[alias.getId()] === undefined) { + this._missingAliases[alias.getId()] = alias.getId(); aliasIds.push(alias.getId()); } } - - var reactions= layout.getReactions(); - for (var k =0; k<reactions.length;k++) { - var reaction= reactions[j]; - if (this._reactions[reaction.getId()]===undefined && this._missingReactions[reaction.getId()]=== undefined) { - this._missingReactions[reaction.getId()]=reaction.getId(); + + var reactions = layout.getReactions(); + for (var k = 0; k < reactions.length; k++) { + var reaction = reactions[j]; + if (this._reactions[reaction.getId()] === undefined && this._missingReactions[reaction.getId()] === undefined) { + this._missingReactions[reaction.getId()] = reaction.getId(); reactionIds.push(reaction.getId()); } } } } var reactionPromise = null; - if (reactionIds.length>0){ + if (reactionIds.length > 0) { reactionPromise = ServerConnector.getReactions(reactionIds); } - var aliasPromise = null; - if (aliasIds.length>0){ - if (elements.complete){ - aliasPromise = ServerConnector.getAliases(aliasIds); + var aliasPromise = null; + if (aliasIds.length > 0) { + if (elements.complete) { + aliasPromise = ServerConnector.getAliases(aliasIds); } else { - aliasPromise = ServerConnector.getLightAliases(aliasIds); - + aliasPromise = ServerConnector.getLightAliases(aliasIds); + } } - return new Promise(function(resolve, reject) { - Promise.all([reactionPromise, aliasPromise]).then(function(values) { - var result = [], i; - var reactions = values[0]; - var aliases = values[1]; - if (reactions!==null) { - for (i=0;i<reactions.length;i++) { - var reaction = reactions[i]; - self.addReaction(reaction); - result.push(reaction); - } + return Promise.all([ reactionPromise, aliasPromise ]).then(function(values) { + var result = [], i; + var reactions = values[0]; + var aliases = values[1]; + if (reactions !== null) { + for (i = 0; i < reactions.length; i++) { + var reaction = reactions[i]; + self.addReaction(reaction); + result.push(reaction); } - if (aliases!==null) { - for (i=0;i<aliases.length;i++) { - var alias= aliases[i]; - self.addAlias(alias); - result.push(alias); - } + } + if (aliases !== null) { + for (i = 0; i < aliases.length; i++) { + var alias = aliases[i]; + self.addAlias(alias); + result.push(alias); } - resolve(result); - }).catch(function(err) { - reject(err); - }); + } + return result; }); }; @@ -332,16 +329,14 @@ MapModel.prototype.getMissingElements = function(elements) { */ MapModel.prototype.getLayoutDataById = function(layoutId) { var self = this; - return new Promise(function(resolve, reject) { - if (self._layoutsData[layoutId] !== undefined) { - resolve(self._layoutsData[layoutId]); - } else { - ServerConnector.getOverlayById(layoutId).then(function(layout) { - self.addLayout(layout); - resolve(self._layoutsData[layoutId]); - }, reject); - } - }); + if (self._layoutsData[layoutId] !== undefined) { + return Promise.resolve(self._layoutsData[layoutId]); + } else { + return ServerConnector.getOverlayById(layoutId).then(function(layout) { + self.addLayout(layout); + return self._layoutsData[layoutId]; + }); + } }; /** @@ -352,7 +347,7 @@ MapModel.prototype.getLayoutDataById = function(layoutId) { */ MapModel.prototype.addAlias = function(aliasData) { var alias = aliasData; - if (!(aliasData instanceof Alias)) { + if (!(aliasData instanceof Alias)) { alias = new Alias(aliasData); } if (this._aliases[alias.getId()] !== undefined) { @@ -584,10 +579,10 @@ MapModel.prototype.getSubmodels = function() { MapModel.prototype.getSubmodelById = function(id) { if (this.getId() === id) { return this; - } - for (var i=0;i<this._submodels.length;i++) { - if (this._submodels[i].getId()===id) { - return this._submodels[i]; + } + for (var i = 0; i < this._submodels.length; i++) { + if (this._submodels[i].getId() === id) { + return this._submodels[i]; } } return null; @@ -595,12 +590,8 @@ MapModel.prototype.getSubmodelById = function(id) { MapModel.prototype.initLayoutDataByLayoutId = function(layoutId) { var self = this; - return new Promise(function(resolve, reject) { - self.getLayoutDataById(layoutId).then(function(layout) { - return layout.init(); - }).then(function(){ - resolve(); - }).catch(reject); + return self.getLayoutDataById(layoutId).then(function(layout) { + return layout.init(); }); }; @@ -615,12 +606,12 @@ MapModel.prototype._getLayouts = function() { }; MapModel.prototype.getByIdentifiedElement = function(ie, complete) { - if (ie.getType()==="ALIAS") { + if (ie.getType() === "ALIAS") { return this.getAliasById(ie.getId(), complete); - } else if (ie.getType()==="REACTION") { + } else if (ie.getType() === "REACTION") { return this.getReactionById(ie.getId(), complete); } else { - throw new Error("Unknown type: "+ie.getType(), complete); + throw new Error("Unknown type: " + ie.getType(), complete); } }; @@ -628,45 +619,44 @@ MapModel.prototype.getByIdentifiedElements = function(identifiedElements, comple var self = this; var missingAliases = []; var missingReactions = []; - - for (var i=0;i<identifiedElements.length;i++) { + + for (var i = 0; i < identifiedElements.length; i++) { var ie = identifiedElements[i]; if (!this.isAvailable(ie, complete)) { - if (ie.getType()==="ALIAS") { + if (ie.getType() === "ALIAS") { missingAliases.push(ie.getId()); - } else if (ie.getType()==="REACTION") { + } else if (ie.getType() === "REACTION") { missingReactions.push(ie.getId()); } else { - throw new Error("Unknown type "+ie); + throw new Error("Unknown type " + ie); } } } - - return new Promise(function(resolve,reject){ - return self.getMissingElements({aliasIds:missingAliases, reactionIds:missingReactions, complete: true}).then(function(){ - var promises =[]; - for (var i=0;i<identifiedElements.length;i++) { - promises.push(self.getByIdentifiedElement(identifiedElements[i])); - } - return Promise.all(promises); - }).then(function(elements){ - resolve(elements); - }).catch(reject); + + return self.getMissingElements({ + aliasIds : missingAliases, + reactionIds : missingReactions, + complete : true + }).then(function() { + var promises = []; + for (var i = 0; i < identifiedElements.length; i++) { + promises.push(self.getByIdentifiedElement(identifiedElements[i])); + } + return Promise.all(promises); }); - -}; +}; MapModel.prototype.isAvailable = function(ie, complete) { var element; - if (ie.getType()==="ALIAS") { + if (ie.getType() === "ALIAS") { element = this._aliases[ie.getId()]; - } else if (ie.getType()==="REACTION") { + } else if (ie.getType() === "REACTION") { element = this._reactions[ie.getId()]; } else { - throw new Error("Unknown type: "+ie.getType(), complete); + throw new Error("Unknown type: " + ie.getType(), complete); } - if (element===undefined) { + if (element === undefined) { return false; } else if (complete) { return element.isComplete(); -- GitLab