|
7 | 7 | * during the migration phase. |
8 | 8 | */ |
9 | 9 |
|
| 10 | +// Simple mock implementations of runtime managers |
| 11 | +class MockMetadataRegistry { |
| 12 | + private store = new Map<string, Map<string, any>>(); |
| 13 | + |
| 14 | + register(type: string, item: any): void { |
| 15 | + if (!this.store.has(type)) { |
| 16 | + this.store.set(type, new Map()); |
| 17 | + } |
| 18 | + const typeMap = this.store.get(type)!; |
| 19 | + typeMap.set(item.id || item.name, item); |
| 20 | + } |
| 21 | + |
| 22 | + get<T = any>(type: string, id: string): T | undefined { |
| 23 | + const typeMap = this.store.get(type); |
| 24 | + return typeMap?.get(id) as T | undefined; |
| 25 | + } |
| 26 | + |
| 27 | + list<T = any>(type: string): T[] { |
| 28 | + const typeMap = this.store.get(type); |
| 29 | + if (!typeMap) return []; |
| 30 | + return Array.from(typeMap.values()) as T[]; |
| 31 | + } |
| 32 | + |
| 33 | + unregister(type: string, id: string): boolean { |
| 34 | + const typeMap = this.store.get(type); |
| 35 | + if (!typeMap) return false; |
| 36 | + return typeMap.delete(id); |
| 37 | + } |
| 38 | + |
| 39 | + getTypes(): string[] { |
| 40 | + return Array.from(this.store.keys()); |
| 41 | + } |
| 42 | + |
| 43 | + unregisterPackage(packageName: string): void { |
| 44 | + // Simple implementation - in real runtime this would filter by package |
| 45 | + for (const [type, typeMap] of this.store.entries()) { |
| 46 | + const toDelete: string[] = []; |
| 47 | + for (const [id, item] of typeMap.entries()) { |
| 48 | + if (item.packageName === packageName || item.package === packageName) { |
| 49 | + toDelete.push(id); |
| 50 | + } |
| 51 | + } |
| 52 | + toDelete.forEach(id => typeMap.delete(id)); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class MockHookManager { |
| 58 | + private hooks = new Map<string, any[]>(); |
| 59 | + |
| 60 | + register(hookName: string, handler: any, packageName?: string): void { |
| 61 | + if (!this.hooks.has(hookName)) { |
| 62 | + this.hooks.set(hookName, []); |
| 63 | + } |
| 64 | + this.hooks.get(hookName)!.push({ handler, packageName }); |
| 65 | + } |
| 66 | + |
| 67 | + async trigger(hookName: string, context: any): Promise<void> { |
| 68 | + const handlers = this.hooks.get(hookName) || []; |
| 69 | + for (const { handler } of handlers) { |
| 70 | + await handler(context); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + unregisterPackage(packageName: string): void { |
| 75 | + for (const [hookName, handlers] of this.hooks.entries()) { |
| 76 | + this.hooks.set(hookName, handlers.filter(h => h.packageName !== packageName)); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class MockActionManager { |
| 82 | + private actions = new Map<string, any>(); |
| 83 | + |
| 84 | + register(objectName: string, actionName: string, handler: any, packageName?: string): void { |
| 85 | + const key = `${objectName}.${actionName}`; |
| 86 | + this.actions.set(key, { handler, packageName }); |
| 87 | + } |
| 88 | + |
| 89 | + async execute(objectName: string, actionName: string, context: any): Promise<any> { |
| 90 | + const key = `${objectName}.${actionName}`; |
| 91 | + const action = this.actions.get(key); |
| 92 | + if (!action) { |
| 93 | + throw new Error(`Action ${actionName} not found for object ${objectName}`); |
| 94 | + } |
| 95 | + return await action.handler(context); |
| 96 | + } |
| 97 | + |
| 98 | + get(objectName: string, actionName: string): any { |
| 99 | + const key = `${objectName}.${actionName}`; |
| 100 | + return this.actions.get(key)?.handler; |
| 101 | + } |
| 102 | + |
| 103 | + unregisterPackage(packageName: string): void { |
| 104 | + const toDelete: string[] = []; |
| 105 | + for (const [key, action] of this.actions.entries()) { |
| 106 | + if (action.packageName === packageName) { |
| 107 | + toDelete.push(key); |
| 108 | + } |
| 109 | + } |
| 110 | + toDelete.forEach(key => this.actions.delete(key)); |
| 111 | + } |
| 112 | +} |
| 113 | + |
10 | 114 | export class ObjectStackKernel { |
11 | 115 | public ql: unknown = null; |
| 116 | + public metadata: MockMetadataRegistry; |
| 117 | + public hooks: MockHookManager; |
| 118 | + public actions: MockActionManager; |
| 119 | + |
12 | 120 | private plugins: any[] = []; |
13 | 121 | private driver: any = null; // Will be set by the ObjectQL app |
14 | 122 |
|
15 | 123 | constructor(plugins: any[] = []) { |
16 | 124 | this.plugins = plugins; |
| 125 | + this.metadata = new MockMetadataRegistry(); |
| 126 | + this.hooks = new MockHookManager(); |
| 127 | + this.actions = new MockActionManager(); |
17 | 128 | } |
18 | 129 |
|
19 | 130 | // Method to set the driver for delegation during migration |
|
0 commit comments