Skip to content

Commit fde5d14

Browse files
Copilothotlong
andcommitted
polish: Final code review improvements - clarify warnings and type safety
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 00321c7 commit fde5d14

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

packages/runtime/src/kernel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class ObjectStackKernel {
2323

2424
if (!hasObjectQLPlugin) {
2525
// Backward compatibility: Initialize ObjectQL directly if no plugin provides it
26-
console.warn('[Kernel] No ObjectQL plugin detected. Using default initialization. Consider using ObjectQLPlugin for explicit registration.');
26+
console.warn('[Kernel] No ObjectQL plugin found, using default initialization. Consider using ObjectQLPlugin.');
2727
this.ql = new ObjectQL({
2828
env: process.env.NODE_ENV || 'development'
2929
});

packages/runtime/src/objectql-plugin.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export const OBJECTQL_PLUGIN_MARKER = Symbol('objectql-plugin');
1111
*
1212
* Registers the ObjectQL engine instance with the kernel.
1313
* This allows users to provide their own ObjectQL implementation or configuration.
14+
*
15+
* Usage:
16+
* - new ObjectQLPlugin() - Creates new ObjectQL with default settings
17+
* - new ObjectQLPlugin(existingQL) - Uses existing ObjectQL instance
18+
* - new ObjectQLPlugin(undefined, { custom: 'context' }) - Creates new ObjectQL with custom context
1419
*/
1520
export class ObjectQLPlugin implements RuntimePlugin {
1621
name = 'com.objectstack.engine.objectql';
@@ -20,10 +25,15 @@ export class ObjectQLPlugin implements RuntimePlugin {
2025

2126
private ql: ObjectQL;
2227

28+
/**
29+
* @param ql - Existing ObjectQL instance to use (optional)
30+
* @param hostContext - Host context for new ObjectQL instance (ignored if ql is provided)
31+
*/
2332
constructor(ql?: ObjectQL, hostContext?: Record<string, any>) {
24-
// Allow passing existing ObjectQL instance or create a new one
25-
// Note: If 'ql' is provided, 'hostContext' is ignored
26-
// To create a new instance with custom context, pass only hostContext
33+
if (ql && hostContext) {
34+
console.warn('[ObjectQLPlugin] Both ql and hostContext provided. hostContext will be ignored.');
35+
}
36+
2737
if (ql) {
2838
this.ql = ql;
2939
} else {

test-objectql-plugin.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ async function testBackwardCompatibility() {
4242
async function testCustomObjectQL() {
4343
console.log('\n=== Test 3: Custom ObjectQL Instance ===');
4444

45+
// Create a WeakMap to track custom instances in a type-safe way
46+
const customInstances = new WeakMap<ObjectQL, string>();
47+
4548
const customQL = new ObjectQL({
4649
env: 'test',
4750
customProperty: 'test-value'
4851
});
4952

50-
// Add a marker to identify this instance
51-
(customQL as any).customMarker = 'custom-instance';
53+
// Mark this as a custom instance
54+
customInstances.set(customQL, 'custom-instance');
5255

5356
const kernel = new ObjectStackKernel([
5457
new ObjectQLPlugin(customQL)
@@ -59,12 +62,17 @@ async function testCustomObjectQL() {
5962
throw new Error('FAILED: ObjectQL not set');
6063
}
6164

62-
if ((kernel.ql as any).customMarker !== 'custom-instance') {
65+
if (!customInstances.has(kernel.ql)) {
6366
throw new Error('FAILED: Custom ObjectQL instance not used');
6467
}
6568

69+
const marker = customInstances.get(kernel.ql);
70+
if (marker !== 'custom-instance') {
71+
throw new Error('FAILED: Custom ObjectQL instance marker mismatch');
72+
}
73+
6674
console.log('✅ Custom ObjectQL instance registered correctly');
67-
console.log('Custom marker:', (kernel.ql as any).customMarker);
75+
console.log('Custom marker:', marker);
6876
}
6977

7078
async function testMultiplePlugins() {

0 commit comments

Comments
 (0)