mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-09 21:26:14 +03:00
Implement Server::add_request_handler in terms of ::add_message_handler
This commit is contained in:
parent
1aff42302c
commit
68917c78be
@ -121,36 +121,27 @@ impl Server {
|
||||
Fut: 'static + Send + Future<Output = tide::Result<M::Response>>,
|
||||
M: RequestMessage,
|
||||
{
|
||||
let prev_handler = self.handlers.insert(
|
||||
TypeId::of::<M>(),
|
||||
Box::new(move |server, envelope| {
|
||||
let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
|
||||
let receipt = envelope.receipt();
|
||||
let response = (handler)(server.clone(), *envelope);
|
||||
async move {
|
||||
match response.await {
|
||||
Ok(response) => {
|
||||
server.peer.respond(receipt, response)?;
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => {
|
||||
server.peer.respond_with_error(
|
||||
receipt,
|
||||
proto::Error {
|
||||
message: error.to_string(),
|
||||
},
|
||||
)?;
|
||||
Err(error)
|
||||
}
|
||||
self.add_message_handler(move |server, envelope| {
|
||||
let receipt = envelope.receipt();
|
||||
let response = (handler)(server.clone(), envelope);
|
||||
async move {
|
||||
match response.await {
|
||||
Ok(response) => {
|
||||
server.peer.respond(receipt, response)?;
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => {
|
||||
server.peer.respond_with_error(
|
||||
receipt,
|
||||
proto::Error {
|
||||
message: error.to_string(),
|
||||
},
|
||||
)?;
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
.boxed()
|
||||
}),
|
||||
);
|
||||
if prev_handler.is_some() {
|
||||
panic!("registered a handler for the same message twice");
|
||||
}
|
||||
self
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn handle_connection(
|
||||
|
Loading…
Reference in New Issue
Block a user