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
This commit is contained in:
Daniel Büchele
2019-06-04 07:43:21 -07:00
committed by Facebook Github Bot
parent eba84a7e08
commit 72a39cb09d

View File

@@ -21,6 +21,7 @@ import {textContent} from '../utils/index';
import GK from '../fb-stubs/GK'; import GK from '../fb-stubs/GK';
type NotificationEvents = 'show' | 'click' | 'close' | 'reply' | 'action'; type NotificationEvents = 'show' | 'click' | 'close' | 'reply' | 'action';
const NOTIFICATION_THROTTLE = 5 * 1000; // in milliseconds
export default (store: Store, logger: Logger) => { export default (store: Store, logger: Logger) => {
if (GK.get('flipper_disable_notifications')) { if (GK.get('flipper_disable_notifications')) {
@@ -29,6 +30,7 @@ export default (store: Store, logger: Logger) => {
const knownNotifications: Set<string> = new Set(); const knownNotifications: Set<string> = new Set();
const knownPluginStates: Map<string, Object> = new Map(); const knownPluginStates: Map<string, Object> = new Map();
const lastNotificationTime: Map<string, number> = new Map();
ipcRenderer.on( ipcRenderer.on(
'notificationEvent', 'notificationEvent',
@@ -135,6 +137,19 @@ export default (store: Store, logger: Logger) => {
(!n.notification.category || (!n.notification.category ||
blacklistedCategories.indexOf(n.notification.category) === -1) 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', { ipcRenderer.send('sendNotification', {
payload: { payload: {
title: n.notification.title, title: n.notification.title,
@@ -159,7 +174,6 @@ export default (store: Store, logger: Logger) => {
pluginNotification: n, pluginNotification: n,
}); });
logger.track('usage', 'native-notification', n.notification); logger.track('usage', 'native-notification', n.notification);
knownNotifications.add(n.notification.id);
} }
}); });
}); });