#include "DoricGlobalBroadcast.h" QString DoricGlobalBroadcast::subscribe(QString name, std::function callback) { int id = this->idGenerator.fetchAndAddAcquire(1); QPair> pair(QString::number(id), callback); if (this->subjects.contains(name)) { QList>> value = this->subjects[name]; value.append(pair); this->subjects.insert(name, value); } else { QList>> value; value.append(pair); this->subjects.insert(name, value); } return QString::number(id); } void DoricGlobalBroadcast::unsubscribe(QString subscribeId) { QString targetKey; int targetIndex = -1; foreach (QString key, this->subjects.keys()) { QList>> value = this->subjects[key]; for (int i = 0; i != value.size(); i++) { QPair> pair = value.at(i); if (pair.first == subscribeId) { targetKey = key; targetIndex = i; } } } if (targetKey.isEmpty()) { return; } else { QList>> value = this->subjects[targetKey]; value.removeAt(targetIndex); if (value.size() == 0) { this->subjects.remove(targetKey); } else { this->subjects.insert(targetKey, value); } } } void DoricGlobalBroadcast::publish(QString name, QString data) { if (this->subjects.contains(name)) { QList>> value = this->subjects[name]; for (int i = 0; i != value.size(); i++) { QPair> pair = value.at(i); pair.second(data); } } }