From 9be9e36aa5e1bc122c72298e505e04f78ea2537a Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sun, 27 Jun 2021 12:06:42 -0600 Subject: [PATCH] fix: Resolve some memory leaks 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. --- src/lib/converter/plugins/DecoratorPlugin.ts | 3 --- src/lib/converter/plugins/SourcePlugin.ts | 12 ++++-------- src/lib/converter/plugins/TypePlugin.ts | 7 +++---- src/lib/output/plugins/LegendPlugin.ts | 5 +++-- src/lib/output/plugins/MarkedLinksPlugin.ts | 1 + src/lib/output/plugins/NavigationPlugin.ts | 5 +++-- 6 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/lib/converter/plugins/DecoratorPlugin.ts b/src/lib/converter/plugins/DecoratorPlugin.ts index f71fff957..9d68f7ddf 100644 --- a/src/lib/converter/plugins/DecoratorPlugin.ts +++ b/src/lib/converter/plugins/DecoratorPlugin.ts @@ -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(); /** diff --git a/src/lib/converter/plugins/SourcePlugin.ts b/src/lib/converter/plugins/SourcePlugin.ts index f01db1330..77f618fd3 100644 --- a/src/lib/converter/plugins/SourcePlugin.ts +++ b/src/lib/converter/plugins/SourcePlugin.ts @@ -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, @@ -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; } /** diff --git a/src/lib/converter/plugins/TypePlugin.ts b/src/lib/converter/plugins/TypePlugin.ts index 82dd2f446..c139a9302 100644 --- a/src/lib/converter/plugins/TypePlugin.ts +++ b/src/lib/converter/plugins/TypePlugin.ts @@ -13,7 +13,7 @@ import { Context } from "../context"; */ @Component({ name: "type" }) export class TypePlugin extends ConverterComponent { - reflections: DeclarationReflection[] = []; + reflections = new Set(); /** * Create a new TypeHandler instance. @@ -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(), }); } @@ -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); } /** diff --git a/src/lib/output/plugins/LegendPlugin.ts b/src/lib/output/plugins/LegendPlugin.ts index 5098e1ef1..2b908ce81 100644 --- a/src/lib/output/plugins/LegendPlugin.ts +++ b/src/lib/output/plugins/LegendPlugin.ts @@ -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. @@ -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), }); } @@ -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); } diff --git a/src/lib/output/plugins/MarkedLinksPlugin.ts b/src/lib/output/plugins/MarkedLinksPlugin.ts index f52c8d97c..2c707cec9 100644 --- a/src/lib/output/plugins/MarkedLinksPlugin.ts +++ b/src/lib/output/plugins/MarkedLinksPlugin.ts @@ -173,6 +173,7 @@ export class MarkedLinksPlugin extends ContextAwareRendererComponent { this.warnings.join("\n ") ); } + this.warnings = []; } /** diff --git a/src/lib/output/plugins/NavigationPlugin.ts b/src/lib/output/plugins/NavigationPlugin.ts index c82402355..d163c3352 100644 --- a/src/lib/output/plugins/NavigationPlugin.ts +++ b/src/lib/output/plugins/NavigationPlugin.ts @@ -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. @@ -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), }); } @@ -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;