Add sample setInterval call

This commit is contained in:
Rob Watson 2020-09-10 22:07:08 +02:00
parent 7aecf65a86
commit a25014ef0d
2 changed files with 23 additions and 0 deletions

View File

@ -26,6 +26,7 @@ features = [
"AudioBuffer",
"HtmlCanvasElement",
"CanvasRenderingContext2d",
"Window",
]
[profile.release]

View File

@ -1,6 +1,7 @@
use crate::agents::audio_agent::{AudioAgent, AudioData};
use crate::utils;
use std::sync::Arc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::HtmlCanvasElement;
use yew::agent::Bridged;
@ -12,6 +13,7 @@ pub struct Canvas {
_audio_agent: Box<dyn Bridge<AudioAgent>>,
canvas_node: NodeRef,
audio_data: Option<Arc<AudioData>>,
cb: Option<Closure<dyn FnMut()>>,
}
#[derive(Debug)]
@ -29,6 +31,26 @@ impl Component for Canvas {
_audio_agent: AudioAgent::bridge(link.callback(Msg::AudioAgentMessage)),
canvas_node: NodeRef::default(),
audio_data: None,
cb: None,
}
}
fn rendered(&mut self, first_render: bool) {
if first_render {
let cb = Closure::wrap(Box::new(|| {
ConsoleService::log(&format!("In the callback"));
}) as Box<dyn FnMut()>);
// TODO: request_animation_frame() ?
let window = web_sys::window().unwrap();
window
.set_interval_with_callback_and_timeout_and_arguments_0(
cb.as_ref().unchecked_ref(),
1000,
)
.unwrap();
self.cb = Some(cb);
}
}