fix(container): cache the in-flight promise of an app-scoped binding (GitHub #46)
Container.resolve marked an app-scoped binding resolved only after its factory settled. Two resolutions that started before then both found nothing cached and both ran the factory, so one token could open two pools. The binding now caches the promise as soon as the factory starts, and every resolution returns that promise. Retry semantics are unchanged: before, a rejected factory set nothing and the next resolution ran it again. Now a rejection clears the cached promise, so the next resolution still retries, and only the callers already waiting on the failed attempt share its rejection. The factory runs inside an async wrapper so a synchronous throw takes the same path. createApp was not exposed to this in practice. bootProvider runs each provider's runner once, inside the memoized boot, and registers the finished value as a constant factory, and Container is not exported. The fix makes the container's own guarantee hold instead of relying on that. Refs: GitHub #46 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+67
-10
No files matched your search
+14
-10
@@ -6,8 +6,8 @@ export type Factory = (ctx: Record<string, unknown>) => unknown | Promise<unknow
|
||||
interface Binding {
|
||||
scope: Scope;
|
||||
factory: Factory;
|
||||
cached?: unknown;
|
||||
resolved?: boolean;
|
||||
/** The app-scoped value, cached as a promise from the moment the factory starts. */
|
||||
cached?: Promise<unknown>;
|
||||
}
|
||||
|
||||
/** Minimal token-keyed dependency container with app-scoped memoization and async, mesh-ready resolution. */
|
||||
@@ -30,14 +30,18 @@ export class Container {
|
||||
async resolve(token: string, ctx: Record<string, unknown> = {}): Promise<unknown> {
|
||||
const binding = this.bindings.get(token);
|
||||
if (!binding) throw new Error(`unknown token: ${token}`);
|
||||
if (binding.scope === 'app' && binding.resolved) return binding.cached;
|
||||
const value = await binding.factory(ctx);
|
||||
if (binding.scope === 'request') return binding.factory(ctx);
|
||||
if (binding.cached) return binding.cached;
|
||||
|
||||
if (binding.scope === 'app') {
|
||||
binding.cached = value;
|
||||
binding.resolved = true;
|
||||
}
|
||||
|
||||
return value;
|
||||
// The pending promise is cached, not the settled value (GitHub #46): a resolve that starts
|
||||
// while the factory is still running must wait for it, or it runs the factory a second time.
|
||||
// A rejection is not kept — it clears the cache, so the next resolve retries, as it did when
|
||||
// only successes were cached. The async wrapper turns a synchronous throw into that rejection.
|
||||
const pending = (async () => binding.factory(ctx))();
|
||||
binding.cached = pending;
|
||||
pending.catch(() => {
|
||||
if (binding.cached === pending) binding.cached = undefined;
|
||||
});
|
||||
return pending;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user