UNPKG

@cloud-copilot/iam-lens

Version:

Visibility in IAM in and across AWS accounts

46 lines 2.08 kB
import { MessagePort } from 'worker_threads'; import {} from './SharedArrayBufferMainCache.js'; import { convertArrayBufferToObject, convertObjectToArrayBuffer } from './buffers.js'; //TODO: Add a Prefix to avoid collisions if there are multiple caches export class SharedArrayBufferWorkerCache { constructor(parentPort) { this.parentPort = parentPort; this.requestPromiseMap = {}; this.parentPromiseMap = {}; 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 = convertObjectToArrayBuffer(theValue); this.parentPort.postMessage({ type: 'saveCache', cacheKey, value: arrayBuffer }); return theValue; } return convertArrayBufferToObject(response); } finally { delete this.requestPromiseMap[cacheKey]; // Clean up the queue regardless of success or failure. } })(); return this.requestPromiseMap[cacheKey]; } } //# sourceMappingURL=SharedArrayBufferWorkerCache.js.map