Implemented exectution to Context
This commit is contained in:
+17
-11
@@ -16,6 +16,7 @@ pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Any) + Send + Sy
|
||||
|
||||
pub struct Scope {
|
||||
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
|
||||
executor: Arc<dyn CommandExecutor>,
|
||||
}
|
||||
|
||||
pub struct Context {
|
||||
@@ -23,16 +24,20 @@ pub struct Context {
|
||||
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
|
||||
view: Mutex<View>,
|
||||
pub(crate) scopes: Mutex<Vec<Arc<Scope>>>,
|
||||
// executor: Arc<dyn CommandExecutor>,
|
||||
executor: Arc<dyn CommandExecutor>,
|
||||
}
|
||||
|
||||
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 {
|
||||
component: Mutex::new(Arc::new(component)),
|
||||
event_handlers: Mutex::new(HashMap::new()),
|
||||
view: Mutex::new(Arc::new(|_| {})),
|
||||
scopes: Mutex::new(Vec::new()),
|
||||
executor,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -124,9 +129,7 @@ impl Context {
|
||||
}
|
||||
|
||||
pub fn scope(self: &Arc<Self>) -> Arc<Scope> {
|
||||
let scope = Arc::new(Scope {
|
||||
children: Mutex::new(Vec::new()),
|
||||
});
|
||||
let scope = Scope::new(self.executor.clone());
|
||||
self.scopes.lock().unwrap().push(scope.clone());
|
||||
|
||||
scope
|
||||
@@ -137,9 +140,7 @@ impl Context {
|
||||
drawer: F,
|
||||
dependencies: &[&dyn HookDependency],
|
||||
) -> Arc<Scope> {
|
||||
let scope = Arc::new(Scope {
|
||||
children: Mutex::new(Vec::new()),
|
||||
});
|
||||
let scope = Scope::new(self.executor.clone());
|
||||
self.scopes.lock().unwrap().push(scope.clone());
|
||||
|
||||
drawer(&scope);
|
||||
@@ -170,12 +171,17 @@ impl Context {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_executor(self: &Arc<Self>) -> Arc<dyn CommandExecutor> {
|
||||
self.executor.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
pub fn new() -> Arc<Self> {
|
||||
pub fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
children: Mutex::new(Vec::new()),
|
||||
executor,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -184,7 +190,7 @@ impl Scope {
|
||||
child: F,
|
||||
view_wrapper: Option<ViewWrapper>,
|
||||
) {
|
||||
let ctx = Context::new(child);
|
||||
let ctx = Context::new(child, self.executor.clone());
|
||||
|
||||
ctx.refresh();
|
||||
|
||||
@@ -192,7 +198,7 @@ impl Scope {
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ impl Engine for Console {
|
||||
&self,
|
||||
component: F,
|
||||
) -> Arc<Context> {
|
||||
let cx = Context::new(component);
|
||||
let cx = Context::new(component, self.executor.clone());
|
||||
cx.refresh();
|
||||
|
||||
for thread in self.threads.lock().unwrap().iter() {
|
||||
|
||||
+1
-1
@@ -37,6 +37,6 @@ pub trait Command {
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
pub trait CommandExecutor {
|
||||
pub trait CommandExecutor: Send + Sync {
|
||||
fn execute_command(&self, command: &Arc<dyn Command>) -> crate::Result<()>;
|
||||
}
|
||||
|
||||
+3
-1
@@ -35,10 +35,12 @@ impl Rsx {
|
||||
}
|
||||
|
||||
pub fn view(&self, context: Arc<Context>) -> View {
|
||||
let executor = context.get_executor();
|
||||
|
||||
for scope in &self.0 {
|
||||
match scope {
|
||||
RsxScope::Static(scope_fn) => {
|
||||
let scope = Scope::new();
|
||||
let scope = Scope::new(executor.clone());
|
||||
(scope_fn)(&scope);
|
||||
context.scopes.lock().unwrap().push(scope)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user