|
| 1 | +// Copyright (c) 2025 Spotify AB. |
| 2 | +// |
| 3 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +// or more contributor license agreements. See the NOTICE file |
| 5 | +// distributed with this work for additional information |
| 6 | +// regarding copyright ownership. The ASF licenses this file |
| 7 | +// to you under the Apache License, Version 2.0 (the |
| 8 | +// "License"); you may not use this file except in compliance |
| 9 | +// with the License. You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, |
| 14 | +// software distributed under the License is distributed on an |
| 15 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +// KIND, either express or implied. See the License for the |
| 17 | +// specific language governing permissions and limitations |
| 18 | +// under the License. |
| 19 | + |
| 20 | +import BuildServerProtocol |
| 21 | +import Foundation |
| 22 | +import LanguageServerProtocol |
| 23 | + |
| 24 | +/// Base object that forwards BSP messages to a separate queue. |
| 25 | +/// This exists simply to make sure we can continue to receive messages |
| 26 | +/// as we process them. Otherwise, each request would require its own queue. |
| 27 | +final class AsyncMessageHandler: MessageHandler { |
| 28 | + |
| 29 | + private let queue = DispatchQueue(label: "AsyncMessageHandler", qos: .userInitiated) |
| 30 | + private let messageHandler: MessageHandler |
| 31 | + |
| 32 | + init(wrapping handler: MessageHandler) { |
| 33 | + self.messageHandler = handler |
| 34 | + } |
| 35 | + |
| 36 | + func handle<Notification: NotificationType>(_ notification: Notification) { |
| 37 | + queue.async { [weak self] in |
| 38 | + self?.messageHandler.handle(notification) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + func handle<Request: RequestType>( |
| 43 | + _ request: Request, |
| 44 | + id: RequestID, |
| 45 | + reply: @escaping @Sendable (LSPResult<Request.Response>) -> Void |
| 46 | + ) { |
| 47 | + queue.async { [weak self] in |
| 48 | + self?.messageHandler.handle(request, id: id, reply: reply) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments