1
1
import { ReactElement , useEffect , useRef , useState } from 'react'
2
2
import { createLanguageClientManager , LanguageClientId , StatusChangeEvent , LanguageClientManager , WillShutdownParams } from '@codingame/monaco-languageclient-wrapper'
3
+ import useIsUserActive from './hooks/useIsUserActive'
4
+ import useShouldShutdownLanguageClient from './hooks/useShouldShutdownLanguageClient'
3
5
4
6
export interface LanguageClientProps {
5
7
id : LanguageClientId
@@ -12,8 +14,10 @@ export interface LanguageClientProps {
12
14
onDidChangeStatus ?: ( status : StatusChangeEvent ) => void
13
15
/** The language client will be shutdown by the server */
14
16
onWillShutdown ?: ( status : WillShutdownParams ) => void
15
- /** Allows the client to reconnect to another server when it receives the willShutdown notification */
16
- restartAllowed ?: boolean
17
+ /** User is considered inactive when there is no event during this delay (default 30 seconds) or when the tab is blurred */
18
+ userInactivityDelay ?: number
19
+ /** Shutdown the language client when the user stay inactive during this duration (default 60 seconds) */
20
+ userInactivityShutdownDelay ?: number
17
21
}
18
22
19
23
const defaultLibraryUrls : string [ ] = [ ]
@@ -28,7 +32,8 @@ function LanguageClient ({
28
32
onError,
29
33
onDidChangeStatus,
30
34
onWillShutdown,
31
- restartAllowed = true
35
+ userInactivityDelay = 30 * 1000 ,
36
+ userInactivityShutdownDelay = 60 * 1000
32
37
} : LanguageClientProps ) : ReactElement | null {
33
38
const onErrorRef = useRef < ( error : Error ) => void > ( )
34
39
const onDidChangeStatusRef = useRef < ( status : StatusChangeEvent ) => void > ( )
@@ -38,6 +43,10 @@ function LanguageClient ({
38
43
const [ willShutdown , setWillShutdown ] = useState ( false )
39
44
const [ counter , setCounter ] = useState ( 1 )
40
45
46
+ const isUserInactive = useIsUserActive ( userInactivityDelay )
47
+ const shouldShutdownLanguageClient = useShouldShutdownLanguageClient ( isUserInactive , userInactivityShutdownDelay )
48
+ const restartAllowed = ! isUserInactive
49
+
41
50
useEffect ( ( ) => {
42
51
if ( willShutdown && restartAllowed ) {
43
52
console . info ( 'Restarting language client because the current instance will be shutdown' )
@@ -49,6 +58,10 @@ function LanguageClient ({
49
58
useEffect ( ( ) => {
50
59
setWillShutdown ( false )
51
60
61
+ if ( shouldShutdownLanguageClient ) {
62
+ return
63
+ }
64
+
52
65
console . info ( `Starting language server for language ${ id } ` )
53
66
const languageClient = createLanguageClientManager ( id , sessionId , languageServerUrl , getSecurityToken , libraryUrls , useMutualizedProxy )
54
67
languageClientRef . current = languageClient
@@ -82,7 +95,7 @@ function LanguageClient ({
82
95
console . error ( 'Unable to dispose language client' , err )
83
96
} )
84
97
}
85
- } , [ getSecurityToken , id , languageServerUrl , libraryUrls , sessionId , counter , useMutualizedProxy ] )
98
+ } , [ getSecurityToken , id , languageServerUrl , libraryUrls , sessionId , counter , useMutualizedProxy , shouldShutdownLanguageClient ] )
86
99
87
100
useEffect ( ( ) => {
88
101
onErrorRef . current = onError
0 commit comments