1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00
onion/js/stores/global_notification_store.js

70 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-06-08 18:14:25 +02:00
'use strict';
import { alt } from '../alt';
2015-06-08 18:14:25 +02:00
import GlobalNotificationActions from '../actions/global_notification_actions';
const GLOBAL_NOTIFICATION_COOLDOWN = 400;
2015-06-08 18:14:25 +02:00
class GlobalNotificationStore {
constructor() {
this.notificationQueue = [];
this.notificationStatus = 'ready';
this.notificationsPaused = false;
2015-06-08 18:14:25 +02:00
this.bindActions(GlobalNotificationActions);
}
onAppendGlobalNotification(newNotification) {
if (newNotification && newNotification.message) {
this.notificationQueue.push(newNotification);
2015-06-08 18:14:25 +02:00
if (!this.notificationsPaused && this.notificationStatus === 'ready') {
this.showNextNotification();
}
}
2015-06-08 18:14:25 +02:00
}
showNextNotification() {
this.notificationStatus = 'show';
setTimeout(GlobalNotificationActions.cooldownGlobalNotifications, this.notificationQueue[0].dismissAfter);
}
onCooldownGlobalNotifications() {
// When still paused on cooldown, don't shift the queue so we can repeat the current notification.
if (!this.notificationsPaused) {
this.notificationStatus = 'cooldown';
// Leave some time between consecutive notifications
setTimeout(GlobalNotificationActions.shiftGlobalNotification, GLOBAL_NOTIFICATION_COOLDOWN);
} else {
this.notificationStatus = 'ready';
}
}
onShiftGlobalNotification() {
this.notificationQueue.shift();
if (!this.notificationsPaused && this.notificationQueue.length > 0) {
this.showNextNotification();
} else {
this.notificationStatus = 'ready';
}
}
onPauseGlobalNotifications() {
this.notificationsPaused = true;
}
onResumeGlobalNotifications() {
this.notificationsPaused = false;
if (this.notificationStatus === 'ready' && this.notificationQueue.length > 0) {
this.showNextNotification();
}
2015-06-08 18:14:25 +02:00
}
}
2015-06-15 08:44:44 +02:00
export default alt.createStore(GlobalNotificationStore, 'GlobalNotificationStore');