Skip to content

Commit

Permalink
Add Q.clearTimeout() : Prevent the function set with the setTimeout()…
Browse files Browse the repository at this point in the history
… to execute:
  • Loading branch information
benlau committed Feb 18, 2017
1 parent 5474802 commit f0cf169
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 12 deletions.
6 changes: 5 additions & 1 deletion QuickPromise/Q.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ QtObject {
id : component

function setTimeout(callback,interval) {
QPTimer.setTimeout(callback,interval);
return QPTimer.setTimeout(callback,interval);
}

function clearTimeout(id) {
QPTimer.clearTimeout(id);
}

function promise() {
Expand Down
39 changes: 30 additions & 9 deletions qptimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,43 @@ QPTimer::~QPTimer()

}

void QPTimer::setTimeout(QJSValue func, int interval)
int QPTimer::setTimeout(QJSValue func, int interval)
{
// It can't use the Timer from Quick Component to implement the function.
// Because setTimeout(0) could not be executed in next tick with < Qt 5.4

QTimer *timer = new QTimer(this);
int id = startTimer(interval);

connect(timer,SIGNAL(timeout()),
this,SLOT(onTriggered()),Qt::QueuedConnection);
callbacks[id] = func;

QVariant v = QVariant::fromValue<QJSValue>(func);
return id;
}

void QPTimer::clearTimeout(int id)
{
if (callbacks.contains(id)) {
killTimer(id);
callbacks.remove(id);
}
}

void QPTimer::timerEvent(QTimerEvent *event)
{
int id = event->timerId();

QJSValue func = callbacks[id];

callbacks.remove(id);

QJSValue res = func.call();

if (res.isError()) {
qDebug() << "Q.setTimeout() - Uncaught exception at line"
<< res.property("lineNumber").toInt()
<< ":" << res.toString();
}

timer->setProperty("__quick_promise_func___",v);
timer->setInterval(interval);
timer->setSingleShot(true);
timer->start();
killTimer(id);
}

void QPTimer::onTriggered()
Expand Down
11 changes: 9 additions & 2 deletions qptimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QObject>
#include <QJSValue>
#include <QMap>

/// QuickPromise's timer utility

Expand All @@ -14,12 +15,18 @@ class QPTimer : public QObject
~QPTimer();

/// Implement the setTimeout function by C++.
Q_INVOKABLE void setTimeout(QJSValue func,int interval);
Q_INVOKABLE int setTimeout(QJSValue func,int interval);

private:
Q_INVOKABLE void clearTimeout(int id);

protected:
void timerEvent(QTimerEvent *event);

private:
Q_INVOKABLE void onTriggered();

QMap<int, QJSValue> callbacks;

};

#endif // QPTIMER_H
1 change: 1 addition & 0 deletions tests/unittests/TestSuite.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ TestCase {
wait(0);
wait(0);
wait(0);
wait(0);
}


Expand Down
4 changes: 4 additions & 0 deletions tests/unittests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <QtQuickTest/quicktest.h>
#include <QtCore>

namespace AutoTestRegister {
QUICK_TEST_MAIN(QuickTests)
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Expand Down
33 changes: 33 additions & 0 deletions tests/unittests/tst_timer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,37 @@ TestSuite {
wait(10);
}

function test_setTimeout() {
var count1 = 0, count2 = 0;
var id1 = Q.setTimeout(function(){
count1++
}, 0);
var id2 = Q.setTimeout(function(){
count2++;
}, 0);
compare(count1, 0);
compare(count2, 0);
compare(parseInt(id1), id1);
compare(parseInt(id2), id2);
compare(id1 !== id2, true);
wait(0);
compare(count1, 1);
compare(count2, 1);
wait(100);
compare(count1, 1);
compare(count2, 1);
}

function test_clearTimeout() {
var count = 0;
var id = Q.setTimeout(function(){
count++
}, 0);
compare(count, 0);
compare(id !== 0, true);
Q.clearTimeout(id);
wait(100);
compare(count, 0);
}

}
2 changes: 2 additions & 0 deletions tests/unittests/unittests.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ SOURCES += main.cpp
include(../../quickpromise.pri)

DEFINES += SRCDIR=\\\"$$PWD/\\\" BASEDIR=\\\"$$PWD/..\\\"
DEFINES += QUICK_TEST_SOURCE_DIR=\\\"$$PWD\\\"


DISTFILES += \
tst_promise_resolve_signal.qml \
Expand Down

0 comments on commit f0cf169

Please sign in to comment.