-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
187 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::{ | ||
collections::VecDeque, | ||
fmt::{self, Debug, Formatter}, | ||
pin::Pin, | ||
task::{ready, Context, Poll}, | ||
}; | ||
|
||
use super::{AggregatorClient, TaskIds}; | ||
use crate::clients::ClientError; | ||
use futures_lite::{stream::Stream, Future}; | ||
|
||
#[derive(Clone, Debug)] | ||
struct Page { | ||
task_ids: VecDeque<String>, | ||
pagination_token: Option<String>, | ||
} | ||
|
||
impl From<TaskIds> for Page { | ||
fn from( | ||
TaskIds { | ||
task_ids, | ||
pagination_token, | ||
}: TaskIds, | ||
) -> Self { | ||
Page { | ||
task_ids: task_ids.into_iter().map(|t| t.to_string()).collect(), | ||
pagination_token, | ||
} | ||
} | ||
} | ||
|
||
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
|
||
pub struct TaskIdStream<'a> { | ||
client: &'a AggregatorClient, | ||
page: Option<Page>, | ||
future: Option<BoxFuture<'a, Result<TaskIds, ClientError>>>, | ||
} | ||
|
||
impl<'a> TaskIdStream<'a> { | ||
pub(super) fn new(client: &'a AggregatorClient) -> Self { | ||
Self { | ||
client, | ||
page: None, | ||
future: None, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Debug for TaskIdStream<'a> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("TaskIdStream") | ||
.field("client", &self.client) | ||
.field("current_page", &self.page) | ||
.field("current_future", &"..") | ||
.finish() | ||
} | ||
} | ||
|
||
impl Stream for TaskIdStream<'_> { | ||
type Item = Result<String, ClientError>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let Self { | ||
client, | ||
ref mut page, | ||
ref mut future, | ||
} = *self; | ||
|
||
loop { | ||
if let Some(page) = page { | ||
if let Some(task_id) = page.task_ids.pop_front() { | ||
return Poll::Ready(Some(Ok(task_id))); | ||
} | ||
|
||
if page.pagination_token.is_none() { | ||
return Poll::Ready(None); | ||
} | ||
} | ||
|
||
if let Some(fut) = future { | ||
*page = Some(ready!(Pin::new(&mut *fut).poll(cx))?.into()); | ||
*future = None; | ||
} else { | ||
let pagination_token = page.as_ref().and_then(|page| page.pagination_token.clone()); | ||
|
||
*future = Some(Box::pin(async move { | ||
client.get_task_id_page(pagination_token.as_deref()).await | ||
})); | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::{ | ||
fmt::{self, Debug, Formatter}, | ||
pin::Pin, | ||
task::{ready, Context, Poll}, | ||
}; | ||
|
||
use super::{task_id_stream::TaskIdStream, AggregatorClient, TaskResponse}; | ||
use crate::clients::ClientError; | ||
use futures_lite::{stream::Stream, Future}; | ||
|
||
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
|
||
pub struct TaskStream<'a> { | ||
client: &'a AggregatorClient, | ||
task_id_stream: TaskIdStream<'a>, | ||
task_future: Option<BoxFuture<'a, Option<Result<TaskResponse, ClientError>>>>, | ||
} | ||
|
||
impl<'a> Debug for TaskStream<'a> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("TaskStream").field("future", &"..").finish() | ||
} | ||
} | ||
|
||
impl<'a> TaskStream<'a> { | ||
pub(super) fn new(client: &'a AggregatorClient) -> Self { | ||
Self { | ||
task_id_stream: client.task_id_stream(), | ||
client, | ||
task_future: None, | ||
} | ||
} | ||
} | ||
|
||
impl Stream for TaskStream<'_> { | ||
type Item = Result<TaskResponse, ClientError>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let Self { | ||
client, | ||
ref mut task_id_stream, | ||
ref mut task_future, | ||
} = *self; | ||
|
||
loop { | ||
if let Some(future) = task_future { | ||
let res = ready!(Pin::new(&mut *future).poll(cx)); | ||
*task_future = None; | ||
return Poll::Ready(res); | ||
} | ||
|
||
*task_future = match ready!(Pin::new(&mut *task_id_stream).poll_next(cx)) { | ||
Some(Ok(task_id)) => Some(Box::pin(async move { | ||
let task_id = task_id; | ||
Some(client.get_task(&task_id).await) | ||
})), | ||
None => return Poll::Ready(None), | ||
Some(Err(e)) => return Poll::Ready(Some(Err(e))), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.