2015-06-08 18:14:25 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 16:47:59 +02:00
|
|
|
import { alt } from '../alt';
|
2015-06-08 18:14:25 +02:00
|
|
|
|
|
|
|
import GlobalNotificationActions from '../actions/global_notification_actions';
|
|
|
|
|
2015-11-21 19:23:20 +01:00
|
|
|
const GLOBAL_NOTIFICATION_COOLDOWN = 400;
|
|
|
|
|
2015-06-08 18:14:25 +02:00
|
|
|
class GlobalNotificationStore {
|
|
|
|
constructor() {
|
2015-11-21 19:23:20 +01:00
|
|
|
this.notificationQueue = [];
|
|
|
|
this.notificationStatus = 'ready';
|
|
|
|
this.notificationsPaused = false;
|
2015-06-08 18:14:25 +02:00
|
|
|
|
|
|
|
this.bindActions(GlobalNotificationActions);
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:08:14 +02:00
|
|
|
onAppendGlobalNotification(newNotification) {
|
2016-03-10 12:45:21 +01:00
|
|
|
if (newNotification && newNotification.message) {
|
|
|
|
this.notificationQueue.push(newNotification);
|
2015-06-08 18:14:25 +02:00
|
|
|
|
2016-03-10 12:45:21 +01:00
|
|
|
if (!this.notificationsPaused && this.notificationStatus === 'ready') {
|
|
|
|
this.showNextNotification();
|
|
|
|
}
|
2015-11-21 19:23:20 +01:00
|
|
|
}
|
2015-06-08 18:14:25 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 19:23:20 +01:00
|
|
|
showNextNotification() {
|
|
|
|
this.notificationStatus = 'show';
|
2015-06-09 12:08:14 +02:00
|
|
|
|
2015-11-21 19:23:20 +01:00
|
|
|
setTimeout(GlobalNotificationActions.cooldownGlobalNotifications, this.notificationQueue[0].dismissAfter);
|
|
|
|
}
|
2015-06-09 12:08:14 +02:00
|
|
|
|
2015-11-21 19:23:20 +01:00
|
|
|
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';
|
|
|
|
}
|
2015-06-09 12:08:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onShiftGlobalNotification() {
|
2015-11-21 19:23:20 +01:00
|
|
|
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');
|