mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-03-13 01:09:48 -04:00
implement changes
This commit is contained in:
parent
f34e62fb6b
commit
ba6082a32f
2 changed files with 20 additions and 17 deletions
|
@ -14,7 +14,6 @@ members = [
|
||||||
"use_cycle_list",
|
"use_cycle_list",
|
||||||
"use_debounce_fn",
|
"use_debounce_fn",
|
||||||
"use_display_media",
|
"use_display_media",
|
||||||
"use_display_media",
|
|
||||||
"use_document_visibility",
|
"use_document_visibility",
|
||||||
"use_draggable",
|
"use_draggable",
|
||||||
"use_drop_zone",
|
"use_drop_zone",
|
||||||
|
|
|
@ -3,23 +3,8 @@ use wasm_bindgen::{JsValue, JsCast};
|
||||||
use web_sys::{DisplayMediaStreamConstraints, MediaStream};
|
use web_sys::{DisplayMediaStreamConstraints, MediaStream};
|
||||||
use crate::use_window::use_window;
|
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 {
|
/// Get a media stream from the user's display.
|
||||||
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>>;
|
|
||||||
|
|
||||||
///
|
|
||||||
///
|
///
|
||||||
/// ## Demo
|
/// ## Demo
|
||||||
///
|
///
|
||||||
|
@ -46,3 +31,22 @@ where
|
||||||
create_local_resource(move || opts.with(|o| o.as_ref().cloned()), create_media)
|
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>>;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue