2016-04-07 19:03:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-11 21:29:57 +02:00
|
|
|
let pendingMessages = new Map();
|
|
|
|
let listeners = new Map();
|
2016-04-07 19:03:49 +02:00
|
|
|
|
2016-04-09 18:54:23 +02:00
|
|
|
function unlisten(messageClass) {
|
2016-05-11 21:29:57 +02:00
|
|
|
listeners.set(messageClass, []);
|
2016-04-09 18:54:23 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 19:03:49 +02:00
|
|
|
function listen(messageClass, handler) {
|
2016-05-11 21:29:57 +02:00
|
|
|
if (pendingMessages.has(messageClass)) {
|
|
|
|
let newPendingMessages = [];
|
|
|
|
for (let message of pendingMessages.get(messageClass)) {
|
|
|
|
if (!handler(message)) {
|
|
|
|
newPendingMessages.push(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pendingMessages.set(messageClass, newPendingMessages);
|
2016-04-07 19:03:49 +02:00
|
|
|
}
|
2016-05-11 21:29:57 +02:00
|
|
|
if (!listeners.has(messageClass)) {
|
|
|
|
listeners.set(messageClass, []);
|
|
|
|
}
|
|
|
|
listeners.get(messageClass).push(handler);
|
2016-04-07 19:03:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function notify(messageClass, message) {
|
2016-05-11 21:29:57 +02:00
|
|
|
if (!listeners.has(messageClass) || !listeners.get(messageClass).length) {
|
|
|
|
if (!pendingMessages.has(messageClass)) {
|
|
|
|
pendingMessages.set(messageClass, []);
|
|
|
|
}
|
|
|
|
pendingMessages.get(messageClass).push(message);
|
2016-04-07 19:03:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-05-11 21:29:57 +02:00
|
|
|
for (let handler of listeners.get(messageClass)) {
|
2016-04-07 19:03:49 +02:00
|
|
|
handler(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2016-05-11 21:29:57 +02:00
|
|
|
Success: 'success',
|
|
|
|
Error: 'error',
|
|
|
|
Info: 'info',
|
|
|
|
Authentication: 'auth',
|
|
|
|
SettingsChange: 'settings-change',
|
|
|
|
TagsChange: 'tags-change',
|
2016-04-07 19:03:49 +02:00
|
|
|
|
|
|
|
notify: notify,
|
|
|
|
listen: listen,
|
2016-04-09 18:54:23 +02:00
|
|
|
unlisten: unlisten,
|
2016-04-07 19:03:49 +02:00
|
|
|
};
|