1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
class CEventHub {
static EVENT = '_EVENT';
static EVENT_NATIVE = 'native';
static EVENT_SUBSCRIBE = 'subscribe';
static EVENT_UNSUBSCRIBE = 'unsubscribe';
#subscribers = new Map();
#latest_data = new Map();
publish({data, descriptor}) {
descriptor = {
[CEventHub.EVENT]: CEventHub.EVENT_NATIVE,
...descriptor
};
descriptor = Object.keys(descriptor).sort().reduce(
(descriptor_sorted, key) => {
descriptor_sorted[key] = descriptor[key];
return descriptor_sorted;
},
{}
);
const descriptor_hash = JSON.stringify(descriptor);
this.#latest_data.delete(descriptor_hash);
this.#latest_data.set(descriptor_hash, {data, descriptor});
for (const {require, callback} of this.#subscribers.values()) {
if (CEventHub.#match(require, descriptor)) {
callback({data, descriptor});
}
}
return this;
}
subscribe({require = {}, callback}) {
require = {[CEventHub.EVENT]: CEventHub.EVENT_NATIVE, ...require};
for (const {data, descriptor} of [...this.#latest_data.values()].reverse()) {
if (CEventHub.#match(require, descriptor)) {
callback({data, descriptor});
break;
}
}
const subscription = {};
this.#subscribers.set(subscription, {require, callback});
this
.publish({
data: require,
descriptor: {...require, [CEventHub.EVENT]: CEventHub.EVENT_SUBSCRIBE}
})
.invalidateData({[CEventHub.EVENT]: CEventHub.EVENT_SUBSCRIBE});
return subscription;
}
unsubscribe(subscription) {
if (!this.#subscribers.has(subscription)) {
return false;
}
const {require} = this.#subscribers.get(subscription);
this.#subscribers.delete(subscription);
this
.publish({
data: require,
descriptor: {...require, [CEventHub.EVENT]: CEventHub.EVENT_UNSUBSCRIBE}
})
.invalidateData({[CEventHub.EVENT]: CEventHub.EVENT_UNSUBSCRIBE});
return true;
}
hasSubscribers(require = {}) {
require = {[CEventHub.EVENT]: CEventHub.EVENT_NATIVE, ...require};
for (const subscriber of this.#subscribers.values()) {
if (CEventHub.#match(require, subscriber.require)) {
return true;
}
}
return false;
}
getData(require) {
for (const {data, descriptor} of [...this.#latest_data.values()].reverse()) {
if (CEventHub.#match(require, descriptor)) {
return data;
}
}
return undefined;
}
invalidateData(require) {
for (const [descriptor_hash, {descriptor}] of this.#latest_data.entries()) {
if (CEventHub.#match(require, descriptor)) {
this.#latest_data.delete(descriptor_hash);
}
}
return this;
}
static #match(require, descriptor) {
for (const [key, value] of Object.entries(require)) {
if (!(key in descriptor) || descriptor[key] !== value) {
return false;
}
}
return true;
}
}
|