aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorØystein Heskestad <oystein.heskestad@qt.io>2022-10-20 17:40:58 +0200
committerØystein Heskestad <oystein.heskestad@qt.io>2022-11-11 13:54:39 +0100
commit88d2e64cb1436732ce389cbc190bf89312879eef (patch)
treeac51a61c599316199a5f5cb99c9169576073339f /examples
parentb3e747360f5d341d34c71fce6b68708f699c9907 (diff)
Add wrappers to optionally convert incoming data
Wrappers can be added as a third argument to WebChannel constructors or by explicitly calling addConverter. They can be added by supplying their name or by supplying a function. If the function is not applicable it must return undefined to proceed to to the next wrapper. Data is transmitted as JSON when sent over the WebChannel. Because JSON does not have a date, type QDateTime objects are sent as ISO 8601 text strings. The "Date" converter converts such strings to ECMAScript Date objects. Fixes: QTBUG-98490 Change-Id: I83bb7476b50838359db5b981f500871142fa41f0 Reviewed-by: Arno Rehn <a.rehn@menlosystems.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/webchannel/shared/qwebchannel.js45
1 files changed, 44 insertions, 1 deletions
diff --git a/examples/webchannel/shared/qwebchannel.js b/examples/webchannel/shared/qwebchannel.js
index 948c836..56fa66a 100644
--- a/examples/webchannel/shared/qwebchannel.js
+++ b/examples/webchannel/shared/qwebchannel.js
@@ -17,7 +17,7 @@ var QWebChannelMessageTypes = {
response: 10,
};
-var QWebChannel = function(transport, initCallback)
+var QWebChannel = function(transport, initCallback, converters)
{
if (typeof transport !== "object" || typeof transport.send !== "function") {
console.error("The QWebChannel expects a transport object with a send function and onmessage callback property." +
@@ -28,6 +28,43 @@ var QWebChannel = function(transport, initCallback)
var channel = this;
this.transport = transport;
+ var converterRegistry =
+ {
+ Date : function(response) {
+ if (typeof response === "string"
+ && response.match(
+ /^-?\d+-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?([-+\u2212](\d{2}):(\d{2})|Z)?$/)) {
+ var date = new Date(response);
+ if (!isNaN(date))
+ return date;
+ }
+ return undefined; // Return undefined if current converter is not applicable
+ }
+ };
+
+ this.usedConverters = [];
+
+ this.addConverter = function(converter)
+ {
+ if (typeof converter === "string") {
+ if (converterRegistry.hasOwnProperty(converter))
+ this.usedConverters.push(converterRegistry[converter]);
+ else
+ console.error("Converter '" + converter + "' not found");
+ } else if (typeof converter === "function") {
+ this.usedConverters.push(converter);
+ } else {
+ console.error("Invalid converter object type " + typeof converter);
+ }
+ }
+
+ if (Array.isArray(converters)) {
+ for (const converter of converters)
+ this.addConverter(converter);
+ } else if (converters !== undefined) {
+ this.addConverter(converters);
+ }
+
this.send = function(data)
{
if (typeof(data) !== "string") {
@@ -154,6 +191,12 @@ function QObject(name, data, webChannel)
this.unwrapQObject = function(response)
{
+ for (const converter of webChannel.usedConverters) {
+ var result = converter(response);
+ if (result !== undefined)
+ return result;
+ }
+
if (response instanceof Array) {
// support list of objects
return response.map(qobj => object.unwrapQObject(qobj))