diff --git a/src/agents/audio_agent.rs b/src/agents/audio_agent.rs index cdd0411..e6d7cd8 100644 --- a/src/agents/audio_agent.rs +++ b/src/agents/audio_agent.rs @@ -29,11 +29,6 @@ pub enum Request { LoadSamplesFromFile(File), } -#[derive(Debug)] -pub enum Response { - SamplesLoaded(AudioData), -} - #[derive(Debug)] pub enum Msg { FileProgress(Option), @@ -58,7 +53,7 @@ impl Agent for AudioAgent { type Reach = Context; type Message = Msg; type Input = Request; - type Output = Response; + type Output = Result; fn create(link: AgentLink) -> Self { // TODO: where should the AudioContext be initialized and stored? @@ -87,8 +82,7 @@ impl Agent for AudioAgent { let audio_data = Rc::new(audio_buffer); for subscriber in self.subscribers.iter() { - self.link - .respond(*subscriber, Response::SamplesLoaded(audio_data.clone())); + self.link.respond(*subscriber, Ok(audio_data.clone())); } } Msg::AudioDecodingFailed(err) => { @@ -105,10 +99,12 @@ impl Agent for AudioAgent { } fn connected(&mut self, id: HandlerId) { - // if id.is_respondable() { + if !id.is_respondable() { + return; + } + Console::log(&format!("Subscriber connected: {:?}", id)); self.subscribers.insert(id); - // } fn disconnected(&mut self, id: HandlerId) { diff --git a/src/canvas.rs b/src/canvas.rs index 0b0b597..c2e260c 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,7 +1,8 @@ -use crate::agents::audio_agent::{self, AudioAgent, AudioData}; +use crate::agents::audio_agent::{AudioAgent, AudioData}; use web_sys::HtmlCanvasElement; use yew::agent::Bridged; use yew::prelude::*; +use yew::services::ConsoleService; use yew::Bridge; pub struct Canvas { @@ -13,7 +14,7 @@ pub struct Canvas { #[derive(Debug)] pub enum Msg { Reset, - HandleAudioAgentResponse(audio_agent::Response), + AudioAgentMessage(Result), } impl Component for Canvas { @@ -22,7 +23,7 @@ impl Component for Canvas { fn create(_: Self::Properties, link: ComponentLink) -> Self { Self { - _audio_agent: AudioAgent::bridge(link.callback(Msg::HandleAudioAgentResponse)), + _audio_agent: AudioAgent::bridge(link.callback(Msg::AudioAgentMessage)), canvas_node: NodeRef::default(), audio_data: None, } @@ -30,15 +31,9 @@ impl Component for Canvas { fn update(&mut self, msg: Self::Message) -> ShouldRender { match msg { - Msg::Reset => { - self.audio_data = None; - self.redraw_canvas(); - } - Msg::HandleAudioAgentResponse(audio_data) => match audio_data { - audio_agent::Response::SamplesLoaded(audio_data) => { - self.handle_samples_loaded(audio_data) - } - }, + Msg::AudioAgentMessage(Ok(audio_data)) => self.handle_samples_loaded(audio_data), + Msg::AudioAgentMessage(Err(err)) => self.handle_samples_loaded_error(&err), + Msg::Reset => self.reset(), }; false } @@ -53,14 +48,26 @@ impl Component for Canvas { } impl Canvas { + fn reset(&mut self) { + self.audio_data = None; + self.redraw_canvas(); + } + fn handle_samples_loaded(&mut self, audio_data: AudioData) { self.audio_data = Some(audio_data); self.redraw_canvas(); } + fn handle_samples_loaded_error(&mut self, err: &str) { + self.reset(); + + ConsoleService::error(&format!("Error loading samples: {}", err)); + } + fn redraw_canvas(&mut self) { let canvas_element = self.canvas_node.cast::().unwrap(); let _context = canvas_element.get_context("2d").unwrap(); // TODO: draw canvas... + ConsoleService::log("TODO: draw on canvas..."); } }