Poll on Spawn, not on Future
@@ -24,6 +24,8 @@ use std::io::Write;
use std::marker::PhantomData;
use futures::{Async, Future};
+use futures::executor::{Notify, Spawn};
+
use {Component, Entity, Entities, FetchMut, Join, RunningTime, System, WriteStorage};
/// A boxed error implementing `Debug`, `Display` and `Error`.
@@ -117,7 +119,6 @@ impl Errors {
/// In case of an error, it will be added to the `Errors` resource.
pub struct Merge<F> {
future_type: PhantomData<F>,
- tmp: Vec<Entity>,
}
impl<F> Merge<F> {
@@ -125,39 +126,38 @@ impl<F> Merge<F> {
pub fn new() -> Self {
Merge {
future_type: PhantomData,
- tmp: Vec::new(),
}
}
}
+struct NotifyIgnore;
+impl Notify for NotifyIgnore {
+ fn notify(&self, id: usize) {
+ /* Intetionally ignore */
+ }
+}
+
+static NOTIFY_IGNORE: NotifyIgnore = NotifyIgnore;
+
impl<'a, T, F> System<'a> for Merge<F>
where T: Component + Send + Sync + 'static,
- F: Future<Item = T, Error = BoxedErr> + Component + Send + Sync,
+ F: Future<Item = T, Error = BoxedErr>,
+ Spawn<F>: Component + Send + Sync,
{
type SystemData = (Entities<'a>,
FetchMut<'a, Errors>,
- WriteStorage<'a, F>,
+ WriteStorage<'a, Spawn<F>>,
WriteStorage<'a, T>);
- fn run(&mut self, (entities, mut errors, mut future, mut pers): Self::SystemData) {
- let mut delete = &mut self.tmp;
-
- for (e, future) in (&*entities, &mut future).join() {
- match future.poll() {
- Ok(Async::Ready(x)) => {
- pers.insert(e, x);
- delete.push(e);
- }
- Ok(Async::NotReady) => {}
- Err(err) => {
- errors.add(err);
- delete.push(e);
- }
- }
- }
+ fn run(&mut self, (entities, mut errors, mut spawns, mut pers): Self::SystemData) {
- for e in delete.drain(..) {
- future.remove(e);
+ for (e, ()) in (&*entities, &spawns.check()).join() {
+ let mut spawn = spawns.remove(e).unwrap();
+ match spawn.poll_future_notify(&NOTIFY_IGNORE, 0) {
+ Ok(Async::NotReady) => { spawns.insert(e, spawn); }
+ Ok(Async::Ready(value)) => { pers.insert(e, value); }
+ Err(err) => errors.add(err),
+ };
}
}
