Skip to content

Commit

Permalink
test and documentation update
Browse files Browse the repository at this point in the history
  • Loading branch information
phyBrackets committed Sep 25, 2023
1 parent 286dc67 commit 22b384b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/kdbindings/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ class ScopedConnectionHandle
*
* The Args type parameter pack describe which value types the Signal will emit.
*
* Deferred Connection:
*
* KDBindings::Signal supports deferred connections, enabling the decoupling of signal
* emission from the execution of connected slots. With deferred connections, you can
* connect slots to the Signal that are not immediately executed when the signal is emitted.
* Instead, you can evaluate these deferred connections at a later time, allowing for
* asynchronous or delayed execution of connected slots.
*
* Examples:
* - @ref 01-simple-connection/main.cpp
* - @ref 02-signal-member/main.cpp
Expand Down Expand Up @@ -309,7 +317,7 @@ class Signal
{
auto weakEvaluator = std::weak_ptr<ConnectionEvaluator>(evaluator);

auto connection = [weakEvaluator, slot](Args... args) {
auto connection = [weakEvaluator = std::move(weakEvaluator), slot](Args... args) {
// Check if the ConnectionEvaluator is still alive
if (auto evaluatorPtr = weakEvaluator.lock()) {
auto lambda = [slot, args...]() {
Expand Down Expand Up @@ -444,6 +452,10 @@ class Signal
*
* @return An instance of ConnectionHandle, that can be used to disconnect
* or temporarily block the connection.
*
* @note
* The `KDBindings::Signal` class itself is not thread-safe. While the `ConnectionEvaluator` is inherently
* thread-safe, ensure that any concurrent access to this Signal is protected externally to maintain thread safety.
*/
ConnectionHandle connectDeferred(const std::shared_ptr<ConnectionEvaluator> &evaluator, std::function<void(Args...)> const &slot)
{
Expand Down
10 changes: 10 additions & 0 deletions tests/signal/tst_signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ TEST_CASE("Signal connections")
REQUIRE(connection.isActive());

signal.emit(4);
REQUIRE(val == 4); // val not changing immediately after emit

connection.disconnect();
REQUIRE(!connection.isActive());

signal.emit(6); // It will not affect the result as the signal is disconnected
REQUIRE(val == 4);

evaluator->evaluateDeferredConnections();
REQUIRE(val == 8);
Expand Down Expand Up @@ -126,6 +128,7 @@ TEST_CASE("Signal connections")

signal1.emit(2);
signal2.emit(3);
REQUIRE(val == 4); // val not changing immediately after emit

evaluator->evaluateDeferredConnections();

Expand Down Expand Up @@ -158,6 +161,9 @@ TEST_CASE("Signal connections")

thread1.join();
thread2.join();

REQUIRE(val1 == 4);
REQUIRE(val2 == 4);

evaluator->evaluateDeferredConnections();

Expand All @@ -178,6 +184,8 @@ TEST_CASE("Signal connections")
REQUIRE(connection.isActive());

signal.emit(2);
REQUIRE(val == 4);

connection.disconnect();
evaluator->evaluateDeferredConnections();

Expand All @@ -195,6 +203,8 @@ TEST_CASE("Signal connections")
});

signal.emit(2);
REQUIRE(val == 4);

evaluator->evaluateDeferredConnections();
evaluator->evaluateDeferredConnections();

Expand Down

0 comments on commit 22b384b

Please sign in to comment.