Fix event passing from agent to/from component
This commit is contained in:
parent
981426b681
commit
07867624be
|
@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
yew = "0.17"
|
yew = "0.17"
|
||||||
|
#yew = { path = "/home/rob/dev/yew/yew" }
|
||||||
yewtil = "0.3.1"
|
yewtil = "0.3.1"
|
||||||
wasm-bindgen = "0.2"
|
wasm-bindgen = "0.2"
|
||||||
gloo-events = "0.1.0"
|
gloo-events = "0.1.0"
|
||||||
|
@ -23,4 +24,5 @@ features = [
|
||||||
"console",
|
"console",
|
||||||
"AudioContext",
|
"AudioContext",
|
||||||
"AudioBuffer",
|
"AudioBuffer",
|
||||||
|
"HtmlCanvasElement",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,36 +1,54 @@
|
||||||
use js_sys::Uint8Array;
|
use js_sys::Uint8Array;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::rc::Rc;
|
||||||
use wasm_bindgen::JsValue;
|
use wasm_bindgen::JsValue;
|
||||||
use wasm_bindgen_futures::JsFuture;
|
use wasm_bindgen_futures::JsFuture;
|
||||||
|
use web_sys::console;
|
||||||
use web_sys::{AudioContext, File};
|
use web_sys::{AudioContext, File};
|
||||||
use yew::services::reader::{FileChunk, ReaderTask};
|
use yew::services::reader::{FileChunk, ReaderTask};
|
||||||
use yew::services::{ConsoleService, ReaderService};
|
use yew::services::{ConsoleService as Console, ReaderService};
|
||||||
use yew::worker::*;
|
use yew::worker::*;
|
||||||
use yewtil::future::LinkFuture;
|
use yewtil::future::LinkFuture;
|
||||||
|
|
||||||
const FILE_LOAD_CHUNK_SIZE: usize = 4096;
|
const FILE_LOAD_CHUNK_SIZE: usize = 4096;
|
||||||
|
|
||||||
|
pub type AudioData = Rc<web_sys::AudioBuffer>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct AudioAgent {
|
pub struct AudioAgent {
|
||||||
link: AgentLink<Self>,
|
link: AgentLink<Self>,
|
||||||
reader_service: ReaderService,
|
reader_service: ReaderService,
|
||||||
reader_task: Option<ReaderTask>,
|
reader_task: Option<ReaderTask>,
|
||||||
audio_context: AudioContext,
|
audio_context: AudioContext,
|
||||||
bytes_buffer: Vec<u8>,
|
bytes_buffer: Vec<u8>,
|
||||||
|
subscribers: HashSet<HandlerId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Request {
|
pub enum Request {
|
||||||
LoadSamplesFromFile(File),
|
LoadSamplesFromFile(File),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Response {
|
||||||
|
SamplesLoaded(AudioData),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
FileProgress(Option<FileChunk>),
|
FileProgress(Option<FileChunk>),
|
||||||
AudioDecoded(JsValue),
|
AudioDecoded(JsValue),
|
||||||
AudioDecodingFailed(JsValue),
|
AudioDecodingFailed(JsValue),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert the result of AudioContext.decode_audio_data():
|
||||||
impl From<Result<JsValue, JsValue>> for Msg {
|
impl From<Result<JsValue, JsValue>> for Msg {
|
||||||
fn from(item: Result<JsValue, JsValue>) -> Self {
|
fn from(item: Result<JsValue, JsValue>) -> Self {
|
||||||
match item {
|
match item {
|
||||||
Ok(samples) => Msg::AudioDecoded(samples),
|
Ok(samples) => {
|
||||||
|
console::log_2(&"Got js value".into(), &samples);
|
||||||
|
Msg::AudioDecoded(samples)
|
||||||
|
}
|
||||||
Err(e) => Msg::AudioDecodingFailed(e),
|
Err(e) => Msg::AudioDecodingFailed(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +58,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 = ();
|
type Output = Response;
|
||||||
|
|
||||||
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?
|
||||||
|
@ -50,6 +68,7 @@ impl Agent for AudioAgent {
|
||||||
reader_task: None,
|
reader_task: None,
|
||||||
audio_context: AudioContext::new().unwrap(),
|
audio_context: AudioContext::new().unwrap(),
|
||||||
bytes_buffer: vec![],
|
bytes_buffer: vec![],
|
||||||
|
subscribers: HashSet::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,17 +79,20 @@ impl Agent for AudioAgent {
|
||||||
}
|
}
|
||||||
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() {
|
||||||
ConsoleService::error(&e);
|
Console::error(&e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Msg::AudioDecoded(samples) => {
|
Msg::AudioDecoded(samples) => {
|
||||||
let audio_buffer = web_sys::AudioBuffer::from(samples);
|
let audio_buffer = web_sys::AudioBuffer::from(samples);
|
||||||
let data = audio_buffer.get_channel_data(0).unwrap(); // TODO: error handling
|
let audio_data = Rc::new(audio_buffer);
|
||||||
let vec: Vec<f32> = data.to_vec();
|
|
||||||
ConsoleService::log(&format!("Success decoding: {:?}", vec));
|
for subscriber in self.subscribers.iter() {
|
||||||
|
self.link
|
||||||
|
.respond(*subscriber, Response::SamplesLoaded(audio_data.clone()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Msg::AudioDecodingFailed(err) => {
|
Msg::AudioDecodingFailed(err) => {
|
||||||
ConsoleService::error(&format!("Error decoding: {:?}", err));
|
Console::error(&format!("Error decoding: {:?}", err));
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
|
@ -81,28 +103,41 @@ impl Agent for AudioAgent {
|
||||||
Request::LoadSamplesFromFile(file) => self.load_samples_from_file(file),
|
Request::LoadSamplesFromFile(file) => self.load_samples_from_file(file),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn connected(&mut self, id: HandlerId) {
|
||||||
|
// if id.is_respondable() {
|
||||||
|
Console::log(&format!("Subscriber connected: {:?}", id));
|
||||||
|
self.subscribers.insert(id);
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disconnected(&mut self, id: HandlerId) {
|
||||||
|
self.subscribers.remove(&id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AudioAgent {
|
impl AudioAgent {
|
||||||
fn load_samples_from_file(&mut self, file: File) -> Result<(), String> {
|
fn load_samples_from_file(&mut self, file: File) {
|
||||||
self.bytes_buffer = Vec::with_capacity(file.size() as usize);
|
self.bytes_buffer = Vec::with_capacity(file.size() as usize);
|
||||||
let cb = self.link.callback(Msg::FileProgress);
|
let cb = self.link.callback(Msg::FileProgress);
|
||||||
|
|
||||||
|
// TODO: error handling
|
||||||
let task = self
|
let task = self
|
||||||
.reader_service
|
.reader_service
|
||||||
.read_file_by_chunks(file, cb, FILE_LOAD_CHUNK_SIZE)
|
.read_file_by_chunks(file, cb, FILE_LOAD_CHUNK_SIZE)
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
self.reader_task = Some(task);
|
self.reader_task = Some(task);
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_file_progress(&mut self, mut data: Vec<u8>, _progress: f32) {
|
fn handle_file_progress(&mut self, mut data: Vec<u8>, _progress: f32) {
|
||||||
self.bytes_buffer.append(&mut data);
|
self.bytes_buffer.append(&mut data);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_file_loaded(&self) -> Result<(), String> {
|
fn handle_file_loaded(&mut self) -> Result<(), String> {
|
||||||
|
self.reader_task = None;
|
||||||
|
|
||||||
let audio_data = Uint8Array::from(&self.bytes_buffer[..]);
|
let audio_data = Uint8Array::from(&self.bytes_buffer[..]);
|
||||||
match self.audio_context.decode_audio_data(&audio_data.buffer()) {
|
match self.audio_context.decode_audio_data(&audio_data.buffer()) {
|
||||||
Ok(promise) => {
|
Ok(promise) => {
|
||||||
|
|
|
@ -1,22 +1,46 @@
|
||||||
|
use crate::agents::audio_agent::{self, AudioAgent, AudioData};
|
||||||
|
use web_sys::HtmlCanvasElement;
|
||||||
|
use yew::agent::Bridged;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
|
use yew::Bridge;
|
||||||
|
|
||||||
pub struct Canvas {}
|
pub struct Canvas {
|
||||||
|
_audio_agent: Box<dyn Bridge<AudioAgent>>,
|
||||||
|
canvas_node: NodeRef,
|
||||||
|
audio_data: Option<AudioData>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
Reset,
|
Reset,
|
||||||
SamplesLoaded,
|
HandleAudioAgentResponse(audio_agent::Response),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Component for Canvas {
|
impl Component for Canvas {
|
||||||
type Message = Msg;
|
type Message = Msg;
|
||||||
type Properties = ();
|
type Properties = ();
|
||||||
|
|
||||||
fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
|
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||||
Self {}
|
Self {
|
||||||
|
_audio_agent: AudioAgent::bridge(link.callback(Msg::HandleAudioAgentResponse)),
|
||||||
|
canvas_node: NodeRef::default(),
|
||||||
|
audio_data: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, _: Self::Message) -> ShouldRender {
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||||
true
|
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)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
||||||
|
@ -24,6 +48,19 @@ impl Component for Canvas {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Html {
|
fn view(&self) -> Html {
|
||||||
html! { <canvas width="800" height="300" style="border: 1px solid grey"></canvas> }
|
html! { <canvas ref=self.canvas_node.clone() width="800" height="300" style="border: 1px solid grey"></canvas> }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Canvas {
|
||||||
|
fn handle_samples_loaded(&mut self, audio_data: AudioData) {
|
||||||
|
self.audio_data = Some(audio_data);
|
||||||
|
self.redraw_canvas();
|
||||||
|
}
|
||||||
|
|
||||||
|
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...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::agents::audio_agent::{self, AudioAgent};
|
use crate::agents::audio_agent::{self, AudioAgent};
|
||||||
use web_sys::HtmlInputElement;
|
use web_sys::HtmlInputElement;
|
||||||
|
use yew::agent::Dispatched;
|
||||||
use yew::agent::Dispatcher;
|
use yew::agent::Dispatcher;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
use yew::services::ConsoleService;
|
use yew::services::ConsoleService;
|
||||||
|
|
Loading…
Reference in New Issue