From 9d148f5a7362514a0afc8e1fd2f0aea81cbd5ca8 Mon Sep 17 00:00:00 2001 From: CaselIT Date: Sun, 11 Sep 2016 14:39:24 +0200 Subject: [PATCH] feature(removeToast): Added removeToast observable. https://github.com/Stabzs/Angular2-Toaster/issues/42 --- src/toaster-container.component.spec.ts | 15 +++++++++++++++ src/toaster-container.component.ts | 1 + src/toaster.service.ts | 8 +++++++- src/tsconfig.json | 3 ++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/toaster-container.component.spec.ts b/src/toaster-container.component.spec.ts index fe6ef7e8..3dd828c8 100644 --- a/src/toaster-container.component.spec.ts +++ b/src/toaster-container.component.spec.ts @@ -548,6 +548,21 @@ describe('ToasterContainerComponent with sync ToasterService', () => { expect(status).toBe('updated'); }); + it('removeToast notifies the removeToast subscribers', (done) => { + toasterContainer.ngOnInit(); + + var toast: Toast = { type: 'info', title: 'default' }; + toasterService.pop(toast); + + toasterService.removeToast.subscribe(t => { + expect(t.toastId).toEqual(toast.toastId); + expect(t.toastContainerId).toEqual(toast.toastContainerId); + done(); + }); + + toasterService.clear(toast.toastId); + }); + it('clearToasts will clear toasts from all containers if toastContainerId is undefined', () => { toasterContainer.ngOnInit(); diff --git a/src/toaster-container.component.ts b/src/toaster-container.component.ts index 080b6599..17f7e4d5 100644 --- a/src/toaster-container.component.ts +++ b/src/toaster-container.component.ts @@ -173,6 +173,7 @@ export class ToasterContainerComponent { toast.timeoutId = null; } if (toast.onHideCallback) toast.onHideCallback(toast); + this.toasterService._removeToastSubject.next({ toastId: toast.toastId, toastContainerId: toast.toastContainerId }); } private removeAllToasts() { diff --git a/src/toaster.service.ts b/src/toaster.service.ts index 155aa45c..6e37cdcc 100644 --- a/src/toaster.service.ts +++ b/src/toaster.service.ts @@ -3,6 +3,7 @@ import {Toast} from './toast'; import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/share'; import {Observer} from 'rxjs/Observer'; +import {Subject} from 'rxjs/Subject'; @Injectable() @@ -13,13 +14,18 @@ export class ToasterService { clearToasts: Observable; private _clearToasts: Observer; - + removeToast: Observable; + /** @internal */ + _removeToastSubject: Subject + /** * Creates an instance of ToasterService. */ constructor() { this.addToast = new Observable(observer => this._addToast = observer).share(); this.clearToasts = new Observable(observer => this._clearToasts = observer).share(); + this._removeToastSubject = new Subject() + this.removeToast = this._removeToastSubject.share(); } diff --git a/src/tsconfig.json b/src/tsconfig.json index d663b678..1d099e69 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -9,7 +9,8 @@ "removeComments": false, "noImplicitAny": false, "declaration": true, - "outDir": "../lib" + "outDir": "../lib", + "stripInternal": true }, "files": [ "bodyOutputType.ts",