Implemented commandExecutor logic
This commit is contained in:
@@ -5,6 +5,7 @@ use std::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
engine::CommandExecutor,
|
||||
render::DrawContext,
|
||||
state::{use_effect, HookDependency},
|
||||
View, ViewWrapper,
|
||||
@@ -22,6 +23,7 @@ pub struct Context {
|
||||
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
|
||||
view: Mutex<View>,
|
||||
pub(crate) scopes: Mutex<Vec<Arc<Scope>>>,
|
||||
// executor: Arc<dyn CommandExecutor>,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
|
||||
@@ -87,4 +87,8 @@ impl<T: Engine> Engine for Benchmark<T> {
|
||||
fn render_delay(&self) {
|
||||
self.0.render_delay();
|
||||
}
|
||||
|
||||
fn executor(&self) -> Arc<dyn super::CommandExecutor> {
|
||||
self.0.executor()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
pub struct Stop;
|
||||
+50
-1
@@ -5,18 +5,31 @@ use std::{
|
||||
|
||||
use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
||||
|
||||
use crate::{prelude::Context, render::Area, DrawContext, View};
|
||||
use crate::{
|
||||
engine::{commands, CommandExecutor},
|
||||
prelude::Context,
|
||||
render::Area,
|
||||
DrawContext, View,
|
||||
};
|
||||
|
||||
use super::Engine;
|
||||
|
||||
pub struct ConsoleExecutor {
|
||||
running: Mutex<bool>,
|
||||
}
|
||||
|
||||
pub struct Console {
|
||||
threads: Mutex<Vec<Arc<dyn Fn(Arc<Context>) + Send + Sync>>>,
|
||||
executor: Arc<ConsoleExecutor>,
|
||||
}
|
||||
|
||||
impl Console {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
threads: Mutex::new(Vec::new()),
|
||||
executor: Arc::new(ConsoleExecutor {
|
||||
running: Mutex::new(true),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,4 +97,40 @@ impl Engine for Console {
|
||||
|
||||
cx
|
||||
}
|
||||
|
||||
fn executor(&self) -> Arc<dyn super::CommandExecutor> {
|
||||
self.executor.clone()
|
||||
}
|
||||
|
||||
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) {
|
||||
let cx = self.init(component);
|
||||
|
||||
while self.executor.is_running() {
|
||||
self.render(&cx);
|
||||
self.render_delay();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ConsoleExecutor {
|
||||
pub fn is_running(self: &Arc<ConsoleExecutor>) -> bool {
|
||||
*self.running.lock().unwrap()
|
||||
}
|
||||
|
||||
pub fn stop(&self) -> crate::Result<()> {
|
||||
*self.running.lock()? = false;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandExecutor for ConsoleExecutor {
|
||||
fn execute_command(&self, command: &Arc<dyn super::Command>) -> crate::Result<()> {
|
||||
let command = command.as_any();
|
||||
|
||||
if let Some(commands::Stop) = command.downcast_ref() {
|
||||
self.stop()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod benchmark;
|
||||
pub mod commands;
|
||||
pub mod console;
|
||||
|
||||
pub use benchmark::*;
|
||||
@@ -29,6 +30,7 @@ pub trait Engine {
|
||||
|
||||
fn render_view(&self, area: &Area, view: &View) -> DrawContext;
|
||||
fn draw_context(&self, ctx: &DrawContext);
|
||||
fn executor(&self) -> Arc<dyn CommandExecutor>;
|
||||
}
|
||||
|
||||
pub trait Command {
|
||||
|
||||
+9
-1
@@ -21,4 +21,12 @@ pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
|
||||
pub type ViewWrapper = Arc<dyn Fn(&mut DrawContext, View) + Send + Sync>;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub enum Error {}
|
||||
pub enum Error {
|
||||
PoisonError,
|
||||
}
|
||||
|
||||
impl From<std::sync::PoisonError<std::sync::MutexGuard<'_, bool>>> for Error {
|
||||
fn from(_value: std::sync::PoisonError<std::sync::MutexGuard<'_, bool>>) -> Self {
|
||||
Error::PoisonError
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user