Struct JsArc

Source
pub struct JsArc { /* private fields */ }

Implementations§

Source§

impl JsArc

Source

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;
Source

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;
Source

pub fn arc(self) -> Arc<Self>

Wraps in an Arc. The impl is the following

Arc::new(self)
Source

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;
Source

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;
Source

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;
Source

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;
Source

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;
Source

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;    

Trait Implementations§

Source§

impl<'a> Add for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a> BitAnd for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl<'a> BitOr for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl<'a> BitXor for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXor for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<'a> Div for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl Drop for JsArc

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> Mul for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a> Neg for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Neg for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<'a> Not for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl Not for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<'a> Shl for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Self) -> Self::Output

Performs the << operation. Read more
Source§

impl Shl for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Self) -> Self::Output

Performs the << operation. Read more
Source§

impl<'a> Shr for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Self) -> Self::Output

Performs the >> operation. Read more
Source§

impl Shr for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Self) -> Self::Output

Performs the >> operation. Read more
Source§

impl<'a> Sub for &'a JsArc

Source§

type Output = JsArcFutureWrapLifetime<'a>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for JsArc

Source§

type Output = JsArcFutureWrap

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Freeze for JsArc

§

impl RefUnwindSafe for JsArc

§

impl Send for JsArc

§

impl Sync for JsArc

§

impl Unpin for JsArc

§

impl UnwindSafe for JsArc

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.