summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Webster <awebster@arcx.com>2018-09-14 11:18:11 -0400
committerMilian Wolff <milian.wolff@kdab.com>2019-04-10 08:54:00 +0000
commitb4342f3567ec08a959f132bc1ef0c4f8eb89bcf8 (patch)
tree0c18d5996f8a3dd945ad6e064b04d9d614195e1b
parentd7ece941aebdac1be820fdfe74c273fb963dfcad (diff)
downloadqtwebchannel-b4342f3567ec08a959f132bc1ef0c4f8eb89bcf8.tar.gz
Return a Promise when no callback is provided for a function call
This returns a Promise if Promise is supported and no callback is provided. The Promise resolves when the function call succeeds, or rejects when it fails. Change-Id: I529f5c2c0ff8997820f3d1b4d4b364cd8521e9b5 Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
-rw-r--r--examples/webchannel/shared/qwebchannel.js16
-rw-r--r--tests/auto/qml/tst_webchannel.qml11
2 files changed, 26 insertions, 1 deletions
diff --git a/examples/webchannel/shared/qwebchannel.js b/examples/webchannel/shared/qwebchannel.js
index 6cc3690..fca45d9 100644
--- a/examples/webchannel/shared/qwebchannel.js
+++ b/examples/webchannel/shared/qwebchannel.js
@@ -346,6 +346,7 @@ function QObject(name, data, webChannel)
object[methodName] = function() {
var args = [];
var callback;
+ var errCallback;
for (var i = 0; i < arguments.length; ++i) {
var argument = arguments[i];
if (typeof argument === "function")
@@ -358,6 +359,17 @@ function QObject(name, data, webChannel)
args.push(argument);
}
+ var result;
+ // during test, webChannel.exec synchronously calls the callback
+ // therefore, the promise must be constucted before calling
+ // webChannel.exec to ensure the callback is set up
+ if (!callback && (typeof(Promise) === 'function')) {
+ result = new Promise(function(resolve, reject) {
+ callback = resolve;
+ errCallback = reject;
+ });
+ }
+
webChannel.exec({
"type": QWebChannelMessageTypes.invokeMethod,
"object": object.__id__,
@@ -369,8 +381,12 @@ function QObject(name, data, webChannel)
if (callback) {
(callback)(result);
}
+ } else if (errCallback) {
+ (errCallback)();
}
});
+
+ return result;
};
}
diff --git a/tests/auto/qml/tst_webchannel.qml b/tests/auto/qml/tst_webchannel.qml
index b0f3263..ed1c4a1 100644
--- a/tests/auto/qml/tst_webchannel.qml
+++ b/tests/auto/qml/tst_webchannel.qml
@@ -87,6 +87,7 @@ TestCase {
property var myProperty : 0
function myMethod(arg) {
lastMethodArg = arg;
+ return myProperty;
}
signal mySignal(var arg1, var arg2)
}
@@ -238,6 +239,7 @@ TestCase {
var testObjBeforeDeletion;
var testObjAfterDeletion;
var testObjId;
+ var testReturn;
var channel = client.createChannel(function(channel) {
channel.objects.myFactory.create("testObj", function(obj) {
testObjId = obj.__id__;
@@ -249,7 +251,9 @@ TestCase {
testObjAfterDeletion = obj;
});
obj.myProperty = 42;
- obj.myMethod("foobar");
+ obj.myMethod("foobar").then(function(result) {
+ testReturn = result;
+ });
});
});
client.awaitInit();
@@ -288,6 +292,11 @@ TestCase {
// the server should eventually notify the client about the property update
client.awaitPropertyUpdate();
+ // check that the Promise from myMethod was resolved
+ // must happen after waiting for something so the Promise callback
+ // can execute
+ compare(testReturn, 42);
+
client.awaitIdle();
// property should be wrapped