@finos/legend-application-marketplace
Version:
Legend Marketplace application core
75 lines • 2.58 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Default TTL for cached pending-tasks responses. The entitlements server
* was reporting overload from this endpoint, so we cache responses briefly
* and dedupe in-flight requests across all UI consumers (header badge,
* entitlements dashboard, etc.).
*/
export const PENDING_TASKS_CACHE_TTL_MS = 60_000;
/**
* Shared cache + in-flight request dedupe for `getPendingTasks`.
*
* - Multiple concurrent callers for the same user share a single network call.
* - Subsequent callers within `PENDING_TASKS_CACHE_TTL_MS` get the cached
* response without hitting the server.
* - Mutating operations (approve/deny) should call `invalidate()` so the
* next read refreshes.
*/
export class PendingTasksCache {
client;
cache = new Map();
inFlight = new Map();
constructor(client) {
this.client = client;
}
static keyFor(user) {
return user ?? '<self>';
}
invalidate(user) {
if (user === undefined) {
this.cache.clear();
}
else {
this.cache.delete(PendingTasksCache.keyFor(user));
}
}
async fetch(user, token, options) {
const key = PendingTasksCache.keyFor(user);
if (!options?.forceRefresh) {
const entry = this.cache.get(key);
if (entry && Date.now() - entry.fetchedAt < PENDING_TASKS_CACHE_TTL_MS) {
return entry.response;
}
const pending = this.inFlight.get(key);
if (pending) {
return pending;
}
}
const request = this.client
.getPendingTasks(user, token)
.then((response) => {
this.cache.set(key, { response, fetchedAt: Date.now() });
return response;
})
.finally(() => {
this.inFlight.delete(key);
});
this.inFlight.set(key, request);
return request;
}
}
//# sourceMappingURL=PendingTasksCache.js.map