Refactor, add AudioAgent
This commit is contained in:
parent
ef1159c139
commit
9fe3c89ce1
|
@ -0,0 +1,76 @@
|
|||
use web_sys::File;
|
||||
use yew::services::reader::{FileChunk, ReaderTask};
|
||||
use yew::services::{ConsoleService, ReaderService};
|
||||
use yew::worker::*;
|
||||
|
||||
pub struct AudioAgent {
|
||||
link: AgentLink<Self>,
|
||||
reader_service: ReaderService,
|
||||
reader_task: Option<ReaderTask>,
|
||||
}
|
||||
|
||||
pub enum Request {
|
||||
LoadSamplesFromFile(File),
|
||||
}
|
||||
|
||||
pub enum Msg {
|
||||
FileProgress(Option<FileChunk>),
|
||||
}
|
||||
|
||||
impl Agent for AudioAgent {
|
||||
type Reach = Context<Self>;
|
||||
type Message = Msg;
|
||||
type Input = Request;
|
||||
type Output = ();
|
||||
|
||||
fn create(link: AgentLink<Self>) -> Self {
|
||||
Self {
|
||||
link,
|
||||
reader_service: ReaderService::new(),
|
||||
reader_task: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, msg: Self::Message) {
|
||||
match msg {
|
||||
Msg::FileProgress(Some(chunk)) => {
|
||||
if let Err(e) = self.handle_file_progress(chunk) {
|
||||
ConsoleService::error(&e);
|
||||
}
|
||||
}
|
||||
Msg::FileProgress(None) => (),
|
||||
};
|
||||
}
|
||||
|
||||
fn handle_input(&mut self, input: Self::Input, _: HandlerId) {
|
||||
match input {
|
||||
Request::LoadSamplesFromFile(file) => self.load_samples_from_file(file),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl AudioAgent {
|
||||
fn load_samples_from_file(&mut self, file: File) -> Result<(), String> {
|
||||
let cb = self.link.callback(Msg::FileProgress);
|
||||
|
||||
let task = self
|
||||
.reader_service
|
||||
.read_file_by_chunks(file, cb, 256)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
self.reader_task = Some(task);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_file_progress(&mut self, chunk: FileChunk) -> Result<(), String> {
|
||||
match chunk {
|
||||
FileChunk::Started { name, .. } => ConsoleService::log(&format!("Started: {}", name)),
|
||||
FileChunk::DataChunk { progress, .. } => {
|
||||
ConsoleService::log(&format!("Got chunk, progress: {}", progress))
|
||||
}
|
||||
FileChunk::Finished => ConsoleService::log("Finished"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod audio_agent;
|
|
@ -1,18 +1,17 @@
|
|||
use crate::agents::audio_agent::{self, AudioAgent};
|
||||
use web_sys::HtmlInputElement;
|
||||
use yew::agent::Dispatcher;
|
||||
use yew::prelude::*;
|
||||
use yew::services::reader::{FileChunk, ReaderTask};
|
||||
use yew::services::{ConsoleService, ReaderService};
|
||||
use yew::services::ConsoleService;
|
||||
|
||||
pub struct Controls {
|
||||
link: ComponentLink<Self>,
|
||||
file_input: NodeRef,
|
||||
reader_service: ReaderService,
|
||||
reader_task: Option<ReaderTask>,
|
||||
audio_agent: Dispatcher<AudioAgent>,
|
||||
}
|
||||
|
||||
pub enum Msg {
|
||||
SubmitForm,
|
||||
FileProgress(Option<FileChunk>),
|
||||
}
|
||||
|
||||
impl Component for Controls {
|
||||
|
@ -23,8 +22,7 @@ impl Component for Controls {
|
|||
Self {
|
||||
link,
|
||||
file_input: NodeRef::default(),
|
||||
reader_service: ReaderService::new(),
|
||||
reader_task: None,
|
||||
audio_agent: AudioAgent::dispatcher(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,11 +33,6 @@ impl Component for Controls {
|
|||
ConsoleService::error(&e);
|
||||
};
|
||||
}
|
||||
Msg::FileProgress(c) => {
|
||||
if let Err(e) = self.handle_file_progress(c) {
|
||||
ConsoleService::error(&e);
|
||||
};
|
||||
}
|
||||
};
|
||||
true
|
||||
}
|
||||
|
@ -72,28 +65,10 @@ impl Controls {
|
|||
|
||||
let file_list = file_input.files().ok_or("could not get file list")?;
|
||||
let file = file_list.get(0).ok_or("could not get file")?;
|
||||
let cb = self.link.callback(Msg::FileProgress);
|
||||
|
||||
let task = self
|
||||
.reader_service
|
||||
.read_file_by_chunks(file, cb, 256)
|
||||
.map_err(|e| e.to_string())?;
|
||||
self.audio_agent
|
||||
.send(audio_agent::Request::LoadSamplesFromFile(file));
|
||||
|
||||
self.reader_task = Some(task);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_file_progress(&self, chunk: Option<FileChunk>) -> Result<(), String> {
|
||||
match chunk {
|
||||
Some(FileChunk::Started { name, .. }) => {
|
||||
ConsoleService::log(&format!("Started: {}", name))
|
||||
}
|
||||
Some(FileChunk::DataChunk { progress, .. }) => {
|
||||
ConsoleService::log(&format!("Got chunk, progress: {}", progress))
|
||||
}
|
||||
Some(FileChunk::Finished) => ConsoleService::log("Finished"),
|
||||
_ => (),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ use std::panic;
|
|||
use wasm_bindgen::prelude::*;
|
||||
use yew::prelude::*;
|
||||
|
||||
mod agents;
|
||||
mod app;
|
||||
mod canvas;
|
||||
mod controls;
|
||||
|
|
Loading…
Reference in New Issue