Skip to content

Commit

Permalink
feat: auto unload enabled instance when fastEnable-ing a different one
Browse files Browse the repository at this point in the history
  • Loading branch information
Delusoire committed Aug 17, 2024
1 parent 309aa00 commit 21375a1
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,16 @@ export class Module extends ModuleBase<Module, ModuleInstance> {
}

const parent = this.getLastParentOf(identifier) as Module;
const descendant = new Module(parent, {}, identifier, local ? module.enabled : "");
const descendant = new Module(
parent,
{},
identifier,
local ? module.enabled : "",
);
for (const child of parent.getChildren()) {
if (child.getIdentifier().startsWith(identifier) && child != descendant) {
if (
child.getIdentifier().startsWith(identifier) && child != descendant
) {
child.parent = descendant;
}
}
Expand Down Expand Up @@ -246,6 +253,10 @@ export class Module extends ModuleBase<Module, ModuleInstance> {
await enabledInstance?.transition.block();
}

if (enabledInstance?.canUnload()) {
await enabledInstance.unload();
}

if (
instance.isEnabled() ||
(enabledInstance && !this.canDisable(enabledInstance))
Expand Down Expand Up @@ -351,7 +362,7 @@ export abstract class ModuleInstanceBase<
public metadata: Metadata | null,
public artifacts: Array<string>,
public checksum: string,
) { }
) {}

// ?
public updateMetadata(metadata: Metadata) {
Expand Down Expand Up @@ -576,8 +587,11 @@ export class ModuleInstance extends ModuleInstanceBase<Module>
throw `can't load \`${this.getModuleIdentifier()}\` because it is not enabled, installed, or satisfies the range \`${range}\``;
}

for (const [dependency, range] of Object.entries(this.metadata.dependencies)) {
const module = RootModule.INSTANCE.getDescendant(dependency)?.getEnabledInstance();
for (
const [dependency, range] of Object.entries(this.metadata.dependencies)
) {
const module = RootModule.INSTANCE.getDescendant(dependency)
?.getEnabledInstance();
if (!module?.canLoadRecur(isPreload, range)) {
return false;
}
Expand Down

0 comments on commit 21375a1

Please sign in to comment.