Skip to content

Commit

Permalink
Live location sharing - expose room liveBeaconIds (#2296)
Browse files Browse the repository at this point in the history
* updates rooms live beacon ids on destroy

Signed-off-by: Kerry Archibald <kerrya@element.io>

* expose live beacons ids

Signed-off-by: Kerry Archibald <kerrya@element.io>

* room state emit all the time on beacon liveness change

Signed-off-by: Kerry Archibald <kerrya@element.io>

* update comment

Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry authored Apr 14, 2022
1 parent c3d7a49 commit fbe81ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion spec/unit/room-state.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ describe("RoomState", function() {
state.setStateEvents([updatedLiveBeaconEvent]);

expect(state.hasLiveBeacons).toBe(false);
expect(filterEmitCallsByEventType(RoomStateEvent.BeaconLiveness, emitSpy).length).toBe(2);
expect(filterEmitCallsByEventType(RoomStateEvent.BeaconLiveness, emitSpy).length).toBe(3);
expect(emitSpy).toHaveBeenCalledWith(RoomStateEvent.BeaconLiveness, state, false);
});
});
Expand Down
18 changes: 9 additions & 9 deletions src/models/room-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
public paginationToken: string = null;

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

/**
* Construct room state.
Expand Down Expand Up @@ -251,6 +251,10 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
return !!this.liveBeaconIds?.length;
}

public get liveBeaconIds(): BeaconIdentifier[] {
return this._liveBeaconIds;
}

/**
* Creates a copy of this room state so that mutations to either won't affect the other.
* @return {RoomState} the copy of the room state
Expand Down Expand Up @@ -502,26 +506,22 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>

this.emit(BeaconEvent.New, event, beacon);
beacon.on(BeaconEvent.LivenessChange, this.onBeaconLivenessChange.bind(this));
beacon.on(BeaconEvent.Destroy, this.onBeaconLivenessChange.bind(this));

this.beacons.set(beacon.identifier, beacon);
}

/**
* @experimental
* Check liveness of room beacons
* emit RoomStateEvent.BeaconLiveness when
* roomstate.hasLiveBeacons has changed
* emit RoomStateEvent.BeaconLiveness event
*/
private onBeaconLivenessChange(): void {
const prevHasLiveBeacons = !!this.liveBeaconIds?.length;
this.liveBeaconIds = Array.from(this.beacons.values())
this._liveBeaconIds = Array.from(this.beacons.values())
.filter(beacon => beacon.isLive)
.map(beacon => beacon.identifier);

const hasLiveBeacons = !!this.liveBeaconIds.length;
if (prevHasLiveBeacons !== hasLiveBeacons) {
this.emit(RoomStateEvent.BeaconLiveness, this, hasLiveBeacons);
}
this.emit(RoomStateEvent.BeaconLiveness, this, this.hasLiveBeacons);
}

private getStateEventMatching(event: MatrixEvent): MatrixEvent | null {
Expand Down

0 comments on commit fbe81ad

Please sign in to comment.