Fix addListeners

This commit is contained in:
Florent Fayolle 2021-07-06 13:38:20 +02:00
parent f21f752cbb
commit 85d38f7939

View File

@ -61,25 +61,29 @@ async function register({
} }
async function addListeners(listenerInput: string) { async function addListeners(listenerInput: string) {
let objs: ListenerData[]; let listeners: ListenerData[];
try { try {
objs = JSON.parse(listenerInput); listeners = JSON.parse(listenerInput);
} catch { } catch {
logger.warn('Erreur: malformé'); logger.warn('Erreur: malformé');
return; return;
} }
let newListeners = listeners.filter(item => !listenersDataBinding.has(item.address));
let removedUrls = Array.from(listenersDataBinding.keys()).filter(url => !listeners.some(listener => listener.url === url));
for (const item of objs) { for (const newItem of newListeners) {
if (listenersDataBinding.has(item.address)) break; listenersDataBinding.set(newItem.address, {
listenersDataBinding.set(item.address, { ChannelId: newItem.ChannelId,
ChannelId: item.ChannelId, firstUpdate: newItem.firstUpdate,
firstUpdate: item.firstUpdate, address: newItem.address,
address: item.address,
}); });
} }
for (const removedUrl of removedUrls) {
listenersDataBinding.delete(removedUrl);
}
myManager.stopAll(); myManager.stopAll();
await myManager.saveOverride(objs); await myManager.saveOverride(listeners);
firstUpdate = true; firstUpdate = true;
if (logger) logger.warn('Configuration modifiée: ' + listenerInput); if (logger) logger.warn('Configuration modifiée: ' + listenerInput);