@1hive/connect-core
Version:
Access and interact with Aragon Organizations and their apps.
33 lines • 1.12 kB
JavaScript
export function createCacheStore(limit = 10) {
const cache = Array(limit);
return {
clear() {
cache.length = 0;
},
cachedIndex(id) {
return cache.findIndex((entry) => id === (entry === null || entry === void 0 ? void 0 : entry[0]));
},
touch(index) {
cache.unshift(cache.splice(index, 1)[0]);
},
async get(id, callback) {
let cachedIndex = this.cachedIndex(id);
if (cachedIndex > -1) {
this.touch(cachedIndex);
return cache[0][1];
}
const data = await callback();
// Prevents to cache the same value multiple times, in case the same
// value gets loaded several times in parallel.
cachedIndex = this.cachedIndex(id);
if (cachedIndex > -1) {
this.touch(cachedIndex);
return cache[0][1];
}
cache.unshift([id, data]);
cache.length = limit;
return data;
},
};
}
//# sourceMappingURL=cache-store.js.map