Skip to content

Commit

Permalink
fix: Resolve some memory leaks
Browse files Browse the repository at this point in the history
Starting to investigate #1599. This appears to almost completely fix the issue when outputting JSON only. I believe Handlebar's cache is to blame for the majority of the remaining leaks.
  • Loading branch information
Gerrit0 committed Jun 27, 2021
1 parent bb44819 commit 9be9e36
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 19 deletions.
3 changes: 0 additions & 3 deletions src/lib/converter/plugins/DecoratorPlugin.ts
Expand Up @@ -11,9 +11,6 @@ import { Context } from "../context";
*/
@Component({ name: "decorator" })
export class DecoratorPlugin extends ConverterComponent {
/**
* Defined in this.onBegin
*/
private readonly usages = new Map<ts.Symbol, ReferenceType[]>();

/**
Expand Down
12 changes: 4 additions & 8 deletions src/lib/converter/plugins/SourcePlugin.ts
Expand Up @@ -40,7 +40,7 @@ export class SourcePlugin extends ConverterComponent {
*/
initialize() {
this.listenTo(this.owner, {
[Converter.EVENT_BEGIN]: this.onBegin,
[Converter.EVENT_END]: this.onEnd,
[Converter.EVENT_CREATE_DECLARATION]: this.onDeclaration,
[Converter.EVENT_CREATE_SIGNATURE]: this.onDeclaration,
[Converter.EVENT_RESOLVE_BEGIN]: this.onBeginResolve,
Expand All @@ -62,14 +62,10 @@ export class SourcePlugin extends ConverterComponent {
return this.fileMappings[fileName];
}

/**
* Triggered once per project before the dispatcher invokes the compiler.
*
* @param event An event object containing the related project and compiler instance.
*/
private onBegin() {
this.fileNames.clear();
private onEnd() {
this.fileMappings = {};
this.fileNames.clear();
this.basePath = void 0;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/lib/converter/plugins/TypePlugin.ts
Expand Up @@ -13,7 +13,7 @@ import { Context } from "../context";
*/
@Component({ name: "type" })
export class TypePlugin extends ConverterComponent {
reflections: DeclarationReflection[] = [];
reflections = new Set<DeclarationReflection>();

/**
* Create a new TypeHandler instance.
Expand All @@ -22,6 +22,7 @@ export class TypePlugin extends ConverterComponent {
this.listenTo(this.owner, {
[Converter.EVENT_RESOLVE]: this.onResolve,
[Converter.EVENT_RESOLVE_END]: this.onResolveEnd,
[Converter.EVENT_END]: () => this.reflections.clear(),
});
}

Expand Down Expand Up @@ -87,9 +88,7 @@ export class TypePlugin extends ConverterComponent {
}

private postpone(reflection: DeclarationReflection) {
if (!this.reflections.includes(reflection)) {
this.reflections.push(reflection);
}
this.reflections.add(reflection);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/lib/output/plugins/LegendPlugin.ts
Expand Up @@ -266,7 +266,7 @@ export class LegendBuilder {
*/
@Component({ name: "legend" })
export class LegendPlugin extends RendererComponent {
private _project!: ProjectReflection;
private _project?: ProjectReflection;

/**
* Create a new LegendPlugin instance.
Expand All @@ -275,6 +275,7 @@ export class LegendPlugin extends RendererComponent {
this.listenTo(this.owner, {
[RendererEvent.BEGIN]: this.onRenderBegin,
[PageEvent.BEGIN]: this.onRendererBeginPage,
[RendererEvent.END]: () => (this._project = void 0),
});
}

Expand All @@ -295,7 +296,7 @@ export class LegendPlugin extends RendererComponent {
this.buildLegend(model, builder);

// top level items (as appears in navigation)
this._project.children?.forEach((reflection) => {
this._project?.children?.forEach((reflection) => {
if (reflection !== model) {
this.buildLegend(reflection, builder);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/output/plugins/MarkedLinksPlugin.ts
Expand Up @@ -173,6 +173,7 @@ export class MarkedLinksPlugin extends ContextAwareRendererComponent {
this.warnings.join("\n ")
);
}
this.warnings = [];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/lib/output/plugins/NavigationPlugin.ts
Expand Up @@ -15,7 +15,7 @@ export class NavigationPlugin extends RendererComponent {
/**
* The navigation structure generated by the current theme.
*/
navigation!: NavigationItem;
navigation?: NavigationItem;

/**
* Create a new NavigationPlugin instance.
Expand All @@ -24,6 +24,7 @@ export class NavigationPlugin extends RendererComponent {
this.listenTo(this.owner, {
[RendererEvent.BEGIN]: this.onBeginRenderer,
[PageEvent.BEGIN]: this.onBeginPage,
[RendererEvent.END]: () => (this.navigation = void 0),
});
}

Expand Down Expand Up @@ -58,7 +59,7 @@ export class NavigationPlugin extends RendererComponent {
if (item.children) {
item.children.forEach((child) => updateItem(child));
}
})(this.navigation);
})(this.navigation!);

currentItems.forEach((item: NavigationItem | undefined) => {
item!.isCurrent = true;
Expand Down

0 comments on commit 9be9e36

Please sign in to comment.