From cb7f660a931efacce5c149c194b63071593f1d34 Mon Sep 17 00:00:00 2001 From: Kirill Burtsev Date: Fri, 1 Nov 2019 12:47:21 +0100 Subject: Example 'qwclient': fix lsObject() output of registered objects Change-Id: I6b3f771eea2d92f0c3a7d05eeb13cf0b3d8b1ef4 Reviewed-by: Kai Koehne Reviewed-by: Milian Wolff --- examples/webchannel/qwclient/qwclient.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/webchannel/qwclient/qwclient.js b/examples/webchannel/qwclient/qwclient.js index 0b0909f..fa8cbf7 100755 --- a/examples/webchannel/qwclient/qwclient.js +++ b/examples/webchannel/qwclient/qwclient.js @@ -114,7 +114,8 @@ var setupRepl = function() { var r = repl.start({ prompt: "webchannel> ", input: process.stdin, - output: process.stdout + output: process.stdout, + ignoreUndefined: true }); r.context.serverAddress = serverAddress; @@ -122,10 +123,15 @@ var setupRepl = function() { r.context.channels = channels; r.context.lsObjects = function() { - channels.forEach(function(channel) { - console.log('Channel ' + channel); - Object.keys(channel.objects); - }); + for (let i = 0; i < channels.length; ++i) { + const channel = channels[i]; + if (!channel) // closed and removed channel in repl + continue; + + console.log('-- Channel "c' + i + '" objects:'); + for (const obj of Object.keys(channel.objects)) + console.log(obj, ':', channel.objects[obj]); + } } return r; } -- cgit v1.2.1 From b19e610e7de40815fd15e5398ad69ec69d05c41b Mon Sep 17 00:00:00 2001 From: Kirill Burtsev Date: Fri, 1 Nov 2019 12:42:11 +0100 Subject: Examples: fix 'nodejs' and 'qwclient' compatibility with 'standalone' Amends split of classes from 13294ce605 Fixes: QTBUG-75221 Change-Id: I9491fc59a76974c7a9e4784526ac8c2ba729edd7 Reviewed-by: Milian Wolff --- examples/webchannel/nodejs/chatclient.js | 4 ++-- examples/webchannel/qwclient/qwclient.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/webchannel/nodejs/chatclient.js b/examples/webchannel/nodejs/chatclient.js index 76b620c..6d231a2 100644 --- a/examples/webchannel/nodejs/chatclient.js +++ b/examples/webchannel/nodejs/chatclient.js @@ -85,7 +85,7 @@ var createWebChannel = function(transport, rlif) { // i.e. the server wants to 'send text'. // This can be confusing, as we connect to the signal // to receive incoming messages on our side - channel.objects.dialog.sendText.connect(function(message) { + channel.objects.core.sendText.connect(function(message) { process.stdout.cursorTo(0); process.stdout.clearLine(0); console.log(' << ' + message); @@ -107,7 +107,7 @@ var createWebChannel = function(transport, rlif) { // is called with our message. // Again the naming is for the server side, // i.e. the slot is used _by the server_ to receive text. - channel.objects.dialog.receiveText(l); + channel.objects.core.receiveText(l); console.log(' >> ' + l); } rlif.prompt(); diff --git a/examples/webchannel/qwclient/qwclient.js b/examples/webchannel/qwclient/qwclient.js index fa8cbf7..cd277f1 100755 --- a/examples/webchannel/qwclient/qwclient.js +++ b/examples/webchannel/qwclient/qwclient.js @@ -141,7 +141,7 @@ var welcome = function() { console.log('Use openChannel(url) to connect to a service.'); console.log('For the standalone example, just openChannel() should suffice.'); console.log('Opened channels have their objects aliased to c, i.e. c0'); - console.log('So for the standalone example try: c0.dialog.receiveText(\'hello world\')'); + console.log('So for the standalone example try: c0.core.receiveText(\'hello world\')'); } welcome(); -- cgit v1.2.1 From b2685b64d1f2fef2ef1f3f3542379fb8f052d113 Mon Sep 17 00:00:00 2001 From: Kirill Burtsev Date: Fri, 25 Oct 2019 13:13:48 +0200 Subject: Avoid using 'for..in' to iterate over collections 'for..in' construct iterates through enumerable properties meaning that user added properties to default type's prototype (like Array.prototype.first) are also included. Proper usage involves 'hasOwnProperty' but is not as clean and generally discouraged by popular style guides in favor of 'for..of' and 'Object.keys' and higher-order functions like 'forEach'. Adopt them for iteration to fix unexpected property warnings. Fixes: QTBUG-50999 Change-Id: Ia0846b0c5a2c34f5ee6f1dcb82198cb3946a95f1 Reviewed-by: Milian Wolff --- examples/webchannel/shared/qwebchannel.js | 42 +++++++++++-------------------- 1 file changed, 15 insertions(+), 27 deletions(-) (limited to 'examples') diff --git a/examples/webchannel/shared/qwebchannel.js b/examples/webchannel/shared/qwebchannel.js index fca45d9..32ad51e 100644 --- a/examples/webchannel/shared/qwebchannel.js +++ b/examples/webchannel/shared/qwebchannel.js @@ -140,15 +140,14 @@ var QWebChannel = function(transport, initCallback) this.handlePropertyUpdate = function(message) { - for (var i in message.data) { - var data = message.data[i]; + message.data.forEach(data => { var object = channel.objects[data.object]; if (object) { object.propertyUpdate(data.signals, data.properties); } else { console.warn("Unhandled property update: " + data.object + "::" + data.signal); } - } + }); channel.exec({type: QWebChannelMessageTypes.idle}); } @@ -158,13 +157,15 @@ var QWebChannel = function(transport, initCallback) }; channel.exec({type: QWebChannelMessageTypes.init}, function(data) { - for (var objectName in data) { - var object = new QObject(objectName, data[objectName], channel); + for (const objectName of Object.keys(data)) { + new QObject(objectName, data[objectName], channel); } + // now unwrap properties, which might reference other registered objects - for (var objectName in channel.objects) { + for (const objectName of Object.keys(channel.objects)) { channel.objects[objectName].unwrapProperties(); } + if (initCallback) { initCallback(channel); } @@ -191,19 +192,14 @@ function QObject(name, data, webChannel) { if (response instanceof Array) { // support list of objects - var ret = new Array(response.length); - for (var i = 0; i < response.length; ++i) { - ret[i] = object.unwrapQObject(response[i]); - } - return ret; + return response.map(qobj => object.unwrapQObject(qobj)) } if (!(response instanceof Object)) return response; - if (!response["__QObject*__"] - || response.id === undefined) { + if (!response["__QObject*__"] || response.id === undefined) { var jObj = {}; - for (var propName in response) { + for (const propName of Object.keys(response)) { jObj[propName] = object.unwrapQObject(response[propName]); } return jObj; @@ -226,13 +222,7 @@ function QObject(name, data, webChannel) // just assigning {} though would not have the desired effect, but the // below also ensures all external references will see the empty map // NOTE: this detour is necessary to workaround QTBUG-40021 - var propertyNames = []; - for (var propertyName in qObject) { - propertyNames.push(propertyName); - } - for (var idx in propertyNames) { - delete qObject[propertyNames[idx]]; - } + Object.keys(qObject).forEach(name => delete qObject[name]); } }); // here we are already initialized, and thus must directly unwrap the properties @@ -242,7 +232,7 @@ function QObject(name, data, webChannel) this.unwrapProperties = function() { - for (var propertyIdx in object.__propertyCache__) { + for (const propertyIdx of Object.keys(object.__propertyCache__)) { object.__propertyCache__[propertyIdx] = object.unwrapQObject(object.__propertyCache__[propertyIdx]); } } @@ -318,12 +308,12 @@ function QObject(name, data, webChannel) this.propertyUpdate = function(signals, propertyMap) { // update property cache - for (var propertyIndex in propertyMap) { + for (const propertyIndex of Object.keys(propertyMap)) { var propertyValue = propertyMap[propertyIndex]; object.__propertyCache__[propertyIndex] = this.unwrapQObject(propertyValue); } - for (var signalName in signals) { + for (const signalName of Object.keys(signals)) { // Invoke all callbacks, as signalEmitted() does not. This ensures the // property cache is updated before the callbacks are invoked. invokeSignalCallbacks(signalName, signals[signalName]); @@ -447,9 +437,7 @@ function QObject(name, data, webChannel) data.signals.forEach(function(signal) { addSignal(signal, false); }); - for (var name in data.enums) { - object[name] = data.enums[name]; - } + Object.assign(object, data.enums); } //required for use with nodejs -- cgit v1.2.1