Skip to content

Add dynamic options and simplify status #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codingame/monaco-languageclient-react",
"version": "1.5.0",
"version": "1.6.1",
"description": "Monaco Editor React component",
"scripts": {
"build": "tsc",
Expand All @@ -19,7 +19,7 @@
],
"types": "dist/index.d.ts",
"dependencies": {
"@codingame/monaco-languageclient-wrapper": "^1.6.0",
"@codingame/monaco-languageclient-wrapper": "^1.7.0",
"activity-detector": "^3.0.0",
"react": ">=16.0.0"
},
Expand Down
62 changes: 26 additions & 36 deletions src/LanguageClient.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ReactElement, useEffect, useRef, useState } from 'react'
import { createLanguageClientManager, LanguageClientId, StatusChangeEvent, LanguageClientManager, WillShutdownParams } from '@codingame/monaco-languageclient-wrapper'
import { createLanguageClientManager, LanguageClientId, StatusChangeEvent as WrapperStatusChangeEvent, LanguageClientManager, WillShutdownParams } from '@codingame/monaco-languageclient-wrapper'
import useIsUserActive from './hooks/useIsUserActive'
import useShouldShutdownLanguageClient from './hooks/useShouldShutdownLanguageClient'
import { useLastVersion } from './hooks/useLastVersion'

export interface StatusChangeEvent {
status: WrapperStatusChangeEvent['status'] | 'inactivityShutdown'
}

export interface LanguageClientProps {
id: LanguageClientId
Expand All @@ -22,29 +27,33 @@ export interface LanguageClientProps {

const defaultLibraryUrls: string[] = []

const noop = () => null

function LanguageClient ({
id,
sessionId,
languageServerUrl,
useMutualizedProxy,
getSecurityToken,
getSecurityToken: _getSecurityToken,
libraryUrls = defaultLibraryUrls,
onError,
onDidChangeStatus,
onWillShutdown,
onError: _onError,
onDidChangeStatus: _onDidChangeStatus,
onWillShutdown: _onWillShutdown,
userInactivityDelay = 30 * 1000,
userInactivityShutdownDelay = 60 * 1000
}: LanguageClientProps): ReactElement | null {
const onErrorRef = useRef<(error: Error) => void>()
const onDidChangeStatusRef = useRef<(status: StatusChangeEvent) => void>()
const onWillShutdownRef = useRef<(params: WillShutdownParams) => void>()
const getSecurityToken = useLastVersion(_getSecurityToken)
const onError = useLastVersion(_onError ?? noop)
const onDidChangeStatus = useLastVersion(_onDidChangeStatus ?? noop)
const onWillShutdown = useLastVersion(_onWillShutdown ?? noop)

const languageClientRef = useRef<LanguageClientManager>()

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

const isUserInactive = useIsUserActive(userInactivityDelay)
const shouldShutdownLanguageClient = useShouldShutdownLanguageClient(isUserInactive, userInactivityShutdownDelay)
const shouldShutdownLanguageClientForInactivity = useShouldShutdownLanguageClient(isUserInactive, userInactivityShutdownDelay)
const restartAllowed = !isUserInactive

useEffect(() => {
Expand All @@ -58,29 +67,22 @@ function LanguageClient ({
useEffect(() => {
setWillShutdown(false)

if (shouldShutdownLanguageClient) {
if (shouldShutdownLanguageClientForInactivity) {
onDidChangeStatus({
status: 'inactivityShutdown'
})
return
}

console.info(`Starting language server for language ${id}`)
const languageClient = createLanguageClientManager(id, sessionId, languageServerUrl, getSecurityToken, libraryUrls, useMutualizedProxy)
languageClientRef.current = languageClient
const errorDisposable = languageClient.onError((error: Error) => {
if (onErrorRef.current != null) {
onErrorRef.current(error)
}
})
const statusChangeDisposable = languageClient.onDidChangeStatus((status: StatusChangeEvent) => {
if (onDidChangeStatusRef.current != null) {
onDidChangeStatusRef.current(status)
}
})
const errorDisposable = languageClient.onError(onError)
const statusChangeDisposable = languageClient.onDidChangeStatus(onDidChangeStatus)
const startTimeout = setTimeout(() => languageClient.start())

languageClient.onWillShutdown((params: WillShutdownParams) => {
if (onWillShutdownRef.current != null) {
onWillShutdownRef.current(params)
}
onWillShutdown(params)
setWillShutdown(true)
})

Expand All @@ -95,19 +97,7 @@ function LanguageClient ({
console.error('Unable to dispose language client', err)
})
}
}, [getSecurityToken, id, languageServerUrl, libraryUrls, sessionId, counter, useMutualizedProxy, shouldShutdownLanguageClient])

useEffect(() => {
onErrorRef.current = onError
}, [onError])

useEffect(() => {
onDidChangeStatusRef.current = onDidChangeStatus
}, [onDidChangeStatus])

useEffect(() => {
onWillShutdownRef.current = onWillShutdown
}, [onWillShutdown])
}, [getSecurityToken, id, languageServerUrl, libraryUrls, sessionId, counter, useMutualizedProxy, shouldShutdownLanguageClientForInactivity, onError, onDidChangeStatus, onWillShutdown])

return null
}
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/useLastVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useCallback, useEffect, useRef } from 'react'

export function useLastVersion<P extends unknown[], R> (func: (...args: P) => R): (...args: P) => R {
const ref = useRef<(...args: P) => R>(func)
useEffect(() => {
ref.current = func
}, [func])
return useCallback((...args: P) => {
return ref.current(...args)
}, [])
}
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { LanguageClientId, loadExtensionConfigurations } from '@codingame/monaco-languageclient-wrapper'
import LanguageClient, { LanguageClientProps } from './LanguageClient'
import { LanguageClientId, loadExtensionConfigurations, registerLanguageClient } from '@codingame/monaco-languageclient-wrapper'
import LanguageClient, { LanguageClientProps, StatusChangeEvent } from './LanguageClient'

export default LanguageClient
export {
loadExtensionConfigurations
loadExtensionConfigurations,
registerLanguageClient
}
export type {
LanguageClientProps,
LanguageClientId
LanguageClientId,
StatusChangeEvent
}