diff --git a/apps/web-next/src/server/bind-production.ts b/apps/web-next/src/server/bind-production.ts index 021ebe9..4dce47a 100644 --- a/apps/web-next/src/server/bind-production.ts +++ b/apps/web-next/src/server/bind-production.ts @@ -28,7 +28,7 @@ import { bindDevSeedMarketingPages } from "@repo/marketing-pages/di/bind-dev-see import { bindDevSeedNavigation } from "@repo/navigation/di/bind-dev-seed"; import { bindDevSeedMedia } from "@repo/media/di/bind-dev-seed"; -let bound = false; +let bindPromise: Promise | null = null; // Shared container holds TRACER + LOGGER bindings; per-feature containers // receive references via parameter passing. This separates the instrumentation @@ -87,8 +87,6 @@ function resolveJobsDevSeed(): { queue: IJobQueue } { * feature via `bindProductionX` exports. */ export async function bindAllProduction(): Promise { - if (bound) return; - bound = true; const { tracer, logger } = resolveInstrumentation(); // Rule 0 const { queue } = await resolveJobsProduction(); const resolvedConfig = await config; @@ -114,8 +112,6 @@ export async function bindAllProduction(): Promise { * Payload booted. Mutually exclusive with `bindAllProduction()`. */ export async function bindAllDevSeed(): Promise { - if (bound) return; - bound = true; const { tracer, logger } = resolveInstrumentation(); // Rule 0 const { queue } = resolveJobsDevSeed(); @@ -150,25 +146,25 @@ export async function bindAllDevSeed(): Promise { * When @repo/core-realtime is scaffolded, extend to accept realtime deps * (IRealtimeBroadcaster, IRealtimeHandlerRegistry) and pass them through. */ -export async function bindAll(): Promise { +export function bindAll(): Promise { + if (bindPromise) return bindPromise; + if (process.env.USE_DEV_SEED === "false") { - await bindAllProduction(); - return; + bindPromise = bindAllProduction(); + } else if (process.env.USE_DEV_SEED === "true") { + bindPromise = bindAllDevSeed(); + } else if (process.env.NODE_ENV === "production") { + bindPromise = bindAllProduction(); + } else { + bindPromise = bindAllDevSeed(); } - if (process.env.USE_DEV_SEED === "true") { - await bindAllDevSeed(); - return; - } - if (process.env.NODE_ENV === "production") { - await bindAllProduction(); - return; - } - await bindAllDevSeed(); + + return bindPromise; } /** Test-only resets — not exported via package. Used by bind-production.test.ts. */ export function __resetBindStateForTests(): void { - bound = false; + bindPromise = null; resolvedTracer = null; resolvedLogger = null; resolvedQueue = null;