Implemented exectution to Context

This commit is contained in:
2026-01-28 23:41:54 +01:00
parent 1df6cd4c5e
commit 93ba2f4a78
4 changed files with 22 additions and 14 deletions
+17 -11
View File
@@ -16,6 +16,7 @@ pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Any) + Send + Sy
pub struct Scope { pub struct Scope {
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>, pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
executor: Arc<dyn CommandExecutor>,
} }
pub struct Context { pub struct Context {
@@ -23,16 +24,20 @@ pub struct Context {
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>, event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
view: Mutex<View>, view: Mutex<View>,
pub(crate) scopes: Mutex<Vec<Arc<Scope>>>, pub(crate) scopes: Mutex<Vec<Arc<Scope>>>,
// executor: Arc<dyn CommandExecutor>, executor: Arc<dyn CommandExecutor>,
} }
impl Context { impl Context {
pub fn new<F: Fn(&Arc<Self>) -> View + Send + Sync + 'static>(component: F) -> Arc<Self> { pub fn new<F: Fn(&Arc<Self>) -> View + Send + Sync + 'static>(
component: F,
executor: Arc<dyn CommandExecutor>,
) -> Arc<Self> {
Arc::new(Self { Arc::new(Self {
component: Mutex::new(Arc::new(component)), component: Mutex::new(Arc::new(component)),
event_handlers: Mutex::new(HashMap::new()), event_handlers: Mutex::new(HashMap::new()),
view: Mutex::new(Arc::new(|_| {})), view: Mutex::new(Arc::new(|_| {})),
scopes: Mutex::new(Vec::new()), scopes: Mutex::new(Vec::new()),
executor,
}) })
} }
@@ -124,9 +129,7 @@ impl Context {
} }
pub fn scope(self: &Arc<Self>) -> Arc<Scope> { pub fn scope(self: &Arc<Self>) -> Arc<Scope> {
let scope = Arc::new(Scope { let scope = Scope::new(self.executor.clone());
children: Mutex::new(Vec::new()),
});
self.scopes.lock().unwrap().push(scope.clone()); self.scopes.lock().unwrap().push(scope.clone());
scope scope
@@ -137,9 +140,7 @@ impl Context {
drawer: F, drawer: F,
dependencies: &[&dyn HookDependency], dependencies: &[&dyn HookDependency],
) -> Arc<Scope> { ) -> Arc<Scope> {
let scope = Arc::new(Scope { let scope = Scope::new(self.executor.clone());
children: Mutex::new(Vec::new()),
});
self.scopes.lock().unwrap().push(scope.clone()); self.scopes.lock().unwrap().push(scope.clone());
drawer(&scope); drawer(&scope);
@@ -170,12 +171,17 @@ impl Context {
} }
} }
} }
pub fn get_executor(self: &Arc<Self>) -> Arc<dyn CommandExecutor> {
self.executor.clone()
}
} }
impl Scope { impl Scope {
pub fn new() -> Arc<Self> { pub fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self> {
Arc::new(Self { Arc::new(Self {
children: Mutex::new(Vec::new()), children: Mutex::new(Vec::new()),
executor,
}) })
} }
@@ -184,7 +190,7 @@ impl Scope {
child: F, child: F,
view_wrapper: Option<ViewWrapper>, view_wrapper: Option<ViewWrapper>,
) { ) {
let ctx = Context::new(child); let ctx = Context::new(child, self.executor.clone());
ctx.refresh(); ctx.refresh();
@@ -192,7 +198,7 @@ impl Scope {
} }
pub fn view(self: &Arc<Self>, view: View) { pub fn view(self: &Arc<Self>, view: View) {
let ctx = Context::new(move |_| view.clone()); let ctx = Context::new(move |_| view.clone(), self.executor.clone());
ctx.refresh(); ctx.refresh();
+1 -1
View File
@@ -83,7 +83,7 @@ impl Engine for Console {
&self, &self,
component: F, component: F,
) -> Arc<Context> { ) -> Arc<Context> {
let cx = Context::new(component); let cx = Context::new(component, self.executor.clone());
cx.refresh(); cx.refresh();
for thread in self.threads.lock().unwrap().iter() { for thread in self.threads.lock().unwrap().iter() {
+1 -1
View File
@@ -37,6 +37,6 @@ pub trait Command {
fn as_any(&self) -> &dyn Any; fn as_any(&self) -> &dyn Any;
} }
pub trait CommandExecutor { pub trait CommandExecutor: Send + Sync {
fn execute_command(&self, command: &Arc<dyn Command>) -> crate::Result<()>; fn execute_command(&self, command: &Arc<dyn Command>) -> crate::Result<()>;
} }
+3 -1
View File
@@ -35,10 +35,12 @@ impl Rsx {
} }
pub fn view(&self, context: Arc<Context>) -> View { pub fn view(&self, context: Arc<Context>) -> View {
let executor = context.get_executor();
for scope in &self.0 { for scope in &self.0 {
match scope { match scope {
RsxScope::Static(scope_fn) => { RsxScope::Static(scope_fn) => {
let scope = Scope::new(); let scope = Scope::new(executor.clone());
(scope_fn)(&scope); (scope_fn)(&scope);
context.scopes.lock().unwrap().push(scope) context.scopes.lock().unwrap().push(scope)
} }