-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNotificationManager.py
34 lines (29 loc) · 1.21 KB
/
NotificationManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import time
from typing import Callable, List
class NotificationManager:
notification_queue: List[str] = []
time_since_last_message: float = 0
last_message_time: float = 0
message_duration: float
send_notification_func: Callable[[str], bool]
def __init__(
self, message_duration: float, send_notification_func: Callable[[str], bool]
):
self.message_duration = (
message_duration / 2
) # If there are multiple messages, the duration is shorter
self.send_notification_func = send_notification_func
def queue_notification(self, message: str):
self.notification_queue.append(message)
def handle_notifications(self):
self.time_since_last_message = time.time() - self.last_message_time
if (
len(self.notification_queue) > 0
and self.time_since_last_message >= self.message_duration
):
notification = self.notification_queue[0]
result = self.send_notification_func(notification)
if result:
self.notification_queue.pop(0)
self.last_message_time = time.time()
self.time_since_last_message = 0