Skip to content

Commit

Permalink
Merge pull request #56 from fleetbase/dev-v0.2.21
Browse files Browse the repository at this point in the history
removed legacy boot, added option preboot call during engine boot step
  • Loading branch information
roncodes authored Oct 8, 2024
2 parents b4dabd6 + f4b1e64 commit 4669d83
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 82 deletions.
107 changes: 26 additions & 81 deletions addon/services/universe.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class UniverseService extends Service.extend(Evented) {
};
@tracked hooks = {};
@tracked bootCallbacks = A([]);
@tracked initialLocation = { ...window.location };

/**
* Computed property that returns all administrative menu items.
Expand Down Expand Up @@ -137,6 +138,16 @@ export default class UniverseService extends Service.extend(Evented) {
return this.router.transitionTo(route, ...args);
}

/**
* Initialize the universe service.
*
* @memberof UniverseService
*/
initialize() {
this.initialLocation = { ...window.location };
this.trigger('init', this);
}

/**
* Sets the application instance.
*
Expand Down Expand Up @@ -1803,6 +1814,7 @@ export default class UniverseService extends Service.extend(Evented) {
}

// Set application instance
this.initialize();
this.setApplicationInstance(owner);

const tryBootEngine = (extension) => {
Expand Down Expand Up @@ -1861,6 +1873,9 @@ export default class UniverseService extends Service.extend(Evented) {
pending.push(...stillPending);
};

// Run pre-boots if any
await this.preboot();

return loadInstalledExtensions(additionalCoreExtensions).then(async (extensions) => {
for (let i = 0; i < extensions.length; i++) {
const extension = extensions[i];
Expand All @@ -1874,90 +1889,20 @@ export default class UniverseService extends Service.extend(Evented) {
}

/**
* Boots all installed engines, ensuring dependencies are resolved.
*
* This method loads all installed extensions and then attempts to boot each engine.
* For each extension, it loads the engine and, if the engine has a `setupExtension`
* method in its base, it calls this method to complete the setup. This function ensures
* that dependencies are resolved before booting the engines. If some dependencies are
* never booted, an error is logged.
* Run engine preboots from all indexed engines.
*
* @method legacyBootEngines
* @param {ApplicationInstance|null} owner - The Ember ApplicationInstance that owns the engines.
* @return {void}
* @param {ApplicationInstance} owner
* @memberof UniverseService
*/
legacyBootEngines(owner = null) {
const booted = [];
const pending = [];

// If no owner provided use the owner of this service
if (owner === null) {
owner = getOwner(this);
}

// Set application instance
this.setApplicationInstance(owner);

const tryBootEngine = (extension) => {
return this.loadEngine(extension.name).then((engineInstance) => {
if (engineInstance.base && engineInstance.base.setupExtension) {
const engineDependencies = getWithDefault(engineInstance.base, 'engineDependencies', []);

// Check if all dependency engines are booted
const allDependenciesBooted = engineDependencies.every((dep) => booted.includes(dep));

if (!allDependenciesBooted) {
pending.push({ extension, engineInstance });
return;
}

engineInstance.base.setupExtension(owner, engineInstance, this);
booted.push(extension.name);
this.bootedExtensions.pushObject(extension.name);
this.trigger('extension.booted', extension);
debug(`Booted : ${extension.name}`);

// Try booting pending engines again
tryBootPendingEngines();
}
});
};

const tryBootPendingEngines = () => {
const stillPending = [];

pending.forEach(({ extension, engineInstance }) => {
const engineDependencies = getWithDefault(engineInstance.base, 'engineDependencies', []);
const allDependenciesBooted = engineDependencies.every((dep) => booted.includes(dep));

if (allDependenciesBooted) {
engineInstance.base.setupExtension(owner, engineInstance, this);
booted.push(extension.name);
this.bootedExtensions.pushObject(extension.name);
this.trigger('extension.booted', extension);
debug(`Booted : ${extension.name}`);
} else {
stillPending.push({ extension, engineInstance });
}
});

// If no progress was made, log an error in debug/development mode
assert('Some engines have unmet dependencies and cannot be booted:', pending.length === stillPending.length);

pending.length = 0;
pending.push(...stillPending);
};

return loadExtensions().then(async (extensions) => {
for (let i = 0; i < extensions.length; i++) {
const extension = extensions[i];
await tryBootEngine(extension);
async preboot(owner) {
const extensions = await loadExtensions();
for (let i = 0; i < extensions.length; i++) {
const extension = extensions[i];
const instance = await this.loadEngine(extension.name);
if (instance.base && typeof instance.base.preboot === 'function') {
instance.base.preboot(owner, instance, this);
}

this.runBootCallbacks(owner, () => {
this.enginesBooted = true;
});
});
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/ember-core",
"version": "0.2.20",
"version": "0.2.21",
"description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
"keywords": [
"fleetbase-core",
Expand Down

0 comments on commit 4669d83

Please sign in to comment.