Skip to content

Commit 291437c

Browse files
committed
refactor(toast): subscribe to observables on init to prevent leaks
1 parent 1f5df66 commit 291437c

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

projects/igniteui-angular/src/lib/toast/toast.component.spec.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,11 @@ fdescribe('IgxToast', () => {
7777
expect(toast.isVisible).toBeTruthy();
7878
}));
7979

80-
it('should emit onShowing when super onOpening is fired', waitForAsync(() => {
80+
it('should emit onShowing when toast is shown', () => {
81+
spyOn(toast.onShowing, 'emit');
8182
toast.show();
82-
83-
toast.onOpening.subscribe(() => {
84-
spyOn(toast.onShowing, 'emit');
85-
expect(toast.onShowing.emit).toHaveBeenCalled();
86-
});
87-
}));
83+
expect(toast.onShowing.emit).toHaveBeenCalled();
84+
});
8885

8986
it('should emit onShown when super onAppended is fired', waitForAsync(() => {
9087
toast.show();
@@ -95,15 +92,11 @@ fdescribe('IgxToast', () => {
9592
});
9693
}));
9794

98-
it('should emit onHiding when super onClosing is fired', waitForAsync(() => {
99-
toast.isVisible = true;
95+
it('should emit onHiding when toast is hidden', () => {
96+
spyOn(toast.onHiding, 'emit');
10097
toast.hide();
101-
102-
toast.onClosing.subscribe(() => {
103-
spyOn(toast.onHiding, 'emit');
104-
expect(toast.onHiding.emit).toHaveBeenCalled();
105-
});
106-
}));
98+
expect(toast.onHiding.emit).toHaveBeenCalled();
99+
});
107100

108101
it('should emit onHidden when super onClosed is fired', waitForAsync(() => {
109102
toast.isVisible = true;
@@ -115,14 +108,15 @@ fdescribe('IgxToast', () => {
115108
});
116109
}));
117110

118-
it('visibility is updated by the toggle() method', waitForAsync(() => {
119-
toast.isVisible = true;
111+
it('visibility is updated by the toggle() method', () => {
120112
toast.toggle();
113+
fixture.detectChanges();
114+
expect(toast.isVisible).toEqual(true);
121115

122-
toast.onHiding.subscribe(() => {
123-
expect(toast.isVisible).toEqual(false);
124-
});
125-
}));
116+
toast.toggle();
117+
fixture.detectChanges();
118+
expect(toast.isVisible).toEqual(false);
119+
});
126120

127121
it('can set message through show method', fakeAsync(() => {
128122
toast.displayTime = 100;

projects/igniteui-angular/src/lib/toast/toast.component.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ import {
99
Inject,
1010
Input,
1111
NgModule,
12+
OnDestroy,
13+
OnInit,
1214
Optional,
1315
Output,
1416
} from '@angular/core';
17+
import { Subject } from 'rxjs';
18+
import { takeUntil } from 'rxjs/operators';
1519
import { IgxNavigationService, IToggleView } from '../core/navigation';
1620
import { IgxToggleDirective } from '../directives/toggle/toggle.directive';
1721
import { IgxOverlayOutletDirective } from '../directives/toggle/toggle.directive';
@@ -61,7 +65,8 @@ export type IgxToastPosition = keyof typeof IgxToastPositionEnum;
6165
templateUrl: 'toast.component.html',
6266
})
6367
export class IgxToastComponent extends IgxToggleDirective
64-
implements IToggleView {
68+
implements IToggleView, OnInit, OnDestroy {
69+
private d$ = new Subject<boolean>();
6570
private _isVisible = false;
6671

6772
/**
@@ -262,7 +267,7 @@ export class IgxToastComponent extends IgxToggleDirective
262267
* @memberof IgxToastComponent
263268
*/
264269
public get element() {
265-
return this.element.nativeElement;
270+
return this._element.nativeElement;
266271
}
267272

268273
/**
@@ -277,7 +282,7 @@ export class IgxToastComponent extends IgxToggleDirective
277282
private timeoutId: number;
278283

279284
constructor(
280-
_element: ElementRef,
285+
private _element: ElementRef,
281286
cdr: ChangeDetectorRef,
282287
@Optional() navService: IgxNavigationService,
283288
@Inject(IgxOverlayService) overlayService: IgxOverlayService
@@ -316,14 +321,8 @@ export class IgxToastComponent extends IgxToggleDirective
316321
this.toastMessage = message;
317322
}
318323

319-
this.onOpening.subscribe(() => {
320-
this.isVisible = true;
321-
this.onShowing.emit(this);
322-
});
323-
324-
this.onAppended.subscribe(() => {
325-
this.onShown.emit(this);
326-
});
324+
this.isVisible = true;
325+
this.onShowing.emit(this);
327326

328327
super.open(overlaySettings);
329328

@@ -343,17 +342,9 @@ export class IgxToastComponent extends IgxToggleDirective
343342
*/
344343
public hide(): void {
345344
clearInterval(this.timeoutId);
346-
347-
this.onClosing.subscribe(() => {
348-
this.isVisible = false;
349-
this.onHiding.emit(this);
350-
});
351-
345+
this.isVisible = false;
346+
this.onHiding.emit(this);
352347
super.close();
353-
354-
this.onClosed.subscribe(() => {
355-
this.onHidden.emit(this);
356-
});
357348
}
358349

359350
/**
@@ -382,6 +373,27 @@ export class IgxToastComponent extends IgxToggleDirective
382373
public toggle() {
383374
super.toggle();
384375
}
376+
377+
/**
378+
* @hidden
379+
*/
380+
ngOnInit() {
381+
this.onAppended.pipe(takeUntil(this.d$)).subscribe(() => {
382+
this.onShown.emit(this);
383+
});
384+
385+
this.onClosed.pipe(takeUntil(this.d$)).subscribe(() => {
386+
this.onHidden.emit(this);
387+
});
388+
}
389+
390+
/**
391+
* @hidden
392+
*/
393+
ngOnDestroy() {
394+
this.d$.next(true);
395+
this.d$.complete();
396+
}
385397
}
386398

387399
/**

0 commit comments

Comments
 (0)