-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbrowser.ts
More file actions
68 lines (54 loc) · 2.23 KB
/
Copy pathbrowser.ts
File metadata and controls
68 lines (54 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* MSW Browser Worker Setup via ObjectStack Service
*
* This creates a complete ObjectStack environment in the browser using the In-Memory Driver
* and the MSW Plugin which automatically exposes the API.
*/
import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime';
import { ObjectQLPlugin } from '@objectstack/objectql';
import { InMemoryDriver } from '@objectstack/driver-memory';
import { MSWPlugin } from '@objectstack/plugin-msw';
// import appConfig from '../../objectstack.config';
import todoConfig from '@example/app-todo/objectstack.config';
let kernel: ObjectKernel | null = null;
export async function startMockServer() {
if (kernel) return;
console.log('[MSW] Starting ObjectStack Runtime (Browser Mode)...');
const driver = new InMemoryDriver();
// Create kernel with MiniKernel architecture
kernel = new ObjectKernel();
kernel
// Register ObjectQL engine
.use(new ObjectQLPlugin())
// Register the driver
.use(new DriverPlugin(driver, 'memory'))
// Load todo app config as a plugin
.use(new AppPlugin(todoConfig))
// MSW Plugin (intercepts network requests)
.use(new MSWPlugin({
enableBrowser: true,
baseUrl: '/api/v1',
logRequests: true
}));
await kernel.bootstrap();
// Initialize default data from manifest if available
const manifest = (todoConfig as any).manifest;
if (manifest && Array.isArray(manifest.data)) {
console.log('[MSW] Loading initial data...');
for (const dataset of manifest.data) {
if (dataset.object && Array.isArray(dataset.records)) {
for (const record of dataset.records) {
// Check if record already exists to avoid duplicates on hot reload?
// Since it's in-memory and we create new kernel/driver on refresh...
// But 'kernel' variable is module-scoped singleton.
// On HMR replacement, this module might re-execute.
// If 'kernel' is not null, we return early (line 18).
// So data loading happens only once per session. Good.
await driver.create(dataset.object, record);
}
console.log(`[MSW] Loaded ${dataset.records.length} records for ${dataset.object}`);
}
}
}
return kernel;
}