From 85d38f793907c468cbd7c99a4fd764ee20075b2e Mon Sep 17 00:00:00 2001 From: Florent Date: Tue, 6 Jul 2021 13:38:20 +0200 Subject: [PATCH] Fix addListeners --- src/main.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main.ts b/src/main.ts index 07a0f00..58303d9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -61,25 +61,29 @@ async function register({ } async function addListeners(listenerInput: string) { - let objs: ListenerData[]; + let listeners: ListenerData[]; try { - objs = JSON.parse(listenerInput); + listeners = JSON.parse(listenerInput); } catch { logger.warn('Erreur: malformé'); 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) { - if (listenersDataBinding.has(item.address)) break; - listenersDataBinding.set(item.address, { - ChannelId: item.ChannelId, - firstUpdate: item.firstUpdate, - address: item.address, + for (const newItem of newListeners) { + listenersDataBinding.set(newItem.address, { + ChannelId: newItem.ChannelId, + firstUpdate: newItem.firstUpdate, + address: newItem.address, }); } + for (const removedUrl of removedUrls) { + listenersDataBinding.delete(removedUrl); + } myManager.stopAll(); - await myManager.saveOverride(objs); + await myManager.saveOverride(listeners); firstUpdate = true; if (logger) logger.warn('Configuration modifiée: ' + listenerInput);