#![allow(unused_qualifications)]
use std::fmt::Debug;
use crate::channel::{AsyncComponentSender, Sender};
use crate::loading_widgets::LoadingWidgets;
use super::{AsyncComponentBuilder, AsyncComponentParts};
pub trait AsyncComponent: Sized + 'static {
type CommandOutput: Debug + Send + 'static;
type Input: Debug + 'static;
type Output: Debug + 'static;
type Init;
type Root: Debug + Clone;
type Widgets: 'static;
#[must_use]
fn builder() -> AsyncComponentBuilder<Self> {
AsyncComponentBuilder::<Self>::default()
}
#[must_use]
fn init_root() -> Self::Root;
#[must_use]
fn init_loading_widgets(_root: Self::Root) -> Option<LoadingWidgets> {
None
}
fn init(
init: Self::Init,
root: Self::Root,
sender: AsyncComponentSender<Self>,
) -> impl std::future::Future<Output = AsyncComponentParts<Self>>;
#[allow(unused)]
fn update(
&mut self,
message: Self::Input,
sender: AsyncComponentSender<Self>,
root: &Self::Root,
) -> impl std::future::Future<Output = ()> {
async {}
}
#[allow(unused)]
fn update_cmd(
&mut self,
message: Self::CommandOutput,
sender: AsyncComponentSender<Self>,
root: &Self::Root,
) -> impl std::future::Future<Output = ()> {
async {}
}
fn update_cmd_with_view(
&mut self,
widgets: &mut Self::Widgets,
message: Self::CommandOutput,
sender: AsyncComponentSender<Self>,
root: &Self::Root,
) -> impl std::future::Future<Output = ()> {
async {
self.update_cmd(message, sender.clone(), root).await;
self.update_view(widgets, sender);
}
}
#[allow(unused)]
fn update_view(&self, widgets: &mut Self::Widgets, sender: AsyncComponentSender<Self>) {}
fn update_with_view(
&mut self,
widgets: &mut Self::Widgets,
message: Self::Input,
sender: AsyncComponentSender<Self>,
root: &Self::Root,
) -> impl std::future::Future<Output = ()> {
async {
self.update(message, sender.clone(), root).await;
self.update_view(widgets, sender);
}
}
#[allow(unused)]
fn shutdown(&mut self, widgets: &mut Self::Widgets, output: Sender<Self::Output>) {}
#[must_use]
fn id(&self) -> String {
format!("{:p}", &self)
}
}
pub trait SimpleAsyncComponent: Sized + 'static {
type Input: Debug + 'static;
type Output: Debug + 'static;
type Init;
type Root: Debug + Clone;
type Widgets: 'static;
#[must_use]
fn init_root() -> Self::Root;
#[must_use]
fn init_loading_widgets(_root: Self::Root) -> Option<LoadingWidgets> {
None
}
fn init(
init: Self::Init,
root: Self::Root,
sender: AsyncComponentSender<Self>,
) -> impl std::future::Future<Output = AsyncComponentParts<Self>>;
#[allow(unused)]
fn update(
&mut self,
message: Self::Input,
sender: AsyncComponentSender<Self>,
) -> impl std::future::Future<Output = ()> {
async {}
}
#[allow(unused)]
fn update_cmd(
&mut self,
input: &Sender<Self::Input>,
output: Sender<Self::Output>,
) -> impl std::future::Future<Output = ()> {
async {}
}
#[allow(unused)]
fn update_view(&self, widgets: &mut Self::Widgets, sender: AsyncComponentSender<Self>) {}
#[allow(unused)]
fn shutdown(&mut self, widgets: &mut Self::Widgets, output: Sender<Self::Output>) {}
}
impl<C> AsyncComponent for C
where
C: SimpleAsyncComponent,
{
type Init = C::Init;
type Input = C::Input;
type Output = C::Output;
type Root = C::Root;
type Widgets = C::Widgets;
type CommandOutput = ();
fn init_root() -> Self::Root {
C::init_root()
}
fn init_loading_widgets(root: Self::Root) -> Option<LoadingWidgets> {
C::init_loading_widgets(root)
}
async fn init(
init: Self::Init,
root: Self::Root,
sender: AsyncComponentSender<Self>,
) -> AsyncComponentParts<Self> {
C::init(init, root, sender).await
}
async fn update(
&mut self,
message: Self::Input,
sender: AsyncComponentSender<Self>,
_root: &Self::Root,
) {
C::update(self, message, sender).await;
}
fn update_view(&self, widgets: &mut Self::Widgets, sender: AsyncComponentSender<Self>) {
C::update_view(self, widgets, sender);
}
fn shutdown(&mut self, widgets: &mut Self::Widgets, output: Sender<Self::Output>) {
self.shutdown(widgets, output);
}
}
impl SimpleAsyncComponent for () {
type Input = ();
type Output = ();
type Init = ();
type Root = ();
type Widgets = ();
fn init_root() -> Self::Root {}
async fn init(
_init: Self::Init,
_root: Self::Root,
_sender: AsyncComponentSender<Self>,
) -> AsyncComponentParts<Self> {
AsyncComponentParts {
model: (),
widgets: (),
}
}
}