Use a normal Result as AudioAgent output

This commit is contained in:
Rob Watson 2020-09-07 21:58:17 +02:00
parent 07867624be
commit 08ee712448
2 changed files with 25 additions and 22 deletions

View File

@ -29,11 +29,6 @@ pub enum Request {
LoadSamplesFromFile(File),
}
#[derive(Debug)]
pub enum Response {
SamplesLoaded(AudioData),
}
#[derive(Debug)]
pub enum Msg {
FileProgress(Option<FileChunk>),
@ -58,7 +53,7 @@ impl Agent for AudioAgent {
type Reach = Context<Self>;
type Message = Msg;
type Input = Request;
type Output = Response;
type Output = Result<AudioData, String>;
fn create(link: AgentLink<Self>) -> 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) {

View File

@ -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<AudioData, String>),
}
impl Component for Canvas {
@ -22,7 +23,7 @@ impl Component for Canvas {
fn create(_: Self::Properties, link: ComponentLink<Self>) -> 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::<HtmlCanvasElement>().unwrap();
let _context = canvas_element.get_context("2d").unwrap();
// TODO: draw canvas...
ConsoleService::log("TODO: draw on canvas...");
}
}