Skip to content

Commit

Permalink
feat: return active toast if preventDuplicates is enabled (#550) (#551)
Browse files Browse the repository at this point in the history
Fixes #550
  • Loading branch information
rkettelerij authored and scttcper committed Sep 19, 2018
1 parent 45e2594 commit ecab216
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ imports: [

### Toastr Service methods return:

Toastr Service will return undefined if prevent duplicates is enabled

```typescript
export interface ActiveToast {
/** Your Toast ID. Use this to close it individually */
Expand Down
28 changes: 14 additions & 14 deletions src/lib/toastr/toastr.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,19 @@ export class ToastrService {
}

/**
* Determines if toast message is already shown
* Finds a duplicate toast if one exists
*/
isDuplicate(message: string, resetOnDuplicate: boolean) {
private findDuplicate(message: string, resetOnDuplicate: boolean) {
for (let i = 0; i < this.toasts.length; i++) {
if (this.toasts[i].message === message) {
if (
resetOnDuplicate &&
this.toasts[i].toastRef.componentInstance.resetTimeout
) {
this.toasts[i].toastRef.resetTimeout();
const toast = this.toasts[i];
if (toast.message === message) {
if (resetOnDuplicate && toast.toastRef.componentInstance.resetTimeout) {
toast.toastRef.resetTimeout();
}
return true;
return toast;
}
}
return false;
return null;
}

/** create a clone of global config and apply individual settings */
Expand Down Expand Up @@ -231,7 +229,7 @@ export class ToastrService {

/**
* Creates and attaches toast data to component
* returns null if toast is duplicate and preventDuplicates == True
* returns the active toast, or in case preventDuplicates is enabled the original/non-duplicate active toast.
*/
private _buildNotification(
toastType: string,
Expand All @@ -245,10 +243,12 @@ export class ToastrService {
// max opened and auto dismiss = true
if (
message &&
this.toastrConfig.preventDuplicates &&
this.isDuplicate(message, this.toastrConfig.resetTimeoutOnDuplicate)
this.toastrConfig.preventDuplicates
) {
return null;
const duplicate = this.findDuplicate(message, this.toastrConfig.resetTimeoutOnDuplicate);
if (duplicate !== null) {
return duplicate;
}
}
this.previousToastMessage = message;
let keepInactive = false;
Expand Down

0 comments on commit ecab216

Please sign in to comment.