Skip to content

Commit

Permalink
Refactor AbstractRenderer to use a Map instead of a Set for tracking …
Browse files Browse the repository at this point in the history
…application references, and add support for resetting applications for a specific DOM node in resetApplications().
  • Loading branch information
valentinpalkovic committed Apr 5, 2023
1 parent 74bd0b5 commit 9204e55
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
25 changes: 14 additions & 11 deletions code/frameworks/angular/src/client/angular-beta/AbstractRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApplicationRef, enableProdMode, importProvidersFrom, NgModule } from '@angular/core';
import { ApplicationRef, enableProdMode, NgModule } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

import { BehaviorSubject, Subject } from 'rxjs';
Expand All @@ -14,16 +14,16 @@ type StoryRenderInfo = {
moduleMetadataSnapshot: string;
};

const applicationRefs = new Set<ApplicationRef>();
const applicationRefs = new Map<HTMLElement, ApplicationRef>();

export abstract class AbstractRenderer {
/**
* Wait and destroy the platform
*/
public static resetApplications() {
public static resetApplications(domNode?: HTMLElement) {
componentNgModules.clear();
applicationRefs.forEach((appRef) => {
if (!appRef.destroyed) {
applicationRefs.forEach((appRef, appDOMNode) => {
if (!appRef.destroyed && (!domNode || appDOMNode === domNode)) {
appRef.destroy();
}
});
Expand All @@ -50,7 +50,7 @@ export abstract class AbstractRenderer {
}
};

protected previousStoryRenderInfo: StoryRenderInfo;
protected previousStoryRenderInfo = new Map<HTMLElement, StoryRenderInfo>();

// Observable to change the properties dynamically without reloading angular module&component
protected storyProps$: Subject<ICollection | undefined>;
Expand All @@ -67,7 +67,7 @@ export abstract class AbstractRenderer {
}
}

protected abstract beforeFullRender(): Promise<void>;
protected abstract beforeFullRender(domNode?: HTMLElement): Promise<void>;

protected abstract afterFullRender(): Promise<void>;

Expand Down Expand Up @@ -100,6 +100,7 @@ export abstract class AbstractRenderer {

if (
!this.fullRendererRequired({
targetDOMNode,
storyFnAngular,
moduleMetadata: {
...storyFnAngular.moduleMetadata,
Expand All @@ -112,7 +113,7 @@ export abstract class AbstractRenderer {
return;
}

await this.beforeFullRender();
await this.beforeFullRender(targetDOMNode);

// Complete last BehaviorSubject and set a new one for the current module
if (this.storyProps$) {
Expand Down Expand Up @@ -140,7 +141,7 @@ export abstract class AbstractRenderer {
],
});

applicationRefs.add(applicationRef);
applicationRefs.set(targetDOMNode, applicationRef);

await this.afterFullRender();
}
Expand Down Expand Up @@ -171,22 +172,24 @@ export abstract class AbstractRenderer {
}

private fullRendererRequired({
targetDOMNode,
storyFnAngular,
moduleMetadata,
forced,
}: {
targetDOMNode: HTMLElement;
storyFnAngular: StoryFnAngularReturnType;
moduleMetadata: NgModule;
forced: boolean;
}) {
const { previousStoryRenderInfo } = this;
const previousStoryRenderInfo = this.previousStoryRenderInfo.get(targetDOMNode);

const currentStoryRender = {
storyFnAngular,
moduleMetadataSnapshot: stringify(moduleMetadata),
};

this.previousStoryRenderInfo = currentStoryRender;
this.previousStoryRenderInfo.set(targetDOMNode, currentStoryRender);

if (
// check `forceRender` of story RenderContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class DocsRenderer extends AbstractRenderer {
await super.render({ ...options, forced: false });
}

async beforeFullRender(): Promise<void> {
DocsRenderer.resetApplications();
async beforeFullRender(domNode?: HTMLElement): Promise<void> {
DocsRenderer.resetApplications(domNode);
}

async afterFullRender(): Promise<void> {
Expand Down

0 comments on commit 9204e55

Please sign in to comment.