Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1x 1x 1x 1x 3x 3x 2x 2x 2x 3x 3x | import { CoreSemaphor } from './CoreSemaphor';
/**
* A cache wrapper around an asynchronous function that stores the result
* for a specified timeout period. Subsequent calls within the timeout
* return the cached value instead of invoking the function again.
*
* @template T The type of the value returned by the wrapped function.
*/
export default class CoreCachedFc<T> {
fc: () => Promise<T>;
timeOut: number;
data: T | null;
lock: CoreSemaphor;
constructor(timeout: number, fc: () => Promise<T>) {
this.fc = fc;
this.data = null;
this.timeOut = timeout;
this.lock = new CoreSemaphor();
}
async get() {
const release = await this.lock.request();
if (!this.data) {
this.data = await this.fc();
setTimeout(() => {
this.data = null;
}, this.timeOut);
}
release();
return this.data;
}
}
|