storage.js: Allow to subscribe with multiple handlers to the same key

This commit is contained in:
Johannes Meyer 2019-07-10 16:12:04 +02:00
parent 2bf050f57d
commit 9561057b81
1 changed files with 10 additions and 3 deletions

View File

@ -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]);
}
};