From 9561057b814f0b0befb3e04bc4c008790545815a Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 10 Jul 2019 16:12:04 +0200 Subject: [PATCH] storage.js: Allow to subscribe with multiple handlers to the same key --- public/js/icinga/storage.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/public/js/icinga/storage.js b/public/js/icinga/storage.js index 2f991afcb..b27af9f66 100644 --- a/public/js/icinga/storage.js +++ b/public/js/icinga/storage.js @@ -44,8 +44,9 @@ } if (typeof Icinga.Storage.subscribers[event.key] !== 'undefined') { - var subscriber = Icinga.Storage.subscribers[event.key]; - subscriber[0].call(subscriber[1], JSON.parse(event.newValue), JSON.parse(event.oldValue), event); + Icinga.Storage.subscribers[event.key].forEach(function (subscriber) { + subscriber[0].call(subscriber[1], JSON.parse(event.newValue), JSON.parse(event.oldValue), event); + }); } }); @@ -122,7 +123,13 @@ * @returns {void} */ onChange: function(key, callback, context) { - Icinga.Storage.subscribers[this.prefixKey(key)] = [callback, context]; + var prefixedKey = this.prefixKey(key); + + if (typeof Icinga.Storage.subscribers[prefixedKey] === 'undefined') { + Icinga.Storage.subscribers[prefixedKey] = []; + } + + Icinga.Storage.subscribers[prefixedKey].push([callback, context]); } };