Skip to content

Commit 0b6a159

Browse files
B4nanclaude
andauthored
refactor: replace service locator forwarding object with Proxy (#3515)
## Summary - Replace the hand-written method-by-method forwarding object on `serviceLocator` with a `Proxy`-based approach - Eliminates repetitive delegation pattern — new methods added to `ServiceLocator` are automatically forwarded without updating the exported object - Adds a `set` trap that throws a helpful `TypeError` when users try to assign properties directly instead of using setter methods (e.g. `setConfiguration()`) ## Context Split out from #3080 (Configuration class redesign) since this change is independent and worth discussing separately. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cd55e00 commit 0b6a159

File tree

1 file changed

+13
-52
lines changed

1 file changed

+13
-52
lines changed

packages/core/src/service_locator.ts

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -383,57 +383,18 @@ export function bindMethodsToServiceLocator(
383383
};
384384
}
385385

386-
export const serviceLocator: ServiceLocatorInterface = {
387-
getConfiguration(): Configuration {
388-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
389-
return currentServiceLocator.getConfiguration();
390-
},
391-
setConfiguration(configuration: Configuration): void {
392-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
393-
currentServiceLocator.setConfiguration(configuration);
394-
},
395-
getEventManager(): EventManager {
396-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
397-
return currentServiceLocator.getEventManager();
398-
},
399-
setEventManager(eventManager: EventManager): void {
400-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
401-
currentServiceLocator.setEventManager(eventManager);
402-
},
403-
getStorageClient(): StorageClient {
404-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
405-
return currentServiceLocator.getStorageClient();
406-
},
407-
setStorageClient(storageClient: StorageClient): void {
408-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
409-
currentServiceLocator.setStorageClient(storageClient);
410-
},
411-
getLogger(): CrawleeLogger {
412-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
413-
return currentServiceLocator.getLogger();
414-
},
415-
setLogger(logger: CrawleeLogger): void {
416-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
417-
currentServiceLocator.setLogger(logger);
418-
},
419-
getChildLog(prefix: string): CrawleeLogger {
420-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
421-
return currentServiceLocator.getChildLog(prefix);
422-
},
423-
getStorageManager(constructor: Constructor<IStorage>): StorageManager | undefined {
424-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
425-
return currentServiceLocator.getStorageManager(constructor);
426-
},
427-
setStorageManager(constructor: Constructor<IStorage>, storageManager: StorageManager): void {
428-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
429-
currentServiceLocator.setStorageManager(constructor, storageManager);
430-
},
431-
clearStorageManagerCache(): void {
432-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
433-
currentServiceLocator.clearStorageManagerCache();
386+
export const serviceLocator = new Proxy({} as ServiceLocatorInterface, {
387+
get(_target, prop) {
388+
const active = serviceLocatorStorage.getStore() ?? globalServiceLocator;
389+
const value = Reflect.get(active, prop, active);
390+
if (typeof value === 'function') {
391+
return value.bind(active);
392+
}
393+
return value;
434394
},
435-
reset(): void {
436-
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
437-
currentServiceLocator.reset();
395+
set(_target, prop) {
396+
throw new TypeError(
397+
`Cannot set property '${String(prop)}' on serviceLocator directly. Use the setter methods (e.g. setConfiguration(), setStorageClient()) instead.`,
398+
);
438399
},
439-
};
400+
});

0 commit comments

Comments
 (0)