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

Commit

Permalink
emit liveness change whenever livebeaconIds changes
Browse files Browse the repository at this point in the history
Signed-off-by: Kerry Archibald <[email protected]>
  • Loading branch information
Kerry Archibald committed Mar 21, 2022
1 parent 09383e5 commit 112e33b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
13 changes: 6 additions & 7 deletions src/stores/OwnBeaconStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import {
import defaultDispatcher from "../dispatcher/dispatcher";
import { ActionPayload } from "../dispatcher/payloads";
import { AsyncStoreWithClient } from "./AsyncStoreWithClient";
import { arrayHasDiff } from "../utils/arrays";

const isOwnBeacon = (beacon: Beacon, userId: string): boolean => beacon.beaconInfoOwner === userId;

export enum OwnBeaconStoreEvent {
LivenessChange = 'OwnBeaconStore.LivenessChange'
LivenessChange = 'OwnBeaconStore.LivenessChange',
}

type OwnBeaconStoreState = {
Expand Down Expand Up @@ -132,7 +133,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {

// TODO start location polling here

this.emit(OwnBeaconStoreEvent.LivenessChange, this.hasLiveBeacons());
this.emit(OwnBeaconStoreEvent.LivenessChange, this.getLiveBeaconIds());
};

private initialiseBeaconState = () => {
Expand Down Expand Up @@ -163,15 +164,13 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
};

private checkLiveness = (): void => {
const prevLiveness = this.hasLiveBeacons();
const prevLiveness = this.getLiveBeaconIds();
this.liveBeaconIds = [...this.beacons.values()]
.filter(beacon => beacon.isLive)
.map(beacon => beacon.identifier);

const newLiveness = this.hasLiveBeacons();

if (prevLiveness !== newLiveness) {
this.emit(OwnBeaconStoreEvent.LivenessChange, newLiveness);
if (arrayHasDiff(prevLiveness, this.liveBeaconIds)) {
this.emit(OwnBeaconStoreEvent.LivenessChange, this.liveBeaconIds);
}
};

Expand Down
13 changes: 8 additions & 5 deletions test/stores/OwnBeaconStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ describe('OwnBeaconStore', () => {

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

expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, true);
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, [alicesRoom1BeaconInfo.getType()]);
});

it('does not emit a liveness change event when new beacons do not change live state', async () => {
it('emits a liveness change event when new beacons do not change live state', async () => {
makeRoomsWithStateEvents([
alicesRoom2BeaconInfo,
]);
Expand All @@ -342,7 +342,7 @@ describe('OwnBeaconStore', () => {

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

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

Expand Down Expand Up @@ -381,7 +381,7 @@ describe('OwnBeaconStore', () => {

expect(store.hasLiveBeacons()).toBe(false);
expect(store.hasLiveBeacons(room1Id)).toBe(false);
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, false);
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, []);
});

it('stops beacon when liveness changes from true to false and beacon is expired', async () => {
Expand Down Expand Up @@ -434,7 +434,10 @@ describe('OwnBeaconStore', () => {

expect(store.hasLiveBeacons()).toBe(true);
expect(store.hasLiveBeacons(room1Id)).toBe(true);
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, true);
expect(emitSpy).toHaveBeenCalledWith(
OwnBeaconStoreEvent.LivenessChange,
[alicesOldRoomIdBeaconInfo.getType()]
);
});
});

Expand Down

0 comments on commit 112e33b

Please sign in to comment.