summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAndrew Webster <awebster@arcx.com>2020-02-06 09:25:00 -0500
committerAndrew Webster <awebster@arcx.com>2020-02-21 10:46:35 -0500
commitb808e4f8ec42df64fee5f6f170c225b6fbd62070 (patch)
tree0bf4fca57f721ed4e70a2bf8bd64562d8112a93c /examples
parent295b33d05396cad96bbed3094bf23d880fe58109 (diff)
downloadqtwebchannel-b808e4f8ec42df64fee5f6f170c225b6fbd62070.tar.gz
Skip changes made while iterating over connections for emit
If changes are made to the connections while iterating over them during an emit, these new changes should be ignored until the next emit. If not, the following things could happen: * newly added connections could be called for the current emit * an existing connection may not be called at all New connections added to the list are already ignored because forEach caches the length before processing an array. However, if an item is removed during forEach, the current index is not adjusted, which will result in next item being skipped if the removed item is before the current index. This is dealt with by creating a new list during disconnect with the diconnected item removed. This way the list that forEach uses is not modified. This logic is slightly different than QObject, which uses a linked list. If handlers A and B have been attached to the same signal, and handler A disconnects handler B, handler B would still be called, even if it was connected after handler A, which is not the case for QObject. A more complex solution may be required if this behavior needs to match QObject. This also removes an error message when disconnect fails to find any matching connection. This more closely matches QObject. Change-Id: Ief04426a962362055022f450d9767d4b5fe152a1 Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/webchannel/shared/qwebchannel.js12
1 files changed, 5 insertions, 7 deletions
diff --git a/examples/webchannel/shared/qwebchannel.js b/examples/webchannel/shared/qwebchannel.js
index a57a740..3cfcd62 100644
--- a/examples/webchannel/shared/qwebchannel.js
+++ b/examples/webchannel/shared/qwebchannel.js
@@ -273,13 +273,11 @@ function QObject(name, data, webChannel)
console.error("Bad callback given to disconnect from signal " + signalName);
return;
}
- object.__objectSignals__[signalIndex] = object.__objectSignals__[signalIndex] || [];
- var idx = object.__objectSignals__[signalIndex].indexOf(callback);
- if (idx === -1) {
- console.error("Cannot find connection of signal " + signalName + " to " + callback.name);
- return;
- }
- object.__objectSignals__[signalIndex].splice(idx, 1);
+ // This makes a new list. This is important because it won't interfere with
+ // signal processing if a disconnection happens while emittig a signal
+ object.__objectSignals__[signalIndex] = (object.__objectSignals__[signalIndex] || []).filter(function(c) {
+ return c != callback;
+ });
if (!isPropertyNotifySignal && object.__objectSignals__[signalIndex].length === 0) {
// only required for "pure" signals, handled separately for properties in propertyUpdate
webChannel.exec({