Skip to content

Commit

Permalink
Connect WTimer's timeout() signal first in constructor
Browse files Browse the repository at this point in the history
This is so that you can add a slot that restarts a singleshot timer.
Otherwise, you have to make sure you connect the slot after calling
start().

This code wouldn't work properly:

timer->setSingleShot(true);
timer->timeout().connect([timer]{
  timer->start();
});
timer->start();

Because this would cause: start(), start(), stop()

This:

timer->setSingleShot(true);
timer->start();
timer->timeout().connect([timer]{
  timer->start();
});

would correctly cause: start(), stop(), start()
  • Loading branch information
RockinRoel committed Jul 23, 2020
1 parent 6f92c4e commit 1141a15
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/Wt/WTimer.C
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace Wt {

WTimer::WTimer()
: uTimerWidget_(new WTimerWidget(this)),
singleShot_(false),
interval_(0),
singleShot_(false),
active_(false),
timeoutConnected_(false),
timeout_(new Time())
{
timerWidget_ = uTimerWidget_.get();
timeout().connect(this, &WTimer::gotTimeout);
}

EventSignal<WMouseEvent>& WTimer::timeout()
Expand Down Expand Up @@ -63,11 +63,6 @@ void WTimer::start()
!timeout().isExposedSignal());

timerWidget_->timerStart(jsRepeat);

if (!timeoutConnected_) {
timeout().connect(this, &WTimer::gotTimeout);
timeoutConnected_ = true;
}
}

void WTimer::stop()
Expand Down
3 changes: 1 addition & 2 deletions src/Wt/WTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,9 @@ class WT_API WTimer : public WObject
observing_ptr<WTimerWidget> timerWidget_;
std::unique_ptr<WTimerWidget> uTimerWidget_;

bool singleShot_;
std::chrono::milliseconds interval_;
bool singleShot_;
bool active_;
bool timeoutConnected_;

std::unique_ptr<Time> timeout_;

Expand Down

0 comments on commit 1141a15

Please sign in to comment.