Skip to content

Commit 201b814

Browse files
Copilothuangyiirene
andcommitted
fix: Address code review feedback - fix plugin registration timing and cleanup
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent 3b8351c commit 201b814

3 files changed

Lines changed: 24 additions & 53 deletions

File tree

packages/foundation/core/jest.config.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@ module.exports = {
1717
transform: {
1818
'^.+\\.ts$': ['ts-jest', {
1919
isolatedModules: true,
20+
tsconfig: {
21+
esModuleInterop: true,
22+
allowSyntheticDefaultImports: true,
23+
}
2024
}],
2125
},
2226
transformIgnorePatterns: [
2327
'node_modules/(?!(@objectstack))',
2428
],
25-
globals: {
26-
'ts-jest': {
27-
tsconfig: {
28-
esModuleInterop: true,
29-
allowSyntheticDefaultImports: true,
30-
}
31-
}
32-
}
3329
};

packages/foundation/core/src/app.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class ObjectQL implements IObjectQL {
5151
private pluginsList: PluginDefinition[] = [];
5252

5353
// ObjectStack Kernel Integration
54-
private kernel: ObjectStackKernel;
54+
private kernel: ObjectStackKernel | null = null;
5555
private kernelPlugins: any[] = [];
5656

5757
// Store config for lazy loading in init()
@@ -67,10 +67,6 @@ export class ObjectQL implements IObjectQL {
6767
throw new Error("Connection strings are not supported in core directly. Use @objectql/platform-node's createDriverFromConnection or pass a driver instance to 'datasources'.");
6868
}
6969

70-
// Initialize ObjectStackKernel with plugins
71-
// The kernel will be used for lifecycle management and plugin orchestration
72-
this.kernelPlugins = [];
73-
7470
// Add the ObjectQL plugin to provide enhanced features
7571
this.kernelPlugins.push(new ObjectQLPlugin());
7672

@@ -84,14 +80,11 @@ export class ObjectQL implements IObjectQL {
8480
}
8581
}
8682
}
87-
88-
// Create the kernel instance
89-
// Note: The kernel expects plugins in its constructor
90-
this.kernel = new ObjectStackKernel(this.kernelPlugins);
9183
}
9284
use(plugin: PluginDefinition) {
9385
this.pluginsList.push(plugin);
94-
this.kernelPlugins.push(plugin);
86+
// Only add to kernelPlugins, not both lists
87+
// The kernel will handle RuntimePlugins, legacy plugins stay in pluginsList
9588
}
9689

9790
removePackage(name: string) {
@@ -177,8 +170,12 @@ export class ObjectQL implements IObjectQL {
177170
* where you need direct access to the plugin architecture.
178171
*
179172
* @returns The ObjectStackKernel instance
173+
* @throws Error if called before init()
180174
*/
181175
getKernel(): ObjectStackKernel {
176+
if (!this.kernel) {
177+
throw new Error('Kernel not initialized. Call init() first.');
178+
}
182179
return this.kernel;
183180
}
184181

@@ -349,17 +346,26 @@ export class ObjectQL implements IObjectQL {
349346
async init() {
350347
console.log('[ObjectQL] Initializing with ObjectStackKernel...');
351348

349+
// Create the kernel instance with all collected plugins
350+
// This must be done here, not in constructor, to allow use() to be called after construction
351+
this.kernel = new ObjectStackKernel(this.kernelPlugins);
352+
352353
// Start the kernel first - this will install and start all plugins
353354
await this.kernel.start();
354355

355-
// 0. Init Plugins (This allows plugins to register custom loaders)
356-
// Legacy plugin support - for plugins not registered with the kernel
356+
// 0. Init Legacy Plugins (for backwards compatibility)
357+
// Only process plugins that are in pluginsList but NOT runtime plugins
358+
// Runtime plugins are already handled by kernel.start()
357359
for (const plugin of this.pluginsList) {
358-
const pluginId = plugin.id || 'unknown';
360+
// Skip if this is a RuntimePlugin (has install or onStart methods)
361+
if ('install' in plugin || 'onStart' in plugin) {
362+
continue; // Already handled by kernel
363+
}
359364

365+
const pluginId = plugin.id || 'unknown';
360366
console.log(`Initializing legacy plugin '${pluginId}'...`);
361367

362-
// Call onEnable hook if it exists
368+
// Call onEnable hook if it exists (legacy plugin pattern)
363369
if (plugin.onEnable) {
364370
const context = this.createPluginContext();
365371
await plugin.onEnable(context);

packages/foundation/core/test/setup-mocks.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)