Skip to main content

JsObservable

Struct JsObservable 

Source
pub struct JsObservable { /* private fields */ }
Expand description

§Wrapper around Observable for usage in javascript/typescript



#[derive(Clone, Default)]
#[wasm_bindgen]
pub struct CatState { cats: usize };
impl CatState {
  fn new (cats: usize) -> Self {
    CatState{cats}
  }
}
#[wasm_bindgen]
impl CatState {
  #[wasm_bindgen(getter)]
  pub fn cats(&self) -> usize {
    self.cats
  }
}

#[derive(Default, Clone, Serialize)]
pub struct Bar(pub Vec<usize>);

impl Into<JsValue> for Bar {
    fn into(self) -> JsValue {
        JsValue::from_serde(&self).unwrap()
    }
}

let obs = Observable::new(CatState::new(1));
let obsJs: JsObservable = obs.clone().into();
let lastRustCats: Rc<RefCell<Option<usize>>> = Rc::new(RefCell::new(None));
let lrc = lastRustCats.clone();
obs.subscribe(Box::new(move |v|{
  *(lrc.borrow_mut()) = Some(v.cats);
  println!("Rust Cats: {}", v.cats);
}));
// In JS:
// catStateJs.subscribe(() => console.log(`JS Cats ${catstate.cats}`));

// We are not presently firing the listener for initial state
assert_eq!(*(lastRustCats.borrow()), None);
obs.set(CatState::new(7));
assert_eq!(*(lastRustCats.borrow()), Some(7));

// Both Rust Cats and JS Cats logs are printed

let barObsJs: JsObservable = Observable::new(Bar::default()).into();
let strObsJs: JsObservable = Observable::new(String::from("Meow")).into();
let intObsJs: JsObservable = Observable::new(123).into();
let fltObsJs: JsObservable = Observable::new(123.0).into();

Implementations§

Source§

impl JsObservable

Source

pub fn new(obs: Box<dyn JsObserve>) -> Self

Source§

impl JsObservable

Source

pub fn get(&self) -> JsValue

Source

pub fn map(&self, cb: Function) -> JsValue

Source

pub fn subscribe(&mut self, cb: Function) -> Function

Source

pub fn destroy(&self)

Source

pub fn value(&self) -> JsValue

Source

pub fn load(&self) -> Promise

Trait Implementations§

Source§

impl From<JsObservable> for JsValue

Source§

fn from(value: JsObservable) -> Self

Converts to this type from the input type.
Source§

impl<O> From<O> for JsObservable
where O: JsObserve + 'static + Sized,

Source§

fn from(obs: O) -> Self

Converts to this type from the input type.
Source§

impl FromWasmAbi for JsObservable

Source§

type Abi = WasmPtr<WasmRefCell<JsObservable>>

The Wasm ABI type that this converts from when coming back out from the ABI boundary.
Source§

unsafe fn from_abi(js: Self::Abi) -> Self

Recover a Self from Self::Abi. Read more
Source§

impl IntoWasmAbi for JsObservable

Source§

type Abi = WasmPtr<WasmRefCell<JsObservable>>

The Wasm ABI type that this converts into when crossing the ABI boundary.
Source§

fn into_abi(self) -> Self::Abi

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
Source§

impl LongRefFromWasmAbi for JsObservable

Source§

type Abi = WasmPtr<WasmRefCell<JsObservable>>

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRef<JsObservable>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl OptionFromWasmAbi for JsObservable

Source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
Source§

impl OptionIntoWasmAbi for JsObservable

Source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
Source§

impl RefFromWasmAbi for JsObservable

Source§

type Abi = WasmPtr<WasmRefCell<JsObservable>>

The Wasm ABI type references to Self are recovered from.
Source§

type Anchor = RcRef<JsObservable>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
Source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
Source§

impl RefMutFromWasmAbi for JsObservable

Source§

type Abi = WasmPtr<WasmRefCell<JsObservable>>

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRefMut<JsObservable>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl SupportsConstructor for JsObservable

Source§

impl SupportsInstanceProperty for JsObservable

Source§

impl SupportsStaticProperty for JsObservable

Source§

impl TryFromJsValue for JsObservable

Source§

fn try_from_js_value(value: JsValue) -> Result<Self, JsValue>

Performs the conversion.
Source§

fn try_from_js_value_ref(value: &JsValue) -> Option<Self>

Performs the conversion.
Source§

impl VectorFromWasmAbi for JsObservable

Source§

impl VectorIntoWasmAbi for JsObservable

Source§

impl WasmDescribe for JsObservable

Source§

impl WasmDescribeVector for JsObservable

Auto Trait Implementations§

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> ReturnWasmAbi for T
where T: IntoWasmAbi,

Source§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
Source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more