From 7e693ad7a6b2ee7c3f5c62a4f9819718b3b8b9f1 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 19 Oct 2021 10:00:42 +0200 Subject: options: don't notify on idempotent values --- options/value.hpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'options/value.hpp') diff --git a/options/value.hpp b/options/value.hpp index 10903b19..85b76cb6 100644 --- a/options/value.hpp +++ b/options/value.hpp @@ -39,7 +39,9 @@ template class value final : public value_ { static_assert(std::is_same_v>); + mutable QMutex mtx; const t def; + mutable t cached_value; using traits = detail::value_traits; never_inline @@ -95,11 +97,25 @@ public: never_inline void notify() const override { - if (!is_null()) + if (is_null()) + return; + + auto x = get(); { - maybe_trace(true); - emit valueChanged(traits::storage_from_value(get())); - maybe_trace(false); + QMutexLocker l(&mtx); + if (traits::is_equal(x, cached_value)) + { + //maybe_trace("notify ~"); + return; + } + else + { + cached_value = x; + l.unlock(); + maybe_trace("notify +"); + emit valueChanged(traits::storage_from_value(x)); + maybe_trace("notify -"); + } } } @@ -131,7 +147,7 @@ public: static constexpr Qt::ConnectionType DIRECT_CONNTYPE = Qt::DirectConnection; static constexpr Qt::ConnectionType SAFE_CONNTYPE = Qt::QueuedConnection; - value(bundle b, const QString& name, t def) noexcept : value_(b, name), def(std::move(def)) + value(bundle b, const QString& name, t def) noexcept : value_(b, name), def(std::move(def)), cached_value{get()} { } -- cgit v1.2.3