Skip to main content

eureka_mmanager/download/traits/
task.rs

1use actix::prelude::*;
2use dev::ToEnvelope;
3
4use super::MailBoxResult;
5
6use crate::{
7    download::{
8        messages::{
9            CancelTaskMessage, StartDownload, SubcribeMessage, TaskStateMessage,
10            TaskSubscriberMessages, WaitForFinishedMessage,
11        },
12        state::{TaskState, WaitForFinished},
13    },
14    recipients::MaybeWeakRecipient,
15};
16
17pub trait Cancelable: Actor {
18    fn cancel(&mut self, ctx: &mut Self::Context);
19}
20
21pub trait Download: Actor {
22    fn download(&mut self, ctx: &mut Self::Context);
23}
24
25pub trait State: Actor
26where
27    Self::State: Into<TaskState> + Send,
28{
29    type State;
30    fn state(&self) -> TaskState {
31        self.inner_state().into()
32    }
33    fn inner_state(&self) -> Self::State;
34}
35
36pub trait Subscribe: State {
37    fn subscribe(&mut self, subscriber: MaybeWeakRecipient<TaskSubscriberMessages<Self::State>>);
38}
39
40pub trait CanBeWaited: State {
41    type Ok;
42    type Loading;
43    fn wait(&mut self) -> WaitForFinished<Self::Ok, Self::Loading>;
44}
45
46pub trait AsyncCancelable: Sync {
47    fn cancel(&self) -> impl std::future::Future<Output = MailBoxResult<()>> + Send;
48}
49
50impl<A> AsyncCancelable for Addr<A>
51where
52    A: Handler<CancelTaskMessage> + Cancelable,
53    <A as actix::Actor>::Context:
54        actix::dev::ToEnvelope<A, crate::download::messages::CancelTaskMessage>,
55{
56    async fn cancel(&self) -> MailBoxResult<()> {
57        self.send(CancelTaskMessage).await
58    }
59}
60
61pub trait AsyncDownload: Sync {
62    fn download(&self) -> impl std::future::Future<Output = MailBoxResult<()>> + Send;
63}
64
65impl<A> AsyncDownload for Addr<A>
66where
67    A: Handler<StartDownload> + Download,
68    <A as Actor>::Context: ToEnvelope<A, StartDownload>,
69{
70    async fn download(&self) -> MailBoxResult<()> {
71        self.send(StartDownload).await
72    }
73}
74
75pub trait AsyncState: Sync {
76    type State: Send;
77    fn state(&self) -> impl std::future::Future<Output = MailBoxResult<TaskState>> + Send;
78}
79
80impl<A> AsyncState for Addr<A>
81where
82    A: Handler<TaskStateMessage> + State,
83    <A as Actor>::Context: ToEnvelope<A, TaskStateMessage>,
84{
85    type State = <A as State>::State;
86    async fn state(&self) -> MailBoxResult<TaskState> {
87        self.send(TaskStateMessage).await
88    }
89}
90
91pub trait AsyncSubscribe: AsyncState {
92    fn subscribe(
93        &self,
94        subscriber: MaybeWeakRecipient<TaskSubscriberMessages<Self::State>>,
95    ) -> impl std::future::Future<Output = MailBoxResult<()>> + Send;
96}
97
98impl<A> AsyncSubscribe for Addr<A>
99where
100    A: Handler<SubcribeMessage<<A as State>::State>>
101        + Subscribe
102        + Handler<TaskStateMessage>
103        + State,
104    <A as State>::State: Send + Sync,
105    <A as Actor>::Context:
106        ToEnvelope<A, SubcribeMessage<<A as State>::State>> + ToEnvelope<A, TaskStateMessage>,
107{
108    async fn subscribe(
109        &self,
110        subscriber: MaybeWeakRecipient<TaskSubscriberMessages<Self::State>>,
111    ) -> MailBoxResult<()> {
112        self.send(SubcribeMessage(subscriber)).await
113    }
114}
115
116pub trait AsyncCanBeWaited: AsyncState {
117    type Ok;
118    type Loading;
119    fn wait(
120        &mut self,
121    ) -> impl std::future::Future<Output = MailBoxResult<WaitForFinished<Self::Ok, Self::Loading>>> + Send;
122}
123
124impl<A> AsyncCanBeWaited for Addr<A>
125where
126    A: Handler<SubcribeMessage<<A as State>::State>>
127        + Subscribe
128        + Handler<TaskStateMessage>
129        + State
130        + CanBeWaited
131        + Handler<WaitForFinishedMessage<<A as CanBeWaited>::Ok, <A as CanBeWaited>::Loading>>,
132    <A as State>::State: Send + Sync,
133    <A as CanBeWaited>::Loading: Send + Sync,
134    <A as CanBeWaited>::Ok: Send + Sync,
135    <A as Actor>::Context: ToEnvelope<A, SubcribeMessage<<A as State>::State>>
136        + ToEnvelope<A, TaskStateMessage>
137        + ToEnvelope<A, WaitForFinishedMessage<<A as CanBeWaited>::Ok, <A as CanBeWaited>::Loading>>,
138{
139    type Loading = <A as CanBeWaited>::Loading;
140    type Ok = <A as CanBeWaited>::Ok;
141    async fn wait(&mut self) -> MailBoxResult<WaitForFinished<Self::Ok, Self::Loading>> {
142        self.send(WaitForFinishedMessage::<
143            <A as CanBeWaited>::Ok,
144            <A as CanBeWaited>::Loading,
145        >::new())
146            .await
147    }
148}