Skip to content

Commit 425b73c

Browse files
committed
refactor: Factorize code using a hook
1 parent 30043c7 commit 425b73c

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

src/LanguageClient.tsx

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ReactElement, useEffect, useRef, useState } from 'react'
22
import { createLanguageClientManager, LanguageClientId, StatusChangeEvent, LanguageClientManager, WillShutdownParams } from '@codingame/monaco-languageclient-wrapper'
33
import useIsUserActive from './hooks/useIsUserActive'
44
import useShouldShutdownLanguageClient from './hooks/useShouldShutdownLanguageClient'
5+
import { useLastVersion } from './hooks/useLastVersion'
56

67
export interface LanguageClientProps {
78
id: LanguageClientId
@@ -22,22 +23,25 @@ export interface LanguageClientProps {
2223

2324
const defaultLibraryUrls: string[] = []
2425

26+
const noop = () => null
27+
2528
function LanguageClient ({
2629
id,
2730
sessionId,
2831
languageServerUrl,
2932
useMutualizedProxy,
3033
getSecurityToken,
3134
libraryUrls = defaultLibraryUrls,
32-
onError,
33-
onDidChangeStatus,
34-
onWillShutdown,
35+
onError: _onError,
36+
onDidChangeStatus: _onDidChangeStatus,
37+
onWillShutdown: _onWillShutdown,
3538
userInactivityDelay = 30 * 1000,
3639
userInactivityShutdownDelay = 60 * 1000
3740
}: 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+
4145
const languageClientRef = useRef<LanguageClientManager>()
4246

4347
const [willShutdown, setWillShutdown] = useState(false)
@@ -65,22 +69,12 @@ function LanguageClient ({
6569
console.info(`Starting language server for language ${id}`)
6670
const languageClient = createLanguageClientManager(id, sessionId, languageServerUrl, getSecurityToken, libraryUrls, useMutualizedProxy)
6771
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)
7874
const startTimeout = setTimeout(() => languageClient.start())
7975

8076
languageClient.onWillShutdown((params: WillShutdownParams) => {
81-
if (onWillShutdownRef.current != null) {
82-
onWillShutdownRef.current(params)
83-
}
77+
onWillShutdown(params)
8478
setWillShutdown(true)
8579
})
8680

@@ -95,19 +89,7 @@ function LanguageClient ({
9589
console.error('Unable to dispose language client', err)
9690
})
9791
}
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])
11193

11294
return null
11395
}

src/hooks/useLastVersion.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useCallback, useEffect, useRef } from 'react'
2+
3+
export function useLastVersion<P extends unknown[], R> (func: (...args: P) => R): (...args: P) => R {
4+
const ref = useRef<(...args: P) => R>(func)
5+
useEffect(() => {
6+
ref.current = func
7+
}, [func])
8+
return useCallback((...args: P) => {
9+
return ref.current(...args)
10+
}, [])
11+
}

0 commit comments

Comments
 (0)