Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
move advanceDateAndTime to utils, tidy
Browse files Browse the repository at this point in the history
Signed-off-by: Kerry Archibald <[email protected]>
  • Loading branch information
Kerry Archibald committed Mar 25, 2022
1 parent bc18bd2 commit 3a9d931
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
21 changes: 11 additions & 10 deletions src/stores/OwnBeaconStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
BeaconInfoState, makeBeaconContent, makeBeaconInfoContent,
} from "matrix-js-sdk/src/content-helpers";
import { M_BEACON } from "matrix-js-sdk/src/@types/beacon";
import { logger } from "matrix-js-sdk/src/logger";

import defaultDispatcher from "../dispatcher/dispatcher";
import { ActionPayload } from "../dispatcher/payloads";
Expand Down Expand Up @@ -63,10 +64,11 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
private geolocationError: GeolocationError | undefined;
private clearPositionWatch: ClearWatchCallback | undefined;
/**
* Track the last published position and when it was published
* so it can be republished while the user is static
* Track when the last position was published
* So we can manually get position on slow interval
* when the target is status
*/
private lastPublishedPosition: { position: TimedGeoUri, publishedTimestamp: number } | undefined;
private lastPublishedPositionTimestamp: number | undefined;

public constructor() {
super(defaultDispatcher);
Expand Down Expand Up @@ -241,13 +243,12 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
this.clearPositionWatch = await watchPosition(this.onWatchedPosition, this.onWatchedPositionError);

this.locationInterval = setInterval(() => {
if (!this.lastPublishedPosition) {
if (!this.lastPublishedPositionTimestamp) {
return;
}
const { publishedTimestamp } = this.lastPublishedPosition;
// if position was last updated STATIC_UPDATE_INTERVAL ms ago or more
// get our position and publish it
if (publishedTimestamp <= Date.now() - STATIC_UPDATE_INTERVAL) {
if (this.lastPublishedPositionTimestamp <= Date.now() - STATIC_UPDATE_INTERVAL) {
this.publishCurrentLocationToBeacons();
}
}, STATIC_UPDATE_INTERVAL);
Expand All @@ -257,7 +258,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
const timedGeoPosition = mapGeolocationPositionToTimedGeo(position);

// if this is our first position, publish immediateley
if (!this.lastPublishedPosition) {
if (!this.lastPublishedPositionTimestamp) {
this.publishLocationToBeacons(timedGeoPosition);
} else {
this.debouncedPublishLocationToBeacons(timedGeoPosition);
Expand All @@ -266,13 +267,13 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {

private onWatchedPositionError = (error: GeolocationError) => {
this.geolocationError = error;
console.log(this.geolocationError);
logger.error(this.geolocationError);
};

private stopPollingLocation = () => {
clearInterval(this.locationInterval);
this.locationInterval = undefined;
this.lastPublishedPosition = undefined;
this.lastPublishedPositionTimestamp = undefined;
this.geolocationError = undefined;

if (this.clearPositionWatch) {
Expand All @@ -286,7 +287,7 @@ export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
* Sets last published beacon
*/
private publishLocationToBeacons = async (position: TimedGeoUri) => {
this.lastPublishedPosition = { position, publishedTimestamp: Date.now() };
this.lastPublishedPositionTimestamp = Date.now();
// TODO handle failure in individual beacon without rejecting rest
await Promise.all(this.liveBeaconIds.map(beaconId =>
this.sendLocationToBeacon(this.beacons.get(beaconId), position)),
Expand Down
9 changes: 1 addition & 8 deletions test/components/views/beacon/RoomLiveShareWarning-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import '../../../skinned-sdk';
import RoomLiveShareWarning from '../../../../src/components/views/beacon/RoomLiveShareWarning';
import { OwnBeaconStore } from '../../../../src/stores/OwnBeaconStore';
import {
advanceDateAndTime,
findByTestId,
getMockClientWithEventEmitter,
makeBeaconInfoEvent,
Expand Down Expand Up @@ -72,14 +73,6 @@ describe('<RoomLiveShareWarning />', () => {
return [room1, room2];
};

const advanceDateAndTime = (ms: number) => {
// bc liveness check uses Date.now we have to advance this mock
jest.spyOn(global.Date, 'now').mockReturnValue(Date.now() + ms);

// then advance time for the interval by the same amount
jest.advanceTimersByTime(ms);
};

const makeOwnBeaconStore = async () => {
const store = OwnBeaconStore.instance;

Expand Down
14 changes: 6 additions & 8 deletions test/stores/OwnBeaconStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { makeBeaconContent } from "matrix-js-sdk/src/content-helpers";
import { M_BEACON, M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";

import { OwnBeaconStore, OwnBeaconStoreEvent } from "../../src/stores/OwnBeaconStore";
import { flushPromisesWithFakeTimers, resetAsyncStoreWithClient, setupAsyncStoreWithClient } from "../test-utils";
import {
advanceDateAndTime,
flushPromisesWithFakeTimers,
resetAsyncStoreWithClient,
setupAsyncStoreWithClient,
} from "../test-utils";
import {
makeBeaconInfoEvent,
makeGeolocationPosition,
Expand Down Expand Up @@ -107,13 +112,6 @@ describe('OwnBeaconStore', () => {
return [room1, room2];
};

const advanceDateAndTime = (ms: number) => {
// bc liveness check uses Date.now we have to advance this mock
jest.spyOn(global.Date, 'now').mockReturnValue(Date.now() + ms);
// then advance time for the interval by the same amount
jest.advanceTimersByTime(ms);
};

const makeOwnBeaconStore = async () => {
const store = OwnBeaconStore.instance;

Expand Down
12 changes: 11 additions & 1 deletion test/test-utils/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const findByTagAndTestId = findByTagAndAttr('data-test-id');
export const flushPromises = async () => await new Promise(resolve => setTimeout(resolve));

// with jest's modern fake timers process.nextTick is also mocked,
// flushing promises in the normal way waiting for some advancement
// flushing promises in the normal way then waits for some advancement
// of the fake timers
// https://gist.github.com/apieceofbart/e6dea8d884d29cf88cdb54ef14ddbcc4?permalink_comment_id=4018174#gistcomment-4018174
export const flushPromisesWithFakeTimers = async (): Promise<void> => {
Expand Down Expand Up @@ -67,3 +67,13 @@ export function waitForUpdate(inst: React.Component, updates = 1): Promise<void>
};
});
}

/**
* Advance jests fake timers and Date.now mock by ms
* Useful for testing code using timeouts or intervals
* that also checks timestamps
*/
export const advanceDateAndTime = (ms: number) => {
jest.spyOn(global.Date, 'now').mockReturnValue(Date.now() + ms);
jest.advanceTimersByTime(ms);
};

0 comments on commit 3a9d931

Please sign in to comment.