2021-02-26 10:23:09 +00:00
|
|
|
/*
|
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
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";
|
2022-03-03 21:42:18 +00:00
|
|
|
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 {
|
2021-07-27 20:15:40 +01:00
|
|
|
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;
|
2021-07-27 20:15:40 +01:00
|
|
|
private allRoomsInHome: boolean = SpaceStore.instance.allRoomsInHome;
|
2021-02-26 10:23:09 +00:00
|
|
|
|
2022-09-07 16:42:39 +01:00
|
|
|
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);
|
2021-07-27 20:15:40 +01:00
|
|
|
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) => {
|
2021-07-27 20:15:40 +01:00
|
|
|
if (activeSpace === this.activeSpace && allRoomsInHome === this.allRoomsInHome) return; // nop
|
2021-05-04 18:06:43 +01:00
|
|
|
|
2021-11-11 13:07:41 +00:00
|
|
|
const neededFilter = SpaceWatcher.needsFilter(this.activeSpace, this.allRoomsInHome);
|
|
|
|
|
const needsFilter = SpaceWatcher.needsFilter(activeSpace, allRoomsInHome);
|
|
|
|
|
|
2021-07-27 20:15:40 +01:00
|
|
|
this.activeSpace = activeSpace;
|
|
|
|
|
this.allRoomsInHome = allRoomsInHome;
|
|
|
|
|
|
2021-11-11 13:07:41 +00:00
|
|
|
if (needsFilter) {
|
2021-05-04 18:06:43 +01:00
|
|
|
this.updateFilter();
|
|
|
|
|
}
|
2021-07-27 20:15:40 +01:00
|
|
|
|
2021-11-11 13:07:41 +00:00
|
|
|
if (!neededFilter && needsFilter) {
|
2021-07-27 20:15:40 +01:00
|
|
|
this.store.addFilter(this.filter);
|
2021-11-11 13:07:41 +00:00
|
|
|
} else if (neededFilter && !needsFilter) {
|
2021-07-27 20:15:40 +01:00
|
|
|
this.store.removeFilter(this.filter);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private onHomeBehaviourUpdated = (allRoomsInHome: boolean) => {
|
|
|
|
|
this.onSelectedSpaceUpdated(this.activeSpace, allRoomsInHome);
|
2021-04-19 16:15:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private updateFilter = () => {
|
|
|
|
|
this.filter.updateSpace(this.activeSpace);
|
2021-02-26 10:23:09 +00:00
|
|
|
};
|
|
|
|
|
}
|