-
Notifications
You must be signed in to change notification settings - Fork 27
Serialized event handling #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
* Check what we're doing with outgoing request parameters, there are 2 allocations going on in call_args! and rpc_args!, and we're just reading it in the end. | ||
|
||
* Can we use the non-generic `split` methods from tokio for unixstream, tcpstream? Supposedly better performance, but introduces lifetimes... | ||
|
||
* Propogate errors from `model::encode()` in `handler_loop()` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
use nvim_rs::{ | ||
compat::tokio::Compat, | ||
create::tokio as create, | ||
neovim::Neovim, | ||
Handler, | ||
}; | ||
|
||
use async_trait::async_trait; | ||
|
||
use rmpv::Value; | ||
|
||
use std::{ | ||
sync::Arc, | ||
path::Path, | ||
}; | ||
|
||
use tokio::{ | ||
self, | ||
process::{ChildStdin, Command}, | ||
sync::Mutex, | ||
spawn | ||
}; | ||
|
||
const NVIM_BIN: &str = if cfg!(windows) { | ||
"nvim.exe" | ||
} else { | ||
"nvim" | ||
}; | ||
const NVIM_PATH: &str = if cfg!(windows) { | ||
"neovim/build/bin/nvim.exe" | ||
} else { | ||
"neovim/build/bin/nvim" | ||
}; | ||
|
||
#[derive(Clone)] | ||
struct NeovimHandler { | ||
froodle: Arc<Mutex<String>>, | ||
} | ||
|
||
#[async_trait] | ||
impl Handler for NeovimHandler { | ||
type Writer = Compat<ChildStdin>; | ||
|
||
async fn handle_request( | ||
&self, | ||
name: String, | ||
args: Vec<Value>, | ||
neovim: Neovim<Compat<ChildStdin>>, | ||
) -> Result<Value, Value> { | ||
match name.as_ref() { | ||
"dummy" => Ok(Value::from("o")), | ||
"req" => { | ||
let v = args[0].as_str().unwrap(); | ||
|
||
let neovim = neovim.clone(); | ||
match v { | ||
"y" => { | ||
let mut x: String = neovim | ||
.get_vvar("progname") | ||
.await | ||
.unwrap() | ||
.as_str() | ||
.unwrap() | ||
.into(); | ||
x.push_str(" - "); | ||
x.push_str( | ||
neovim.get_var("oogle").await.unwrap().as_str().unwrap(), | ||
); | ||
x.push_str(" - "); | ||
x.push_str( | ||
neovim | ||
.eval("rpcrequest(1,'dummy')") | ||
.await | ||
.unwrap() | ||
.as_str() | ||
.unwrap(), | ||
); | ||
x.push_str(" - "); | ||
x.push_str( | ||
neovim | ||
.eval("rpcrequest(1,'req', 'z')") | ||
.await | ||
.unwrap() | ||
.as_str() | ||
.unwrap(), | ||
); | ||
Ok(Value::from(x)) | ||
} | ||
"z" => { | ||
let x: String = neovim | ||
.get_vvar("progname") | ||
.await | ||
.unwrap() | ||
.as_str() | ||
.unwrap() | ||
.into(); | ||
Ok(Value::from(x)) | ||
} | ||
&_ => Err(Value::from("wrong argument to req")), | ||
} | ||
} | ||
&_ => Err(Value::from("wrong method name for request")), | ||
} | ||
} | ||
|
||
async fn handle_notify( | ||
&self, | ||
name: String, | ||
args: Vec<Value>, | ||
_neovim: Neovim<Compat<ChildStdin>>, | ||
) { | ||
match name.as_ref() { | ||
"set_froodle" => { | ||
*self.froodle.lock().await = args[0].as_str().unwrap().to_string() | ||
} | ||
_ => {} | ||
}; | ||
} | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() { | ||
let rs = r#"exe ":fun M(timer) | ||
call rpcnotify(1, 'set_froodle', rpcrequest(1, 'req', 'y')) | ||
endfun""#; | ||
let rs2 = r#"exe ":fun N(timer) | ||
call chanclose(1) | ||
endfun""#; | ||
|
||
let froodle = Arc::new(Mutex::new(String::new())); | ||
let handler = NeovimHandler { | ||
froodle: froodle.clone(), | ||
}; | ||
|
||
let path = if Path::new(NVIM_PATH).exists() { | ||
NVIM_PATH | ||
} else { | ||
NVIM_BIN | ||
}; | ||
let (nvim, io, _child) = create::new_child_cmd( | ||
Command::new(path).args(&[ | ||
"-u", | ||
"NONE", | ||
"--embed", | ||
"--headless", | ||
"-c", | ||
rs, | ||
"-c", | ||
":let timer = timer_start(500, 'M')", | ||
"-c", | ||
rs2, | ||
"-c", | ||
":let timer = timer_start(1500, 'N')", | ||
]), | ||
handler, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let nv = nvim.clone(); | ||
spawn(async move { nv.set_var("oogle", Value::from("doodle")).await }); | ||
|
||
// The 2nd timer closes the channel, which will be returned as an error from | ||
// the io handler. We only fail the test if we got another error | ||
if let Err(err) = io.await.unwrap() { | ||
if !err.is_channel_closed() { | ||
panic!("Error in io: '{:?}'", err); | ||
} | ||
} | ||
|
||
assert_eq!( | ||
format!("{nvim} - doodle - o - {nvim}", nvim = NVIM_BIN), | ||
*froodle.lock().await | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.