Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Simulation page request #1135

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ThreeEditor/components/Dialog/RunSimulationDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Card, CardContent, Fade, Modal } from '@mui/material';
import { enqueueSnackbar } from 'notistack';
import { useCallback, useState } from 'react';
import { useState } from 'react';

import { useShSimulation } from '../../../services/ShSimulatorService';
import { useStore } from '../../../services/StoreService';
Expand Down
2 changes: 1 addition & 1 deletion src/WrapperApp/components/Simulation/RunSimulationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export function RunSimulationForm({
onChange={handleSourceTypeChange}>
<ToggleButton
size='small'
value='project'
value='editor'
color='primary'>
Editor Project Data
</ToggleButton>
Expand Down
14 changes: 7 additions & 7 deletions src/WrapperApp/components/Simulation/SimulationPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, Fade, Modal } from '@mui/material';
import { ChangeEvent, useCallback, useEffect, useState } from 'react';
import { ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react';

import { useConfig } from '../../../config/ConfigService';
import { isFullSimulationData } from '../../../services/LoaderService';
Expand Down Expand Up @@ -70,7 +70,6 @@ export default function SimulationPanel({
const [simulationInfo, setSimulationInfo] = useState<SimulationInfo[]>([]);
const [simulationsStatusData, setSimulationsStatusData] = useState<JobStatusData[]>([]);

const [simulationIDInterval, setSimulationIDInterval] = useState<number>();
const [controller] = useState(new AbortController());

const updateSimulationInfo = useCallback(
Expand Down Expand Up @@ -111,7 +110,6 @@ export default function SimulationPanel({
.then(() => {
setBackendAlive(true);
updateSimulationInfo();
setSimulationIDInterval(30000);
})
.catch(() => {
setBackendAlive(false);
Expand All @@ -120,22 +118,24 @@ export default function SimulationPanel({

return () => {
controller.abort();
setSimulationIDInterval(undefined);
};
}, [
controller.signal,
getHelloWorld,
updateSimulationInfo,
setSimulationIDInterval,
isBackendAlive,
setBackendAlive,
demoMode,
trackedId,
controller
]);

useIntervalAsync(updateSimulationInfo, simulationIDInterval);
const simulationDataInterval = useMemo(() => {
grzanka marked this conversation as resolved.
Show resolved Hide resolved
if (simulationInfo.length > 0 && !demoMode && isBackendAlive) return 1000;
// interval 1 second if there are simulations to track and there is connection to backend
}, [simulationInfo, demoMode, isBackendAlive]);

useIntervalAsync(updateSimulationData, simulationInfo.length > 0 ? 1000 : simulationIDInterval);
useIntervalAsync(updateSimulationData, simulationDataInterval);

const handleLoadResults = async (taskId: string | null, simulation: unknown) => {
if (taskId === null) return goToResults?.call(null);
Expand Down
14 changes: 4 additions & 10 deletions src/util/hooks/useIntervalAsync.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import { useEffect, useRef } from 'react';
import { useEffect } from 'react';
import { clearIntervalAsync, setIntervalAsync } from 'set-interval-async';

function useIntervalAsync(
callback: () => Promise<void>,
delay?: number,
immediate: boolean = delay === undefined
) {
const callbackRef = useRef(callback);

useEffect(() => {
callbackRef.current = callback;
}, [callback]);

useEffect(() => {
let interval: ReturnType<typeof setIntervalAsync> | null = null;

if (immediate) callbackRef.current();
else interval = setIntervalAsync(callbackRef.current, delay!);
if (immediate) callback();
if (delay !== undefined) interval = setIntervalAsync(callback, delay);

return () => {
if (interval) clearIntervalAsync(interval);
};
}, [delay, immediate]);
}, [delay, immediate, callback]);
}

export default useIntervalAsync;