partway towards commands
I got very confused and started mucking about with "spawn" when in fact all I needed was the "inline" extension type in orcx that allows the interpreter to expose custom constants.
This commit is contained in:
@@ -11,22 +11,50 @@ use crate::ext_port::ExtPort;
|
||||
/// shutdown.
|
||||
#[cfg(feature = "tokio")]
|
||||
pub async fn tokio_entrypoint(builder: ExtensionBuilder) {
|
||||
use std::cell::RefCell;
|
||||
|
||||
use async_event::Event;
|
||||
use tokio::io::{stderr, stdin, stdout};
|
||||
use tokio::task::{LocalSet, spawn_local};
|
||||
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};
|
||||
|
||||
let local_set = LocalSet::new();
|
||||
local_set.spawn_local(async {
|
||||
builder.build(ExtPort {
|
||||
input: Box::pin(stdin().compat()),
|
||||
output: Box::pin(stdout().compat_write()),
|
||||
log: Box::pin(stderr().compat_write()),
|
||||
spawn: Rc::new(|fut| {
|
||||
spawn_local(fut);
|
||||
}),
|
||||
});
|
||||
});
|
||||
local_set.await;
|
||||
let cc = Rc::new(Event::new());
|
||||
let c = Rc::new(RefCell::new(1));
|
||||
LocalSet::new()
|
||||
.run_until(async {
|
||||
let counter = (c.clone(), cc.clone());
|
||||
builder
|
||||
.run(ExtPort {
|
||||
input: Box::pin(stdin().compat()),
|
||||
output: Box::pin(stdout().compat_write()),
|
||||
log: Box::pin(stderr().compat_write()),
|
||||
spawn: Rc::new(move |delay, fut| {
|
||||
let (c, cc) = counter.clone();
|
||||
if delay.is_zero() {
|
||||
*c.borrow_mut() += 1;
|
||||
cc.notify_all();
|
||||
spawn_local(async move {
|
||||
fut.await;
|
||||
*c.borrow_mut() -= 1;
|
||||
cc.notify_all();
|
||||
});
|
||||
} else {
|
||||
let at = tokio::time::Instant::now() + delay;
|
||||
spawn_local(async move {
|
||||
tokio::time::sleep_until(at).await;
|
||||
*c.borrow_mut() += 1;
|
||||
cc.notify_all();
|
||||
fut.await;
|
||||
*c.borrow_mut() -= 1;
|
||||
cc.notify_all();
|
||||
});
|
||||
}
|
||||
}),
|
||||
})
|
||||
.await;
|
||||
cc.wait_until(|| (*c.borrow() == 0).then_some(())).await;
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
|
||||
Reference in New Issue
Block a user