Skip to content

Commit ac43203

Browse files
committed
refactor getService
1 parent 1bce83c commit ac43203

File tree

1 file changed

+39
-42
lines changed
  • services/static-webserver/client/source/class/osparc/store

1 file changed

+39
-42
lines changed

services/static-webserver/client/source/class/osparc/store/Services.js

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -108,59 +108,56 @@ qx.Class.define("osparc.store.Services", {
108108
},
109109

110110
getService: function(key, version, useCache = true) {
111+
if (!this.__servicesPromisesCached) {
112+
this.__servicesPromisesCached = {};
113+
}
114+
if (!(key in this.__servicesPromisesCached)) {
115+
this.__servicesPromisesCached[key] = {};
116+
}
117+
111118
// avoid request deduplication
112-
if (key in this.__servicesPromisesCached && version in this.__servicesPromisesCached[key]) {
119+
if (this.__servicesPromisesCached[key][version]) {
113120
return this.__servicesPromisesCached[key][version];
114121
}
115122

116-
// Create a new promise
117-
const promise = new Promise((resolve, reject) => {
118-
if (
119-
useCache &&
120-
this.__isInCache(key, version) &&
121-
(
122-
this.__servicesCached[key][version] === null ||
123-
"history" in this.__servicesCached[key][version]
124-
)
125-
) {
126-
resolve(this.__servicesCached[key][version]);
127-
return;
128-
}
123+
if (
124+
useCache &&
125+
this.__isInCache(key, version) &&
126+
(
127+
this.__servicesCached[key][version] === null ||
128+
"history" in this.__servicesCached[key][version]
129+
)
130+
) {
131+
return Promise.resolve(this.__servicesCached[key][version]);
132+
}
129133

130-
if (!(key in this.__servicesPromisesCached)) {
131-
this.__servicesPromisesCached[key] = {};
132-
}
133-
const params = {
134-
url: osparc.data.Resources.getServiceUrl(key, version)
135-
};
136-
this.__servicesPromisesCached[key][version] = osparc.data.Resources.fetch("services", "getOne", params)
137-
.then(service => {
138-
this.__addServiceToCache(service);
139-
// Resolve the promise locally before deleting it
140-
resolve(service);
141-
})
142-
.catch(err => {
143-
// Store null in cache to avoid repeated failed requests
144-
this.__addToCache(key, version, null);
145-
console.error(err);
146-
reject(err);
147-
})
148-
.finally(() => {
149-
// Remove the promise from the cache
150-
delete this.__servicesPromisesCached[key][version];
151-
});
152-
});
134+
const params = {
135+
url: osparc.data.Resources.getServiceUrl(key, version)
136+
};
137+
const fetchPromise = osparc.data.Resources.fetch("services", "getOne", params)
138+
.then(service => {
139+
this.__addServiceToCache(service);
140+
// Resolve the promise locally before deleting it
141+
return service;
142+
})
143+
.catch(err => {
144+
// Store null in cache to avoid repeated failed requests
145+
this.__addToCache(key, version, null);
146+
console.error(err);
147+
throw err;
148+
})
149+
.finally(() => {
150+
// Remove the promise from the cache
151+
delete this.__servicesPromisesCached[key][version];
152+
});
153153

154154
// Store the promise in the cache
155155
// The point of keeping this assignment outside of the main Promise block is to
156156
// ensure that the promise is immediately stored in the cache before any asynchronous
157157
// operations (like fetch) are executed. This prevents duplicate requests for the
158158
// same key and version when multiple consumers call getService concurrently.
159-
if (!(key in this.__servicesPromisesCached)) {
160-
this.__servicesPromisesCached[key] = {};
161-
}
162-
this.__servicesPromisesCached[key][version] = promise;
163-
return promise;
159+
this.__servicesPromisesCached[key][version] = fetchPromise;
160+
return fetchPromise;
164161
},
165162

166163
getStudyServices: function(studyId) {

0 commit comments

Comments
 (0)