diff --git a/src/impl/background_collection.cpp b/src/impl/background_collection.cpp index 62e7fa42..7e623ab6 100644 --- a/src/impl/background_collection.cpp +++ b/src/impl/background_collection.cpp @@ -208,15 +208,8 @@ bool BackgroundCollection::deliver(SharedGroup& sg, std::exception_ptr err) void BackgroundCollection::call_callbacks() { - while (auto cb = next_callback()) { - auto fn = cb->fn; - if (!cb->initial_delivered && should_deliver_initial()) { - cb->initial_delivered = true; - fn({}, m_error); // note: may invalidate `cb` - } - else { - fn(m_changes_to_deliver, m_error); - } + while (auto fn = next_callback()) { + fn(m_changes_to_deliver, m_error); } if (m_error) { @@ -227,17 +220,17 @@ void BackgroundCollection::call_callbacks() } } -BackgroundCollection::Callback* BackgroundCollection::next_callback() +CollectionChangeCallback BackgroundCollection::next_callback() { std::lock_guard callback_lock(m_callback_mutex); for (++m_callback_index; m_callback_index < m_callbacks.size(); ++m_callback_index) { auto& callback = m_callbacks[m_callback_index]; - bool deliver_initial = !callback.initial_delivered && should_deliver_initial(); - if (!m_error && !deliver_initial && m_changes_to_deliver.empty()) { + if (!m_error && callback.initial_delivered && m_changes_to_deliver.empty()) { continue; } - return &callback; + callback.initial_delivered = true; + return callback.fn; } m_callback_index = npos; diff --git a/src/impl/background_collection.hpp b/src/impl/background_collection.hpp index d2058a21..425c7b2e 100644 --- a/src/impl/background_collection.hpp +++ b/src/impl/background_collection.hpp @@ -119,7 +119,6 @@ private: virtual void do_prepare_handover(SharedGroup&) = 0; virtual bool do_deliver(SharedGroup&) { return true; } virtual bool do_add_required_change_info(TransactionChangeInfo&) { return true; } - virtual bool should_deliver_initial() const noexcept { return false; } const std::thread::id m_thread_id = std::this_thread::get_id(); bool is_for_current_thread() const { return m_thread_id == std::this_thread::get_id(); } @@ -158,7 +157,7 @@ private: // remove_callback() updates this when needed size_t m_callback_index = npos; - Callback* next_callback(); + CollectionChangeCallback next_callback(); }; } // namespace _impl diff --git a/src/impl/list_notifier.cpp b/src/impl/list_notifier.cpp index aba0244a..a0143552 100644 --- a/src/impl/list_notifier.cpp +++ b/src/impl/list_notifier.cpp @@ -86,7 +86,7 @@ bool ListNotifier::do_add_required_change_info(TransactionChangeInfo& info) void ListNotifier::run() { - if (!m_lv) { + if (!m_lv || !m_lv->is_attached()) { // LV was deleted, so report all of the rows being removed if this is // the first run after that if (m_prev_size) { diff --git a/src/impl/results_notifier.hpp b/src/impl/results_notifier.hpp index bb4bd024..81f659c4 100644 --- a/src/impl/results_notifier.hpp +++ b/src/impl/results_notifier.hpp @@ -75,8 +75,6 @@ private: void release_data() noexcept override; void do_attach_to(SharedGroup& sg) override; void do_detach_from(SharedGroup& sg) override; - - bool should_deliver_initial() const noexcept override { return true; } }; } // namespace _impl diff --git a/tests/list.cpp b/tests/list.cpp index 5b38a4d4..7db9a953 100644 --- a/tests/list.cpp +++ b/tests/list.cpp @@ -66,15 +66,21 @@ TEST_CASE("list") { }; auto require_change = [&] { - return lst.add_notification_callback([&](CollectionChangeIndices c, std::exception_ptr err) { + auto token = lst.add_notification_callback([&](CollectionChangeIndices c, std::exception_ptr err) { change = c; }); + advance_and_notify(*r); + return token; }; auto require_no_change = [&] { - return lst.add_notification_callback([&](CollectionChangeIndices c, std::exception_ptr err) { - REQUIRE(false); + bool first = true; + auto token = lst.add_notification_callback([&, first](CollectionChangeIndices c, std::exception_ptr err) mutable { + REQUIRE(first); + first = false; }); + advance_and_notify(*r); + return token; }; SECTION("modifying the list sends a change notifications") { diff --git a/tests/results.cpp b/tests/results.cpp index db809758..e0216c59 100644 --- a/tests/results.cpp +++ b/tests/results.cpp @@ -276,9 +276,9 @@ TEST_CASE("Results") { REQUIRE(notification_calls == 1); } - SECTION("the first call of a notification always passes an empty change even if it previously ran for a different callback") { + SECTION("the first call of a notification can include changes if it previously ran for a different callback") { auto token2 = results.add_notification_callback([&](CollectionChangeIndices c, std::exception_ptr) { - REQUIRE(c.empty()); + REQUIRE(!c.empty()); }); write([&] {