Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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