Move presync callback functionality from SkyBrowserModule to ServerModule in order to expose the functionality to other topics as well

This commit is contained in:
Ylva Selling
2022-06-01 15:06:40 -04:00
parent fed07a0847
commit c9a63274a8
5 changed files with 44 additions and 43 deletions

View File

@@ -50,6 +50,15 @@ ServerModule::ServerModule()
, _interfaceOwner({"Interfaces", "Interfaces", "Server Interfaces"})
{
addPropertySubOwner(_interfaceOwner);
global::callback::preSync->emplace_back([this]() {
// Trigger callbacks
using K = CallbackHandle;
using V = CallbackFunction;
for (const std::pair<const K, V>& it : _preSyncCallbacks) {
it.second(); // call function
}
});
}
ServerModule::~ServerModule() {
@@ -233,4 +242,28 @@ void ServerModule::consumeMessages() {
}
}
ServerModule::CallbackHandle ServerModule::addPreSyncCallback(CallbackFunction cb)
{
CallbackHandle handle = _nextCallbackHandle++;
_preSyncCallbacks.emplace_back(handle, std::move(cb));
return handle;
}
void ServerModule::removePreSyncCallback(CallbackHandle handle) {
const auto it = std::find_if(
_preSyncCallbacks.begin(),
_preSyncCallbacks.end(),
[handle](const std::pair<CallbackHandle, CallbackFunction>& cb) {
return cb.first == handle;
}
);
ghoul_assert(
it != _preSyncCallbacks.end(),
"handle must be a valid callback handle"
);
_preSyncCallbacks.erase(it);
}
} // namespace openspace

View File

@@ -49,6 +49,8 @@ struct Message {
class ServerModule : public OpenSpaceModule {
public:
static constexpr const char* Name = "Server";
using CallbackHandle = int;
using CallbackFunction = std::function<void()>;
ServerModule();
virtual ~ServerModule();
@@ -57,6 +59,9 @@ public:
int skyBrowserUpdateTime() const;
CallbackHandle addPreSyncCallback(CallbackFunction cb);
void removePreSyncCallback(CallbackHandle handle);
protected:
void internalInitialize(const ghoul::Dictionary& configuration) override;
@@ -79,6 +84,10 @@ private:
std::vector<std::unique_ptr<ServerInterface>> _interfaces;
properties::PropertyOwner _interfaceOwner;
int _skyBrowserUpdateTime = 100;
// Callbacks for tiggering topic
int _nextCallbackHandle = 0;
std::vector<std::pair<CallbackHandle, CallbackFunction>> _preSyncCallbacks;
};
} // namespace openspace

View File

@@ -55,7 +55,7 @@ SkyBrowserTopic::SkyBrowserTopic()
SkyBrowserTopic::~SkyBrowserTopic() {
if (_targetDataCallbackHandle != UnsetOnChangeHandle) {
SkyBrowserModule* module = global::moduleEngine->module<SkyBrowserModule>();
ServerModule* module = global::moduleEngine->module<ServerModule>();
if (module) {
module->removePreSyncCallback(_targetDataCallbackHandle);
}
@@ -78,7 +78,7 @@ void SkyBrowserTopic::handleJson(const nlohmann::json& json) {
return;
}
SkyBrowserModule* module = global::moduleEngine->module<SkyBrowserModule>();
ServerModule* module = global::moduleEngine->module<ServerModule>();
_targetDataCallbackHandle = module->addPreSyncCallback(
[this]() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();