From 02a7f3908052c5d5adbf74fb979222bfa0289b57 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 17 Dec 2018 15:01:44 +0100 Subject: [PATCH] MOBILE-2791 core: Prevent repeated alerts --- src/providers/utils/dom.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index 320344365..faf948769 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -24,6 +24,7 @@ import { CoreAppProvider } from '../app'; import { CoreConfigProvider } from '../config'; import { CoreUrlUtilsProvider } from './url'; import { CoreConstants } from '@core/constants'; +import { Md5 } from 'ts-md5/dist/md5'; /* * "Utils" service with helper functions for UI, DOM elements and HTML code. @@ -41,6 +42,7 @@ export class CoreDomUtilsProvider { protected instances: {[id: string]: any} = {}; // Store component/directive instances by id. protected lastInstanceId = 0; protected debugDisplay = false; // Whether to display debug messages. Store it in a variable to make it synchronous. + protected displayedAlerts = {}; // To prevent duplicated alerts. constructor(private translate: TranslateService, private loadingCtrl: LoadingController, private toastCtrl: ToastController, private alertCtrl: AlertController, private textUtils: CoreTextUtilsProvider, private appProvider: CoreAppProvider, @@ -898,6 +900,12 @@ export class CoreDomUtilsProvider { } return promise.then((message) => { + const alertId = Md5.hashAsciiStr((title || '') + '#' + (message || '')); + + if (this.displayedAlerts[alertId]) { + // There's already an alert with the same message and title. Return it. + return this.displayedAlerts[alertId]; + } const alert = this.alertCtrl.create({ title: title, @@ -913,6 +921,13 @@ export class CoreDomUtilsProvider { } }); + // Store the alert and remove it when dismissed. + this.displayedAlerts[alertId] = alert; + + alert.onDidDismiss(() => { + delete this.displayedAlerts[alertId]; + }); + if (autocloseTime > 0) { setTimeout(() => { alert.dismiss();