add max attempts to lazys

This commit is contained in:
V 2023-09-23 03:25:19 +02:00
parent ac0f834155
commit 08c5d23636
No known key found for this signature in database
GPG key ID: A1DC0CFB5615D905
2 changed files with 18 additions and 5 deletions

View file

@ -16,9 +16,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export function makeLazy<T>(factory: () => T): () => T {
export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
let tries = 0;
let cache: T;
return () => cache ?? (cache = factory());
return () => {
if (!cache && attempts > tries++) {
cache = factory();
if (!cache && attempts === tries)
console.error("Lazy factory failed:", factory);
}
return cache;
};
}
// Proxies demand that these properties be unmodified, so proxyLazy
@ -85,6 +93,8 @@ export function proxyLazy<T>(factory: () => T, attempts = 5): T {
[kGET]() {
if (!proxyDummy[kCACHE] && attempts > tries++) {
proxyDummy[kCACHE] = factory();
if (!proxyDummy[kCACHE] && attempts === tries)
console.error("Lazy factory failed:", factory);
}
return proxyDummy[kCACHE];
}