From b0b0c933372d3a986aaf635c8a2c33b360c34628 Mon Sep 17 00:00:00 2001 From: samanhappy Date: Sun, 20 Jul 2025 20:35:00 +0800 Subject: [PATCH] feat: enable immediate loading of service overrides during registration (#232) --- src/services/registry.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/services/registry.ts b/src/services/registry.ts index 71099ec..c589ef1 100644 --- a/src/services/registry.ts +++ b/src/services/registry.ts @@ -2,12 +2,24 @@ type Class = new (...args: any[]) => T; interface Service { defaultImpl: Class; + override?: Class; } const registry = new Map>(); const instances = new Map(); export function registerService(key: string, entry: Service) { + // Try to load override immediately during registration + const overridePath = './' + key + 'x.js'; + import(overridePath) + .then((mod) => { + const override = mod[key.charAt(0).toUpperCase() + key.slice(1) + 'x']; + if (typeof override === 'function') { + entry.override = override; + } + }) + .catch(() => {}); // Silently ignore if override doesn't exist + registry.set(key, entry); } @@ -19,17 +31,8 @@ export function getService(key: string): T { const entry = registry.get(key); if (!entry) throw new Error(`Service not registered for key: ${key.toString()}`); - let Impl = entry.defaultImpl; - - const overridePath = './' + key + 'x.js'; - import(overridePath) - .then((mod) => { - const override = mod[key.charAt(0).toUpperCase() + key.slice(1) + 'x'] ?? Impl.name; - if (typeof override === 'function') { - Impl = override; - } - }) - .catch(() => {}); + // Use override if available, otherwise use default + const Impl = entry.override || entry.defaultImpl; const instance = new Impl(); instances.set(key, instance);