implement changes

This commit is contained in:
Sean Aye 2023-11-29 18:52:39 -05:00
parent f34e62fb6b
commit ba6082a32f
2 changed files with 20 additions and 17 deletions

View file

@ -14,7 +14,6 @@ members = [
"use_cycle_list",
"use_debounce_fn",
"use_display_media",
"use_display_media",
"use_document_visibility",
"use_draggable",
"use_drop_zone",

View file

@ -3,23 +3,8 @@ use wasm_bindgen::{JsValue, JsCast};
use web_sys::{DisplayMediaStreamConstraints, MediaStream};
use crate::use_window::use_window;
async fn create_media(opts: Option<DisplayMediaStreamConstraints>) -> Result<MediaStream, JsValue> {
let media = use_window()
.navigator()
.ok_or_else(|| JsValue::from_str("Failed to access window.navigator"))
.and_then(|n| n.media_devices())?;
let promise = match opts {
Some(o) => media.get_display_media_with_constraints(&o),
None => media.get_display_media(),
}?;
let res = wasm_bindgen_futures::JsFuture::from(promise).await?;
Ok::<_, JsValue>(MediaStream::unchecked_from_js(res))
}
type UseDisplayReturn = Resource<Option<DisplayMediaStreamConstraints>, Result<MediaStream, JsValue>>;
///
/// Get a media stream from the user's display.
///
/// ## Demo
///
@ -46,3 +31,22 @@ where
create_local_resource(move || opts.with(|o| o.as_ref().cloned()), create_media)
}
async fn create_media(opts: Option<DisplayMediaStreamConstraints>) -> Result<MediaStream, JsValue> {
let media = use_window()
.navigator()
.ok_or_else(|| JsValue::from_str("Failed to access window.navigator"))
.and_then(|n| n.media_devices())?;
let promise = match opts {
Some(o) => media.get_display_media_with_constraints(&o),
None => media.get_display_media(),
}?;
let res = wasm_bindgen_futures::JsFuture::from(promise).await?;
Ok::<_, JsValue>(MediaStream::unchecked_from_js(res))
}
/// A leptos resource which optionally accepts a [DisplayMediaParamContraints](https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.DisplayMediaStreamConstraints.html)
/// The resource contains a result containing the media stream or the rejected JsValue
type UseDisplayReturn = Resource<Option<DisplayMediaStreamConstraints>, Result<MediaStream, JsValue>>;