Skip to content

Commit

Permalink
Automatically resync Handy (#5581)
Browse files Browse the repository at this point in the history
* Resync Handy every hour
* Don't try to upload script after resync if Handy is disconnected
  • Loading branch information
dumdum7 authored Jan 30, 2025
1 parent 9f7d00d commit b0a1039
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions ui/v2.5/src/hooks/Interactive/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ export const InteractiveContext = React.createContext<IState>({
});

const LOCAL_FORAGE_KEY = "interactive";
const TIME_BETWEEN_SYNCS = 60 * 60 * 1000; // 1 hour

interface IInteractiveState {
serverOffset: number;
lastSyncTime: number;
}

export const InteractiveProvider: React.FC = ({ children }) => {
const [{ data: config }, setConfig] = useLocalForage<IInteractiveState>(
LOCAL_FORAGE_KEY,
{ serverOffset: 0 }
{ serverOffset: 0, lastSyncTime: 0 }
);

const { configuration: stashConfig } = React.useContext(ConfigurationContext);
Expand All @@ -91,13 +93,17 @@ export const InteractiveProvider: React.FC = ({ children }) => {
const initialise = useCallback(async () => {
setError(undefined);

if (!config?.serverOffset) {
const shouldResync =
!config?.lastSyncTime ||
Date.now() - config?.lastSyncTime > TIME_BETWEEN_SYNCS;

if (!config?.serverOffset || shouldResync) {
setState(ConnectionState.Syncing);
const offset = await interactive.sync();
setConfig({ serverOffset: offset });
setState(ConnectionState.Ready);
setInitialised(true);
} else {
setConfig({ serverOffset: offset, lastSyncTime: Date.now() });
}

if (config?.serverOffset) {
interactive.setServerTimeOffset(config.serverOffset);
setState(ConnectionState.Connecting);
try {
Expand Down Expand Up @@ -159,7 +165,7 @@ export const InteractiveProvider: React.FC = ({ children }) => {

setState(ConnectionState.Syncing);
const offset = await interactive.sync();
setConfig({ serverOffset: offset });
setConfig({ serverOffset: offset, lastSyncTime: Date.now() });
setState(ConnectionState.Ready);
}, [interactive, state, setConfig, initialised]);

Expand Down

0 comments on commit b0a1039

Please # to comment.