Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 6bfe043

Browse files
authored
Fix infinite loop when pinning/unpinning persistent widgets (#8396)
Pinning or unpinning a persistent widget, such as Jitsi, could cause the PiP view and the app drawer to fight for control over the widget, since the PiP view never realized that it was supposed to relinquish control. This was due to a race between the WidgetLayoutStore update and the AppTile lifecycle tracking update.
1 parent b2bebda commit 6bfe043

File tree

5 files changed

+124
-83
lines changed

5 files changed

+124
-83
lines changed

src/components/views/elements/AppTile.tsx

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,6 @@ export default class AppTile extends React.Component<IProps, IState> {
117117
showLayoutButtons: true,
118118
};
119119

120-
// We track a count of all "live" `AppTile`s for a given widget UID.
121-
// For this purpose, an `AppTile` is considered live from the time it is
122-
// constructed until it is unmounted. This is used to aid logic around when
123-
// to tear down the widget iframe. See `componentWillUnmount` for details.
124-
private static liveTilesByUid = new Map<string, number>();
125-
126-
public static addLiveTile(widgetId: string, roomId: string): void {
127-
const uid = WidgetUtils.calcWidgetUid(widgetId, roomId);
128-
const refs = this.liveTilesByUid.get(uid) ?? 0;
129-
this.liveTilesByUid.set(uid, refs + 1);
130-
}
131-
132-
public static removeLiveTile(widgetId: string, roomId: string): void {
133-
const uid = WidgetUtils.calcWidgetUid(widgetId, roomId);
134-
const refs = this.liveTilesByUid.get(uid);
135-
if (refs) this.liveTilesByUid.set(uid, refs - 1);
136-
}
137-
138-
public static isLive(widgetId: string, roomId: string): boolean {
139-
const uid = WidgetUtils.calcWidgetUid(widgetId, roomId);
140-
const refs = this.liveTilesByUid.get(uid) ?? 0;
141-
return refs > 0;
142-
}
143-
144120
private contextMenuButton = createRef<any>();
145121
private iframe: HTMLIFrameElement; // ref to the iframe (callback style)
146122
private allowedWidgetsWatchRef: string;
@@ -152,7 +128,10 @@ export default class AppTile extends React.Component<IProps, IState> {
152128
constructor(props: IProps) {
153129
super(props);
154130

155-
AppTile.addLiveTile(this.props.app.id, this.props.app.roomId);
131+
// Tiles in miniMode are floating, and therefore not docked
132+
if (!this.props.miniMode) {
133+
ActiveWidgetStore.instance.dockWidget(this.props.app.id, this.props.app.roomId);
134+
}
156135

157136
// The key used for PersistedElement
158137
this.persistKey = getPersistKey(WidgetUtils.getWidgetUid(this.props.app));
@@ -284,27 +263,14 @@ export default class AppTile extends React.Component<IProps, IState> {
284263
public componentWillUnmount(): void {
285264
this.unmounted = true;
286265

287-
// It might seem simplest to always tear down the widget itself here,
288-
// and indeed that would be a bit easier to reason about... however, we
289-
// support moving widgets between containers (e.g. top <-> center).
290-
// During such a move, this component will unmount from the old
291-
// container and remount in the new container. By keeping the widget
292-
// iframe loaded across this transition, the widget doesn't notice that
293-
// anything happened, which improves overall widget UX. During this kind
294-
// of movement between containers, the new `AppTile` for the new
295-
// container is constructed before the old one unmounts. By counting the
296-
// mounted `AppTile`s for each widget, we know to only tear down the
297-
// widget iframe when the last the `AppTile` unmounts.
298-
AppTile.removeLiveTile(this.props.app.id, this.props.app.roomId);
299-
300-
// We also support a separate "persistence" mode where a single widget
301-
// can request to be "sticky" and follow you across rooms in a PIP
302-
// container.
303-
const isActiveWidget = ActiveWidgetStore.instance.getWidgetPersistence(
304-
this.props.app.id, this.props.app.roomId,
305-
);
266+
if (!this.props.miniMode) {
267+
ActiveWidgetStore.instance.undockWidget(this.props.app.id, this.props.app.roomId);
268+
}
306269

307-
if (!AppTile.isLive(this.props.app.id, this.props.app.roomId) && !isActiveWidget) {
270+
// Only tear down the widget if no other component is keeping it alive,
271+
// because we support moving widgets between containers, in which case
272+
// another component will keep it loaded throughout the transition
273+
if (!ActiveWidgetStore.instance.isLive(this.props.app.id, this.props.app.roomId)) {
308274
this.endWidgetActions();
309275
}
310276

src/components/views/voip/PipView.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import { WidgetLayoutStore } from '../../../stores/widgets/WidgetLayoutStore';
3333
import CallViewHeader from './CallView/CallViewHeader';
3434
import ActiveWidgetStore, { ActiveWidgetStoreEvent } from '../../../stores/ActiveWidgetStore';
3535
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
36-
import AppTile from '../elements/AppTile';
3736

3837
const SHOW_CALL_IN_STATES = [
3938
CallState.Connected,
@@ -135,7 +134,9 @@ export default class PipView extends React.Component<IProps, IState> {
135134
if (room) {
136135
WidgetLayoutStore.instance.on(WidgetLayoutStore.emissionForRoom(room), this.updateCalls);
137136
}
138-
ActiveWidgetStore.instance.on(ActiveWidgetStoreEvent.Update, this.onActiveWidgetStoreUpdate);
137+
ActiveWidgetStore.instance.on(ActiveWidgetStoreEvent.Persistence, this.onWidgetPersistence);
138+
ActiveWidgetStore.instance.on(ActiveWidgetStoreEvent.Dock, this.onWidgetDockChanges);
139+
ActiveWidgetStore.instance.on(ActiveWidgetStoreEvent.Undock, this.onWidgetDockChanges);
139140
document.addEventListener("mouseup", this.onEndMoving.bind(this));
140141
}
141142

@@ -149,7 +150,9 @@ export default class PipView extends React.Component<IProps, IState> {
149150
if (room) {
150151
WidgetLayoutStore.instance.off(WidgetLayoutStore.emissionForRoom(room), this.updateCalls);
151152
}
152-
ActiveWidgetStore.instance.off(ActiveWidgetStoreEvent.Update, this.onActiveWidgetStoreUpdate);
153+
ActiveWidgetStore.instance.off(ActiveWidgetStoreEvent.Persistence, this.onWidgetPersistence);
154+
ActiveWidgetStore.instance.off(ActiveWidgetStoreEvent.Dock, this.onWidgetDockChanges);
155+
ActiveWidgetStore.instance.off(ActiveWidgetStoreEvent.Undock, this.onWidgetDockChanges);
153156
document.removeEventListener("mouseup", this.onEndMoving.bind(this));
154157
}
155158

@@ -186,13 +189,17 @@ export default class PipView extends React.Component<IProps, IState> {
186189
this.updateShowWidgetInPip();
187190
};
188191

189-
private onActiveWidgetStoreUpdate = (): void => {
192+
private onWidgetPersistence = (): void => {
190193
this.updateShowWidgetInPip(
191194
ActiveWidgetStore.instance.getPersistentWidgetId(),
192195
ActiveWidgetStore.instance.getPersistentRoomId(),
193196
);
194197
};
195198

199+
private onWidgetDockChanges = (): void => {
200+
this.updateShowWidgetInPip();
201+
};
202+
196203
private updateCalls = (): void => {
197204
if (!this.state.viewedRoomId) return;
198205
const [primaryCall, secondaryCalls] = getPrimarySecondaryCallsForPip(this.state.viewedRoomId);
@@ -231,19 +238,19 @@ export default class PipView extends React.Component<IProps, IState> {
231238
persistentRoomId = this.state.persistentRoomId,
232239
) {
233240
let fromAnotherRoom = false;
234-
let notVisible = false;
241+
let notDocked = false;
235242
// Sanity check the room - the widget may have been destroyed between render cycles, and
236243
// thus no room is associated anymore.
237244
if (persistentWidgetId && MatrixClientPeg.get().getRoom(persistentRoomId)) {
238-
notVisible = !AppTile.isLive(persistentWidgetId, persistentRoomId);
245+
notDocked = !ActiveWidgetStore.instance.isDocked(persistentWidgetId, persistentRoomId);
239246
fromAnotherRoom = this.state.viewedRoomId !== persistentRoomId;
240247
}
241248

242249
// The widget should only be shown as a persistent app (in a floating
243250
// pip container) if it is not visible on screen: either because we are
244251
// viewing a different room OR because it is in none of the possible
245252
// containers of the room view.
246-
const showWidgetInPip = fromAnotherRoom || notVisible;
253+
const showWidgetInPip = fromAnotherRoom || notDocked;
247254

248255
this.setState({ showWidgetInPip, persistentWidgetId, persistentRoomId });
249256
}

src/stores/ActiveWidgetStore.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,25 @@ import WidgetUtils from "../utils/WidgetUtils";
2323
import { WidgetMessagingStore } from "./widgets/WidgetMessagingStore";
2424

2525
export enum ActiveWidgetStoreEvent {
26-
Update = "update",
26+
// Indicates a change in the currently persistent widget
27+
Persistence = "persistence",
28+
// Indicate changes in the currently docked widgets
29+
Dock = "dock",
30+
Undock = "undock",
2731
}
2832

2933
/**
3034
* Stores information about the widgets active in the app right now:
3135
* * What widget is set to remain always-on-screen, if any
3236
* Only one widget may be 'always on screen' at any one time.
33-
* * Negotiated capabilities for active apps
37+
* * Reference counts to keep track of whether a widget is kept docked or alive
38+
* by any components
3439
*/
3540
export default class ActiveWidgetStore extends EventEmitter {
3641
private static internalInstance: ActiveWidgetStore;
3742
private persistentWidgetId: string;
3843
private persistentRoomId: string;
44+
private dockedWidgetsByUid = new Map<string, number>();
3945

4046
public static get instance(): ActiveWidgetStore {
4147
if (!ActiveWidgetStore.internalInstance) {
@@ -79,7 +85,7 @@ export default class ActiveWidgetStore extends EventEmitter {
7985
this.persistentWidgetId = widgetId;
8086
this.persistentRoomId = roomId;
8187
}
82-
this.emit(ActiveWidgetStoreEvent.Update);
88+
this.emit(ActiveWidgetStoreEvent.Persistence);
8389
}
8490

8591
public getWidgetPersistence(widgetId: string, roomId: string): boolean {
@@ -93,6 +99,34 @@ export default class ActiveWidgetStore extends EventEmitter {
9399
public getPersistentRoomId(): string {
94100
return this.persistentRoomId;
95101
}
102+
103+
// Registers the given widget as being docked somewhere in the UI (not a PiP),
104+
// to allow its lifecycle to be tracked.
105+
public dockWidget(widgetId: string, roomId: string): void {
106+
const uid = WidgetUtils.calcWidgetUid(widgetId, roomId);
107+
const refs = this.dockedWidgetsByUid.get(uid) ?? 0;
108+
this.dockedWidgetsByUid.set(uid, refs + 1);
109+
if (refs === 0) this.emit(ActiveWidgetStoreEvent.Dock);
110+
}
111+
112+
public undockWidget(widgetId: string, roomId: string): void {
113+
const uid = WidgetUtils.calcWidgetUid(widgetId, roomId);
114+
const refs = this.dockedWidgetsByUid.get(uid);
115+
if (refs) this.dockedWidgetsByUid.set(uid, refs - 1);
116+
if (refs === 1) this.emit(ActiveWidgetStoreEvent.Undock);
117+
}
118+
119+
// Determines whether the given widget is docked anywhere in the UI (not a PiP)
120+
public isDocked(widgetId: string, roomId: string): boolean {
121+
const uid = WidgetUtils.calcWidgetUid(widgetId, roomId);
122+
const refs = this.dockedWidgetsByUid.get(uid) ?? 0;
123+
return refs > 0;
124+
}
125+
126+
// Determines whether the given widget is being kept alive in the UI, including PiPs
127+
public isLive(widgetId: string, roomId: string): boolean {
128+
return this.isDocked(widgetId, roomId) || this.getWidgetPersistence(widgetId, roomId);
129+
}
96130
}
97131

98132
window.mxActiveWidgetStore = ActiveWidgetStore.instance;

test/components/views/elements/AppTile-test.tsx

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { RightPanelPhases } from "../../../../src/stores/right-panel/RightPanelS
3333
import RightPanelStore from "../../../../src/stores/right-panel/RightPanelStore";
3434
import { UPDATE_EVENT } from "../../../../src/stores/AsyncStore";
3535
import WidgetStore, { IApp } from "../../../../src/stores/WidgetStore";
36+
import ActiveWidgetStore from "../../../../src/stores/ActiveWidgetStore";
3637
import AppTile from "../../../../src/components/views/elements/AppTile";
3738
import { Container, WidgetLayoutStore } from "../../../../src/stores/widgets/WidgetLayoutStore";
3839
import AppsDrawer from "../../../../src/components/views/rooms/AppsDrawer";
@@ -116,26 +117,6 @@ describe("AppTile", () => {
116117
jest.spyOn(SettingsStore, "getValue").mockRestore();
117118
});
118119

119-
it("tracks live tiles correctly", () => {
120-
expect(AppTile.isLive("1", "r1")).toEqual(false);
121-
122-
// Try removing the tile before it gets added
123-
AppTile.removeLiveTile("1", "r1");
124-
expect(AppTile.isLive("1", "r1")).toEqual(false);
125-
126-
AppTile.addLiveTile("1", "r1");
127-
expect(AppTile.isLive("1", "r1")).toEqual(true);
128-
129-
AppTile.addLiveTile("1", "r1");
130-
expect(AppTile.isLive("1", "r1")).toEqual(true);
131-
132-
AppTile.removeLiveTile("1", "r1");
133-
expect(AppTile.isLive("1", "r1")).toEqual(true);
134-
135-
AppTile.removeLiveTile("1", "r1");
136-
expect(AppTile.isLive("1", "r1")).toEqual(false);
137-
});
138-
139120
it("destroys non-persisted right panel widget on room change", async () => {
140121
// Set up right panel state
141122
const realGetValue = SettingsStore.getValue;
@@ -170,7 +151,7 @@ describe("AppTile", () => {
170151
});
171152
await rpsUpdated;
172153

173-
expect(AppTile.isLive("1", "r1")).toBe(true);
154+
expect(ActiveWidgetStore.instance.isLive("1", "r1")).toBe(true);
174155

175156
// We want to verify that as we change to room 2, we should close the
176157
// right panel and destroy the widget.
@@ -190,7 +171,7 @@ describe("AppTile", () => {
190171
</MatrixClientContext.Provider>);
191172

192173
expect(endWidgetActions.mock.calls.length).toBe(1);
193-
expect(AppTile.isLive("1", "r1")).toBe(false);
174+
expect(ActiveWidgetStore.instance.isLive("1", "r1")).toBe(false);
194175

195176
mockSettings.mockRestore();
196177
});
@@ -231,8 +212,8 @@ describe("AppTile", () => {
231212
});
232213
await rpsUpdated1;
233214

234-
expect(AppTile.isLive("1", "r1")).toBe(true);
235-
expect(AppTile.isLive("1", "r2")).toBe(false);
215+
expect(ActiveWidgetStore.instance.isLive("1", "r1")).toBe(true);
216+
expect(ActiveWidgetStore.instance.isLive("1", "r2")).toBe(false);
236217

237218
jest.spyOn(SettingsStore, "getValue").mockImplementation((name, roomId) => {
238219
if (name === "RightPanel.phases") {
@@ -266,8 +247,8 @@ describe("AppTile", () => {
266247
</MatrixClientContext.Provider>);
267248
await rpsUpdated2;
268249

269-
expect(AppTile.isLive("1", "r1")).toBe(false);
270-
expect(AppTile.isLive("1", "r2")).toBe(true);
250+
expect(ActiveWidgetStore.instance.isLive("1", "r1")).toBe(false);
251+
expect(ActiveWidgetStore.instance.isLive("1", "r2")).toBe(true);
271252
});
272253

273254
it("preserves non-persisted widget on container move", async () => {
@@ -300,7 +281,7 @@ describe("AppTile", () => {
300281
/>
301282
</MatrixClientContext.Provider>);
302283

303-
expect(AppTile.isLive("1", "r1")).toBe(true);
284+
expect(ActiveWidgetStore.instance.isLive("1", "r1")).toBe(true);
304285

305286
// We want to verify that as we move the widget to the center container,
306287
// the widget frame remains running.
@@ -316,7 +297,7 @@ describe("AppTile", () => {
316297
});
317298

318299
expect(endWidgetActions.mock.calls.length).toBe(0);
319-
expect(AppTile.isLive("1", "r1")).toBe(true);
300+
expect(ActiveWidgetStore.instance.isLive("1", "r1")).toBe(true);
320301
});
321302

322303
afterAll(async () => {

test/stores/ActiveWidgetStore-test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import ActiveWidgetStore from "../../src/stores/ActiveWidgetStore";
18+
19+
describe("ActiveWidgetStore", () => {
20+
const store = ActiveWidgetStore.instance;
21+
22+
it("tracks docked and live tiles correctly", () => {
23+
expect(store.isDocked("1", "r1")).toEqual(false);
24+
expect(store.isLive("1", "r1")).toEqual(false);
25+
26+
// Try undocking the widget before it gets docked
27+
store.undockWidget("1", "r1");
28+
expect(store.isDocked("1", "r1")).toEqual(false);
29+
expect(store.isLive("1", "r1")).toEqual(false);
30+
31+
store.dockWidget("1", "r1");
32+
expect(store.isDocked("1", "r1")).toEqual(true);
33+
expect(store.isLive("1", "r1")).toEqual(true);
34+
35+
store.dockWidget("1", "r1");
36+
expect(store.isDocked("1", "r1")).toEqual(true);
37+
expect(store.isLive("1", "r1")).toEqual(true);
38+
39+
store.undockWidget("1", "r1");
40+
expect(store.isDocked("1", "r1")).toEqual(true);
41+
expect(store.isLive("1", "r1")).toEqual(true);
42+
43+
// Ensure that persistent widgets remain live even while undocked
44+
store.setWidgetPersistence("1", "r1", true);
45+
store.undockWidget("1", "r1");
46+
expect(store.isDocked("1", "r1")).toEqual(false);
47+
expect(store.isLive("1", "r1")).toEqual(true);
48+
49+
store.setWidgetPersistence("1", "r1", false);
50+
expect(store.isDocked("1", "r1")).toEqual(false);
51+
expect(store.isLive("1", "r1")).toEqual(false);
52+
});
53+
});

0 commit comments

Comments
 (0)