mirror of
https://github.com/vector-im/element-web.git
synced 2024-11-17 05:55:00 +08:00
Reference count calls to start/stopListening on ScalarMessaging (#1164)
Otherwise component mounting/unmounting can race and you end up with nothing listening.
This commit is contained in:
parent
b8941f76d3
commit
11309f34e3
@ -564,12 +564,27 @@ const onMessage = function(event) {
|
||||
});
|
||||
};
|
||||
|
||||
let listenerCount = 0;
|
||||
module.exports = {
|
||||
startListening: function() {
|
||||
window.addEventListener("message", onMessage, false);
|
||||
if (listenerCount === 0) {
|
||||
window.addEventListener("message", onMessage, false);
|
||||
}
|
||||
listenerCount += 1;
|
||||
},
|
||||
|
||||
stopListening: function() {
|
||||
window.removeEventListener("message", onMessage);
|
||||
listenerCount -= 1;
|
||||
if (listenerCount === 0) {
|
||||
window.removeEventListener("message", onMessage);
|
||||
}
|
||||
if (listenerCount < 0) {
|
||||
// Make an error so we get a stack trace
|
||||
const e = new Error(
|
||||
"ScalarMessaging: mismatched startListening / stopListening detected." +
|
||||
" Negative count"
|
||||
);
|
||||
console.error(e);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user