Skip to content

Commit

Permalink
Merge pull request #210 from posit-dev/feature/connection-disconnect
Browse files Browse the repository at this point in the history
Handle connection pane disconnect button
  • Loading branch information
dfalbel authored Jan 30, 2024
2 parents fe0381e + 1bc65a8 commit b83d715
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/amalthea/src/comm/comm_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crossbeam::channel::Select;
use crossbeam::channel::Sender;
use log::info;
use log::warn;
use stdext::result::ResultOrLog;
use stdext::spawn;

use crate::comm::comm_channel::CommMsg;
Expand Down Expand Up @@ -175,6 +176,12 @@ impl CommManager {

// If we found it, remove it.
if let Some(index) = index {
// Notify the comm that it's been closed
let comm = self.open_comms.get(index).unwrap();
comm.incoming_tx
.send(CommMsg::Close)
.or_log_error("Failed to send comm_close to comm.");

self.open_comms.remove(index);
self.comm_shell_tx
.send(CommShellEvent::Removed(comm_id))
Expand Down
11 changes: 11 additions & 0 deletions crates/ark/src/connections/r_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ impl RConnection {
}
}

fn disconnect(&self) -> std::result::Result<(), anyhow::Error> {
// Execute database side disconnect method.
r_task(|| -> Result<(), anyhow::Error> {
let mut call = RFunction::from(".ps.connection_close");
call.add(RObject::from(self.comm.comm_id.clone()));
call.call()?;
Ok(())
})
}

fn handle_messages(&self) -> Result<(), anyhow::Error> {
loop {
let msg = unwrap!(self.comm.incoming_rx.recv(), Err(err) => {
Expand All @@ -223,6 +233,7 @@ impl RConnection {

if let CommMsg::Close = msg {
log::trace!("Connection Pane: Received a close message.");
self.disconnect()?;
break;
}

Expand Down
10 changes: 10 additions & 0 deletions crates/ark/src/modules/positron/connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,13 @@ options("connectionObserver" = .ps.connection_observer())
}
View(con$previewObject(table = table, ..., limit = 100), title = table)
}

.ps.connection_close <- function(id, ...) {
con <- get(id, getOption("connectionObserver")$.connections)
if (is.null(con)) {
return(NULL)
}
# disconnect is resposible for calling connectionClosed that
# will remove the connection from the list of connections
con$disconnect(...)
}

0 comments on commit b83d715

Please sign in to comment.