58 lines
1.6 KiB
C++
58 lines
1.6 KiB
C++
#include "MetaCoreFoundation/MetaCoreRuntimeAssetRegistry.h"
|
|
|
|
#include <utility>
|
|
|
|
namespace MetaCore {
|
|
|
|
MetaCoreRuntimeAssetRegistry& MetaCoreRuntimeAssetRegistry::Get() {
|
|
static MetaCoreRuntimeAssetRegistry registry;
|
|
return registry;
|
|
}
|
|
|
|
MetaCoreRuntimeAssetRegistry::SubscriptionId MetaCoreRuntimeAssetRegistry::Subscribe(AssetChangeHandler handler) {
|
|
if (!handler) {
|
|
return 0;
|
|
}
|
|
|
|
std::scoped_lock lock(Mutex_);
|
|
const SubscriptionId subscriptionId = NextSubscriptionId_++;
|
|
Subscriptions_.emplace(subscriptionId, std::move(handler));
|
|
return subscriptionId;
|
|
}
|
|
|
|
void MetaCoreRuntimeAssetRegistry::Unsubscribe(SubscriptionId subscriptionId) {
|
|
std::scoped_lock lock(Mutex_);
|
|
Subscriptions_.erase(subscriptionId);
|
|
}
|
|
|
|
void MetaCoreRuntimeAssetRegistry::QueueEvent(MetaCoreAssetChangeEvent event) {
|
|
std::scoped_lock lock(Mutex_);
|
|
QueuedEvents_.push_back(std::move(event));
|
|
}
|
|
|
|
void MetaCoreRuntimeAssetRegistry::DispatchQueuedEvents() {
|
|
std::vector<MetaCoreAssetChangeEvent> events;
|
|
std::unordered_map<SubscriptionId, AssetChangeHandler> subscriptions;
|
|
{
|
|
std::scoped_lock lock(Mutex_);
|
|
events.swap(QueuedEvents_);
|
|
subscriptions = Subscriptions_;
|
|
}
|
|
|
|
for (const MetaCoreAssetChangeEvent& event : events) {
|
|
for (const auto& [subscriptionId, handler] : subscriptions) {
|
|
(void)subscriptionId;
|
|
if (handler) {
|
|
handler(event);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void MetaCoreRuntimeAssetRegistry::Clear() {
|
|
std::scoped_lock lock(Mutex_);
|
|
QueuedEvents_.clear();
|
|
}
|
|
|
|
} // namespace MetaCore
|