Skip to content

Commit a9b0c8a

Browse files
committed
portsCompatibilityPromisesCached
1 parent 49ea366 commit a9b0c8a

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

services/static-webserver/client/source/class/osparc/data/Resources.js

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,8 @@ qx.Class.define("osparc.data.Resources", {
14301430
},
14311431

14321432
members: {
1433+
__portsCompatibilityPromisesCached: null,
1434+
14331435
/**
14341436
* @param {String} resource Name of the resource as defined in the static property 'resources'.
14351437
* @param {String} endpoint Name of the endpoint. Several endpoints can be defined for each resource.
@@ -1722,26 +1724,6 @@ qx.Class.define("osparc.data.Resources", {
17221724
*/
17231725
__removeCached: function(resource, deleteId) {
17241726
osparc.store.Store.getInstance().remove(resource, this.self().resources[resource].idField || "uuid", deleteId);
1725-
}
1726-
},
1727-
1728-
statics: {
1729-
API: "/v0",
1730-
fetch: function(resource, endpoint, params, options = {}) {
1731-
return this.getInstance().fetch(resource, endpoint, params, options);
1732-
},
1733-
getOne: function(resource, params, id, useCache) {
1734-
return this.getInstance().getOne(resource, params, id, useCache);
1735-
},
1736-
get: function(resource, params, useCache, options) {
1737-
return this.getInstance().get(resource, params, useCache, options);
1738-
},
1739-
1740-
getServiceUrl: function(key, version) {
1741-
return {
1742-
"key": encodeURIComponent(key),
1743-
"version": version
1744-
};
17451727
},
17461728

17471729
getCompatibleInputs: function(node1, portId1, node2) {
@@ -1753,22 +1735,56 @@ qx.Class.define("osparc.data.Resources", {
17531735
"portKey1": portId1
17541736
};
17551737

1756-
// eslint-disable-next-line no-underscore-dangle
1757-
const cachedCPs = this.getInstance().__getCached("portsCompatibility") || {};
1738+
const cachedCPs = this.__getCached("portsCompatibility") || {};
17581739
const strUrl = JSON.stringify(url);
17591740
if (strUrl in cachedCPs) {
17601741
return Promise.resolve(cachedCPs[strUrl]);
17611742
}
1743+
1744+
// avoid request deduplication
1745+
if (this.__portsCompatibilityPromisesCached === null) {
1746+
this.__portsCompatibilityPromisesCached = {};
1747+
}
1748+
if (strUrl in this.__portsCompatibilityPromisesCached) {
1749+
console.log("returning cached promise for ports compatibility");
1750+
return this.__portsCompatibilityPromisesCached[strUrl];
1751+
}
1752+
17621753
const params = {
17631754
url
17641755
};
1765-
return this.fetch("portsCompatibility", "matchInputs", params)
1756+
this.__portsCompatibilityPromisesCached[strUrl] = this.fetch("portsCompatibility", "matchInputs", params)
17661757
.then(data => {
17671758
cachedCPs[strUrl] = data;
1768-
// eslint-disable-next-line no-underscore-dangle
1769-
this.getInstance().__setCached("portsCompatibility", cachedCPs);
1759+
this.__setCached("portsCompatibility", cachedCPs);
17701760
return data;
1761+
})
1762+
.finally(() => {
1763+
// Remove the promise from the cache
1764+
delete this.__portsCompatibilityPromisesCached[strUrl];
17711765
});
1766+
1767+
return this.__portsCompatibilityPromisesCached[strUrl];
1768+
},
1769+
},
1770+
1771+
statics: {
1772+
API: "/v0",
1773+
fetch: function(resource, endpoint, params, options = {}) {
1774+
return this.getInstance().fetch(resource, endpoint, params, options);
1775+
},
1776+
getOne: function(resource, params, id, useCache) {
1777+
return this.getInstance().getOne(resource, params, id, useCache);
1778+
},
1779+
get: function(resource, params, useCache, options) {
1780+
return this.getInstance().get(resource, params, useCache, options);
1781+
},
1782+
1783+
getServiceUrl: function(key, version) {
1784+
return {
1785+
"key": encodeURIComponent(key),
1786+
"version": version
1787+
};
17721788
},
17731789

17741790
getErrorMsg: function(resp) {

services/static-webserver/client/source/class/osparc/form/renderer/PropForm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ qx.Class.define("osparc.form.renderer.PropForm", {
764764
destinations[node2Id][portId] = "fetching";
765765
}
766766
});
767-
osparc.data.Resources.getCompatibleInputs(node1, dragPortId, this.getNode())
767+
osparc.data.Resources.getInstance().getCompatibleInputs(node1, dragPortId, this.getNode())
768768
.then(compatiblePorts => {
769769
this.getPortIds().forEach(portId => {
770770
destinations[node2Id][portId] = compatiblePorts.includes(portId);

services/static-webserver/client/source/class/osparc/utils/Ports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ qx.Class.define("osparc.utils.Ports", {
2525

2626
statics: {
2727
arePortsCompatible: function(node1, portId1, node2, portId2) {
28-
return osparc.data.Resources.getCompatibleInputs(node1, portId1, node2)
28+
return osparc.data.Resources.getInstance().getCompatibleInputs(node1, portId1, node2)
2929
.then(compatiblePorts => compatiblePorts.includes(portId2));
3030
},
3131

0 commit comments

Comments
 (0)