Create setHasDiff helper and use it
The usage here is identical to how it'll work in Spaces
This commit is contained in:
parent
02ae1e954b
commit
63d95706e9
2 changed files with 41 additions and 8 deletions
|
@ -19,17 +19,17 @@ import { FILTER_CHANGED, FilterPriority, IFilterCondition } from "./IFilterCondi
|
||||||
import { Group } from "matrix-js-sdk/src/models/group";
|
import { Group } from "matrix-js-sdk/src/models/group";
|
||||||
import { EventEmitter } from "events";
|
import { EventEmitter } from "events";
|
||||||
import GroupStore from "../../GroupStore";
|
import GroupStore from "../../GroupStore";
|
||||||
import { arrayHasDiff } from "../../../utils/arrays";
|
|
||||||
import { IDestroyable } from "../../../utils/IDestroyable";
|
import { IDestroyable } from "../../../utils/IDestroyable";
|
||||||
import DMRoomMap from "../../../utils/DMRoomMap";
|
import DMRoomMap from "../../../utils/DMRoomMap";
|
||||||
|
import { setHasDiff } from "../../../utils/sets";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A filter condition for the room list which reveals rooms which
|
* A filter condition for the room list which reveals rooms which
|
||||||
* are a member of a given community.
|
* are a member of a given community.
|
||||||
*/
|
*/
|
||||||
export class CommunityFilterCondition extends EventEmitter implements IFilterCondition, IDestroyable {
|
export class CommunityFilterCondition extends EventEmitter implements IFilterCondition, IDestroyable {
|
||||||
private roomIds: string[] = [];
|
private roomIds = new Set<string>();
|
||||||
private userIds: string[] = [];
|
private userIds = new Set<string>();
|
||||||
|
|
||||||
constructor(private community: Group) {
|
constructor(private community: Group) {
|
||||||
super();
|
super();
|
||||||
|
@ -45,19 +45,18 @@ export class CommunityFilterCondition extends EventEmitter implements IFilterCon
|
||||||
}
|
}
|
||||||
|
|
||||||
public isVisible(room: Room): boolean {
|
public isVisible(room: Room): boolean {
|
||||||
return this.roomIds.includes(room.roomId) ||
|
return this.roomIds.has(room.roomId) || this.userIds.has(DMRoomMap.shared().getUserIdForRoomId(room.roomId));
|
||||||
this.userIds.includes(DMRoomMap.shared().getUserIdForRoomId(room.roomId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private onStoreUpdate = async (): Promise<any> => {
|
private onStoreUpdate = async (): Promise<any> => {
|
||||||
// We don't actually know if the room list changed for the community, so just check it again.
|
// We don't actually know if the room list changed for the community, so just check it again.
|
||||||
const beforeRoomIds = this.roomIds;
|
const beforeRoomIds = this.roomIds;
|
||||||
this.roomIds = (await GroupStore.getGroupRooms(this.community.groupId)).map(r => r.roomId);
|
this.roomIds = new Set((await GroupStore.getGroupRooms(this.community.groupId)).map(r => r.roomId));
|
||||||
|
|
||||||
const beforeUserIds = this.userIds;
|
const beforeUserIds = this.userIds;
|
||||||
this.userIds = (await GroupStore.getGroupMembers(this.community.groupId)).map(u => u.userId);
|
this.userIds = new Set((await GroupStore.getGroupMembers(this.community.groupId)).map(u => u.userId));
|
||||||
|
|
||||||
if (arrayHasDiff(beforeRoomIds, this.roomIds) || arrayHasDiff(beforeUserIds, this.userIds)) {
|
if (setHasDiff(beforeRoomIds, this.roomIds) || setHasDiff(beforeUserIds, this.userIds)) {
|
||||||
this.emit(FILTER_CHANGED);
|
this.emit(FILTER_CHANGED);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
34
src/utils/sets.ts
Normal file
34
src/utils/sets.ts
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if two sets are different through a shallow comparison.
|
||||||
|
* @param a The first set. Must be defined.
|
||||||
|
* @param b The second set. Must be defined.
|
||||||
|
* @returns True if they are different, false otherwise.
|
||||||
|
*/
|
||||||
|
export function setHasDiff<T>(a: Set<T>, b: Set<T>): boolean {
|
||||||
|
if (a.size === b.size) {
|
||||||
|
// When the lengths are equal, check to see if either set is missing an element from the other.
|
||||||
|
if (Array.from(b).some(i => !a.has(i))) return true;
|
||||||
|
if (Array.from(a).some(i => !b.has(i))) return true;
|
||||||
|
|
||||||
|
// if all the keys are common, say so
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true; // different lengths means they are naturally diverged
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue