diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-02-05 16:03:20 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-03-21 15:03:34 +0100 |
commit | e3e4d6a18d63537459f0e616360e53e816927f76 (patch) | |
tree | c52e2d7d402dd1cea731065a673301821e5a72d7 /src/webchannel/qwebchannel.js | |
parent | 3a85e592c050e73e61dc54d22e133bcf08d2f3c5 (diff) | |
download | qtwebchannel-e3e4d6a18d63537459f0e616360e53e816927f76.tar.gz |
Use an enum for message types instead of strings.
This further reduces the network traffic and thus leads to a small
performance boost.
Personally, I also think this code is a bit nicer to read and grasp.
Change-Id: I943c621142e9982f0e52d24e3a0976428856541b
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
Diffstat (limited to 'src/webchannel/qwebchannel.js')
-rw-r--r-- | src/webchannel/qwebchannel.js | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/src/webchannel/qwebchannel.js b/src/webchannel/qwebchannel.js index 9aa174b..33f4cf5 100644 --- a/src/webchannel/qwebchannel.js +++ b/src/webchannel/qwebchannel.js @@ -42,6 +42,18 @@ "use strict"; +var QWebChannelMessageTypes = { + signal: 1, + propertyUpdate: 2, + init: 3, + idle: 4, + debug: 5, + invokeMethod: 6, + connectToSignal: 7, + disconnectFromSignal: 8, + setProperty: 9, +}; + var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) { var channel = this; @@ -152,7 +164,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) var initialized = false; channel.subscribe( - "Qt.signal", + QWebChannelMessageTypes.signal, function(payload) { var object = window[payload.object] || channel.objectMap[payload.object]; if (object) { @@ -164,7 +176,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) ); channel.subscribe( - "Qt.propertyUpdate", + QWebChannelMessageTypes.propertyUpdate, function(payload) { for (var i in payload) { var data = payload[i]; @@ -175,12 +187,12 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) console.warn("Unhandled property update: " + data.object + "::" + data.signal); } } - setTimeout(function() { channel.exec({type: "Qt.idle"}); }, 0); + setTimeout(function() { channel.exec({type: QWebChannelMessageTypes.idle}); }, 0); } ); channel.subscribe( - "Qt.init", + QWebChannelMessageTypes.init, function(payload) { if (initialized) { return; @@ -194,16 +206,16 @@ var QWebChannel = function(baseUrlOrSocket, initCallback, rawChannel) if (doneCallback) { doneCallback(channel); } - setTimeout(function() { channel.exec({type: "Qt.idle"}); }, 0); + setTimeout(function() { channel.exec({type: QWebChannelMessageTypes.idle}); }, 0); } ); channel.debug = function(message) { - channel.send({"data" : {"type" : "Qt.Debug", "message" : message}}); + channel.send({"data" : {"type" : QWebChannelMessageTypes.debug, "message" : message}}); }; - channel.exec({type:"Qt.init"}); + channel.exec({type: QWebChannelMessageTypes.init}); } }; @@ -265,7 +277,7 @@ function QObject(name, data, webChannel) if (!isPropertyNotifySignal) { // only required for "pure" signals, handled separately for properties in propertyUpdate webChannel.exec({ - type: "Qt.connectToSignal", + type: QWebChannelMessageTypes.connectToSignal, object: object.__id__, signal: signalIndex }); @@ -286,7 +298,7 @@ function QObject(name, data, webChannel) if (!isPropertyNotifySignal && object.__objectSignals__[signalIndex].length === 0) { // only required for "pure" signals, handled separately for properties in propertyUpdate webChannel.exec({ - type: "Qt.disconnectFromSignal", + type: QWebChannelMessageTypes.disconnectFromSignal, object: object.__id__, signal: signalIndex }); @@ -342,7 +354,12 @@ function QObject(name, data, webChannel) args.push(arguments[i]); } - webChannel.exec({"type": "Qt.invokeMethod", "object": object.__id__, "method": methodIdx, "args": args}, function(response) { + webChannel.exec({ + "type": QWebChannelMessageTypes.invokeMethod, + "object": object.__id__, + "method": methodIdx, + "args": args + }, function(response) { if ( (response !== undefined) && callback ) { (callback)(unwrapQObject(response)); } @@ -372,7 +389,12 @@ function QObject(name, data, webChannel) return; } object.__propertyCache__[propertyIndex] = value; - webChannel.exec({"type": "Qt.setProperty", "object": object.__id__, "property": propertyIndex, "value": value }); + webChannel.exec({ + "type": QWebChannelMessageTypes.setProperty, + "object": object.__id__, + "property": propertyIndex, + "value": value + }); }); object.__defineGetter__(propertyName, function () { |