diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2015-09-21 23:58:29 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2015-09-21 23:58:29 +0200 |
| commit | 8123900781037703d54730a6032b09745c69007f (patch) | |
| tree | c72a0e446cb075ade41dd199a3d6822fa153e139 /daemon/jskitobjects.cpp | |
| parent | e2c1fb71995cd799e83b7644140750454a3b5900 (diff) | |
Support JS timers
Add support for set and clear Intervals and Timeouts for Javascript
apps. The QT JS engine doesn't support them natively.
Diffstat (limited to 'daemon/jskitobjects.cpp')
| -rw-r--r-- | daemon/jskitobjects.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/daemon/jskitobjects.cpp b/daemon/jskitobjects.cpp index 01a7e20..d9ef02f 100644 --- a/daemon/jskitobjects.cpp +++ b/daemon/jskitobjects.cpp @@ -4,6 +4,7 @@ #include <QAuthenticator> #include <QBuffer> #include <QDir> +#include <QTimerEvent> #include <QCryptographicHash> #include <limits> #include "jskitobjects.h" @@ -38,6 +39,69 @@ void JSKitPebble::removeEventListener(const QString &type, QJSValue function) } } +int JSKitPebble::setInterval(QJSValue expression, int delay) +{ + qCDebug(l) << "Setting interval for " << delay << "ms: " << expression.toString(); + if (expression.isString() || expression.isCallable()) { + int timerId = startTimer(delay); + _intervals.insert(timerId, expression); + qCDebug(l) << "Timer id: " << timerId; + return timerId; + } + return -1; +} + +void JSKitPebble::clearInterval(int timerId) +{ + qCDebug(l) << "Killing interval " << timerId ; + killTimer(timerId); + _intervals.remove(timerId); +} + +int JSKitPebble::setTimeout(QJSValue expression, int delay) +{ + qCDebug(l) << "Setting timeout for " << delay << "ms: " << expression.toString(); + if (expression.isString() || expression.isCallable()) { + int timerId = startTimer(delay); + _timeouts.insert(timerId, expression); + return timerId; + } + return -1; +} + +void JSKitPebble::clearTimeout(int timerId) +{ + qCDebug(l) << "Killing timeout " << timerId ; + killTimer(timerId); + _timeouts.remove(timerId); +} + +void JSKitPebble::timerEvent(QTimerEvent *event) +{ + int id = event->timerId(); + QJSValue expression; // find in either intervals or timeouts + if (_intervals.contains(id)) + expression = _intervals.value(id); + else if (_timeouts.contains(id)) { + expression = _timeouts.value(id); + killTimer(id); // timeouts don't repeat + } + else { + qCWarning(l) << "Unknown timer event"; + killTimer(id); // interval nor timeout exist. kill the timer + return; + } + + if (expression.isCallable()) { // call it if it's a function + QJSValue result = expression.call().toString(); + qCDebug(l) << "Timer function result: " << result.toString(); + } + else { // otherwise evaluate it + QJSValue result = _mgr->engine()->evaluate(expression.toString()); + qCDebug(l) << "Timer expression result: " << result.toString(); + } +} + uint JSKitPebble::sendAppMessage(QJSValue message, QJSValue callbackForAck, QJSValue callbackForNack) { QVariantMap data = message.toVariant().toMap(); |
