From 874077bf87e15208ba4d7bfab0095c3a60e4be17 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 4 Dec 2021 17:35:02 -0700 Subject: cock --- src/lib.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 8c8bcc3..e90202d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ use image::{DynamicImage, ImageFormat}; use std::cell::{Cell, RefCell}; use std::rc::Rc; +use futures::channel::mpsc; +use futures::prelude::*; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; use wasm_bindgen_futures::*; @@ -84,6 +86,8 @@ async fn do_boot() { let history = Rc::new(RefCell::new(history::History::default())); + let (manual_update_tx, manual_update_rx) = mpsc::channel::<()>(5); + let video: HtmlVideoElement = document .get_element_by_id("video") .unwrap() @@ -107,7 +111,9 @@ async fn do_boot() { let width = width.clone(); let height = height.clone(); let target_marker = target_marker.clone(); + let manual_update_tx = manual_update_tx.clone(); gloo::events::EventListener::new(&video, "click", move |event| { + let mut manual_update_tx = manual_update_tx.clone(); let video = also_video.clone(); let mouse_event: &MouseEvent = event.dyn_ref().unwrap(); let mouse_x = mouse_event.offset_x() as f64; @@ -120,6 +126,7 @@ async fn do_boot() { let real_y = mouse_y / fake_height * real_height - QUEUE_SIZE_HEIGHT as f64 / 2.0; queue_position.replace((real_x.round() as u32, real_y.round() as u32)); update_target_marker(&target_marker, &width, &height, &video, &queue_position); + spawn_local(async move { manual_update_tx.send(()).await.unwrap() }); }) }; video_click_listener.forget(); @@ -146,7 +153,9 @@ async fn do_boot() { let also_video = video.clone(); let canvas = canvas.clone(); let queue_position = queue_position.clone(); + let manual_update_tx = manual_update_tx.clone(); gloo::events::EventListener::new(&video, "canplay", move |_event| { + let mut manual_update_tx = manual_update_tx.clone(); let video = also_video.clone(); let _ = video.play().unwrap(); if !streaming.get() { @@ -156,16 +165,21 @@ async fn do_boot() { canvas.set_attribute("height", &format!("{}", height.get())); streaming.set(true); update_target_marker(&target_marker, &width, &height, &video, &queue_position); + spawn_local(async move { manual_update_tx.send(()).await.unwrap() }); } }) }; video_play_listener.forget(); - let update_interval = gloo::timers::callback::Interval::new(2_000, move || { + let update_stream = futures::stream_select!( + gloo::timers::future::IntervalStream::new(2_000), + manual_update_rx, + ); + let update_future = update_stream.for_each(move |_| { let history = history.clone(); let queue_position = queue_position.clone(); if !streaming.get() { - return; + return futures::future::ready(()); } let context = canvas.get_context("2d").unwrap().unwrap(); let context: CanvasRenderingContext2d = context.dyn_into().unwrap(); @@ -247,8 +261,9 @@ async fn do_boot() { .unwrap(), ) .unwrap(); + futures::future::ready(()) }); - update_interval.forget(); + spawn_local(update_future); } #[wasm_bindgen] -- cgit v1.2.3