mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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.
This commit is contained in:
committed by
vivian-hu
parent
e93cc53865
commit
f6b428854c
@@ -15,6 +15,7 @@ import {
|
||||
Optional,
|
||||
SecurityContext,
|
||||
SkipSelf,
|
||||
OnDestroy,
|
||||
} from '@angular/core';
|
||||
import {DomSanitizer, SafeResourceUrl, SafeHtml} from '@angular/platform-browser';
|
||||
import {forkJoin, Observable, of as observableOf, throwError as observableThrow} from 'rxjs';
|
||||
@@ -93,7 +94,7 @@ class SvgIconConfig {
|
||||
* - Loads icons from URLs and extracts individual icons from icon sets.
|
||||
*/
|
||||
@Injectable({providedIn: 'root'})
|
||||
export class MatIconRegistry {
|
||||
export class MatIconRegistry implements OnDestroy {
|
||||
private _document: Document;
|
||||
|
||||
/**
|
||||
@@ -310,6 +311,12 @@ export class MatIconRegistry {
|
||||
return observableThrow(getMatIconNameNotFoundError(key));
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
this._svgIconConfigs.clear();
|
||||
this._iconSetConfigs.clear();
|
||||
this._cachedIconsByUrl.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cached icon for a SvgIconConfig if available, or fetches it from its URL if not.
|
||||
*/
|
||||
|
||||
Vendored
+2
-1
@@ -45,7 +45,7 @@ export interface MatIconLocation {
|
||||
export declare class MatIconModule {
|
||||
}
|
||||
|
||||
export declare class MatIconRegistry {
|
||||
export declare class MatIconRegistry implements OnDestroy {
|
||||
constructor(_httpClient: HttpClient, _sanitizer: DomSanitizer, document: any);
|
||||
addSvgIcon(iconName: string, url: SafeResourceUrl): this;
|
||||
addSvgIconInNamespace(namespace: string, iconName: string, url: SafeResourceUrl): this;
|
||||
@@ -59,6 +59,7 @@ export declare class MatIconRegistry {
|
||||
getDefaultFontSetClass(): string;
|
||||
getNamedSvgIcon(name: string, namespace?: string): Observable<SVGElement>;
|
||||
getSvgIconFromUrl(safeUrl: SafeResourceUrl): Observable<SVGElement>;
|
||||
ngOnDestroy(): void;
|
||||
registerFontClassAlias(alias: string, className?: string): this;
|
||||
setDefaultFontSetClass(className: string): this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user