Skip to content

Commit 5220890

Browse files
authored
Merge pull request #333 from objectstack-ai/copilot/replace-runtime-kernel-with-mini-kernel
2 parents 71bca3c + 068c40f commit 5220890

File tree

13 files changed

+497
-149
lines changed

13 files changed

+497
-149
lines changed

examples/custom-objectql-example.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* custom configuration.
77
*/
88

9-
import { ObjectStackKernel, ObjectQLPlugin, ObjectQL } from '@objectstack/runtime';
9+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, ObjectQL } from '@objectstack/runtime';
1010
import { InMemoryDriver } from '@objectstack/driver-memory';
1111

1212
(async () => {
@@ -27,18 +27,22 @@ import { InMemoryDriver } from '@objectstack/driver-memory';
2727
});
2828

2929
// Create kernel with the custom ObjectQL instance
30-
const kernel = new ObjectStackKernel([
30+
const kernel = new ObjectKernel();
31+
32+
kernel
3133
// Register your custom ObjectQL instance
32-
new ObjectQLPlugin(customQL),
34+
.use(new ObjectQLPlugin(customQL))
3335

3436
// Add your driver
35-
new InMemoryDriver(),
37+
.use(new DriverPlugin(new InMemoryDriver(), 'memory'));
3638

3739
// Add other plugins and app configs as needed
38-
]);
3940

40-
await kernel.start();
41+
await kernel.bootstrap();
4142

4243
console.log('✅ Kernel started with custom ObjectQL instance');
43-
console.log('ObjectQL instance:', kernel.ql);
44+
45+
// Access ObjectQL via service registry
46+
const objectql = kernel.getService('objectql');
47+
console.log('ObjectQL instance:', objectql);
4448
})();

examples/host/debug-registry.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { ObjectStackKernel, ObjectQLPlugin } from '@objectstack/runtime';
2+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, AppManifestPlugin } from '@objectstack/runtime';
33
import { SchemaRegistry, ObjectQL } from '@objectstack/objectql';
44
import { InMemoryDriver } from '@objectstack/driver-memory';
55

@@ -10,13 +10,14 @@ import TodoApp from '@objectstack/example-todo/objectstack.config';
1010
console.log('Apps:', [TodoApp.name]);
1111
console.log('Objects inside App:', TodoApp.objects?.map((o: any) => o.name));
1212

13-
const kernel = new ObjectStackKernel([
14-
new ObjectQLPlugin(),
15-
TodoApp,
16-
new InMemoryDriver()
17-
]);
13+
const kernel = new ObjectKernel();
1814

19-
await kernel.start();
15+
kernel
16+
.use(new ObjectQLPlugin())
17+
.use(new DriverPlugin(new InMemoryDriver(), 'memory'))
18+
.use(new AppManifestPlugin(TodoApp));
19+
20+
await kernel.bootstrap();
2021

2122
console.log('--- Post Start ---');
2223

examples/host/src/index.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ObjectStackKernel, ObjectQLPlugin, ObjectQL } from '@objectstack/runtime';
1+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, AppManifestPlugin, ObjectQL } from '@objectstack/runtime';
22
import { InMemoryDriver } from '@objectstack/driver-memory';
33
import { HonoServerPlugin } from '@objectstack/plugin-hono-server';
44

@@ -9,32 +9,26 @@ import BiPluginManifest from '@objectstack/plugin-bi/objectstack.config';
99
(async () => {
1010
console.log('🚀 Booting Kernel...');
1111

12-
// Option 1: Use default ObjectQL via plugin (recommended)
13-
const kernel = new ObjectStackKernel([
14-
// Register ObjectQL engine explicitly via plugin
15-
new ObjectQLPlugin(),
16-
17-
// App manifests
18-
CrmApp,
19-
TodoApp,
20-
BiPluginManifest,
12+
// Use MiniKernel architecture
13+
const kernel = new ObjectKernel();
14+
15+
kernel
16+
// Register ObjectQL engine
17+
.use(new ObjectQLPlugin())
2118

2219
// Database driver
23-
new InMemoryDriver(),
20+
.use(new DriverPlugin(new InMemoryDriver(), 'memory'))
21+
22+
// App manifests
23+
.use(new AppManifestPlugin(CrmApp))
24+
.use(new AppManifestPlugin(TodoApp))
25+
.use(new AppManifestPlugin(BiPluginManifest))
2426

2527
// Load the Hono Server Plugin
26-
new HonoServerPlugin({
28+
.use(new HonoServerPlugin({
2729
port: 3004,
2830
staticRoot: './public'
29-
})
30-
]);
31-
32-
// Option 2: Use custom ObjectQL instance
33-
// const customQL = new ObjectQL({ env: 'production', customConfig: true });
34-
// const kernel = new ObjectStackKernel([
35-
// new ObjectQLPlugin(customQL),
36-
// ...other plugins
37-
// ]);
31+
}));
3832

39-
await kernel.start();
33+
await kernel.bootstrap();
4034
})();

examples/msw-react-crud/src/mocks/browser.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* and the MSW Plugin which automatically exposes the API.
66
*/
77

8-
import { ObjectStackKernel, ObjectQLPlugin } from '@objectstack/runtime';
8+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, AppManifestPlugin } from '@objectstack/runtime';
99
import { InMemoryDriver } from '@objectstack/driver-memory';
1010
import { MSWPlugin } from '@objectstack/plugin-msw';
1111
// import appConfig from '../../objectstack.config';
1212
import todoConfig from '@objectstack/example-todo/objectstack.config';
1313

14-
let kernel: ObjectStackKernel | null = null;
14+
let kernel: ObjectKernel | null = null;
1515

1616
export async function startMockServer() {
1717
if (kernel) return;
@@ -20,28 +20,27 @@ export async function startMockServer() {
2020

2121
const driver = new InMemoryDriver();
2222

23-
// Define Seed Data using the Dataset Protocol
24-
// We use the data defined in the Todo App config
23+
// Create kernel with MiniKernel architecture
24+
kernel = new ObjectKernel();
2525

26-
kernel = new ObjectStackKernel([
27-
// Register ObjectQL engine explicitly
28-
new ObjectQLPlugin(),
26+
kernel
27+
// Register ObjectQL engine
28+
.use(new ObjectQLPlugin())
2929

30-
// Todo App Config (contains objects and data)
31-
todoConfig,
30+
// Register the driver
31+
.use(new DriverPlugin(driver, 'memory'))
3232

33-
// In-Memory Database (runs in browser)
34-
driver,
33+
// Load todo app config as a plugin
34+
.use(new AppManifestPlugin(todoConfig))
3535

3636
// MSW Plugin (intercepts network requests)
37-
new MSWPlugin({
37+
.use(new MSWPlugin({
3838
enableBrowser: true,
3939
baseUrl: '/api/v1',
4040
logRequests: true
41-
})
42-
]);
43-
44-
await kernel.start();
41+
}));
42+
43+
await kernel.bootstrap();
4544

4645
return kernel;
4746
}

packages/plugin-msw/src/msw-plugin.ts

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { http, HttpResponse } from 'msw';
22
import { setupWorker } from 'msw/browser';
3-
import { RuntimePlugin, RuntimeContext, ObjectStackRuntimeProtocol } from '@objectstack/runtime';
3+
import {
4+
RuntimePlugin,
5+
RuntimeContext,
6+
Plugin,
7+
PluginContext,
8+
ObjectStackRuntimeProtocol,
9+
ObjectKernel
10+
} from '@objectstack/runtime';
411

512
export interface MSWPluginOptions {
613
/**
@@ -149,10 +156,20 @@ export class ObjectStackServer {
149156
* This plugin enables Mock Service Worker integration for testing and development.
150157
* It automatically mocks API endpoints using the ObjectStack runtime protocol.
151158
*
159+
* Supports both legacy RuntimePlugin and new Plugin interfaces.
160+
*
152161
* @example
153162
* ```typescript
154163
* import { MSWPlugin } from '@objectstack/plugin-msw';
155164
*
165+
* // With new ObjectKernel
166+
* const kernel = new ObjectKernel();
167+
* kernel.use(new MSWPlugin({
168+
* enableBrowser: true,
169+
* baseUrl: '/api/v1'
170+
* }));
171+
*
172+
* // With legacy ObjectStackKernel
156173
* const runtime = new ObjectStackRuntime({
157174
* plugins: [
158175
* new MSWPlugin({
@@ -163,11 +180,14 @@ export class ObjectStackServer {
163180
* });
164181
* ```
165182
*/
166-
export class MSWPlugin implements RuntimePlugin {
167-
name = 'msw';
183+
export class MSWPlugin implements Plugin, RuntimePlugin {
184+
name = 'com.objectstack.plugin.msw';
185+
version = '1.0.0';
186+
168187
private options: MSWPluginOptions;
169188
private worker: any;
170189
private handlers: Array<any> = [];
190+
private protocol?: ObjectStackRuntimeProtocol;
171191

172192
constructor(options: MSWPluginOptions = {}) {
173193
this.options = {
@@ -178,9 +198,69 @@ export class MSWPlugin implements RuntimePlugin {
178198
};
179199
}
180200

201+
/**
202+
* New Plugin interface - init phase
203+
*/
204+
async init(ctx: PluginContext) {
205+
// Protocol will be created in start phase
206+
ctx.logger.log('[MSWPlugin] Initialized');
207+
}
208+
209+
/**
210+
* New Plugin interface - start phase
211+
*/
212+
async start(ctx: PluginContext) {
213+
// Get the kernel and create protocol
214+
if (ctx.getKernel) {
215+
const kernel = ctx.getKernel();
216+
this.protocol = new ObjectStackRuntimeProtocol(kernel);
217+
} else {
218+
throw new Error('[MSWPlugin] Cannot access kernel from context - getKernel() not available');
219+
}
220+
221+
this.setupHandlers();
222+
await this.startWorker();
223+
}
224+
225+
/**
226+
* New Plugin interface - destroy phase
227+
*/
228+
async destroy() {
229+
await this.stopWorker();
230+
}
231+
232+
/**
233+
* Legacy RuntimePlugin interface - install
234+
*/
181235
install(ctx: RuntimeContext) {
182236
const { engine } = ctx;
183-
const protocol = new ObjectStackRuntimeProtocol(engine);
237+
this.protocol = new ObjectStackRuntimeProtocol(engine);
238+
this.setupHandlers();
239+
}
240+
241+
/**
242+
* Legacy RuntimePlugin interface - onStart
243+
*/
244+
async onStart(ctx: RuntimeContext) {
245+
await this.startWorker();
246+
}
247+
248+
/**
249+
* Legacy RuntimePlugin interface - onStop
250+
*/
251+
async onStop() {
252+
await this.stopWorker();
253+
}
254+
255+
/**
256+
* Setup MSW handlers
257+
*/
258+
private setupHandlers() {
259+
if (!this.protocol) {
260+
throw new Error('[MSWPlugin] Protocol not initialized');
261+
}
262+
263+
const protocol = this.protocol;
184264

185265
// Initialize ObjectStackServer
186266
ObjectStackServer.init(
@@ -312,7 +392,10 @@ export class MSWPlugin implements RuntimePlugin {
312392
console.log(`[MSWPlugin] Installed ${this.handlers.length} request handlers.`);
313393
}
314394

315-
async onStart(ctx: RuntimeContext) {
395+
/**
396+
* Start the MSW worker
397+
*/
398+
private async startWorker() {
316399
if (this.options.enableBrowser && typeof window !== 'undefined') {
317400
// Browser environment
318401
this.worker = setupWorker(...this.handlers);
@@ -325,7 +408,10 @@ export class MSWPlugin implements RuntimePlugin {
325408
}
326409
}
327410

328-
async onStop() {
411+
/**
412+
* Stop the MSW worker
413+
*/
414+
private async stopWorker() {
329415
if (this.worker) {
330416
this.worker.stop();
331417
console.log(`[MSWPlugin] Stopped MSW worker.`);

0 commit comments

Comments
 (0)