@cloud-copilot/iam-lens
Version:
Visibility in IAM in and across AWS accounts
49 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SharedArrayBufferWorkerCache = void 0;
const buffers_js_1 = require("./buffers.js");
//TODO: Add a Prefix to avoid collisions if there are multiple caches
class SharedArrayBufferWorkerCache {
parentPort;
requestPromiseMap = {};
parentPromiseMap = {};
constructor(parentPort) {
this.parentPort = parentPort;
this.parentPort.on('message', (msg) => {
if (msg.type === 'cacheHit' || msg.type === 'cacheMiss') {
const resolver = this.parentPromiseMap[msg.cacheKey];
if (resolver) {
delete this.parentPromiseMap[msg.cacheKey]; // clean up
resolver(msg.type === 'cacheHit' ? msg.value : false);
}
}
});
}
async withCache(cacheKey, fetcher) {
if (this.requestPromiseMap[cacheKey]) {
return this.requestPromiseMap[cacheKey];
}
this.requestPromiseMap[cacheKey] = (async () => {
try {
const parentPromise = new Promise((resolve) => {
this.parentPromiseMap[cacheKey] = resolve;
this.parentPort.postMessage({ type: 'getCache', cacheKey });
});
const response = await parentPromise;
if (response === false) {
const theValue = await fetcher(); // your async loader
const arrayBuffer = (0, buffers_js_1.convertObjectToArrayBuffer)(theValue);
this.parentPort.postMessage({ type: 'saveCache', cacheKey, value: arrayBuffer });
return theValue;
}
return (0, buffers_js_1.convertArrayBufferToObject)(response);
}
finally {
delete this.requestPromiseMap[cacheKey]; // Clean up the queue regardless of success or failure.
}
})();
return this.requestPromiseMap[cacheKey];
}
}
exports.SharedArrayBufferWorkerCache = SharedArrayBufferWorkerCache;
//# sourceMappingURL=SharedArrayBufferWorkerCache.js.map