diff --git a/src/app/hooks/useDeviceDisplayName.ts b/src/app/hooks/useDeviceDisplayName.ts index 7c9793cc6..23ba70107 100644 --- a/src/app/hooks/useDeviceDisplayName.ts +++ b/src/app/hooks/useDeviceDisplayName.ts @@ -22,26 +22,28 @@ export function useDeviceDisplayName(mx: MatrixClient | undefined) { const updatedRef = useRef(false); useEffect(() => { - if (!mx || updatedRef.current) return; + if (!mx || updatedRef.current) return undefined; + + let cleanup: (() => void) | undefined; const state = mx.getSyncState(); if (state && SYNC_READY.has(state)) { updatedRef.current = true; updateDeviceName(mx).catch(() => {}); - return; + } else { + const onSync = (syncState: SyncState) => { + if (SYNC_READY.has(syncState)) { + mx.removeListener(ClientEvent.Sync, onSync); + updatedRef.current = true; + updateDeviceName(mx).catch(() => {}); + } + }; + mx.on(ClientEvent.Sync, onSync); + cleanup = () => { + mx.removeListener(ClientEvent.Sync, onSync); + }; } - const onSync = (syncState: SyncState) => { - if (SYNC_READY.has(syncState)) { - mx.removeListener(ClientEvent.Sync, onSync); - updatedRef.current = true; - updateDeviceName(mx).catch(() => {}); - } - }; - mx.on(ClientEvent.Sync, onSync); - - return () => { - mx.removeListener(ClientEvent.Sync, onSync); - }; + return cleanup; }, [mx]); } diff --git a/tests/e2e/fixtures/continuwuity.ts b/tests/e2e/fixtures/continuwuity.ts index cdb942818..4f35be88a 100644 --- a/tests/e2e/fixtures/continuwuity.ts +++ b/tests/e2e/fixtures/continuwuity.ts @@ -172,6 +172,7 @@ export async function getRoomMessages( ): Promise { const messages: RoomMessage[] = []; let from: string | undefined; + /* eslint-disable no-await-in-loop */ do { const params = new URLSearchParams({ dir: 'b', limit: '100' }); if (from) params.set('from', from); @@ -198,6 +199,7 @@ export async function getRoomMessages( }); from = data.end; } while (from); + /* eslint-enable no-await-in-loop */ messages.reverse(); return messages; } diff --git a/tests/e2e/live-timeline.spec.ts b/tests/e2e/live-timeline.spec.ts index 36b143cc9..ef628ea10 100644 --- a/tests/e2e/live-timeline.spec.ts +++ b/tests/e2e/live-timeline.spec.ts @@ -119,20 +119,24 @@ test.describe('live timeline', () => { }); await app.openRoom(`${tag} Room`); - for (let i = 1; i <= BURST_SIZE; i += 1) { - await sendText(hsBaseUrl, remote.accessToken, room, `${tag}-b${i}`, i); - } + await Promise.all( + Array.from({ length: BURST_SIZE }, (_, i) => + sendText(hsBaseUrl, remote.accessToken, room, `${tag}-b${i + 1}`, i + 1) + ) + ); const expected = (await getRoomMessages(hsBaseUrl, user.accessToken, room)) .filter((m) => m.body.startsWith(`${tag}-b`)) .map((m) => m.body); expect(expected).toEqual(Array.from({ length: BURST_SIZE }, (_, i) => `${tag}-b${i + 1}`)); + /* eslint-disable no-await-in-loop */ for (const body of expected) { await expect(page.getByText(body, { exact: true })).toHaveCount(1, { timeout: 120_000, }); } + /* eslint-enable no-await-in-loop */ const domOrder = await page.getByText(new RegExp(`^${tag}-b\\d+$`)).allTextContents(); expect(domOrder).toEqual(expected); diff --git a/tests/e2e/sliding-sync-timeline.spec.ts b/tests/e2e/sliding-sync-timeline.spec.ts index 301ba97b8..d59185f72 100644 --- a/tests/e2e/sliding-sync-timeline.spec.ts +++ b/tests/e2e/sliding-sync-timeline.spec.ts @@ -67,9 +67,7 @@ async function sendHistory( prefix: string, bodies: string[] ): Promise { - for (let i = 0; i < bodies.length; i += 1) { - await sendMessage(baseUrl, token, roomId, `${prefix}-${bodies[i]}`); - } + await Promise.all(bodies.map((body) => sendMessage(baseUrl, token, roomId, `${prefix}-${body}`))); } async function wheelToTopUntilVisible(page: Page, text: string): Promise { @@ -170,22 +168,26 @@ test.describe('sliding sync timeline', () => { timeout: 120_000, }); + /* eslint-disable no-await-in-loop */ for (let i = 0; i < SEND_DELAYS_MS.length; i += 1) { if (SEND_DELAYS_MS[i]! > 0) await page.waitForTimeout(SEND_DELAYS_MS[i]!); await sendMessage(hsBaseUrl, user.accessToken, room, `${tag}-live-${i + 1}`); } + /* eslint-enable no-await-in-loop */ await app.openRoom(`${tag} Home`); await expect(page.getByText(`${tag}-seed`, { exact: true })).toBeVisible({ timeout: 120_000, }); + /* eslint-disable no-await-in-loop */ for (let i = 0; i < SEND_DELAYS_MS.length; i += 1) { await expect(page.getByText(`${tag}-live-${i + 1}`, { exact: true })).toHaveCount(1, { timeout: 120_000, }); await expect(page.getByText(`${tag}-live-${i + 1}`, { exact: true })).toBeVisible(); } + /* eslint-enable no-await-in-loop */ const domOrder = await page.getByText(new RegExp(`^${tag}-live-\\d+$`)).allTextContents(); expect(domOrder).toEqual(SEND_DELAYS_MS.map((_, i) => `${tag}-live-${i + 1}`)); diff --git a/tests/e2e/timeline-recovery.spec.ts b/tests/e2e/timeline-recovery.spec.ts index 570aebb44..7968ecd46 100644 --- a/tests/e2e/timeline-recovery.spec.ts +++ b/tests/e2e/timeline-recovery.spec.ts @@ -86,19 +86,23 @@ test.describe('timeline recovery', () => { await context.setOffline(true); const burstCount = 6; + /* eslint-disable no-await-in-loop */ for (let i = 0; i < burstCount; i += 1) { txnCounter += 1; await sendText(hsBaseUrl, bob.accessToken, room, `${tag}-burst-${i + 1}`, txnCounter); } + /* eslint-enable no-await-in-loop */ await context.setOffline(false); + /* eslint-disable no-await-in-loop */ for (let i = 0; i < burstCount; i += 1) { await expect(page.getByText(`${tag}-burst-${i + 1}`, { exact: true })).toHaveCount(1, { timeout: 180_000, }); await expect(page.getByText(`${tag}-burst-${i + 1}`, { exact: true })).toBeVisible(); } + /* eslint-enable no-await-in-loop */ const canonical = (await getRoomMessages(hsBaseUrl, alice.accessToken, room)) .map((m) => m.body)