"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); }); } }; };