fix(ui): throttle audio notification (#869)
This commit is contained in:
commit
2b67bc33cb
|
|
@ -2,6 +2,8 @@ import {reaction} from 'mobx';
|
||||||
import * as Notifications from './snack/browserNotification';
|
import * as Notifications from './snack/browserNotification';
|
||||||
import {StoreMapping} from './stores';
|
import {StoreMapping} from './stores';
|
||||||
|
|
||||||
|
const AUDIO_REPEAT_DELAY = 1000;
|
||||||
|
|
||||||
export const registerReactions = (stores: StoreMapping) => {
|
export const registerReactions = (stores: StoreMapping) => {
|
||||||
const clearAll = () => {
|
const clearAll = () => {
|
||||||
stores.messagesStore.clearAll();
|
stores.messagesStore.clearAll();
|
||||||
|
|
@ -10,13 +12,19 @@ export const registerReactions = (stores: StoreMapping) => {
|
||||||
stores.userStore.clear();
|
stores.userStore.clear();
|
||||||
stores.wsStore.close();
|
stores.wsStore.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let audio: HTMLAudioElement | undefined;
|
||||||
|
let lastAudio = 0;
|
||||||
|
|
||||||
const loadAll = () => {
|
const loadAll = () => {
|
||||||
stores.wsStore.listen((message) => {
|
stores.wsStore.listen((message) => {
|
||||||
stores.messagesStore.publishSingleMessage(message);
|
stores.messagesStore.publishSingleMessage(message);
|
||||||
Notifications.notifyNewMessage(message);
|
Notifications.notifyNewMessage(message);
|
||||||
if (message.priority >= 4) {
|
if (message.priority >= 4 && Date.now() > lastAudio + AUDIO_REPEAT_DELAY) {
|
||||||
const src = 'static/notification.ogg';
|
lastAudio = Date.now();
|
||||||
const audio = new Audio(src);
|
|
||||||
|
audio ??= new Audio('static/notification.ogg');
|
||||||
|
audio.currentTime = 0;
|
||||||
audio.play();
|
audio.play();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue