#[cfg(feature = "tokio")]
use std::sync::mpsc::{self, TryRecvError};
use std::{
any::Any,
future::Future,
pin::Pin,
sync::Arc,
task::{Context as TaskContext, Poll, Wake, Waker},
};
use parking_lot::Mutex;
use super::Ctx;
use crate::{
app::window::WindowWaker,
core::signal::{Signal, SignalValue},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, crate::DevtoolsInspectable)]
pub enum FutureStatus {
Idle,
Pending,
Fulfilled,
Rejected,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FutureState<T, E> {
pub status: FutureStatus,
pub data: Option<T>,
pub error: Option<E>,
}
impl<T, E> crate::app::component::DevtoolsInspectable for FutureState<T, E>
where
T: crate::app::component::DevtoolsInspectable,
E: crate::app::component::DevtoolsInspectable,
{
fn write_info(&self, buffer: &mut Vec<crate::app::component::ComponentInfo>) {
let mut children = Vec::new();
crate::app::component::DevtoolsInspectable::write_info(&self.status, &mut children);
crate::app::component::DevtoolsInspectable::write_info(&self.data, &mut children);
crate::app::component::DevtoolsInspectable::write_info(&self.error, &mut children);
buffer.push(crate::app::component::ComponentInfo::with_children(
"FutureState",
std::any::type_name::<Self>(),
children,
));
}
}
impl<T, E> FutureState<T, E> {
pub fn idle() -> Self {
Self {
status: FutureStatus::Idle,
data: None,
error: None,
}
}
pub fn pending(data: Option<T>) -> Self {
Self {
status: FutureStatus::Pending,
data,
error: None,
}
}
pub fn fulfilled(data: T) -> Self {
Self {
status: FutureStatus::Fulfilled,
data: Some(data),
error: None,
}
}
pub fn rejected(error: E, data: Option<T>) -> Self {
Self {
status: FutureStatus::Rejected,
data,
error: Some(error),
}
}
pub fn is_idle(&self) -> bool {
self.status == FutureStatus::Idle
}
pub fn is_pending(&self) -> bool {
self.status == FutureStatus::Pending
}
pub fn is_fulfilled(&self) -> bool {
self.status == FutureStatus::Fulfilled
}
pub fn is_rejected(&self) -> bool {
self.status == FutureStatus::Rejected
}
pub fn outcome(&self) -> Option<Result<&T, &E>> {
match self.status {
FutureStatus::Fulfilled => self.data.as_ref().map(Ok),
FutureStatus::Rejected => self.error.as_ref().map(Err),
FutureStatus::Idle | FutureStatus::Pending => None,
}
}
pub fn fulfilled_data(&self) -> Option<&T> {
self.outcome().and_then(Result::ok)
}
}
pub struct FutureHandle<T: SignalValue, E: SignalValue> {
state: Signal<FutureState<T, E>>,
task: AsyncTask,
}
pub struct StreamHandle<T: SignalValue, E: SignalValue> {
state: Signal<FutureState<T, E>>,
task: AsyncTask,
}
#[derive(Clone)]
pub struct StreamEmitter<T: SignalValue, E: SignalValue> {
state: Signal<FutureState<T, E>>,
#[cfg(feature = "tokio")]
sender: Option<CompletionSender>,
}
pub struct FutureAction<A, T: SignalValue, E: SignalValue> {
state: Signal<FutureState<T, E>>,
task: AsyncTask,
runner: Arc<Mutex<ActionRunner<A, T, E>>>,
runtime_handle: RuntimeFutureHandle,
}
type BoxFutureResult<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;
type ActionRunner<A, T, E> = Arc<dyn Fn(A) -> BoxFutureResult<T, E> + Send + Sync>;
#[cfg(feature = "tokio")]
type FutureCompletion = Box<dyn FnOnce() + Send>;
#[cfg(feature = "tokio")]
#[derive(Clone)]
struct CompletionSender {
sender: mpsc::Sender<FutureCompletion>,
waker: Waker,
}
#[cfg(feature = "tokio")]
impl CompletionSender {
fn send(&self, completion: FutureCompletion) -> bool {
let sent = self.sender.send(completion).is_ok();
if sent {
self.waker.wake_by_ref();
}
sent
}
}
#[cfg(feature = "tokio")]
pub(super) type RuntimeFutureHandle = Option<tokio::runtime::Handle>;
#[cfg(not(feature = "tokio"))]
pub(super) type RuntimeFutureHandle = ();
#[derive(Clone)]
pub(super) struct AsyncTask {
inner: Arc<Mutex<AsyncTaskInner>>,
waker: Waker,
}
#[derive(Default)]
struct AsyncTaskInner {
work: AsyncWork,
closed: Option<TaskClosed>,
}
#[derive(Clone, Copy, Debug)]
enum TaskClosed {
Unmounted,
DroppedByRender { component: &'static str },
}
#[derive(Default)]
struct AsyncWork {
future: Option<Pin<Box<dyn Future<Output = ()> + Send>>>,
#[cfg(feature = "tokio")]
tokio_task: Option<TokioAsyncTask>,
}
pub(super) struct FutureSlot {
deps: Option<Box<dyn Any + Send + Sync>>,
handle: Box<dyn Any + Send + Sync>,
pub(super) task: AsyncTask,
}
impl FutureSlot {
fn drop_while_mounted(self, component: &'static str) {
self.task.close(TaskClosed::DroppedByRender { component });
}
}
impl Drop for FutureSlot {
fn drop(&mut self) {
self.task.close(TaskClosed::Unmounted);
}
}
#[cfg(feature = "tokio")]
struct TokioAsyncTask {
join: tokio::task::JoinHandle<()>,
receiver: mpsc::Receiver<FutureCompletion>,
finish_on_message: bool,
}
#[cfg(feature = "tokio")]
impl Drop for TokioAsyncTask {
fn drop(&mut self) {
self.join.abort();
}
}
struct NoopWake;
impl Wake for NoopWake {
fn wake(self: Arc<Self>) {}
}
struct LoopWake(WindowWaker);
impl Wake for LoopWake {
fn wake(self: Arc<Self>) {
(self.0)();
}
fn wake_by_ref(self: &Arc<Self>) {
(self.0)();
}
}
impl<T: SignalValue, E: SignalValue> Clone for FutureHandle<T, E> {
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
task: self.task.clone(),
}
}
}
impl<T: SignalValue, E: SignalValue> Clone for StreamHandle<T, E> {
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
task: self.task.clone(),
}
}
}
impl<A, T: SignalValue, E: SignalValue> Clone for FutureAction<A, T, E> {
fn clone(&self) -> Self {
Self {
state: self.state.clone(),
task: self.task.clone(),
runner: self.runner.clone(),
runtime_handle: self.runtime_handle.clone(),
}
}
}
impl<T, E> StreamEmitter<T, E>
where
T: SignalValue + Clone + PartialEq + Send + Sync + 'static,
E: SignalValue + Clone + PartialEq + Send + Sync + 'static,
{
pub fn emit(&self, data: T) -> bool {
#[cfg(feature = "tokio")]
if let Some(sender) = &self.sender {
let state = self.state.clone();
return sender.send(Box::new(move || state.set(FutureState::fulfilled(data))));
}
self.state.set(FutureState::fulfilled(data));
true
}
pub fn reject(&self, error: E) -> bool {
#[cfg(feature = "tokio")]
if let Some(sender) = &self.sender {
let state = self.state.clone();
return sender.send(Box::new(move || {
let previous_data = state.get_untracked().data;
state.set(FutureState::rejected(error, previous_data));
}));
}
let previous_data = self.state.get_untracked().data;
self.state.set(FutureState::rejected(error, previous_data));
true
}
}
impl<T: SignalValue, E: SignalValue> FutureHandle<T, E> {
pub fn state(&self) -> Signal<FutureState<T, E>> {
self.state.clone()
}
pub fn cancel(&self) {
self.task.cancel();
}
pub fn is_active(&self) -> bool {
self.task.is_active()
}
}
impl<T: SignalValue, E: SignalValue> StreamHandle<T, E> {
pub fn state(&self) -> Signal<FutureState<T, E>> {
self.state.clone()
}
pub fn cancel(&self) {
self.task.cancel();
}
pub fn is_active(&self) -> bool {
self.task.is_active()
}
}
impl<A: Send + Sync + 'static, T, E> FutureAction<A, T, E>
where
T: SignalValue + Clone + PartialEq + Send + Sync + 'static,
E: SignalValue + Clone + PartialEq + Send + Sync + 'static,
{
pub fn state(&self) -> Signal<FutureState<T, E>> {
self.state.clone()
}
pub fn run(&self, args: A) {
if self.startable() {
self.start(args);
}
}
pub fn run_if_idle(&self, args: A) -> bool {
if self.task.is_active() || !self.startable() {
return false;
}
self.start(args);
true
}
fn start(&self, args: A) {
let runner = self.runner.lock().clone();
let future = runner(args);
start_future_task(
self.state.clone(),
self.task.clone(),
self.runtime_handle.clone(),
future,
);
}
pub fn cancel(&self) {
self.task.cancel();
}
pub fn is_active(&self) -> bool {
self.task.is_active()
}
fn startable(&self) -> bool {
match self.task.closed() {
None => true,
Some(TaskClosed::Unmounted) => false,
Some(TaskClosed::DroppedByRender { component }) => {
tracing::error!(
component,
"FutureAction::run on an action that a render of {component} dropped: the render no longer made the same ctx.future/ctx.stream/ctx.future_action call at its position. The run does nothing. Create the action in `create`, or make these calls unconditionally and in the same order in every render."
);
false
}
}
}
}
impl AsyncTask {
fn new(waker: Waker) -> Self {
Self {
inner: Arc::new(Mutex::new(AsyncTaskInner::default())),
waker,
}
}
#[cfg(feature = "tokio")]
fn completion_channel(&self) -> (CompletionSender, mpsc::Receiver<FutureCompletion>) {
let (sender, receiver) = mpsc::channel();
let sender = CompletionSender {
sender,
waker: self.waker.clone(),
};
(sender, receiver)
}
fn set(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
let work = AsyncWork {
future: Some(future),
#[cfg(feature = "tokio")]
tokio_task: None,
};
self.replace_work(work);
self.waker.wake_by_ref();
}
#[cfg(feature = "tokio")]
fn spawn(
&self,
runtime: &tokio::runtime::Handle,
future: impl Future<Output = ()> + Send + 'static,
receiver: mpsc::Receiver<FutureCompletion>,
finish_on_message: bool,
) {
if self.is_closed() {
return;
}
let spawned = TokioAsyncTask {
join: runtime.spawn(future),
receiver,
finish_on_message,
};
self.replace_work(AsyncWork {
future: None,
tokio_task: Some(spawned),
});
}
fn replace_work(&self, work: AsyncWork) {
let unused = {
let mut inner = self.inner.lock();
if inner.closed.is_some() {
work
} else {
std::mem::replace(&mut inner.work, work)
}
};
drop(unused);
}
fn cancel(&self) {
let cancelled = std::mem::take(&mut self.inner.lock().work);
drop(cancelled);
}
fn close(&self, reason: TaskClosed) {
let cancelled = {
let mut inner = self.inner.lock();
inner.closed.get_or_insert(reason);
std::mem::take(&mut inner.work)
};
drop(cancelled);
}
fn closed(&self) -> Option<TaskClosed> {
self.inner.lock().closed
}
fn is_closed(&self) -> bool {
self.closed().is_some()
}
pub(super) fn is_active(&self) -> bool {
let inner = self.inner.lock();
inner.work.future.is_some() || {
#[cfg(feature = "tokio")]
{
inner.work.tokio_task.is_some()
}
#[cfg(not(feature = "tokio"))]
{
false
}
}
}
pub(super) fn poll(&self, cx: &mut TaskContext<'_>) -> bool {
let future = self.inner.lock().work.future.take();
if let Some(mut future) = future {
match future.as_mut().poll(cx) {
Poll::Ready(()) => return true,
Poll::Pending => {
let mut inner = self.inner.lock();
if inner.closed.is_none() && inner.work.future.is_none() {
inner.work.future = Some(future);
}
return false;
}
}
}
#[cfg(feature = "tokio")]
{
let mut completion = None;
let mut disconnected = false;
let mut finished = None;
{
let mut inner = self.inner.lock();
if let Some(task) = inner.work.tokio_task.as_mut() {
match task.receiver.try_recv() {
Ok(received) => {
if task.finish_on_message {
disconnected = true;
}
completion = Some(received);
}
Err(TryRecvError::Empty) => {}
Err(TryRecvError::Disconnected) => disconnected = true,
}
}
if disconnected {
finished = inner.work.tokio_task.take();
}
}
drop(finished);
if let Some(completion) = completion {
completion();
self.waker.wake_by_ref();
return true;
}
}
false
}
}
impl Ctx {
pub(super) fn loop_waker(&self) -> Waker {
match &self.window {
Some(window) => Waker::from(Arc::new(LoopWake(window.loop_waker()))),
None => Waker::from(Arc::new(NoopWake)),
}
}
}
fn start_future_task<T, E>(
state: Signal<FutureState<T, E>>,
task: AsyncTask,
runtime_handle: RuntimeFutureHandle,
future: BoxFutureResult<T, E>,
) where
T: SignalValue + Clone + PartialEq + Send + Sync + 'static,
E: SignalValue + Clone + PartialEq + Send + Sync + 'static,
{
let previous_data = state.get_untracked().data;
state.set(FutureState::pending(previous_data));
#[cfg(feature = "tokio")]
if let Some(handle) = runtime_handle {
let completion_state = state.clone();
let (sender, receiver) = task.completion_channel();
let completion_task = async move {
let result = future.await;
let completion: FutureCompletion = Box::new(move || match result {
Ok(data) => completion_state.set(FutureState::fulfilled(data)),
Err(error) => {
let previous_data = completion_state.get_untracked().data;
completion_state.set(FutureState::rejected(error, previous_data));
}
});
sender.send(completion);
};
task.spawn(&handle, completion_task, receiver, true);
return;
}
#[cfg(not(feature = "tokio"))]
let _ = runtime_handle;
let completion_state = state.clone();
task.set(Box::pin(async move {
match future.await {
Ok(data) => completion_state.set(FutureState::fulfilled(data)),
Err(error) => {
let previous_data = completion_state.get_untracked().data;
completion_state.set(FutureState::rejected(error, previous_data));
}
}
}));
}
fn start_stream_task<T, E, Fut>(
state: Signal<FutureState<T, E>>,
task: AsyncTask,
runtime_handle: RuntimeFutureHandle,
factory: impl FnOnce(StreamEmitter<T, E>) -> Fut,
) where
T: SignalValue + Clone + PartialEq + Send + Sync + 'static,
E: SignalValue + Clone + PartialEq + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
let previous_data = state.get_untracked().data;
state.set(FutureState::pending(previous_data));
#[cfg(feature = "tokio")]
if let Some(handle) = runtime_handle {
let (sender, receiver) = task.completion_channel();
let emitter = StreamEmitter {
state,
sender: Some(sender),
};
task.spawn(&handle, factory(emitter), receiver, false);
return;
}
#[cfg(not(feature = "tokio"))]
let _ = runtime_handle;
let emitter = StreamEmitter {
state,
#[cfg(feature = "tokio")]
sender: None,
};
task.set(Box::pin(factory(emitter)));
}
impl Ctx {
pub fn future<D, T, E, F, Fut>(&mut self, deps: D, factory: F) -> FutureHandle<T, E>
where
D: Clone + PartialEq + Send + Sync + 'static,
T: SignalValue + Clone + PartialEq + Send + Sync + 'static,
E: SignalValue + Clone + PartialEq + Send + Sync + 'static,
F: Fn(D) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<T, E>> + Send + 'static,
{
let cursor = self.next_future_cursor();
let runtime_handle = self.runtime_future_handle();
if let Some(slot) = self.positional_future_slot(cursor)
&& let Some(handle) = slot.handle.downcast_ref::<FutureHandle<T, E>>()
{
let deps_changed = slot.deps.as_ref().and_then(|old| old.downcast_ref::<D>()) != Some(&deps);
let handle = handle.clone();
if deps_changed {
slot.deps = Some(Box::new(deps.clone()));
start_future_task(
handle.state.clone(),
handle.task.clone(),
runtime_handle,
Box::pin(factory(deps)),
);
}
return handle;
}
let state = self.signal(FutureState::idle());
let task = AsyncTask::new(self.loop_waker());
let handle = FutureHandle {
state: state.clone(),
task: task.clone(),
};
self.store_future_slot(
cursor,
FutureSlot {
deps: Some(Box::new(deps.clone())),
handle: Box::new(handle.clone()),
task: task.clone(),
},
);
start_future_task(state, task, runtime_handle, Box::pin(factory(deps)));
handle
}
pub fn stream<D, T, E, F, Fut>(&mut self, deps: D, factory: F) -> StreamHandle<T, E>
where
D: Clone + PartialEq + Send + Sync + 'static,
T: SignalValue + Clone + PartialEq + Send + Sync + 'static,
E: SignalValue + Clone + PartialEq + Send + Sync + 'static,
F: Fn(D, StreamEmitter<T, E>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
let cursor = self.next_future_cursor();
let runtime_handle = self.runtime_future_handle();
if let Some(slot) = self.positional_future_slot(cursor)
&& let Some(handle) = slot.handle.downcast_ref::<StreamHandle<T, E>>()
{
let deps_changed = slot.deps.as_ref().and_then(|old| old.downcast_ref::<D>()) != Some(&deps);
let handle = handle.clone();
if deps_changed {
slot.deps = Some(Box::new(deps.clone()));
start_stream_task(
handle.state.clone(),
handle.task.clone(),
runtime_handle,
move |emitter| factory(deps, emitter),
);
}
return handle;
}
let state = self.signal(FutureState::idle());
let task = AsyncTask::new(self.loop_waker());
let handle = StreamHandle {
state: state.clone(),
task: task.clone(),
};
self.store_future_slot(
cursor,
FutureSlot {
deps: Some(Box::new(deps.clone())),
handle: Box::new(handle.clone()),
task: task.clone(),
},
);
start_stream_task(state, task, runtime_handle, move |emitter| factory(deps, emitter));
handle
}
pub fn future_action<A, T, E, F, Fut>(&mut self, factory: F) -> FutureAction<A, T, E>
where
A: Send + Sync + 'static,
T: SignalValue + Clone + PartialEq + Send + Sync + 'static,
E: SignalValue + Clone + PartialEq + Send + Sync + 'static,
F: Fn(A) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<T, E>> + Send + 'static,
{
let cursor = self.next_future_cursor();
let runtime_handle = self.runtime_future_handle();
let runner: ActionRunner<A, T, E> = Arc::new(move |args| Box::pin(factory(args)));
if let Some(slot) = self.positional_future_slot(cursor)
&& let Some(action) = slot.handle.downcast_mut::<FutureAction<A, T, E>>()
{
*action.runner.lock() = runner;
action.runtime_handle = runtime_handle;
return action.clone();
}
let state = self.signal(FutureState::idle());
let task = AsyncTask::new(self.loop_waker());
let action = FutureAction {
state,
task: task.clone(),
runner: Arc::new(Mutex::new(runner)),
runtime_handle,
};
self.store_future_slot(
cursor,
FutureSlot {
deps: None,
handle: Box::new(action.clone()),
task,
},
);
action
}
fn next_future_cursor(&mut self) -> Option<usize> {
if !self.rendering {
return None;
}
let cursor = self.future_cursor;
self.future_cursor += 1;
Some(cursor)
}
fn positional_future_slot(&mut self, cursor: Option<usize>) -> Option<&mut FutureSlot> {
self.future_slots.get_mut(cursor?)
}
fn store_future_slot(&mut self, cursor: Option<usize>, slot: FutureSlot) {
let Some(cursor) = cursor else {
self.stable_future_slots.push(slot);
return;
};
let component = self.component_name;
match self.future_slots.get_mut(cursor) {
Some(current) => std::mem::replace(current, slot).drop_while_mounted(component),
None => self.future_slots.push(slot),
}
}
pub(super) fn drop_unreached_future_slots(&mut self) {
let component = self.component_name;
for slot in self.future_slots.drain(self.future_cursor..) {
slot.drop_while_mounted(component);
}
}
pub(super) fn future_slots(&self) -> impl Iterator<Item = &FutureSlot> {
self.future_slots.iter().chain(&self.stable_future_slots)
}
}