@@ -27,6 +27,7 @@ import {
2727 ViewEncapsulation ,
2828} from '@angular/core' ;
2929import { CanColor , CanColorCtor , mixinColor } from '@angular/material/core' ;
30+ import { Subscription } from 'rxjs' ;
3031import { take } from 'rxjs/operators' ;
3132
3233import { MatIconRegistry } from './icon-registry' ;
@@ -178,6 +179,9 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
178179 /** Keeps track of the elements and attributes that we've prefixed with the current path. */
179180 private _elementsWithExternalReferences ?: Map < Element , { name : string , value : string } [ ] > ;
180181
182+ /** Subscription to the current in-progress SVG icon request. */
183+ private _currentIconFetch = Subscription . EMPTY ;
184+
181185 constructor (
182186 elementRef : ElementRef < HTMLElement > , private _iconRegistry : MatIconRegistry ,
183187 @Attribute ( 'aria-hidden' ) ariaHidden : string ,
@@ -227,10 +231,12 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
227231 const svgIconChanges = changes [ 'svgIcon' ] ;
228232
229233 if ( svgIconChanges ) {
234+ this . _currentIconFetch . unsubscribe ( ) ;
235+
230236 if ( this . svgIcon ) {
231237 const [ namespace , iconName ] = this . _splitIconName ( this . svgIcon ) ;
232238
233- this . _iconRegistry . getNamedSvgIcon ( iconName , namespace )
239+ this . _currentIconFetch = this . _iconRegistry . getNamedSvgIcon ( iconName , namespace )
234240 . pipe ( take ( 1 ) )
235241 . subscribe ( svg => this . _setSvgElement ( svg ) , ( err : Error ) => {
236242 const errorMessage = `Error retrieving icon ${ namespace } :${ iconName } ! ${ err . message } ` ;
@@ -279,6 +285,8 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
279285 }
280286
281287 ngOnDestroy ( ) {
288+ this . _currentIconFetch . unsubscribe ( ) ;
289+
282290 if ( this . _elementsWithExternalReferences ) {
283291 this . _elementsWithExternalReferences . clear ( ) ;
284292 }
0 commit comments