Readable

Struct Readable 

Source
pub struct Readable<T> { /* private fields */ }
Expand description

Rust-managed Readable Svelte store.

Implementations§

Source§

impl<T: 'static> Readable<T>

Source

pub fn new(initial_value: T) -> Self
where T: Clone + Into<JsValue>,

Creates a Readable Svelte store.

This function is only implemented for types that can be converted into JsValue. This includes all types annotated with #[wasm_bindgen]. If your type does not provide an Into<JsValue> implementation, use Readable::new_mapped instead.

§Examples

Using a type that already provides an implementation of Into<JsValue>.

use svelte_store::Readable;

let store = Readable::new(0u8);

Using a type annotated with #[wasm_bindgen].

use svelte_store::Readable;
use wasm_bindgen::prelude::*;

#[derive(Clone)]
#[wasm_bindgen]
pub struct MyStruct;

let store = Readable::new(MyStruct);
Source

pub fn new_mapped<F>(initial_value: T, mapping_fn: F) -> Self
where F: FnMut(&T) -> JsValue + 'static,

Creates a new Readable Svelte store which calls its mapping fn each time the store is set, to produce a JsValue.

This method should be used whenever Readable::new cannot be, due to lacking trait compatibility.

§Examples

Creating a store of Vec<u8>.

use svelte_store::Readable;
use wasm_bindgen::prelude::*;

let values = vec![7u8; 7];

let store = Readable::new_mapped(values, |values: &Vec<u8>| {
    values
        .iter()
        .cloned()
        .map(JsValue::from)
        .collect::<js_sys::Array>()
        .into()
});
Source

pub fn set(&mut self, new_value: T)

Sets the value of the store, notifying all store listeners the value has changed.

Source

pub fn set_with<F, O>(&mut self, f: F) -> O
where F: FnOnce(&mut T) -> O,

Calls the provided f with a &mut T, returning whatever f returns. After this function is called, the store will be updated and all store listeners will be notified.

Source

pub fn get_store(&self) -> JsValue

Gets the store that can be directly passed to JS and subscribed to.

§Examples
use wasm_bindgen::prelude::*;
use svelte_store::Readable;

#[wasm_bindgen(typescript_custom_section)]
const TYPESCRIPT_TYPES: &str = r#"
import type { Readable } from "svelte/store";

type ReadableNumber = Readable<number>;
"#;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(typescript_type = "ReadableNumber")]
    type ReadableNumber;
}

#[wasm_bindgen]
pub struct MyStruct {
    my_number: Readable<u8>,
}

#[wasm_bindgen]
impl MyStruct {
    #[wasm_bindgen(getter)]
    pub fn number(&self) -> ReadableNumber {
        self.my_number.get_store().into()
    }
}

Trait Implementations§

Source§

impl<T> Debug for Readable<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Readable<T>
where T: Default + Clone + Into<JsValue> + 'static,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Deref for Readable<T>

Readable relies on the fact that only one instance can exist at a time to provide transparent dereferencing to the inner value. As a result, it is unsound to implement Clone. If you need shared mutability, try using Rc and RefCell.

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T> Display for Readable<T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Readable<T>

§

impl<T> !RefUnwindSafe for Readable<T>

§

impl<T> Send for Readable<T>
where T: Send,

§

impl<T> !Sync for Readable<T>

§

impl<T> Unpin for Readable<T>
where T: Unpin,

§

impl<T> UnwindSafe for Readable<T>
where T: UnwindSafe,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.