From af383375b35e44f020f7a905f751a24637a9094d Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Fri, 12 May 2023 10:08:05 +0200 Subject: [PATCH] Handle promises --- src/components/structures/TimelinePanel.tsx | 14 ++++++++------ test/components/structures/TimelinePanel-test.tsx | 11 ++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/components/structures/TimelinePanel.tsx b/src/components/structures/TimelinePanel.tsx index b2f8431d183..53beb54c56b 100644 --- a/src/components/structures/TimelinePanel.tsx +++ b/src/components/structures/TimelinePanel.tsx @@ -1110,7 +1110,7 @@ class TimelinePanel extends React.Component { const readMarkerEvent = this.props.timelineSet.findEventById(readMarkerEventId); if (readMarkerEvent) { - proms.push(await this.sendReadMarker(client, readMarkerEvent)); + proms.push(this.sendReadMarker(client, readMarkerEvent)); } } @@ -1160,7 +1160,7 @@ class TimelinePanel extends React.Component { // if the read marker is on the screen, we can now assume we've caught up to the end // of the screen, so move the marker down to the bottom of the screen. - private updateReadMarker = (): void => { + private updateReadMarker = async (): Promise => { if (!this.props.manageReadMarkers) return; if (this.getReadMarkerPosition() === 1) { // the read marker is at an event below the viewport, @@ -1189,7 +1189,7 @@ class TimelinePanel extends React.Component { } // Send the updated read marker (along with read receipt) to the server - this.sendReadReceipts(); + await this.sendReadReceipts(); }; // advance the read marker past any events we sent ourselves. @@ -1277,7 +1277,7 @@ class TimelinePanel extends React.Component { /** * update the read-up-to marker to match the read receipt */ - public forgetReadMarker = (): void => { + public forgetReadMarker = async (): Promise => { if (!this.props.manageReadMarkers) return; // Find the read receipt - we will set the read marker to this @@ -1299,7 +1299,7 @@ class TimelinePanel extends React.Component { this.setReadMarker(rmId, rmTs); // Send the receipts to the server immediately (don't wait for activity) - this.sendReadReceipts(); + await this.sendReadReceipts(); }; /* return true if the content is fully scrolled down and we are @@ -1492,7 +1492,9 @@ class TimelinePanel extends React.Component { } if (this.props.sendReadReceiptOnLoad) { - this.sendReadReceipts(); + this.sendReadReceipts().catch((err) => { + logger.warn("Error sending receipts on load", err); + }); } }, ); diff --git a/test/components/structures/TimelinePanel-test.tsx b/test/components/structures/TimelinePanel-test.tsx index 7d09c0ae711..4cc45141e2d 100644 --- a/test/components/structures/TimelinePanel-test.tsx +++ b/test/components/structures/TimelinePanel-test.tsx @@ -187,9 +187,7 @@ describe("TimelinePanel", () => { // @ts-ignore await timelinePanel.sendReadReceipts(); // @ts-ignore Simulate user activity by calling updateReadMarker on the TimelinePanel. - timelinePanel.updateReadMarker(); - - await flushPromises(); + await timelinePanel.updateReadMarker(); }); it("should send a fully read marker and a public receipt", async () => { @@ -202,7 +200,7 @@ describe("TimelinePanel", () => { client.sendReadReceipt.mockClear(); // @ts-ignore Simulate user activity by calling updateReadMarker on the TimelinePanel. - timelinePanel.updateReadMarker(); + await timelinePanel.updateReadMarker(); }); it("should not send receipts again", () => { @@ -212,8 +210,7 @@ describe("TimelinePanel", () => { it("and forgetting the read markers, should send the stored marker again", async () => { timelineSet.addLiveEvent(ev2, {}); room.addEphemeralEvents([newReceipt(ev2.getId()!, userId, 222, 200)]); - timelinePanel.forgetReadMarker(); - await flushPromises(); + await timelinePanel.forgetReadMarker(); expect(client.sendReadReceipt).toHaveBeenCalledWith(ev2, ReceiptType.FullyRead, true); }); }); @@ -251,7 +248,7 @@ describe("TimelinePanel", () => { client.sendReadReceipt.mockClear(); // @ts-ignore simulate user activity - timelinePanel.updateReadMarker(); + await timelinePanel.updateReadMarker(); // It should not send the receipt again. expect(client.sendReadReceipt).not.toHaveBeenCalledWith(ev1, ReceiptType.ReadPrivate);