Refactored modules and updated loader.
This commit is contained in:
+31
-47
@@ -2,6 +2,8 @@
|
||||
* Base Module Class
|
||||
* Provides common functionality and enforces a consistent interface for all modules
|
||||
*/
|
||||
import { moduleRegistry } from './module-registry.js';
|
||||
|
||||
export class BaseModule {
|
||||
constructor(id, name) {
|
||||
this.id = id;
|
||||
@@ -27,6 +29,9 @@ export class BaseModule {
|
||||
// Dependencies
|
||||
this.dependencies = [];
|
||||
this._loadedDependencies = new Map();
|
||||
|
||||
// Auto-register with module registry
|
||||
moduleRegistry.register(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,23 +89,7 @@ export class BaseModule {
|
||||
this.reportProgress(15, "Waiting for dependencies");
|
||||
|
||||
// Get moduleRegistry - first try import then fallback to window
|
||||
const registry = window.moduleRegistry;
|
||||
if (!registry) {
|
||||
console.error(`${this.id}: Module registry not found, will retry`);
|
||||
|
||||
// Retry after a short delay to allow registry to be initialized
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
// Try again
|
||||
const retryRegistry = window.moduleRegistry;
|
||||
if (!retryRegistry) {
|
||||
console.error(`${this.id}: Module registry still not found after retry`);
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log(`${this.id}: Found module registry after retry`);
|
||||
return this._continueWaitForDependencies(retryRegistry);
|
||||
}
|
||||
const registry = moduleRegistry;
|
||||
|
||||
return this._continueWaitForDependencies(registry);
|
||||
} catch (error) {
|
||||
@@ -121,17 +110,30 @@ export class BaseModule {
|
||||
const results = await registry.waitForModules(this.dependencies);
|
||||
|
||||
// Store references to dependencies
|
||||
let hasErroredDependencies = false;
|
||||
for (let i = 0; i < this.dependencies.length; i++) {
|
||||
const depId = this.dependencies[i];
|
||||
const depModule = registry.getModule(depId);
|
||||
if (depModule) {
|
||||
this._loadedDependencies.set(depId, depModule);
|
||||
|
||||
// Check if this dependency is in ERROR state
|
||||
if (depModule.state === 'ERROR') {
|
||||
hasErroredDependencies = true;
|
||||
console.warn(`${this.id}: Dependency ${depId} is in ERROR state but will be considered resolved`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const allDepsReady = results.every(ready => ready === true);
|
||||
if (allDepsReady) {
|
||||
this.reportProgress(20, "Dependencies ready");
|
||||
// Check if all dependencies have resolved (either success or error)
|
||||
// We consider a module with ERROR state as resolved
|
||||
const allDepsResolved = results.every(result => result === true || result === false);
|
||||
if (allDepsResolved) {
|
||||
if (hasErroredDependencies) {
|
||||
this.reportProgress(20, "Dependencies resolved with some errors");
|
||||
} else {
|
||||
this.reportProgress(20, "Dependencies ready");
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
this.reportProgress(15, "Some dependencies not ready");
|
||||
@@ -143,26 +145,6 @@ export class BaseModule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy method for backwards compatibility
|
||||
* @deprecated Use dependencies array property instead
|
||||
* @returns {Promise<boolean>} - Resolves when dependencies are loaded
|
||||
*/
|
||||
async loadDependencies() {
|
||||
// This is now handled by _waitForModuleDependencies
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy method for backwards compatibility
|
||||
* @deprecated No longer needed as waitForDependencies is handled automatically
|
||||
* @returns {Promise<boolean>} - Resolves when dependencies are ready
|
||||
*/
|
||||
async waitForDependencies() {
|
||||
// This is now handled by _waitForModuleDependencies
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the module - Override this in child classes
|
||||
* @returns {Promise<boolean>} - Resolves when initialization is complete
|
||||
@@ -186,7 +168,7 @@ export class BaseModule {
|
||||
* @param {string} message - Status message
|
||||
*/
|
||||
reportProgress(percent, message) {
|
||||
this.progress = percent;
|
||||
this.progress = Math.min(100, Math.max(0, percent));
|
||||
|
||||
if (this.progressCallback && typeof this.progressCallback === 'function') {
|
||||
this.progressCallback(percent, message);
|
||||
@@ -268,10 +250,6 @@ export class BaseModule {
|
||||
if (this._loadedDependencies.has(moduleId)) {
|
||||
return this._loadedDependencies.get(moduleId);
|
||||
}
|
||||
|
||||
// Then check in the registry
|
||||
return window.moduleRegistry ?
|
||||
window.moduleRegistry.getModule(moduleId) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -283,7 +261,7 @@ export class BaseModule {
|
||||
if (typeof this[methodName] === 'function') {
|
||||
this[methodName] = this[methodName].bind(this);
|
||||
} else {
|
||||
console.warn(`Method ${methodName} not found on ${this.id} module`);
|
||||
console.warn(`Method ${methodName} not found on ${this.id} module.`);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -548,7 +526,13 @@ export class BaseModule {
|
||||
_updateResourceProgress() {
|
||||
if (this._totalResources === 0) return;
|
||||
|
||||
const percent = Math.round((this._loadedResources / this._totalResources) * 100);
|
||||
// Change to FETCHING state when loading resources
|
||||
if (this.state === 'LOADING' && this._loadedResources === 0) {
|
||||
this.changeState('FETCHING');
|
||||
}
|
||||
|
||||
// Scale resource loading to 10% to 50% range of total module progress
|
||||
const percent = Math.round((this._loadedResources / this._totalResources) * 40) + 10;
|
||||
this.reportProgress(percent, `Loading resources: ${this._loadedResources}/${this._totalResources}`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user