Skip to content

Commit 3017d5b

Browse files
committed
feat(kernel): Enhance plugin loader with context management and synchronous service retrieval
1 parent 677204c commit 3017d5b

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

packages/core/src/kernel.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ export class ObjectKernel {
8181
return service as T;
8282
}
8383

84-
// 2. Try to get from plugin loader (supports factories and lifecycle)
84+
// 2. Try to get from plugin loader cache (Sync access to factories)
85+
const loaderService = this.pluginLoader.getServiceInstance<T>(name);
86+
if (loaderService) {
87+
// Cache it locally for faster next access
88+
this.services.set(name, loaderService);
89+
return loaderService;
90+
}
91+
92+
// 3. Try to get from plugin loader (support async factories)
8593
try {
8694
const service = this.pluginLoader.getService(name);
8795
if (service instanceof Promise) {
@@ -116,6 +124,8 @@ export class ObjectKernel {
116124
getKernel: () => this as any, // Type compatibility
117125
};
118126

127+
this.pluginLoader.setContext(this.context);
128+
119129
// Register shutdown handler
120130
if (this.config.gracefulShutdown) {
121131
this.registerShutdownSignals();

packages/core/src/plugin-loader.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { describe, it, expect, beforeEach } from 'vitest';
22
import { PluginLoader, ServiceLifecycle, PluginMetadata } from './plugin-loader';
33
import { createLogger } from './logger';
4-
import type { Plugin } from './types';
4+
import type { Plugin, PluginContext } from './types';
55

66
describe('PluginLoader', () => {
77
let loader: PluginLoader;
88

99
beforeEach(() => {
1010
const logger = createLogger({ level: 'error' }); // Suppress logs in tests
1111
loader = new PluginLoader(logger);
12+
loader.setContext({
13+
registerService: () => {},
14+
getService: () => { throw new Error('Mock service not found'); },
15+
hook: () => {},
16+
trigger: async () => {},
17+
getServices: () => new Map(),
18+
logger: logger,
19+
getKernel: () => ({}) as any
20+
} as PluginContext);
1221
});
1322

1423
describe('Plugin Loading', () => {

packages/core/src/plugin-loader.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export interface VersionCompatibility {
111111
*/
112112
export class PluginLoader {
113113
private logger: Logger;
114+
private context?: PluginContext;
114115
private loadedPlugins: Map<string, PluginMetadata> = new Map();
115116
private serviceFactories: Map<string, ServiceRegistration> = new Map();
116117
private serviceInstances: Map<string, any> = new Map();
@@ -120,6 +121,20 @@ export class PluginLoader {
120121
this.logger = logger;
121122
}
122123

124+
/**
125+
* Set the plugin context for service factories
126+
*/
127+
setContext(context: PluginContext): void {
128+
this.context = context;
129+
}
130+
131+
/**
132+
* Get a synchronous service instance if it exists (Sync Helper)
133+
*/
134+
getServiceInstance<T>(name: string): T | undefined {
135+
return this.serviceInstances.get(name) as T;
136+
}
137+
123138
/**
124139
* Load a plugin asynchronously with validation
125140
*/
@@ -431,9 +446,9 @@ export class PluginLoader {
431446
}
432447

433448
private async createServiceInstance(registration: ServiceRegistration): Promise<any> {
434-
// This is a simplified version - in real implementation,
435-
// we would need to pass proper context with resolved dependencies
436-
const mockContext = {} as PluginContext;
437-
return await registration.factory(mockContext);
449+
if (!this.context) {
450+
throw new Error(`[PluginLoader] Context not set - cannot create service '${registration.name}'`);
451+
}
452+
return await registration.factory(this.context);
438453
}
439454
}

0 commit comments

Comments
 (0)