Add constant, tidy code

This commit is contained in:
Rob Watson 2020-09-05 14:36:22 +02:00
parent e98174470e
commit a794e7fd28
1 changed files with 5 additions and 6 deletions

View File

@ -7,6 +7,8 @@ use yew::services::{ConsoleService, ReaderService};
use yew::worker::*; use yew::worker::*;
use yewtil::future::LinkFuture; use yewtil::future::LinkFuture;
const FILE_LOAD_CHUNK_SIZE: usize = 4096;
pub struct AudioAgent { pub struct AudioAgent {
link: AgentLink<Self>, link: AgentLink<Self>,
reader_service: ReaderService, reader_service: ReaderService,
@ -54,9 +56,7 @@ impl Agent for AudioAgent {
fn update(&mut self, msg: Self::Message) { fn update(&mut self, msg: Self::Message) {
match msg { match msg {
Msg::FileProgress(Some(FileChunk::DataChunk { data, progress })) => { Msg::FileProgress(Some(FileChunk::DataChunk { data, progress })) => {
if let Err(e) = self.handle_file_progress(data, progress) { self.handle_file_progress(data, progress)
ConsoleService::error(&e);
}
} }
Msg::FileProgress(Some(FileChunk::Finished)) => { Msg::FileProgress(Some(FileChunk::Finished)) => {
if let Err(e) = self.handle_file_loaded() { if let Err(e) = self.handle_file_loaded() {
@ -87,7 +87,7 @@ impl AudioAgent {
let task = self let task = self
.reader_service .reader_service
.read_file_by_chunks(file, cb, 256) .read_file_by_chunks(file, cb, FILE_LOAD_CHUNK_SIZE)
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
self.reader_task = Some(task); self.reader_task = Some(task);
@ -95,9 +95,8 @@ impl AudioAgent {
Ok(()) Ok(())
} }
fn handle_file_progress(&mut self, mut data: Vec<u8>, _progress: f32) -> Result<(), String> { fn handle_file_progress(&mut self, mut data: Vec<u8>, _progress: f32) {
self.bytes_buffer.append(&mut data); self.bytes_buffer.append(&mut data);
Ok(())
} }
fn handle_file_loaded(&self) -> Result<(), String> { fn handle_file_loaded(&self) -> Result<(), String> {