Apply strictNullChecks
to src/utils/beacon/*
(#10337)
* strictnullchecks fixes in utils/beacon * user filterBoolean
This commit is contained in:
parent
503df62191
commit
72404d7216
5 changed files with 38 additions and 10 deletions
|
@ -16,6 +16,7 @@ limitations under the License.
|
|||
|
||||
import { Beacon } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { filterBoolean } from "../arrays";
|
||||
import { parseGeoUri } from "../location";
|
||||
|
||||
export type Bounds = {
|
||||
|
@ -36,9 +37,11 @@ export type Bounds = {
|
|||
* west of Greenwich has a negative longitude, min -180
|
||||
*/
|
||||
export const getBeaconBounds = (beacons: Beacon[]): Bounds | undefined => {
|
||||
const coords = beacons
|
||||
.filter((beacon) => !!beacon.latestLocationState)
|
||||
.map((beacon) => parseGeoUri(beacon.latestLocationState.uri));
|
||||
const coords = filterBoolean<GeolocationCoordinates>(
|
||||
beacons.map((beacon) =>
|
||||
!!beacon.latestLocationState?.uri ? parseGeoUri(beacon.latestLocationState.uri) : undefined,
|
||||
),
|
||||
);
|
||||
|
||||
if (!coords.length) {
|
||||
return;
|
||||
|
|
|
@ -28,21 +28,24 @@ export const msUntilExpiry = (startTimestamp: number, durationMs: number): numbe
|
|||
Math.max(0, startTimestamp + durationMs - Date.now());
|
||||
|
||||
export const getBeaconMsUntilExpiry = (beaconInfo: BeaconInfoState): number =>
|
||||
msUntilExpiry(beaconInfo.timestamp, beaconInfo.timeout);
|
||||
msUntilExpiry(beaconInfo.timestamp || 0, beaconInfo.timeout);
|
||||
|
||||
export const getBeaconExpiryTimestamp = (beacon: Beacon): number =>
|
||||
beacon.beaconInfo.timestamp + beacon.beaconInfo.timeout;
|
||||
(beacon.beaconInfo.timestamp || 0) + beacon.beaconInfo.timeout;
|
||||
|
||||
export const sortBeaconsByLatestExpiry = (left: Beacon, right: Beacon): number =>
|
||||
getBeaconExpiryTimestamp(right) - getBeaconExpiryTimestamp(left);
|
||||
|
||||
// aka sort by timestamp descending
|
||||
export const sortBeaconsByLatestCreation = (left: Beacon, right: Beacon): number =>
|
||||
right.beaconInfo.timestamp - left.beaconInfo.timestamp;
|
||||
(right.beaconInfo.timestamp || 0) - (left.beaconInfo.timestamp || 0);
|
||||
|
||||
// a beacon's starting timestamp can be in the future
|
||||
// (either from small deviations in system clock times, or on purpose from another client)
|
||||
// a beacon is only live between its start timestamp and expiry
|
||||
// detect when a beacon is waiting to become live
|
||||
export const isBeaconWaitingToStart = (beacon: Beacon): boolean =>
|
||||
!beacon.isLive && beacon.beaconInfo.timestamp > Date.now() && getBeaconExpiryTimestamp(beacon) > Date.now();
|
||||
!beacon.isLive &&
|
||||
!!beacon.beaconInfo.timestamp &&
|
||||
beacon.beaconInfo.timestamp > Date.now() &&
|
||||
getBeaconExpiryTimestamp(beacon) > Date.now();
|
||||
|
|
|
@ -93,7 +93,7 @@ export const genericPositionFromGeolocation = (geoPosition: GeolocationPosition)
|
|||
timestamp: Date.now(),
|
||||
latitude,
|
||||
longitude,
|
||||
altitude,
|
||||
altitude: altitude ?? undefined,
|
||||
accuracy,
|
||||
};
|
||||
};
|
||||
|
|
|
@ -26,8 +26,13 @@ import { useEventEmitterState } from "../../hooks/useEventEmitter";
|
|||
export const useLiveBeacons = (roomId: Room["roomId"], matrixClient: MatrixClient): Beacon[] => {
|
||||
const room = matrixClient.getRoom(roomId);
|
||||
|
||||
const liveBeacons = useEventEmitterState(room?.currentState, RoomStateEvent.BeaconLiveness, () =>
|
||||
room?.currentState?.liveBeaconIds.map((beaconIdentifier) => room.currentState.beacons.get(beaconIdentifier)),
|
||||
const liveBeacons = useEventEmitterState(
|
||||
room?.currentState,
|
||||
RoomStateEvent.BeaconLiveness,
|
||||
() =>
|
||||
room?.currentState?.liveBeaconIds.map(
|
||||
(beaconIdentifier) => room.currentState.beacons.get(beaconIdentifier)!,
|
||||
) || [],
|
||||
);
|
||||
|
||||
return liveBeacons;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue