-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathSafeInitializingServer.swift
More file actions
62 lines (51 loc) · 1.65 KB
/
SafeInitializingServer.swift
File metadata and controls
62 lines (51 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import LanguageClient
import LanguageServerProtocol
public actor SafeInitializingServer {
private let underlying: InitializingServer
private var initTask: Task<InitializationResponse, Error>? = nil
public init(_ server: InitializingServer) {
self.underlying = server
}
// Ensure initialize request is sent by once
public func initializeIfNeeded() async throws -> InitializationResponse {
if let task = initTask {
return try await task.value
}
let task = Task {
try await underlying.initializeIfNeeded()
}
initTask = task
do {
let result = try await task.value
return result
} catch {
// Retryable failure
initTask = nil
throw error
}
}
public func shutdownAndExit() async throws {
try await underlying.shutdownAndExit()
}
public func sendNotification(_ notif: ClientNotification) async throws {
_ = try await initializeIfNeeded()
try await underlying.sendNotification(notif)
}
public func sendRequest<Response: Decodable & Sendable>(_ request: ClientRequest) async throws -> Response {
_ = try await initializeIfNeeded()
return try await underlying.sendRequest(request)
}
public var capabilities: ServerCapabilities? {
get async {
await underlying.capabilities
}
}
public var serverInfo: ServerInfo? {
get async {
await underlying.serverInfo
}
}
public nonisolated var eventSequence: ServerConnection.EventSequence {
underlying.eventSequence
}
}