diff --git a/src/stores/widgets/WidgetMessagingStore.ts b/src/stores/widgets/WidgetMessagingStore.ts index 6d05cae8c6..dfa8eed943 100644 --- a/src/stores/widgets/WidgetMessagingStore.ts +++ b/src/stores/widgets/WidgetMessagingStore.ts @@ -114,4 +114,24 @@ export class WidgetMessagingStore extends AsyncStoreWithClient { const driver = new SdkWidgetDriver(widget, WidgetKind.Account, userId); return this.generateMessaging(userId, widget, iframe, driver); } + + /** + * Stops the messaging instance for the widget, unregistering it. + * @param {Room} room The room where the widget resides. + * @param {Widget} widget The widget + */ + public stopMessagingForRoomWidget(room: Room, widget: Widget) { + const api = this.widgetMap.getOrCreate(room.roomId, new EnhancedMap()).remove(widget.id); + if (api) api.messaging.stop(); + } + + /** + * Stops the messaging instance for the widget, unregistering it. + * @param {Widget} widget The widget + */ + public stopMessagingForAccountWidget(widget: Widget) { + if (!this.matrixClient) return; + const api = this.widgetMap.getOrCreate(this.matrixClient.getUserId(), new EnhancedMap()).remove(widget.id); + if (api) api.messaging.stop(); + } } diff --git a/src/utils/maps.ts b/src/utils/maps.ts index 630e0af286..57d84bd33f 100644 --- a/src/utils/maps.ts +++ b/src/utils/maps.ts @@ -60,4 +60,10 @@ export class EnhancedMap extends Map { this.set(key, def); return def; } + + public remove(key: K): V { + const v = this.get(key); + this.delete(key); + return v; + } }