You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-11-08 21:42:24 +03: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:
@@ -564,12 +564,27 @@ const onMessage = function(event) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let listenerCount = 0;
|
||||||
module.exports = {
|
module.exports = {
|
||||||
startListening: function() {
|
startListening: function() {
|
||||||
|
if (listenerCount === 0) {
|
||||||
window.addEventListener("message", onMessage, false);
|
window.addEventListener("message", onMessage, false);
|
||||||
|
}
|
||||||
|
listenerCount += 1;
|
||||||
},
|
},
|
||||||
|
|
||||||
stopListening: function() {
|
stopListening: function() {
|
||||||
|
listenerCount -= 1;
|
||||||
|
if (listenerCount === 0) {
|
||||||
window.removeEventListener("message", onMessage);
|
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);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user