-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added stream implementation for poll
- Loading branch information
1 parent
d3193fe
commit 46b1252
Showing
3 changed files
with
45 additions
and
28 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
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 |
---|---|---|
@@ -1,44 +1,52 @@ | ||
use std::task::{Context, Poll}; | ||
use std::{ | ||
collections::{BTreeMap}, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use futures::Future; | ||
use futures::Stream; | ||
|
||
use crate::bindings::wasi::io::poll::{poll, Pollable}; | ||
|
||
///Future that is used to poll changes from the host | ||
///Future that is used to poll changes from the host\ | ||
#[derive(Default)] | ||
pub struct PollTasks { | ||
pollables: Vec<Pollable>, | ||
processed_polls: Vec<u32>, | ||
pollables: BTreeMap<String, Pollable>, | ||
processed_polls: Vec<String>, | ||
} | ||
|
||
impl PollTasks { | ||
pub(crate) fn new(pollables: Vec<Pollable>) -> Self { | ||
Self { | ||
processed_polls: Vec::with_capacity(pollables.len()), | ||
pollables, | ||
} | ||
pub(crate) fn push(&mut self, event_name: String, pollable: Pollable) { | ||
self.pollables.insert(event_name, pollable); | ||
} | ||
} | ||
|
||
impl Future for PollTasks { | ||
type Output = Vec<u32>; | ||
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
impl Stream for PollTasks { | ||
type Item = Vec<String>; | ||
|
||
fn poll_next(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let pollables = self.get_mut(); | ||
let pollable_results = pollables.pollables.iter().collect::<Vec<_>>(); | ||
let pollable_results = pollables.pollables.values().collect::<Vec<_>>(); | ||
let results_vec = poll(pollable_results.as_slice()); | ||
//take all the polls that are not needed away | ||
for index in results_vec { | ||
for (_, key) in results_vec.into_iter().zip(pollables.pollables.keys()) { | ||
//if processed remove it from process queue and add it to processed polls | ||
let _ = pollables.pollables.remove(index as usize); | ||
pollables.processed_polls.push(index); | ||
pollables.processed_polls.push(key.clone()); | ||
} | ||
|
||
//remove pollables | ||
for key in pollables.processed_polls.iter() { | ||
pollables.pollables.remove(key); | ||
} | ||
//if the pollable set is empty that means we have finished processing everything | ||
if pollables.pollables.is_empty() { | ||
Poll::Ready(pollables.processed_polls.clone()) | ||
Poll::Ready(None) | ||
} else { | ||
//still more polls that might need to get processed for a later date | ||
cx.waker().wake_by_ref(); | ||
Poll::Pending | ||
if pollables.processed_polls.is_empty() { | ||
std::task::Poll::Pending | ||
} else { | ||
Poll::Ready(Some(pollables.processed_polls.clone())) | ||
} | ||
} | ||
} | ||
} |