Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 979f91e

Browse files
author
Kerry Archibald
committed
test ownbeaconstore
Signed-off-by: Kerry Archibald <[email protected]>
1 parent 9c8c9cc commit 979f91e

File tree

2 files changed

+336
-27
lines changed

2 files changed

+336
-27
lines changed

src/stores/OwnBeaconStore.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export enum OwnBeaconStoreEvent {
1717
type OwnBeaconStoreState = {
1818
beacons: Map<string, Beacon>;
1919
beaconsByRoomId: Map<Room['roomId'], Set<string>>;
20-
roomIdsWithLiveBeacons: Room['roomId'][];
20+
liveBeaconIds: string[];
2121
};
2222
export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
2323
private static internalInstance = new OwnBeaconStore();
2424
public readonly beacons = new Map<string, Beacon>();
2525
public readonly beaconsByRoomId = new Map<Room['roomId'], Set<string>>();
26-
private roomIdsWithLiveBeacons = [];
26+
private liveBeaconIds = [];
2727

2828
public constructor() {
2929
super(defaultDispatcher);
@@ -38,8 +38,8 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
3838
}
3939

4040
protected async onReady() {
41-
this.matrixClient.on(BeaconEvent.LivenessChange, this.onBeaconLiveness);
42-
this.matrixClient.on(BeaconEvent.New, this.onNewBeacon);
41+
this.matrixClient.on(BeaconEvent.LivenessChange, this.onBeaconLiveness.bind(this));
42+
this.matrixClient.on(BeaconEvent.New, this.onNewBeacon.bind(this));
4343
this.initialiseBeaconState();
4444
}
4545

@@ -48,10 +48,14 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
4848
}
4949

5050
public hasLiveBeacons(roomId?: string): boolean {
51+
return !!this.getLiveBeaconIds(roomId).length;
52+
}
53+
54+
public getLiveBeaconIds(roomId?: string): string[] {
5155
if (!roomId) {
52-
return !!this.roomIdsWithLiveBeacons.length;
56+
return this.liveBeaconIds;
5357
}
54-
return this.roomIdsWithLiveBeacons.includes(roomId);
58+
return this.liveBeaconIds.filter(beaconId => this.beaconsByRoomId.get(roomId)?.has(beaconId));
5559
}
5660

5761
private onNewBeacon(beacon: Beacon): void {
@@ -68,26 +72,25 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
6872
return;
6973
}
7074

71-
if (!isLive && this.roomIdsWithLiveBeacons.includes(beacon.beaconInfoId)) {
72-
this.roomIdsWithLiveBeacons =
73-
this.roomIdsWithLiveBeacons.filter(beaconId => beaconId !== beacon.beaconInfoId);
74-
this.emit(OwnBeaconStoreEvent.LivenessChange, this.hasLiveBeacons);
75+
if (!isLive && this.liveBeaconIds.includes(beacon.beaconInfoId)) {
76+
this.liveBeaconIds =
77+
this.liveBeaconIds.filter(beaconId => beaconId !== beacon.beaconInfoId);
7578
}
7679

77-
if (isLive && !this.roomIdsWithLiveBeacons.includes(beacon.beaconInfoId)) {
78-
this.roomIdsWithLiveBeacons.push(beacon.beaconInfoId);
79-
this.emit(OwnBeaconStoreEvent.LivenessChange, this.hasLiveBeacons);
80+
if (isLive && !this.liveBeaconIds.includes(beacon.beaconInfoId)) {
81+
this.liveBeaconIds.push(beacon.beaconInfoId);
8082
}
8183

84+
this.emit(OwnBeaconStoreEvent.LivenessChange, this.hasLiveBeacons());
8285
// TODO stop or start polling here
8386
// if not content is live but beacon is not, update state event with live: false
8487
}
8588

8689
private initialiseBeaconState() {
8790
const userId = this.matrixClient.getUserId();
8891
const visibleRooms = this.matrixClient.getVisibleRooms();
92+
8993
visibleRooms
90-
.filter(room => room.currentState.hasLiveBeacons)
9194
.forEach(room => {
9295
const roomState = room.currentState;
9396
const beacons = roomState.beacons;
@@ -111,9 +114,9 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
111114

112115
private checkLiveness(): void {
113116
const prevLiveness = this.hasLiveBeacons();
114-
this.roomIdsWithLiveBeacons = [...this.beacons.values()]
117+
this.liveBeaconIds = [...this.beacons.values()]
115118
.filter(beacon => beacon.isLive)
116-
.map(beacon => beacon.roomId);
119+
.map(beacon => beacon.beaconInfoId);
117120

118121
const newLiveness = this.hasLiveBeacons();
119122

0 commit comments

Comments
 (0)