From 72a39cb09d24a8750e0461f71ca3bfddaaea8b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Tue, 4 Jun 2019 07:43:21 -0700 Subject: [PATCH] notification throttling Summary: Notification from plugins (especially network) could be very spammy. This diff introduces a throttle, so a plugin can only send a notification every 5 seconds. If a notification is sent earlier, it is not displayed and the counter is reset to 5 seconds from now. In the notifications panel still all notifications are dispalyed. Reviewed By: passy Differential Revision: D15621427 fbshipit-source-id: ea5e3b39fdd5cdedff3fd5234e752800520a423b --- src/dispatcher/notifications.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/dispatcher/notifications.js b/src/dispatcher/notifications.js index da2e649d9..1e946cc5d 100644 --- a/src/dispatcher/notifications.js +++ b/src/dispatcher/notifications.js @@ -21,6 +21,7 @@ import {textContent} from '../utils/index'; import GK from '../fb-stubs/GK'; type NotificationEvents = 'show' | 'click' | 'close' | 'reply' | 'action'; +const NOTIFICATION_THROTTLE = 5 * 1000; // in milliseconds export default (store: Store, logger: Logger) => { if (GK.get('flipper_disable_notifications')) { @@ -29,6 +30,7 @@ export default (store: Store, logger: Logger) => { const knownNotifications: Set = new Set(); const knownPluginStates: Map = new Map(); + const lastNotificationTime: Map = new Map(); ipcRenderer.on( 'notificationEvent', @@ -135,6 +137,19 @@ export default (store: Store, logger: Logger) => { (!n.notification.category || blacklistedCategories.indexOf(n.notification.category) === -1) ) { + const prevNotificationTime: number = + lastNotificationTime.get(n.pluginId) || 0; + lastNotificationTime.set(n.pluginId, new Date().getTime()); + knownNotifications.add(n.notification.id); + + if ( + new Date().getTime() - prevNotificationTime < + NOTIFICATION_THROTTLE + ) { + // Don't send a notification if the plugin has sent a notification + // within the NOTIFICATION_THROTTLE. + return; + } ipcRenderer.send('sendNotification', { payload: { title: n.notification.title, @@ -159,7 +174,6 @@ export default (store: Store, logger: Logger) => { pluginNotification: n, }); logger.track('usage', 'native-notification', n.notification); - knownNotifications.add(n.notification.id); } }); });