aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/mod.rs
diff options
context:
space:
mode:
authorKunal Mohan <kunalmohan99@gmail.com>2021-02-11 23:07:54 +0530
committerKunal Mohan <kunalmohan99@gmail.com>2021-05-04 20:47:48 +0530
commit685e2eef0ca340f48f5125a13918ca30656aabd5 (patch)
tree43a6486fa4041dd5acf2ee4d6069be3a545eeae2 /src/common/mod.rs
parentremove debug logs (diff)
downloadsl0ppij-685e2eef0ca340f48f5125a13918ca30656aabd5.tar.gz
sl0ppij-685e2eef0ca340f48f5125a13918ca30656aabd5.tar.xz
sl0ppij-685e2eef0ca340f48f5125a13918ca30656aabd5.zip
Introduce IpcSenderWithContext
Diffstat (limited to 'src/common/mod.rs')
-rw-r--r--src/common/mod.rs44
1 files changed, 36 insertions, 8 deletions
diff --git a/src/common/mod.rs b/src/common/mod.rs
index 61897df0..c1b14a0d 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -122,6 +122,37 @@ thread_local!(
/// stack in the form of an [`ErrorContext`].
static OPENCALLS: RefCell<ErrorContext> = RefCell::default()
);
+pub struct IpcSenderWithContext {
+ err_ctx: ErrorContext,
+ sender: UnixStream,
+}
+
+impl IpcSenderWithContext {
+ pub fn new() -> Self {
+ Self {
+ err_ctx: ErrorContext::new(),
+ sender: UnixStream::connect(ZELLIJ_IPC_PIPE).unwrap(),
+ }
+ }
+
+ pub fn update(&mut self, ctx: ErrorContext) {
+ self.err_ctx = ctx;
+ }
+
+ pub fn send(&mut self, msg: ApiCommand) -> std::io::Result<()> {
+ let command = bincode::serialize(&(self.err_ctx, msg)).unwrap();
+ self.sender.write_all(&command)
+ }
+}
+
+impl std::clone::Clone for IpcSenderWithContext {
+ fn clone(&self) -> Self {
+ Self {
+ err_ctx: self.err_ctx,
+ sender: UnixStream::connect(ZELLIJ_IPC_PIPE).unwrap(),
+ }
+ }
+}
task_local! {
/// A key to some task local storage that holds a representation of the task's call
@@ -512,7 +543,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
}
});
- let mut server_stream = UnixStream::connect(ZELLIJ_IPC_PIPE).unwrap();
+ let mut send_server_instructions = IpcSenderWithContext::new();
#[warn(clippy::never_loop)]
loop {
@@ -522,13 +553,13 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
err_ctx.add_call(ContextType::App(AppContext::from(&app_instruction)));
send_screen_instructions.update(err_ctx);
+ send_server_instructions.update(err_ctx);
match app_instruction {
AppInstruction::Exit => {
break;
}
AppInstruction::Error(backtrace) => {
- let api_command = bincode::serialize(&(err_ctx, ApiCommand::Quit)).unwrap();
- server_stream.write_all(&api_command).unwrap();
+ let _ = send_server_instructions.send(ApiCommand::Quit);
let _ = ipc_thread.join();
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
let _ = screen_thread.join();
@@ -554,15 +585,12 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
send_plugin_instructions.send(instruction).unwrap();
}
AppInstruction::ToPty(instruction) => {
- let api_command =
- bincode::serialize(&(err_ctx, ApiCommand::ToPty(instruction))).unwrap();
- server_stream.write_all(&api_command).unwrap();
+ let _ = send_server_instructions.send(ApiCommand::ToPty(instruction));
}
}
}
- let api_command = bincode::serialize(&(err_ctx, ApiCommand::Quit)).unwrap();
- server_stream.write_all(&api_command).unwrap();
+ let _ = send_server_instructions.send(ApiCommand::Quit);
let _ = ipc_thread.join().unwrap();
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
screen_thread.join().unwrap();