diff --git a/src/impl/background_collection.cpp b/src/impl/background_collection.cpp index b1d4dec9..62e7fa42 100644 --- a/src/impl/background_collection.cpp +++ b/src/impl/background_collection.cpp @@ -208,8 +208,15 @@ bool BackgroundCollection::deliver(SharedGroup& sg, std::exception_ptr err) void BackgroundCollection::call_callbacks() { - while (auto fn = next_callback()) { - fn(m_changes_to_deliver, m_error); + 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); + } } if (m_error) { @@ -220,7 +227,7 @@ void BackgroundCollection::call_callbacks() } } -CollectionChangeCallback BackgroundCollection::next_callback() +BackgroundCollection::Callback* BackgroundCollection::next_callback() { std::lock_guard callback_lock(m_callback_mutex); @@ -230,9 +237,7 @@ CollectionChangeCallback BackgroundCollection::next_callback() if (!m_error && !deliver_initial && m_changes_to_deliver.empty()) { continue; } - - callback.initial_delivered = true; - return callback.fn; + return &callback; } m_callback_index = npos; diff --git a/src/impl/background_collection.hpp b/src/impl/background_collection.hpp index 162e0047..d2058a21 100644 --- a/src/impl/background_collection.hpp +++ b/src/impl/background_collection.hpp @@ -158,7 +158,7 @@ private: // remove_callback() updates this when needed size_t m_callback_index = npos; - CollectionChangeCallback next_callback(); + Callback* next_callback(); }; } // namespace _impl diff --git a/tests/results.cpp b/tests/results.cpp index ccf99890..397eb4a8 100644 --- a/tests/results.cpp +++ b/tests/results.cpp @@ -283,6 +283,16 @@ TEST_CASE("Results") { r->notify(); 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") { + auto token2 = results.add_notification_callback([&](CollectionChangeIndices c, std::exception_ptr) { + REQUIRE(c.empty()); + }); + + write([&] { + table->set_int(0, table->add_empty_row(), 5); + }); + } } // Sort in descending order