@@ -3,13 +3,12 @@ import { useState } from 'react';
3
3
import { useInterval } from 'react-use' ;
4
4
import { apiRequest } from '../utils/api' ;
5
5
import { Execution , ExecutionStatus , isExecutionPending , QueueOutput } from '../utils/api.types' ;
6
- import { ToastTimeoutQuick } from '../utils/spectrum.ts' ;
6
+ import { intervalToTimeout , ToastTimeoutQuick } from '../utils/spectrum' ;
7
+ import { useAppState } from './app' ;
7
8
import { useFormatter } from './formatter' ;
8
9
9
- const ExecutionPollInterval = 1000 ;
10
- const ExecutionPollTimeout = 900 ;
11
-
12
- export const useExecutionPolling = ( executionId : string | undefined | null ) => {
10
+ export const useExecutionPolling = ( executionId : string | undefined | null , pollInterval : number ) => {
11
+ const appState = useAppState ( ) ;
13
12
const [ execution , setExecution ] = useState < Execution | null > ( null ) ;
14
13
const [ executing , setExecuting ] = useState < boolean > ( ! ! executionId ) ;
15
14
const [ loading , setLoading ] = useState < boolean > ( true ) ;
@@ -22,7 +21,7 @@ export const useExecutionPolling = (executionId: string | undefined | null) => {
22
21
operation : 'Code execution state' ,
23
22
url : `/apps/acm/api/queue-code.json?executionId=${ executionId } ` ,
24
23
method : 'get' ,
25
- timeout : ExecutionPollTimeout ,
24
+ timeout : intervalToTimeout ( pollInterval ) ,
26
25
} ) ;
27
26
const queuedExecution = response . data . data . executions . find ( ( e : Execution ) => e . id === executionId ) ! ;
28
27
setExecution ( queuedExecution ) ;
@@ -34,7 +33,7 @@ export const useExecutionPolling = (executionId: string | undefined | null) => {
34
33
setExecuting ( false ) ;
35
34
setWasPending ( false ) ;
36
35
37
- const recentlyCompleted = formatter . isRecent ( queuedExecution . endDate , 2 * ExecutionPollInterval ) ;
36
+ const recentlyCompleted = formatter . isRecent ( queuedExecution . endDate , 2 * pollInterval ) ;
38
37
if ( recentlyCompleted || wasPending ) {
39
38
if ( queuedExecution . status === ExecutionStatus . FAILED ) {
40
39
ToastQueue . negative ( 'Code execution failed!' , { timeout : ToastTimeoutQuick } ) ;
@@ -57,24 +56,24 @@ export const useExecutionPolling = (executionId: string | undefined | null) => {
57
56
pollExecutionState ( executionId ) ;
58
57
}
59
58
} ,
60
- executing && executionId ? ExecutionPollInterval : null ,
59
+ executing && executionId ? appState . spaSettings . executionPollInterval : null ,
61
60
) ;
62
61
63
62
return { execution, setExecution, executing, setExecuting, loading } ;
64
63
} ;
65
64
66
- export const pollExecutionPending = async ( executionId : string ) : Promise < Execution > => {
65
+ export const pollExecutionPending = async ( executionId : string , pollInterval : number ) : Promise < Execution > => {
67
66
let queuedExecution : Execution | null = null ;
68
67
69
68
while ( queuedExecution === null || isExecutionPending ( queuedExecution . status ) ) {
70
69
const response = await apiRequest < QueueOutput > ( {
71
70
operation : 'Code execution state' ,
72
71
url : `/apps/acm/api/queue-code.json?executionId=${ executionId } ` ,
73
72
method : 'get' ,
74
- timeout : ExecutionPollTimeout ,
73
+ timeout : intervalToTimeout ( pollInterval ) ,
75
74
} ) ;
76
75
queuedExecution = response . data . data . executions [ 0 ] ! ;
77
- await new Promise ( ( resolve ) => setTimeout ( resolve , ExecutionPollInterval ) ) ;
76
+ await new Promise ( ( resolve ) => setTimeout ( resolve , pollInterval ) ) ;
78
77
}
79
78
80
79
return queuedExecution ;
0 commit comments