You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openNG/src/client/stores/notifications.js

63 lines
1.4 KiB
JavaScript

"use strict";
const xtend = require("xtend");
const nanoid = require("nanoid");
const immutable = require("immutable");
module.exports = function createNotificationStore({onUpdated}) {
let notifications = immutable.OrderedMap([]);
let timers = new Map();
function startTimer(notification, timeout) {
timers.set(notification, setTimeout(() => {
notification.expired = true;
onUpdated();
}, timeout));
}
function cancelTimer(notification) {
if (timers.has(notification)) {
clearTimeout(timers.get(notification));
}
}
return {
add: function (notification) {
let id = nanoid();
notifications = notifications.set(id, xtend(notification, {
id: id
}));
this.unblockExpiry(id);
onUpdated();
return id;
},
markRead: function (id) {
notifications.get(id).markedRead = true;
onUpdated();
},
blockExpiry: function (id) {
let notification = notifications.get(id);
cancelTimer(notification);
},
unblockExpiry: function (id) {
let notification = notifications.get(id);
cancelTimer(notification);
if (notification.timeout != null && notification.timeout !== false) {
startTimer(notification, notification.timeout);
}
},
getAll: function () {
return notifications;
},
getVisible: function () {
return notifications.filter((notification) => {
return (!notification.markedRead && !notification.expired);
});
}
};
};