Files
ThreadNet-Web/src/stores/room-list/SpaceWatcher.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

64 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-02-26 10:23:09 +00:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2021-02-26 10:23:09 +00:00
Copyright 2021 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2021-02-26 10:23:09 +00:00
*/
2022-09-07 16:42:39 +01:00
import { RoomListStore as Interface } from "./Interface";
2021-02-26 10:23:09 +00:00
import { SpaceFilterCondition } from "./filters/SpaceFilterCondition";
2021-11-11 13:07:41 +00:00
import SpaceStore from "../spaces/SpaceStore";
import { MetaSpace, SpaceKey, UPDATE_HOME_BEHAVIOUR, UPDATE_SELECTED_SPACE } from "../spaces";
2021-02-26 10:23:09 +00:00
/**
* Watches for changes in spaces to manage the filter on the provided RoomListStore
*/
export class SpaceWatcher {
private readonly filter = new SpaceFilterCondition();
// we track these separately to the SpaceStore as we need to observe transitions
2021-11-11 13:07:41 +00:00
private activeSpace: SpaceKey = SpaceStore.instance.activeSpace;
private allRoomsInHome: boolean = SpaceStore.instance.allRoomsInHome;
2021-02-26 10:23:09 +00:00
public constructor(private store: Interface) {
2021-11-11 13:07:41 +00:00
if (SpaceWatcher.needsFilter(this.activeSpace, this.allRoomsInHome)) {
2021-06-16 09:01:13 +01:00
this.updateFilter();
store.addFilter(this.filter);
}
2021-02-26 10:23:09 +00:00
SpaceStore.instance.on(UPDATE_SELECTED_SPACE, this.onSelectedSpaceUpdated);
SpaceStore.instance.on(UPDATE_HOME_BEHAVIOUR, this.onHomeBehaviourUpdated);
2021-02-26 10:23:09 +00:00
}
2021-11-11 13:07:41 +00:00
private static needsFilter(spaceKey: SpaceKey, allRoomsInHome: boolean): boolean {
return !(spaceKey === MetaSpace.Home && allRoomsInHome);
}
private onSelectedSpaceUpdated = (activeSpace: SpaceKey, allRoomsInHome = this.allRoomsInHome): void => {
if (activeSpace === this.activeSpace && allRoomsInHome === this.allRoomsInHome) return; // nop
2021-11-11 13:07:41 +00:00
const neededFilter = SpaceWatcher.needsFilter(this.activeSpace, this.allRoomsInHome);
const needsFilter = SpaceWatcher.needsFilter(activeSpace, allRoomsInHome);
this.activeSpace = activeSpace;
this.allRoomsInHome = allRoomsInHome;
2021-11-11 13:07:41 +00:00
if (needsFilter) {
this.updateFilter();
}
2021-11-11 13:07:41 +00:00
if (!neededFilter && needsFilter) {
this.store.addFilter(this.filter);
2021-11-11 13:07:41 +00:00
} else if (neededFilter && !needsFilter) {
this.store.removeFilter(this.filter);
}
};
private onHomeBehaviourUpdated = (allRoomsInHome: boolean): void => {
this.onSelectedSpaceUpdated(this.activeSpace, allRoomsInHome);
};
private updateFilter = (): void => {
this.filter.updateSpace(this.activeSpace);
2021-02-26 10:23:09 +00:00
};
}