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

Commit

Permalink
fix OwnBeaconStore more...
Browse files Browse the repository at this point in the history
Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry Archibald committed Mar 16, 2022
1 parent cb46e28 commit fc25240
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
27 changes: 14 additions & 13 deletions src/stores/OwnBeaconStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ limitations under the License.
*/

import {
Beacon,
BeaconEvent,
MatrixEvent,
Room,
Beacon,
} from "matrix-js-sdk/src/matrix";

import defaultDispatcher from "../dispatcher/dispatcher";
Expand Down Expand Up @@ -61,8 +62,8 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
}

protected async onReady(): Promise<void> {
this.matrixClient.on(BeaconEvent.LivenessChange, this.onBeaconLiveness.bind(this));
this.matrixClient.on(BeaconEvent.New, this.onNewBeacon.bind(this));
this.matrixClient.on(BeaconEvent.LivenessChange, this.onBeaconLiveness);
this.matrixClient.on(BeaconEvent.New, this.onNewBeacon);

this.initialiseBeaconState();
}
Expand All @@ -82,15 +83,15 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
return this.liveBeaconIds.filter(beaconId => this.beaconsByRoomId.get(roomId)?.has(beaconId));
}

private onNewBeacon(beacon: Beacon): void {
private onNewBeacon = (_event: MatrixEvent, beacon: Beacon): void => {
if (!isOwnBeacon(beacon, this.matrixClient.getUserId())) {
return;
}
this.addBeacon(beacon);
this.checkLiveness();
}
};

private onBeaconLiveness(isLive: boolean, beacon: Beacon): void {
private onBeaconLiveness = (isLive: boolean, beacon: Beacon): void => {
// check if we care about this beacon
if (!this.beacons.has(beacon.beaconInfoId)) {
return;
Expand All @@ -108,9 +109,9 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
this.emit(OwnBeaconStoreEvent.LivenessChange, this.hasLiveBeacons());
// TODO stop or start polling here
// if not content is live but beacon is not, update state event with live: false
}
};

private initialiseBeaconState() {
private initialiseBeaconState = () => {
const userId = this.matrixClient.getUserId();
const visibleRooms = this.matrixClient.getVisibleRooms();

Expand All @@ -123,9 +124,9 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
});

this.checkLiveness();
}
};

private addBeacon(beacon: Beacon): void {
private addBeacon = (beacon: Beacon): void => {
this.beacons.set(beacon.beaconInfoId, beacon);

if (!this.beaconsByRoomId.has(beacon.roomId)) {
Expand All @@ -134,9 +135,9 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {

this.beaconsByRoomId.get(beacon.roomId).add(beacon.beaconInfoId);
beacon.monitorLiveness();
}
};

private checkLiveness(): void {
private checkLiveness = (): void => {
const prevLiveness = this.hasLiveBeacons();
this.liveBeaconIds = [...this.beacons.values()]
.filter(beacon => beacon.isLive)
Expand All @@ -147,5 +148,5 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
if (prevLiveness !== newLiveness) {
this.emit(OwnBeaconStoreEvent.LivenessChange, newLiveness);
}
}
};
}
9 changes: 4 additions & 5 deletions test/stores/OwnBeaconStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ describe('OwnBeaconStore', () => {

const makeOwnBeaconStore = async () => {
const store = OwnBeaconStore.instance;
// await resetAsyncStoreWithClient(store)

await setupAsyncStoreWithClient(store, mockClient);
return store;
Expand Down Expand Up @@ -272,7 +271,7 @@ describe('OwnBeaconStore', () => {
const bobsLiveBeacon = new Beacon(bobsRoom1BeaconInfo);
const monitorSpy = jest.spyOn(bobsLiveBeacon, 'monitorLiveness');

mockClient.emit(BeaconEvent.New, bobsLiveBeacon);
mockClient.emit(BeaconEvent.New, bobsRoom1BeaconInfo, bobsLiveBeacon);

// we dont care about bob
expect(monitorSpy).not.toHaveBeenCalled();
Expand All @@ -285,7 +284,7 @@ describe('OwnBeaconStore', () => {
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);
const monitorSpy = jest.spyOn(alicesLiveBeacon, 'monitorLiveness');

mockClient.emit(BeaconEvent.New, alicesLiveBeacon);
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);

expect(monitorSpy).toHaveBeenCalled();
expect(store.hasLiveBeacons()).toBe(true);
Expand All @@ -298,7 +297,7 @@ describe('OwnBeaconStore', () => {
const emitSpy = jest.spyOn(store, 'emit');
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);

mockClient.emit(BeaconEvent.New, alicesLiveBeacon);
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);

expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, true);
});
Expand All @@ -313,7 +312,7 @@ describe('OwnBeaconStore', () => {
const emitSpy = jest.spyOn(store, 'emit');
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);

mockClient.emit(BeaconEvent.New, alicesLiveBeacon);
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);

expect(emitSpy).not.toHaveBeenCalled();
});
Expand Down

0 comments on commit fc25240

Please sign in to comment.