From 50f42288c5d5a3f5ccb186861f843e124695d003 Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Wed, 13 Apr 2022 14:10:51 +0200 Subject: [PATCH 1/4] updates rooms live beacon ids on destroy Signed-off-by: Kerry Archibald --- src/models/room-state.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/models/room-state.ts b/src/models/room-state.ts index 5719034397c..18dc6db15a2 100644 --- a/src/models/room-state.ts +++ b/src/models/room-state.ts @@ -502,6 +502,7 @@ export class RoomState extends TypedEventEmitter 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); } From 37eba3a6011706b3c0fbabc55e5c60513b7c1136 Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Wed, 13 Apr 2022 14:14:17 +0200 Subject: [PATCH 2/4] expose live beacons ids Signed-off-by: Kerry Archibald --- src/models/room-state.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/models/room-state.ts b/src/models/room-state.ts index 18dc6db15a2..f5cebbfa360 100644 --- a/src/models/room-state.ts +++ b/src/models/room-state.ts @@ -84,7 +84,7 @@ export class RoomState extends TypedEventEmitter public paginationToken: string = null; public readonly beacons = new Map(); - private liveBeaconIds: BeaconIdentifier[] = []; + private _liveBeaconIds: BeaconIdentifier[] = []; /** * Construct room state. @@ -251,6 +251,10 @@ export class RoomState extends TypedEventEmitter 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 @@ -515,7 +519,7 @@ export class RoomState extends TypedEventEmitter */ 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); From 445a2a0d45ce8ff3433e20aeb5102d2464e108e1 Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Wed, 13 Apr 2022 17:06:27 +0200 Subject: [PATCH 3/4] room state emit all the time on beacon liveness change Signed-off-by: Kerry Archibald --- spec/unit/room-state.spec.js | 2 +- src/models/room-state.ts | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/spec/unit/room-state.spec.js b/spec/unit/room-state.spec.js index 261f7572d91..c91bff5bd8e 100644 --- a/spec/unit/room-state.spec.js +++ b/spec/unit/room-state.spec.js @@ -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); }); }); diff --git a/src/models/room-state.ts b/src/models/room-state.ts index f5cebbfa360..738dce3ecaa 100644 --- a/src/models/room-state.ts +++ b/src/models/room-state.ts @@ -518,15 +518,11 @@ export class RoomState extends TypedEventEmitter * roomstate.hasLiveBeacons has changed */ private onBeaconLivenessChange(): void { - const prevHasLiveBeacons = !!this.liveBeaconIds?.length; 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 { From a5e2288be94c949bdfc7e090d6dd6319d91cce23 Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Wed, 13 Apr 2022 17:12:40 +0200 Subject: [PATCH 4/4] update comment Signed-off-by: Kerry Archibald --- src/models/room-state.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/models/room-state.ts b/src/models/room-state.ts index 738dce3ecaa..ba127920772 100644 --- a/src/models/room-state.ts +++ b/src/models/room-state.ts @@ -514,15 +514,14 @@ export class RoomState extends TypedEventEmitter /** * @experimental * Check liveness of room beacons - * emit RoomStateEvent.BeaconLiveness when - * roomstate.hasLiveBeacons has changed + * emit RoomStateEvent.BeaconLiveness event */ private onBeaconLivenessChange(): void { this._liveBeaconIds = Array.from(this.beacons.values()) .filter(beacon => beacon.isLive) .map(beacon => beacon.identifier); - this.emit(RoomStateEvent.BeaconLiveness, this, this.hasLiveBeacons); + this.emit(RoomStateEvent.BeaconLiveness, this, this.hasLiveBeacons); } private getStateEventMatching(event: MatrixEvent): MatrixEvent | null {