From 8624e8beeb66d75350aa4ea16176de1e4409a42d Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 9 Jul 2020 15:11:21 -0600 Subject: [PATCH] Break up the event loop tasks for the room list The room list does a hefty amount of work, so instead of blocking the event loop with a `/sync` request and a bunch of room updates (as we can get multiple per sync) we can instead run it over several smaller tasks. The smaller tasks help the event loop do other things between our tasks, ensuring we don't inadvertently block the UI from rendering too slowly. On my account and machine, this cuts the time to render in half (~30ms, down from ~60ms) . --- src/stores/room-list/RoomListStore2.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/stores/room-list/RoomListStore2.ts b/src/stores/room-list/RoomListStore2.ts index c29c0b5a20..60f888f8ed 100644 --- a/src/stores/room-list/RoomListStore2.ts +++ b/src/stores/room-list/RoomListStore2.ts @@ -114,7 +114,13 @@ export class RoomListStore2 extends AsyncStore { if (!quiet) this.updateFn.trigger(); }; - protected async onDispatch(payload: ActionPayload) { + protected onDispatch(payload: ActionPayload) { + // We do this to intentionally break out of the current event loop task, allowing + // us to instead wait for a more convenient time to run our updates. + setImmediate(() => this.onDispatchAsync(payload)); + } + + protected async onDispatchAsync(payload: ActionPayload) { if (payload.action === 'MatrixActions.sync') { // Filter out anything that isn't the first PREPARED sync. if (!(payload.prevState === 'PREPARED' && payload.state !== 'PREPARED')) {