@eleboucher/opencode-memini 0.7.4 → 0.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/memini.js +14 -4
- package/package.json +1 -1
package/memini.js
CHANGED
|
@@ -443,15 +443,25 @@ export function effectiveConfig(cfg, hs) {
|
|
|
443
443
|
// shape MeminiPlugin uses to memoize the handshake per session. `now` is
|
|
444
444
|
// injectable so tests can drive expiry without a real 10-minute sleep.
|
|
445
445
|
// Exported for testing.
|
|
446
|
+
//
|
|
447
|
+
// Caches the promise, not the resolved value, so concurrent callers share
|
|
448
|
+
// one in-flight call. Clears on rejection so a transient failure doesn't
|
|
449
|
+
// poison the TTL window.
|
|
446
450
|
export function memoizeAsync(fn, ttlMs, now = Date.now) {
|
|
447
|
-
let cached = null; // {
|
|
451
|
+
let cached = null; // { promise, expiresAt }
|
|
448
452
|
return async () => {
|
|
449
453
|
const t = now();
|
|
450
454
|
if (!cached || t >= cached.expiresAt) {
|
|
451
|
-
const
|
|
452
|
-
cached = {
|
|
455
|
+
const promise = fn();
|
|
456
|
+
cached = { promise, expiresAt: t + ttlMs };
|
|
457
|
+
promise.then(
|
|
458
|
+
() => {},
|
|
459
|
+
() => {
|
|
460
|
+
if (cached && cached.promise === promise) cached = null;
|
|
461
|
+
},
|
|
462
|
+
);
|
|
453
463
|
}
|
|
454
|
-
return cached.
|
|
464
|
+
return cached.promise;
|
|
455
465
|
};
|
|
456
466
|
}
|
|
457
467
|
|