Skip to content

Commit 96db2d5

Browse files
crisbetoVivian Hu
authored and
Vivian Hu
committedJan 18, 2019
fix(icon): clean up cached references in icon registry on destroy (#14801)
The icon registry can end up caching some fairly large SVG elements which may end up being retained, because we're using a regular `Map`. These changes clear out all the references if the registry is destroyed.
1 parent 8dac174 commit 96db2d5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed
 

‎src/lib/icon/icon-registry.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
Optional,
1616
SecurityContext,
1717
SkipSelf,
18+
OnDestroy,
1819
} from '@angular/core';
1920
import {DomSanitizer, SafeResourceUrl, SafeHtml} from '@angular/platform-browser';
2021
import {forkJoin, Observable, of as observableOf, throwError as observableThrow} from 'rxjs';
@@ -93,7 +94,7 @@ class SvgIconConfig {
9394
* - Loads icons from URLs and extracts individual icons from icon sets.
9495
*/
9596
@Injectable({providedIn: 'root'})
96-
export class MatIconRegistry {
97+
export class MatIconRegistry implements OnDestroy {
9798
private _document: Document;
9899

99100
/**
@@ -310,6 +311,12 @@ export class MatIconRegistry {
310311
return observableThrow(getMatIconNameNotFoundError(key));
311312
}
312313

314+
ngOnDestroy() {
315+
this._svgIconConfigs.clear();
316+
this._iconSetConfigs.clear();
317+
this._cachedIconsByUrl.clear();
318+
}
319+
313320
/**
314321
* Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
315322
*/

‎tools/public_api_guard/lib/icon.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface MatIconLocation {
4545
export declare class MatIconModule {
4646
}
4747

48-
export declare class MatIconRegistry {
48+
export declare class MatIconRegistry implements OnDestroy {
4949
constructor(_httpClient: HttpClient, _sanitizer: DomSanitizer, document: any);
5050
addSvgIcon(iconName: string, url: SafeResourceUrl): this;
5151
addSvgIconInNamespace(namespace: string, iconName: string, url: SafeResourceUrl): this;
@@ -59,6 +59,7 @@ export declare class MatIconRegistry {
5959
getDefaultFontSetClass(): string;
6060
getNamedSvgIcon(name: string, namespace?: string): Observable<SVGElement>;
6161
getSvgIconFromUrl(safeUrl: SafeResourceUrl): Observable<SVGElement>;
62+
ngOnDestroy(): void;
6263
registerFontClassAlias(alias: string, className?: string): this;
6364
setDefaultFontSetClass(className: string): this;
6465
}

0 commit comments

Comments
 (0)
Please sign in to comment.