trigger redraw every 400ms while playing to keep statusbar in sync
This commit is contained in:
@@ -23,6 +23,7 @@ serde = "1.0"
|
|||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
toml = "0.4"
|
toml = "0.4"
|
||||||
tokio-core = "0.1"
|
tokio-core = "0.1"
|
||||||
|
tokio-timer = "0.2"
|
||||||
unicode-width = "0.1.5"
|
unicode-width = "0.1.5"
|
||||||
|
|
||||||
[dependencies.librespot]
|
[dependencies.librespot]
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ extern crate futures;
|
|||||||
extern crate librespot;
|
extern crate librespot;
|
||||||
extern crate rspotify;
|
extern crate rspotify;
|
||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
|
extern crate tokio_timer;
|
||||||
extern crate unicode_width;
|
extern crate unicode_width;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ use futures::Async;
|
|||||||
use futures::Future;
|
use futures::Future;
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
|
use tokio_timer;
|
||||||
|
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
@@ -60,6 +61,8 @@ struct Worker {
|
|||||||
commands: mpsc::UnboundedReceiver<WorkerCommand>,
|
commands: mpsc::UnboundedReceiver<WorkerCommand>,
|
||||||
player: Player,
|
player: Player,
|
||||||
play_task: Box<futures::Future<Item = (), Error = oneshot::Canceled>>,
|
play_task: Box<futures::Future<Item = (), Error = oneshot::Canceled>>,
|
||||||
|
refresh_task: Box<futures::Stream<Item = (), Error = tokio_timer::Error>>,
|
||||||
|
active: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Worker {
|
impl Worker {
|
||||||
@@ -73,10 +76,23 @@ impl Worker {
|
|||||||
commands: commands,
|
commands: commands,
|
||||||
player: player,
|
player: player,
|
||||||
play_task: Box::new(futures::empty()),
|
play_task: Box::new(futures::empty()),
|
||||||
|
refresh_task: Box::new(futures::stream::empty()),
|
||||||
|
active: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Worker {
|
||||||
|
fn create_refresh(&self) -> Box<futures::Stream<Item = (), Error = tokio_timer::Error>> {
|
||||||
|
let ev = self.events.clone();
|
||||||
|
let future =
|
||||||
|
tokio_timer::Interval::new_interval(Duration::from_millis(400)).map(move |_| {
|
||||||
|
ev.trigger();
|
||||||
|
});
|
||||||
|
Box::new(future)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl futures::Future for Worker {
|
impl futures::Future for Worker {
|
||||||
type Item = ();
|
type Item = ();
|
||||||
type Error = ();
|
type Error = ();
|
||||||
@@ -85,7 +101,6 @@ impl futures::Future for Worker {
|
|||||||
loop {
|
loop {
|
||||||
let mut progress = false;
|
let mut progress = false;
|
||||||
|
|
||||||
trace!("Worker is polling");
|
|
||||||
if let Async::Ready(Some(cmd)) = self.commands.poll().unwrap() {
|
if let Async::Ready(Some(cmd)) = self.commands.poll().unwrap() {
|
||||||
progress = true;
|
progress = true;
|
||||||
debug!("message received!");
|
debug!("message received!");
|
||||||
@@ -97,14 +112,18 @@ impl futures::Future for Worker {
|
|||||||
WorkerCommand::Play => {
|
WorkerCommand::Play => {
|
||||||
self.player.play();
|
self.player.play();
|
||||||
self.events.send(Event::Player(PlayerEvent::Playing));
|
self.events.send(Event::Player(PlayerEvent::Playing));
|
||||||
|
self.refresh_task = self.create_refresh();
|
||||||
|
self.active = true;
|
||||||
}
|
}
|
||||||
WorkerCommand::Pause => {
|
WorkerCommand::Pause => {
|
||||||
self.player.pause();
|
self.player.pause();
|
||||||
self.events.send(Event::Player(PlayerEvent::Paused));
|
self.events.send(Event::Player(PlayerEvent::Paused));
|
||||||
|
self.active = false;
|
||||||
}
|
}
|
||||||
WorkerCommand::Stop => {
|
WorkerCommand::Stop => {
|
||||||
self.player.stop();
|
self.player.stop();
|
||||||
self.events.send(Event::Player(PlayerEvent::Stopped));
|
self.events.send(Event::Player(PlayerEvent::Stopped));
|
||||||
|
self.active = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,10 +139,20 @@ impl futures::Future for Worker {
|
|||||||
self.play_task = Box::new(futures::empty());
|
self.play_task = Box::new(futures::empty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
match self.refresh_task.poll() {
|
||||||
|
Ok(Async::Ready(_)) => {
|
||||||
|
self.refresh_task = match self.active {
|
||||||
|
true => {
|
||||||
|
progress = true;
|
||||||
|
self.create_refresh()
|
||||||
|
}
|
||||||
|
false => Box::new(futures::stream::empty()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
info!("worker done");
|
|
||||||
if !progress {
|
if !progress {
|
||||||
trace!("handing executor to other tasks");
|
|
||||||
return Ok(Async::NotReady);
|
return Ok(Async::NotReady);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user