Use a normal Result as AudioAgent output
This commit is contained in:
parent
07867624be
commit
08ee712448
|
@ -29,11 +29,6 @@ pub enum Request {
|
||||||
LoadSamplesFromFile(File),
|
LoadSamplesFromFile(File),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Response {
|
|
||||||
SamplesLoaded(AudioData),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
FileProgress(Option<FileChunk>),
|
FileProgress(Option<FileChunk>),
|
||||||
|
@ -58,7 +53,7 @@ impl Agent for AudioAgent {
|
||||||
type Reach = Context<Self>;
|
type Reach = Context<Self>;
|
||||||
type Message = Msg;
|
type Message = Msg;
|
||||||
type Input = Request;
|
type Input = Request;
|
||||||
type Output = Response;
|
type Output = Result<AudioData, String>;
|
||||||
|
|
||||||
fn create(link: AgentLink<Self>) -> Self {
|
fn create(link: AgentLink<Self>) -> Self {
|
||||||
// TODO: where should the AudioContext be initialized and stored?
|
// TODO: where should the AudioContext be initialized and stored?
|
||||||
|
@ -87,8 +82,7 @@ impl Agent for AudioAgent {
|
||||||
let audio_data = Rc::new(audio_buffer);
|
let audio_data = Rc::new(audio_buffer);
|
||||||
|
|
||||||
for subscriber in self.subscribers.iter() {
|
for subscriber in self.subscribers.iter() {
|
||||||
self.link
|
self.link.respond(*subscriber, Ok(audio_data.clone()));
|
||||||
.respond(*subscriber, Response::SamplesLoaded(audio_data.clone()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Msg::AudioDecodingFailed(err) => {
|
Msg::AudioDecodingFailed(err) => {
|
||||||
|
@ -105,10 +99,12 @@ impl Agent for AudioAgent {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn connected(&mut self, id: HandlerId) {
|
fn connected(&mut self, id: HandlerId) {
|
||||||
// if id.is_respondable() {
|
if !id.is_respondable() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Console::log(&format!("Subscriber connected: {:?}", id));
|
Console::log(&format!("Subscriber connected: {:?}", id));
|
||||||
self.subscribers.insert(id);
|
self.subscribers.insert(id);
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn disconnected(&mut self, id: HandlerId) {
|
fn disconnected(&mut self, id: HandlerId) {
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use crate::agents::audio_agent::{self, AudioAgent, AudioData};
|
use crate::agents::audio_agent::{AudioAgent, AudioData};
|
||||||
use web_sys::HtmlCanvasElement;
|
use web_sys::HtmlCanvasElement;
|
||||||
use yew::agent::Bridged;
|
use yew::agent::Bridged;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
|
use yew::services::ConsoleService;
|
||||||
use yew::Bridge;
|
use yew::Bridge;
|
||||||
|
|
||||||
pub struct Canvas {
|
pub struct Canvas {
|
||||||
|
@ -13,7 +14,7 @@ pub struct Canvas {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
Reset,
|
Reset,
|
||||||
HandleAudioAgentResponse(audio_agent::Response),
|
AudioAgentMessage(Result<AudioData, String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Component for Canvas {
|
impl Component for Canvas {
|
||||||
|
@ -22,7 +23,7 @@ impl Component for Canvas {
|
||||||
|
|
||||||
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
_audio_agent: AudioAgent::bridge(link.callback(Msg::HandleAudioAgentResponse)),
|
_audio_agent: AudioAgent::bridge(link.callback(Msg::AudioAgentMessage)),
|
||||||
canvas_node: NodeRef::default(),
|
canvas_node: NodeRef::default(),
|
||||||
audio_data: None,
|
audio_data: None,
|
||||||
}
|
}
|
||||||
|
@ -30,15 +31,9 @@ impl Component for Canvas {
|
||||||
|
|
||||||
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||||
match msg {
|
match msg {
|
||||||
Msg::Reset => {
|
Msg::AudioAgentMessage(Ok(audio_data)) => self.handle_samples_loaded(audio_data),
|
||||||
self.audio_data = None;
|
Msg::AudioAgentMessage(Err(err)) => self.handle_samples_loaded_error(&err),
|
||||||
self.redraw_canvas();
|
Msg::Reset => self.reset(),
|
||||||
}
|
|
||||||
Msg::HandleAudioAgentResponse(audio_data) => match audio_data {
|
|
||||||
audio_agent::Response::SamplesLoaded(audio_data) => {
|
|
||||||
self.handle_samples_loaded(audio_data)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
@ -53,14 +48,26 @@ impl Component for Canvas {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Canvas {
|
impl Canvas {
|
||||||
|
fn reset(&mut self) {
|
||||||
|
self.audio_data = None;
|
||||||
|
self.redraw_canvas();
|
||||||
|
}
|
||||||
|
|
||||||
fn handle_samples_loaded(&mut self, audio_data: AudioData) {
|
fn handle_samples_loaded(&mut self, audio_data: AudioData) {
|
||||||
self.audio_data = Some(audio_data);
|
self.audio_data = Some(audio_data);
|
||||||
self.redraw_canvas();
|
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) {
|
fn redraw_canvas(&mut self) {
|
||||||
let canvas_element = self.canvas_node.cast::<HtmlCanvasElement>().unwrap();
|
let canvas_element = self.canvas_node.cast::<HtmlCanvasElement>().unwrap();
|
||||||
let _context = canvas_element.get_context("2d").unwrap();
|
let _context = canvas_element.get_context("2d").unwrap();
|
||||||
// TODO: draw canvas...
|
// TODO: draw canvas...
|
||||||
|
ConsoleService::log("TODO: draw on canvas...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue