@@ -2,6 +2,7 @@ import { ReactElement, useEffect, useRef, useState } from 'react'
2
2
import { createLanguageClientManager , LanguageClientId , StatusChangeEvent , LanguageClientManager , WillShutdownParams } from '@codingame/monaco-languageclient-wrapper'
3
3
import useIsUserActive from './hooks/useIsUserActive'
4
4
import useShouldShutdownLanguageClient from './hooks/useShouldShutdownLanguageClient'
5
+ import { useLastVersion } from './hooks/useLastVersion'
5
6
6
7
export interface LanguageClientProps {
7
8
id : LanguageClientId
@@ -22,22 +23,25 @@ export interface LanguageClientProps {
22
23
23
24
const defaultLibraryUrls : string [ ] = [ ]
24
25
26
+ const noop = ( ) => null
27
+
25
28
function LanguageClient ( {
26
29
id,
27
30
sessionId,
28
31
languageServerUrl,
29
32
useMutualizedProxy,
30
33
getSecurityToken,
31
34
libraryUrls = defaultLibraryUrls ,
32
- onError,
33
- onDidChangeStatus,
34
- onWillShutdown,
35
+ onError : _onError ,
36
+ onDidChangeStatus : _onDidChangeStatus ,
37
+ onWillShutdown : _onWillShutdown ,
35
38
userInactivityDelay = 30 * 1000 ,
36
39
userInactivityShutdownDelay = 60 * 1000
37
40
} : LanguageClientProps ) : ReactElement | null {
38
- const onErrorRef = useRef < ( error : Error ) => void > ( )
39
- const onDidChangeStatusRef = useRef < ( status : StatusChangeEvent ) => void > ( )
40
- const onWillShutdownRef = useRef < ( params : WillShutdownParams ) => void > ( )
41
+ const onError = useLastVersion ( _onError ?? noop )
42
+ const onDidChangeStatus = useLastVersion ( _onDidChangeStatus ?? noop )
43
+ const onWillShutdown = useLastVersion ( _onWillShutdown ?? noop )
44
+
41
45
const languageClientRef = useRef < LanguageClientManager > ( )
42
46
43
47
const [ willShutdown , setWillShutdown ] = useState ( false )
@@ -65,22 +69,12 @@ function LanguageClient ({
65
69
console . info ( `Starting language server for language ${ id } ` )
66
70
const languageClient = createLanguageClientManager ( id , sessionId , languageServerUrl , getSecurityToken , libraryUrls , useMutualizedProxy )
67
71
languageClientRef . current = languageClient
68
- const errorDisposable = languageClient . onError ( ( error : Error ) => {
69
- if ( onErrorRef . current != null ) {
70
- onErrorRef . current ( error )
71
- }
72
- } )
73
- const statusChangeDisposable = languageClient . onDidChangeStatus ( ( status : StatusChangeEvent ) => {
74
- if ( onDidChangeStatusRef . current != null ) {
75
- onDidChangeStatusRef . current ( status )
76
- }
77
- } )
72
+ const errorDisposable = languageClient . onError ( onError )
73
+ const statusChangeDisposable = languageClient . onDidChangeStatus ( onDidChangeStatus )
78
74
const startTimeout = setTimeout ( ( ) => languageClient . start ( ) )
79
75
80
76
languageClient . onWillShutdown ( ( params : WillShutdownParams ) => {
81
- if ( onWillShutdownRef . current != null ) {
82
- onWillShutdownRef . current ( params )
83
- }
77
+ onWillShutdown ( params )
84
78
setWillShutdown ( true )
85
79
} )
86
80
@@ -95,19 +89,7 @@ function LanguageClient ({
95
89
console . error ( 'Unable to dispose language client' , err )
96
90
} )
97
91
}
98
- } , [ getSecurityToken , id , languageServerUrl , libraryUrls , sessionId , counter , useMutualizedProxy , shouldShutdownLanguageClient ] )
99
-
100
- useEffect ( ( ) => {
101
- onErrorRef . current = onError
102
- } , [ onError ] )
103
-
104
- useEffect ( ( ) => {
105
- onDidChangeStatusRef . current = onDidChangeStatus
106
- } , [ onDidChangeStatus ] )
107
-
108
- useEffect ( ( ) => {
109
- onWillShutdownRef . current = onWillShutdown
110
- } , [ onWillShutdown ] )
92
+ } , [ getSecurityToken , id , languageServerUrl , libraryUrls , sessionId , counter , useMutualizedProxy , shouldShutdownLanguageClient , onError , onDidChangeStatus , onWillShutdown ] )
111
93
112
94
return null
113
95
}
0 commit comments