pub struct JsArc { /* private fields */ }
Implementations§
Source§impl JsArc
impl JsArc
Sourcepub async fn new<F: Fn() -> JsValue + Send + Sync + 'static>(
initialization_closure: F,
) -> JsArc
pub async fn new<F: Fn() -> JsValue + Send + Sync + 'static>( initialization_closure: F, ) -> JsArc
Creates a wrapped JsValue from an initialization closure.
let js_value = JsArc::new(|| "Hello World!".into()).await;
Sourcepub async fn new_async<K: Future<Output = JsValue> + 'static, F: Fn() -> K + Send + Sync + 'static>(
initialization_closure: F,
) -> JsArc
pub async fn new_async<K: Future<Output = JsValue> + 'static, F: Fn() -> K + Send + Sync + 'static>( initialization_closure: F, ) -> JsArc
Creates a wrapped JsValue from an async initialization closure.
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen]
pub type HelloWorldModule;
#[wasm_bindgen(method, js_name = "helloWorld")]
pub fn hello_world(this: &HelloWorldModule);
}
pub async fn load_dynamic_hello_world() -> HelloWorldModule {
let module_as_str = r#"export function helloWorld() { console.log("Hello World!"); }"#;
let from_data = Array::new();
from_data.push(&module_as_str.into());
let mut type_set: BlobPropertyBag = BlobPropertyBag::new();
type_set.type_("application/javascript");
let blob = Blob::new_with_str_sequence_and_options(&from_data, &type_set).unwrap();
let module_address = web_sys::Url::create_object_url_with_blob(&blob).unwrap();
let module_promise: Promise = js_sys::eval(&format!(r#"import ("{}")"#, module_address))
.unwrap()
.into();
let module = JsFuture::from(module_promise).await.unwrap();
let as_hello_world: HelloWorldModule = module.into();
as_hello_world.hello_world();
as_hello_world
}
let module = JsArc::new_async(|| async { load_dynamic_hello_world().await.into() }).await;
Sourcepub async fn with_self<F: Fn(JsValue) -> JsValue + Send + Sync + 'static>(
&self,
usage_closure: F,
)
pub async fn with_self<F: Fn(JsValue) -> JsValue + Send + Sync + 'static>( &self, usage_closure: F, )
Provides the ability to use and modify the JsValue abstracted by the wrapped JsValue
let js_v = JsArc::new(|| "Hello World!".into()).await;
js_v.with_self(|js_value| {
web_sys::console::log_1(&js_value);
js_value
})
.await;
The value returned from the closure is the value saved for the abstraction.
The following outputs 7
let js_v = JsArc::new(|| 2.into()).await;
js_v.with_self(|one| one + &5.into()).await;
js_v.with_self(|js_v| {
web_sys::console::log_1(&js_v);
js_v
})
.await;
Sourcepub async fn with_self_async<R: Future<Output = JsValue> + 'static, F: Fn(JsValue) -> R + Send + Sync + 'static>(
&self,
v: F,
)
pub async fn with_self_async<R: Future<Output = JsValue> + 'static, F: Fn(JsValue) -> R + Send + Sync + 'static>( &self, v: F, )
Provides the ability to asynchronously use and modify the JsValue abstracted by the wrapped JsValue
js_value
.with_self_async(|hello| async move {
web_sys::console::log_1(&"Waiting 5 second then printing value".into());
async_std::task::sleep(Duration::from_secs(5)).await;
web_sys::console::log_1(&hello);
hello
})
.await;
Sourcepub async fn with_other<F: Fn(JsValue, JsValue) -> (JsValue, JsValue, JsValue) + Send + Sync + 'static>(
&self,
other: &Self,
v: F,
) -> JsArc
pub async fn with_other<F: Fn(JsValue, JsValue) -> (JsValue, JsValue, JsValue) + Send + Sync + 'static>( &self, other: &Self, v: F, ) -> JsArc
Provides the ability to use and modify two JsValues at once while also creating a new abstracted JsValue for a third value
let one = JsArc::new(|| 1.into()).await;
let two = JsArc::new(|| 2.into()).await;
let three = one
.with_other(&two, |one, two| {
let add_result: JsValue = &one + &two;
(one, two, add_result)
})
.await;
three
.with_self(|three| {
web_sys::console::log_1(&three);
three
})
.await;
Sourcepub async fn with_other_async<R: Future<Output = (JsValue, JsValue, JsValue)> + 'static, F: Fn(JsValue, JsValue) -> R + Send + Sync + 'static>(
&self,
other: &Self,
v: F,
) -> JsArc
pub async fn with_other_async<R: Future<Output = (JsValue, JsValue, JsValue)> + 'static, F: Fn(JsValue, JsValue) -> R + Send + Sync + 'static>( &self, other: &Self, v: F, ) -> JsArc
Provides the ability to asynchronously use and modify two JsValues at once while also creating a new abstracted JsValue for a third value
let five = three
.with_other_async(&two, |three, two| async {
web_sys::console::log_1(&"Waiting 1 second then adding values".into());
async_std::task::sleep(Duration::from_secs(1)).await;
let add_result: JsValue = &three + &two;
(three, two, add_result)
})
.await;
five.with_self(|five| {
web_sys::console::log_1(&five);
five
})
.await;
Sourcepub async fn with_many<F: Fn(Vec<JsValue>) -> Vec<JsValue> + Send + Sync + 'static>(
&self,
others: Vec<&Self>,
closure_all_returns_all_and_extra: F,
) -> JsArc
pub async fn with_many<F: Fn(Vec<JsValue>) -> Vec<JsValue> + Send + Sync + 'static>( &self, others: Vec<&Self>, closure_all_returns_all_and_extra: F, ) -> JsArc
Places the &self value in the front of the Vec, then calls the defined closure. It expects to end with an extra value at the end of the Vec.
let v0 = JsArc::new(|| 0.into()).await;
let v1 = JsArc::new(|| 1.into()).await;
let v2 = JsArc::new(|| 2.into()).await;
let v3 = JsArc::new(|| 3.into()).await;
let v4 = JsArc::new(|| 4.into()).await;
let ten = v0
.with_many(vec![&v1, &v2, &v3, &v4], |mut all| {
let [v0, v1, v2, v3, v4] = &all[..] else {
unreachable!("Not possible");
};
let result = v0 + v1 + v2 + v3 + v4;
all.push(result);
all
})
.await;
Sourcepub async fn with_many_async<FUT: Future<Output = Vec<JsValue>> + 'static, F: Fn(Vec<JsValue>) -> FUT + Send + Sync + 'static>(
&self,
others: Vec<&Self>,
closure_all_returns_all_and_extra: F,
) -> JsArc
pub async fn with_many_async<FUT: Future<Output = Vec<JsValue>> + 'static, F: Fn(Vec<JsValue>) -> FUT + Send + Sync + 'static>( &self, others: Vec<&Self>, closure_all_returns_all_and_extra: F, ) -> JsArc
Places the &self value in the front of the Vec, then calls the defined closure. It expects to end with an extra value at the end of the Vec.
let v0 = JsArc::new(|| 0.into()).await;
let v1 = JsArc::new(|| 1.into()).await;
let v2 = JsArc::new(|| 2.into()).await;
let v3 = JsArc::new(|| 3.into()).await;
let v4 = JsArc::new(|| 4.into()).await;
let twenty = v0
.with_many_async(vec![&v1, &v2, &v3, &v4], |mut all| async {
let [v0, v1, v2, v3, v4] = &all[..] else {
unreachable!("Not possible");
};
let result = (v0 + v1 + v2 + v3 + v4) * v2;
web_sys::console::log_1(&"Waiting 1 second before continuing".into());
async_std::task::sleep(Duration::from_secs(1)).await;
all.push(result);
all
})
.await;
twenty
.with_self(|result| {
web_sys::console::log_1(&result);
result
})
.await;