Merge lp:~abranson/rockwork/jsfixes into lp:rockwork

Proposed by Andrew Branson
Status: Merged
Merged at revision: 119
Proposed branch: lp:~abranson/rockwork/jsfixes
Merge into: lp:rockwork
Diff against target: 178 lines (+62/-3)
6 files modified
rockworkd/libpebble/jskit/jskitmanager.cpp (+4/-1)
rockworkd/libpebble/jskit/jskitsetup.js (+1/-2)
rockworkd/libpebble/jskit/jskitxmlhttprequest.cpp (+42/-0)
rockworkd/libpebble/jskit/jskitxmlhttprequest.h (+6/-0)
rockworkd/libpebble/pebble.cpp (+8/-0)
rockworkd/libpebble/pebble.h (+1/-0)
To merge this branch: bzr merge lp:~abranson/rockwork/jsfixes
Reviewer Review Type Date Requested Status
Brian Douglass (community) Approve
Michael Zanetti Pending
Review via email: mp+290255@code.launchpad.net

Description of the change

Some JS fixes, including a fix for a RockWork issue. If you could check the XMLHttpRequest state flags to confirm the behaviour is the same on Ubuntu as Sailfish, that would be great. It's not behaving as I'd expect.

To post a comment you must log in.
Revision history for this message
Brian Douglass (bhdouglass) wrote :

Should there be an event for timeout as well?

lp:~abranson/rockwork/jsfixes updated
120. By abranson

Make JXHR invokeCallbacks method private

121. By abranson

XMLHttpRequest listeners get 'this' set properly, and the status constants have been re-added.

Revision history for this message
Brian Douglass (bhdouglass) wrote :

I tested this out a little and it seems good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'rockworkd/libpebble/jskit/jskitmanager.cpp'
2--- rockworkd/libpebble/jskit/jskitmanager.cpp 2016-02-23 05:28:51 +0000
3+++ rockworkd/libpebble/jskit/jskitmanager.cpp 2016-04-01 21:35:57 +0000
4@@ -262,8 +262,11 @@
5 return true;
6 });
7
8+ QJSValue eventObj = m_engine->newObject();
9+ eventObj.setProperty("ready",m_engine->toScriptValue(true));
10+ eventObj.setProperty("type","ready");
11 // We try to invoke the callbacks even if script parsing resulted in error...
12- m_jspebble->invokeCallbacks("ready");
13+ m_jspebble->invokeCallbacks("ready",QJSValueList({eventObj}));
14
15 loadJsFile(":/cacheLocalStorage.js");
16
17
18=== modified file 'rockworkd/libpebble/jskit/jskitsetup.js'
19--- rockworkd/libpebble/jskit/jskitsetup.js 2016-02-14 01:42:48 +0000
20+++ rockworkd/libpebble/jskit/jskitsetup.js 2016-04-01 21:35:57 +0000
21@@ -45,7 +45,6 @@
22 ['readyState', 'response', 'responseText', 'responseType', 'status',
23 'statusText', 'timeout', 'onreadystatechange', 'ontimeout', 'onload',
24 'onloadstart', 'onloadend', 'onprogress', 'onerror', 'onabort']);
25-
26 this.UNSENT = 0;
27 this.OPENED = 1;
28 this.HEADERS_RECEIVED = 2;
29@@ -277,4 +276,4 @@
30 MessageEvent._init = function(origin, data) {
31 //Convenience function to call from the engine
32 return new MessageEvent(origin, data)
33-};
34\ No newline at end of file
35+};
36
37=== modified file 'rockworkd/libpebble/jskit/jskitxmlhttprequest.cpp'
38--- rockworkd/libpebble/jskit/jskitxmlhttprequest.cpp 2016-02-09 05:28:21 +0000
39+++ rockworkd/libpebble/jskit/jskitxmlhttprequest.cpp 2016-04-01 21:35:57 +0000
40@@ -208,6 +208,45 @@
41 m_responseType = type;
42 }
43
44+void JSKitXMLHttpRequest::addEventListener(const QString &type, QJSValue function)
45+{
46+ m_listeners[type].append(function);
47+}
48+
49+void JSKitXMLHttpRequest::removeEventListener(const QString &type, QJSValue function)
50+{
51+ if (!m_listeners.contains(type)) return;
52+
53+ QList<QJSValue> &callbacks = m_listeners[type];
54+ for (QList<QJSValue>::iterator it = callbacks.begin(); it != callbacks.end(); ) {
55+ if (it->strictlyEquals(function)) {
56+ it = callbacks.erase(it);
57+ } else {
58+ ++it;
59+ }
60+ }
61+
62+ if (callbacks.empty()) {
63+ m_listeners.remove(type);
64+ }
65+}
66+
67+void JSKitXMLHttpRequest::invokeCallbacks(const QString &type, const QJSValueList &args)
68+{
69+ if (!m_listeners.contains(type)) return;
70+ QList<QJSValue> &callbacks = m_listeners[type];
71+
72+ for (QList<QJSValue>::iterator it = callbacks.begin(); it != callbacks.end(); ++it) {
73+ qCDebug(l) << "invoking callback" << type << it->toString();
74+ QJSValue result = it->callWithInstance(m_engine->newQObject(this), args);
75+ if (result.isError()) {
76+ qCWarning(l) << "error while invoking callback"
77+ << type << it->toString() << ":"
78+ << JSKitManager::describeError(result);
79+ }
80+ }
81+}
82+
83 QJSValue JSKitXMLHttpRequest::response() const
84 {
85 if (m_responseType.isEmpty() || m_responseType == "text") {
86@@ -269,6 +308,7 @@
87 } else {
88 qCDebug(l) << "No onload set";
89 }
90+ invokeCallbacks("load", QJSValueList({m_engine->newQObject(this)}));
91
92 if (m_onreadystatechange.isCallable()) {
93 qCDebug(l) << "going to call onreadystatechange handler:" << m_onreadystatechange.toString();
94@@ -277,6 +317,7 @@
95 qCWarning(l) << "JS error on onreadystatechange handler:" << JSKitManager::describeError(result);
96 }
97 }
98+ invokeCallbacks("readystatechange", QJSValueList({m_engine->newQObject(this)}));
99 }
100
101 void JSKitXMLHttpRequest::handleReplyError(QNetworkReply::NetworkError code)
102@@ -299,6 +340,7 @@
103 qCWarning(l) << "JS error on onerror handler:" << JSKitManager::describeError(result);
104 }
105 }
106+ invokeCallbacks("error", QJSValueList({m_engine->newQObject(this)}));
107 }
108
109 void JSKitXMLHttpRequest::handleAuthenticationRequired(QNetworkReply *reply, QAuthenticator *auth)
110
111=== modified file 'rockworkd/libpebble/jskit/jskitxmlhttprequest.h'
112--- rockworkd/libpebble/jskit/jskitxmlhttprequest.h 2016-02-09 05:28:21 +0000
113+++ rockworkd/libpebble/jskit/jskitxmlhttprequest.h 2016-04-01 21:35:57 +0000
114@@ -40,6 +40,8 @@
115 Q_INVOKABLE void send(const QJSValue &data = QJSValue(QJSValue::NullValue));
116 Q_INVOKABLE void abort();
117
118+ Q_INVOKABLE void addEventListener(const QString &type, QJSValue function);
119+ Q_INVOKABLE void removeEventListener(const QString &type, QJSValue function);
120 QJSValue onload() const;
121 void setOnload(const QJSValue &value);
122 QJSValue onreadystatechange() const;
123@@ -76,6 +78,9 @@
124 void handleAuthenticationRequired(QNetworkReply *reply, QAuthenticator *auth);
125
126 private:
127+ void invokeCallbacks(const QString &type, const QJSValueList &args = QJSValueList());
128+
129+private:
130 QJSEngine *m_engine;
131 QNetworkAccessManager *m_net;
132 QString m_verb;
133@@ -87,6 +92,7 @@
134 QNetworkReply *m_reply;
135 QString m_responseType;
136 QByteArray m_response;
137+ QHash<QString, QList<QJSValue>> m_listeners;
138 QJSValue m_onload;
139 QJSValue m_onreadystatechange;
140 QJSValue m_ontimeout;
141
142=== modified file 'rockworkd/libpebble/pebble.cpp'
143--- rockworkd/libpebble/pebble.cpp 2016-03-14 21:13:19 +0000
144+++ rockworkd/libpebble/pebble.cpp 2016-04-01 21:35:57 +0000
145@@ -61,6 +61,7 @@
146 m_appMsgManager = new AppMsgManager(this, m_appManager, m_connection);
147 m_jskitManager = new JSKitManager(this, m_connection, m_appManager, m_appMsgManager, this);
148 QObject::connect(m_jskitManager, &JSKitManager::openURL, this, &Pebble::openURL);
149+ QObject::connect(m_jskitManager, &JSKitManager::appNotification, this, &Pebble::sendSimpleNotification);
150 QObject::connect(m_appMsgManager, &AppMsgManager::appStarted, this, &Pebble::appStarted);
151
152 m_blobDB = new BlobDB(this, m_connection);
153@@ -300,6 +301,13 @@
154 }
155 }
156
157+void Pebble::sendSimpleNotification(const QUuid &uuid, const QString &title, const QString &body) {
158+ Notification notif = Notification(appInfo(uuid).shortName());
159+ notif.setSubject(title);
160+ notif.setBody(body);
161+ sendNotification(notif);
162+}
163+
164 void Pebble::sendNotification(const Notification &notification)
165 {
166 if (!notificationsFilter().value(notification.sourceId(), true)) {
167
168=== modified file 'rockworkd/libpebble/pebble.h'
169--- rockworkd/libpebble/pebble.h 2016-02-14 22:10:12 +0000
170+++ rockworkd/libpebble/pebble.h 2016-04-01 21:35:57 +0000
171@@ -70,6 +70,7 @@
172 public slots:
173 QHash<QString, bool> notificationsFilter() const;
174 void setNotificationFilter(const QString &sourceId, bool enabled);
175+ void sendSimpleNotification(const QUuid &uuid, const QString &title, const QString &body);
176 void sendNotification(const Notification &notification);
177
178 void clearTimeline();

Subscribers

People subscribed via source and target branches