Fix duplicate message listeners
parent
910623d287
commit
23bef68a8f
|
@ -178,6 +178,10 @@ export default class WidgetMessaging extends MatrixPostMessageApi {
|
||||||
constructor(widgetId, targetWindow) {
|
constructor(widgetId, targetWindow) {
|
||||||
super(targetWindow);
|
super(targetWindow);
|
||||||
this.widgetId = widgetId;
|
this.widgetId = widgetId;
|
||||||
|
|
||||||
|
this.startListening = this.startListening.bind(this);
|
||||||
|
this.stopListening = this.stopListening.bind(this);
|
||||||
|
this.onMessage = this.onMessage.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
exec(action) {
|
exec(action) {
|
||||||
|
@ -206,9 +210,11 @@ export default class WidgetMessaging extends MatrixPostMessageApi {
|
||||||
* Register widget message event listeners
|
* Register widget message event listeners
|
||||||
*/
|
*/
|
||||||
startListening() {
|
startListening() {
|
||||||
this.start();
|
|
||||||
if (global.mxWidgetMessagingListenerCount === 0) {
|
if (global.mxWidgetMessagingListenerCount === 0) {
|
||||||
window.addEventListener("message", (evt) => this.onMessage(evt), false);
|
// Start postMessage API listener
|
||||||
|
this.start();
|
||||||
|
// Start widget specific listener
|
||||||
|
window.addEventListener("message", this.onMessage, false);
|
||||||
}
|
}
|
||||||
global.mxWidgetMessagingListenerCount += 1;
|
global.mxWidgetMessagingListenerCount += 1;
|
||||||
}
|
}
|
||||||
|
@ -217,10 +223,12 @@ export default class WidgetMessaging extends MatrixPostMessageApi {
|
||||||
* De-register widget message event listeners
|
* De-register widget message event listeners
|
||||||
*/
|
*/
|
||||||
stopListening() {
|
stopListening() {
|
||||||
this.stop();
|
|
||||||
global.mxWidgetMessagingListenerCount -= 1;
|
global.mxWidgetMessagingListenerCount -= 1;
|
||||||
if (global.mxWidgetMessagingListenerCount === 0) {
|
if (global.mxWidgetMessagingListenerCount === 0) {
|
||||||
window.removeEventListener("message", () => this.onMessage);
|
// Stop widget specific listener
|
||||||
|
window.removeEventListener("message", this.onMessage, false);
|
||||||
|
// Stop postMessage API listener
|
||||||
|
this.stop();
|
||||||
}
|
}
|
||||||
if (global.mxWidgetMessagingListenerCount < 0) {
|
if (global.mxWidgetMessagingListenerCount < 0) {
|
||||||
// Make an error so we get a stack trace
|
// Make an error so we get a stack trace
|
||||||
|
|
|
@ -171,12 +171,8 @@ export default class AppTile extends React.Component {
|
||||||
|
|
||||||
// Widget postMessage listeners
|
// Widget postMessage listeners
|
||||||
try {
|
try {
|
||||||
if (this.widgetMessaging) {
|
this.widgetMessaging.stopListening();
|
||||||
this.widgetMessaging.stopListening();
|
this.widgetMessaging.removeEndpoint(this.props.id, this.props.url);
|
||||||
this.widgetMessaging.removeEndpoint(this.props.id, this.props.url);
|
|
||||||
} else {
|
|
||||||
console.warn('WidgetMessaging not initialised. Not stopping.');
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to stop listening for widgetMessaging events', e.message);
|
console.error('Failed to stop listening for widgetMessaging events', e.message);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue