Skip to content

Commit fbe81ad

Browse files
author
Kerry
authored
Live location sharing - expose room liveBeaconIds (#2296)
* updates rooms live beacon ids on destroy Signed-off-by: Kerry Archibald <[email protected]> * expose live beacons ids Signed-off-by: Kerry Archibald <[email protected]> * room state emit all the time on beacon liveness change Signed-off-by: Kerry Archibald <[email protected]> * update comment Signed-off-by: Kerry Archibald <[email protected]>
1 parent c3d7a49 commit fbe81ad

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

spec/unit/room-state.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ describe("RoomState", function() {
334334
state.setStateEvents([updatedLiveBeaconEvent]);
335335

336336
expect(state.hasLiveBeacons).toBe(false);
337-
expect(filterEmitCallsByEventType(RoomStateEvent.BeaconLiveness, emitSpy).length).toBe(2);
337+
expect(filterEmitCallsByEventType(RoomStateEvent.BeaconLiveness, emitSpy).length).toBe(3);
338338
expect(emitSpy).toHaveBeenCalledWith(RoomStateEvent.BeaconLiveness, state, false);
339339
});
340340
});

src/models/room-state.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
8484
public paginationToken: string = null;
8585

8686
public readonly beacons = new Map<BeaconIdentifier, Beacon>();
87-
private liveBeaconIds: BeaconIdentifier[] = [];
87+
private _liveBeaconIds: BeaconIdentifier[] = [];
8888

8989
/**
9090
* Construct room state.
@@ -251,6 +251,10 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
251251
return !!this.liveBeaconIds?.length;
252252
}
253253

254+
public get liveBeaconIds(): BeaconIdentifier[] {
255+
return this._liveBeaconIds;
256+
}
257+
254258
/**
255259
* Creates a copy of this room state so that mutations to either won't affect the other.
256260
* @return {RoomState} the copy of the room state
@@ -502,26 +506,22 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
502506

503507
this.emit(BeaconEvent.New, event, beacon);
504508
beacon.on(BeaconEvent.LivenessChange, this.onBeaconLivenessChange.bind(this));
509+
beacon.on(BeaconEvent.Destroy, this.onBeaconLivenessChange.bind(this));
505510

506511
this.beacons.set(beacon.identifier, beacon);
507512
}
508513

509514
/**
510515
* @experimental
511516
* Check liveness of room beacons
512-
* emit RoomStateEvent.BeaconLiveness when
513-
* roomstate.hasLiveBeacons has changed
517+
* emit RoomStateEvent.BeaconLiveness event
514518
*/
515519
private onBeaconLivenessChange(): void {
516-
const prevHasLiveBeacons = !!this.liveBeaconIds?.length;
517-
this.liveBeaconIds = Array.from(this.beacons.values())
520+
this._liveBeaconIds = Array.from(this.beacons.values())
518521
.filter(beacon => beacon.isLive)
519522
.map(beacon => beacon.identifier);
520523

521-
const hasLiveBeacons = !!this.liveBeaconIds.length;
522-
if (prevHasLiveBeacons !== hasLiveBeacons) {
523-
this.emit(RoomStateEvent.BeaconLiveness, this, hasLiveBeacons);
524-
}
524+
this.emit(RoomStateEvent.BeaconLiveness, this, this.hasLiveBeacons);
525525
}
526526

527527
private getStateEventMatching(event: MatrixEvent): MatrixEvent | null {

0 commit comments

Comments
 (0)