Skip to content

Commit 73c47af

Browse files
authored
Merge pull request #8 from CodinGame/add-dynamic-options
Add dynamic options and simplify status
2 parents 69dde23 + 2c24fcf commit 73c47af

File tree

5 files changed

+54
-51
lines changed

5 files changed

+54
-51
lines changed

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codingame/monaco-languageclient-react",
3-
"version": "1.5.0",
3+
"version": "1.6.1",
44
"description": "Monaco Editor React component",
55
"scripts": {
66
"build": "tsc",
@@ -19,7 +19,7 @@
1919
],
2020
"types": "dist/index.d.ts",
2121
"dependencies": {
22-
"@codingame/monaco-languageclient-wrapper": "^1.6.0",
22+
"@codingame/monaco-languageclient-wrapper": "^1.7.0",
2323
"activity-detector": "^3.0.0",
2424
"react": ">=16.0.0"
2525
},

src/LanguageClient.tsx

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { ReactElement, useEffect, useRef, useState } from 'react'
2-
import { createLanguageClientManager, LanguageClientId, StatusChangeEvent, LanguageClientManager, WillShutdownParams } from '@codingame/monaco-languageclient-wrapper'
2+
import { createLanguageClientManager, LanguageClientId, StatusChangeEvent as WrapperStatusChangeEvent, 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'
6+
7+
export interface StatusChangeEvent {
8+
status: WrapperStatusChangeEvent['status'] | 'inactivityShutdown'
9+
}
510

611
export interface LanguageClientProps {
712
id: LanguageClientId
@@ -22,29 +27,33 @@ export interface LanguageClientProps {
2227

2328
const defaultLibraryUrls: string[] = []
2429

30+
const noop = () => null
31+
2532
function LanguageClient ({
2633
id,
2734
sessionId,
2835
languageServerUrl,
2936
useMutualizedProxy,
30-
getSecurityToken,
37+
getSecurityToken: _getSecurityToken,
3138
libraryUrls = defaultLibraryUrls,
32-
onError,
33-
onDidChangeStatus,
34-
onWillShutdown,
39+
onError: _onError,
40+
onDidChangeStatus: _onDidChangeStatus,
41+
onWillShutdown: _onWillShutdown,
3542
userInactivityDelay = 30 * 1000,
3643
userInactivityShutdownDelay = 60 * 1000
3744
}: LanguageClientProps): ReactElement | null {
38-
const onErrorRef = useRef<(error: Error) => void>()
39-
const onDidChangeStatusRef = useRef<(status: StatusChangeEvent) => void>()
40-
const onWillShutdownRef = useRef<(params: WillShutdownParams) => void>()
45+
const getSecurityToken = useLastVersion(_getSecurityToken)
46+
const onError = useLastVersion(_onError ?? noop)
47+
const onDidChangeStatus = useLastVersion(_onDidChangeStatus ?? noop)
48+
const onWillShutdown = useLastVersion(_onWillShutdown ?? noop)
49+
4150
const languageClientRef = useRef<LanguageClientManager>()
4251

4352
const [willShutdown, setWillShutdown] = useState(false)
4453
const [counter, setCounter] = useState(1)
4554

4655
const isUserInactive = useIsUserActive(userInactivityDelay)
47-
const shouldShutdownLanguageClient = useShouldShutdownLanguageClient(isUserInactive, userInactivityShutdownDelay)
56+
const shouldShutdownLanguageClientForInactivity = useShouldShutdownLanguageClient(isUserInactive, userInactivityShutdownDelay)
4857
const restartAllowed = !isUserInactive
4958

5059
useEffect(() => {
@@ -58,29 +67,22 @@ function LanguageClient ({
5867
useEffect(() => {
5968
setWillShutdown(false)
6069

61-
if (shouldShutdownLanguageClient) {
70+
if (shouldShutdownLanguageClientForInactivity) {
71+
onDidChangeStatus({
72+
status: 'inactivityShutdown'
73+
})
6274
return
6375
}
6476

6577
console.info(`Starting language server for language ${id}`)
6678
const languageClient = createLanguageClientManager(id, sessionId, languageServerUrl, getSecurityToken, libraryUrls, useMutualizedProxy)
6779
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-
})
80+
const errorDisposable = languageClient.onError(onError)
81+
const statusChangeDisposable = languageClient.onDidChangeStatus(onDidChangeStatus)
7882
const startTimeout = setTimeout(() => languageClient.start())
7983

8084
languageClient.onWillShutdown((params: WillShutdownParams) => {
81-
if (onWillShutdownRef.current != null) {
82-
onWillShutdownRef.current(params)
83-
}
85+
onWillShutdown(params)
8486
setWillShutdown(true)
8587
})
8688

@@ -95,19 +97,7 @@ function LanguageClient ({
9597
console.error('Unable to dispose language client', err)
9698
})
9799
}
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])
100+
}, [getSecurityToken, id, languageServerUrl, libraryUrls, sessionId, counter, useMutualizedProxy, shouldShutdownLanguageClientForInactivity, onError, onDidChangeStatus, onWillShutdown])
111101

112102
return null
113103
}

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+
}

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { LanguageClientId, loadExtensionConfigurations } from '@codingame/monaco-languageclient-wrapper'
2-
import LanguageClient, { LanguageClientProps } from './LanguageClient'
1+
import { LanguageClientId, loadExtensionConfigurations, registerLanguageClient } from '@codingame/monaco-languageclient-wrapper'
2+
import LanguageClient, { LanguageClientProps, StatusChangeEvent } from './LanguageClient'
33

44
export default LanguageClient
55
export {
6-
loadExtensionConfigurations
6+
loadExtensionConfigurations,
7+
registerLanguageClient
78
}
89
export type {
910
LanguageClientProps,
10-
LanguageClientId
11+
LanguageClientId,
12+
StatusChangeEvent
1113
}

0 commit comments

Comments
 (0)