Skip to content

Commit

Permalink
feat: update delay
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Jensen committed Oct 21, 2023
1 parent 5a49cd6 commit 908bf2c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
4 changes: 4 additions & 0 deletions __tests__/OpenSkyClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {DateTime} from "luxon";

describe("OpenSkyClient", () => {
describe("getAircraftPositions()", () => {
test("test", () => {
const check = DateTime.fromISO("2023-10-21T16:30:00Z", {setZone: true});
console.dir(DateTime.now().diff(check).toMillis());
});
test("Empty Mode-S code array", async () => {
const instance = OpenSkyClient.create();
expect(await instance.getPositions([])).toStrictEqual({});
Expand Down
52 changes: 32 additions & 20 deletions src/AircraftPositionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react";
import _ from "lodash";
import {createContext, PropsWithChildren, useContext, useEffect, useMemo, useState} from "react";
import Axios, {AxiosBasicCredentials} from "axios";
import {AircraftPosition, AircraftPositionService} from "./flight-types";
import {AircraftPosition} from "./flight-types";
import {OpenSkyClient} from "./OpenSkyClient";
import {immerable, produce} from "immer";
import {DateTime} from "luxon";
Expand Down Expand Up @@ -34,28 +34,40 @@ interface OpenSkyConfig {

export function AircraftPositionProvider(props: PropsWithChildren<AircraftPositionProviderProps>) {
const {children, config, modeSCodes} = props;
const initialState = useMemo(() => new AircraftPositionProviderState(modeSCodes.sort()), [modeSCodes.sort().join(":")]);
const [state, updateState] = useState(initialState);
const service = useMemo<AircraftPositionService>(() => {
const service = useMemo(() => {
const {auth} = config;
if (null == auth) {
return OpenSkyClient.create();
}
return OpenSkyClient.create(config => Axios.create(_.assign({auth}, config)));
return OpenSkyClient.create(config => Axios.create(_.assign({
...(null == auth ? {} : {auth})
}, config)));
}, [JSON.stringify(config)]);
const initialState = useMemo(() => new AircraftPositionProviderState(modeSCodes.sort()), [modeSCodes.sort().join(":")]);
const [state, updateState] = useState(initialState);

/* Trigger position updates upon reaching next update time. */
useEffect(() => {
const nextUpdate = state.lastUpdate.plus({minutes: 5});
const delay = Math.max(0, nextUpdate.diff(DateTime.now()).toMillis());
const timeout = setTimeout(() => Promise.resolve().then(async () => {
const positions = await service.getPositions(state.modeSCodes);
updateState(previous => produce(previous, draft => {
draft.positions = positions;
}));
}), delay);
return () => clearTimeout(timeout);
}, [state.lastUpdate, state.modeSCodes, service, updateState]);

/* Callback to invoke the position service and update state. */
const updatePositions = () => {
service.getPositions(state.modeSCodes)
.then(positions => {
updateState(previous => produce(previous, draft => {
draft.nextUpdate = DateTime.now().plus({minute: 1});
draft.positions = positions;
}));
});
}

/* Calculate delay until next update; if <= 0, invoke now, else invoke after timeout. */
const delay = DateTime.now().diff(state.nextUpdate).toMillis();
if (delay <= 0) {
updatePositions();
} else {
const id = setTimeout(updatePositions, delay);
return () => clearTimeout(id);
}
}, [state.nextUpdate, state.modeSCodes, service]);

/* Component body. */
return (
<AircraftPositionContext.Provider value={state.positions}>
{children}
Expand All @@ -74,11 +86,11 @@ export function useAircraftPosition() {
class AircraftPositionProviderState {
[immerable] = true;

lastUpdate: DateTime;
nextUpdate: DateTime;
positions: Record<Lowercase<string>, AircraftPosition>;

constructor(readonly modeSCodes: Lowercase<string>[]) {
this.lastUpdate = DateTime.fromSeconds(0, {zone: "UTC"});
this.nextUpdate = DateTime.fromSeconds(0, {zone: "UTC"});
this.positions = {};
}
}
Expand Down

0 comments on commit 908bf2c

Please sign in to comment.