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§
Trait Implementations§
Source§impl From<JsObservable> for JsValue
impl From<JsObservable> for JsValue
Source§fn from(value: JsObservable) -> Self
fn from(value: JsObservable) -> Self
Converts to this type from the input type.
Source§impl<O> From<O> for JsObservable
impl<O> From<O> for JsObservable
Source§impl FromWasmAbi for JsObservable
impl FromWasmAbi for JsObservable
Source§impl IntoWasmAbi for JsObservable
impl IntoWasmAbi for JsObservable
Source§impl LongRefFromWasmAbi for JsObservable
impl LongRefFromWasmAbi for JsObservable
Source§impl OptionFromWasmAbi for JsObservable
impl OptionFromWasmAbi for JsObservable
Source§impl OptionIntoWasmAbi for JsObservable
impl OptionIntoWasmAbi for JsObservable
Source§impl RefFromWasmAbi for JsObservable
impl RefFromWasmAbi for JsObservable
Source§type Anchor = RcRef<JsObservable>
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§impl RefMutFromWasmAbi for JsObservable
impl RefMutFromWasmAbi for JsObservable
Source§impl TryFromJsValue for JsObservable
impl TryFromJsValue for JsObservable
Source§impl VectorFromWasmAbi for JsObservable
impl VectorFromWasmAbi for JsObservable
type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi
unsafe fn vector_from_abi(js: Self::Abi) -> Box<[JsObservable]>
Source§impl VectorIntoJsValue for JsObservable
impl VectorIntoJsValue for JsObservable
fn vector_into_jsvalue(vector: Box<[JsObservable]>) -> JsValue
Source§impl VectorIntoWasmAbi for JsObservable
impl VectorIntoWasmAbi for JsObservable
type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi
fn vector_into_abi(vector: Box<[JsObservable]>) -> Self::Abi
Source§impl WasmDescribeVector for JsObservable
impl WasmDescribeVector for JsObservable
impl SupportsConstructor for JsObservable
impl SupportsInstanceProperty for JsObservable
impl SupportsStaticProperty for JsObservable
Auto Trait Implementations§
impl Freeze for JsObservable
impl !RefUnwindSafe for JsObservable
impl !Send for JsObservable
impl !Sync for JsObservable
impl Unpin for JsObservable
impl !UnwindSafe for JsObservable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
Same as
IntoWasmAbi::Abi
Source§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
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
.