ladybird/Userland/Libraries/LibConfig/Client.cpp
Andreas Kling edf7843409 ConfigServer+LibConfig: Add way for clients to listen for config changes
This patch adds a Config::Listener abstract class that anyone can
inherit from and receive notifications when configuration values change.

We don't yet monitor file system changes, so these only work for changes
made by ConfigServer itself.

In order to receive these notifications, clients must monitor the domain
by calling monitor_domain(). Only pledged domains can be monitored.

Note that the client initiating the change does not get notified.
2021-08-26 23:41:38 +02:00

85 lines
2.2 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibConfig/Client.h>
#include <LibConfig/Listener.h>
namespace Config {
static RefPtr<Client> s_the = nullptr;
Client& Client::the()
{
if (!s_the || !s_the->is_open()) {
VERIFY(Core::EventLoop::has_been_instantiated());
s_the = Client::construct();
}
return *s_the;
}
void Client::pledge_domains(Vector<String> const& domains)
{
async_pledge_domains(domains);
}
void Client::monitor_domain(String const& domain)
{
async_monitor_domain(domain);
}
String Client::read_string(StringView domain, StringView group, StringView key, StringView fallback)
{
return read_string_value(domain, group, key).value_or(fallback);
}
i32 Client::read_i32(StringView domain, StringView group, StringView key, i32 fallback)
{
return read_i32_value(domain, group, key).value_or(fallback);
}
bool Client::read_bool(StringView domain, StringView group, StringView key, bool fallback)
{
return read_bool_value(domain, group, key).value_or(fallback);
}
void Client::write_string(StringView domain, StringView group, StringView key, StringView value)
{
async_write_string_value(domain, group, key, value);
}
void Client::write_i32(StringView domain, StringView group, StringView key, i32 value)
{
async_write_i32_value(domain, group, key, value);
}
void Client::write_bool(StringView domain, StringView group, StringView key, bool value)
{
async_write_bool_value(domain, group, key, value);
}
void Client::notify_changed_string_value(String const& domain, String const& group, String const& key, String const& value)
{
Listener::for_each([&](auto& listener) {
listener.config_string_did_change(domain, group, key, value);
});
}
void Client::notify_changed_i32_value(String const& domain, String const& group, String const& key, i32 value)
{
Listener::for_each([&](auto& listener) {
listener.config_i32_did_change(domain, group, key, value);
});
}
void Client::notify_changed_bool_value(String const& domain, String const& group, String const& key, bool value)
{
Listener::for_each([&](auto& listener) {
listener.config_bool_did_change(domain, group, key, value);
});
}
}