From 9e76a2cebff40ecc7631df69993a560ec4218de6 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 10 Jul 2020 11:07:47 -0600 Subject: [PATCH] Handle off-cycle filtering updates in the new room list --- src/stores/room-list/RoomListStore2.ts | 8 +++++++- src/stores/room-list/algorithms/Algorithm.ts | 11 +++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/stores/room-list/RoomListStore2.ts b/src/stores/room-list/RoomListStore2.ts index 05f678160e..4741e1a30f 100644 --- a/src/stores/room-list/RoomListStore2.ts +++ b/src/stores/room-list/RoomListStore2.ts @@ -25,7 +25,7 @@ import { IListOrderingMap, ITagMap, ITagSortingMap, ListAlgorithm, SortAlgorithm import { ActionPayload } from "../../dispatcher/payloads"; import defaultDispatcher from "../../dispatcher/dispatcher"; import { readReceiptChangeIsFor } from "../../utils/read-receipts"; -import { IFilterCondition } from "./filters/IFilterCondition"; +import { FILTER_CHANGED, IFilterCondition } from "./filters/IFilterCondition"; import { TagWatcher } from "./TagWatcher"; import RoomViewStore from "../RoomViewStore"; import { Algorithm, LIST_UPDATED_EVENT } from "./algorithms/Algorithm"; @@ -65,6 +65,7 @@ export class RoomListStore2 extends AsyncStore { for (const settingName of this.watchedSettings) SettingsStore.monitorSetting(settingName, null); RoomViewStore.addListener(() => this.handleRVSUpdate({})); this.algorithm.on(LIST_UPDATED_EVENT, this.onAlgorithmListUpdated); + this.algorithm.on(FILTER_CHANGED, this.onAlgorithmFilterUpdated); } public get orderedLists(): ITagMap { @@ -479,6 +480,11 @@ export class RoomListStore2 extends AsyncStore { this.updateFn.mark(); }; + private onAlgorithmFilterUpdated = () => { + // The filter can happen off-cycle, so trigger an update if we need to. + this.updateFn.triggerIfWillMark(); + }; + /** * Regenerates the room whole room list, discarding any previous results. * @param trigger Set to false to prevent a list update from being sent. Should only diff --git a/src/stores/room-list/algorithms/Algorithm.ts b/src/stores/room-list/algorithms/Algorithm.ts index d985abd392..17e8283c74 100644 --- a/src/stores/room-list/algorithms/Algorithm.ts +++ b/src/stores/room-list/algorithms/Algorithm.ts @@ -153,11 +153,11 @@ export class Algorithm extends EventEmitter { // Populate the cache of the new filter this.allowedByFilter.set(filterCondition, this.rooms.filter(r => filterCondition.isVisible(r))); this.recalculateFilteredRooms(); - filterCondition.on(FILTER_CHANGED, this.recalculateFilteredRooms.bind(this)); + filterCondition.on(FILTER_CHANGED, this.handleFilterChange.bind(this)); } public removeFilterCondition(filterCondition: IFilterCondition): void { - filterCondition.off(FILTER_CHANGED, this.recalculateFilteredRooms.bind(this)); + filterCondition.off(FILTER_CHANGED, this.handleFilterChange.bind(this)); if (this.allowedByFilter.has(filterCondition)) { this.allowedByFilter.delete(filterCondition); @@ -169,6 +169,13 @@ export class Algorithm extends EventEmitter { } } + private async handleFilterChange() { + await this.recalculateFilteredRooms(); + + // re-emit the update so the list store can fire an off-cycle update if needed + this.emit(FILTER_CHANGED); + } + private async updateStickyRoom(val: Room) { try { return await this.doUpdateStickyRoom(val);