summaryrefslogtreecommitdiff
path: root/daemon/jskitobjects.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'daemon/jskitobjects.cpp')
-rw-r--r--daemon/jskitobjects.cpp64
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();