The uncomplicated Yew State management library

Overview

Bounce

The uncomplicated state management library for Yew.

Bounce is inspired by Redux and Recoil.

Rationale

Yew state management solutions that are currently available all have some (or all) of the following limitations:

  • Too much boilerplate.

    Users either have to manually control whether to notify subscribers or have to manually define contexts.

    Changes should be detected automatically by PartialEq and not have to define contexts manually.

  • State change notifies all.

    State changes will notify all subscribers.

    The useSelector hook for Redux will only trigger a re-render when the selected value changes.

    If we were to create a state management library, it should also only notify the relevant subscribers.

  • Needless clones.

    A clone of the state will be produced for all subscribers whenever there's a change.

Example

For bounce states to function, a <BounceRoot /> must be registered.

#[function_component(App)]
fn app() -> Html {
    html! {
        <BounceRoot>
            {children}
        </BounceRoot>
    }
}

A simple state is called an Atom.

You can derive Atom for any struct that implements PartialEq and Default.

#[derive(PartialEq, Atom)]
struct Username {
    inner: String,
}

impl Default for Username {
    fn default() -> Self {
        Self {
            inner: "Jane Doe".into(),
        }
    }
}

You can then use it with the use_atom hook.

When an Atom is first used, it will be initialised with it's Default value.

#[function_component(Setter)]
fn setter() -> Html {
    let username = use_atom::<Username>();

    let on_text_input = {
        let username = username.clone();

        Callback::from(move |e: InputEvent| {
            let input: HtmlInputElement = e.target_unchecked_into();

            username.set(Username { inner: input.value().into() });
        })
    };

    html! {
        <div>
            <input type_="text" oninput={on_text_input} value={username.inner.to_string()} />
        </div>
    }
}

If you wish to create a read-only (or set-only) handle, you can use use_atom_value (or use_atom_setter).

#[function_component(Reader)]
fn reader() -> Html {
    let username = use_atom_value::<Username>();

    html! { <div>{"Hello, "}{&username.inner}</div> }
}

That's it!

You can find the full example here.

License

Bounce is dual licensed under the MIT license and the Apache License (Version 2.0).

Issues
  • Remove Selectors from master

    Remove Selectors from master

    Selectors are not ready yet.

    opened by futursolo 0
  • Derived States

    Derived States

    This issue aims to implement derived states (tentatively named Selector & Notion).

    A Selector is a read-only state that derives from other states.

    A Notion is a set-only state that manipulates other states.

    pub struct StateReader { ... }
    
    impl StateReader {
        pub fn get_slice_value<T: Slice>(&self);
        pub fn get_atom_value<T: Atom>(&self);
        pub fn get_selector_value<T: Selector>(&self);
    }
    
    pub struct StateSetter { ... }
    
    impl StateSetter {
        pub fn get_slice_value <T: Slice>(&self);
        pub fn get_atom_value <T: Atom>(&self);
        pub fn get_selector_value <T: Selector>(&self);
    
        pub fn dispatch_slice_action<T: Slice>(&self, T::Action);
        pub fn set_atom_value<T: Atom>(&self, val: T);
        pub fn apply_notion<T: Notion>(&self, val: T);
    }
    
    pub trait Selector {
        fn get(states: StateReader) -> Rc<Self>;
    }
    
    pub trait Notion {
        fn apply(self, states: StateSetter);
    }
    
    let val: Rc<T> = use_selector_value::<T>();
    let apply_t: Rc<dyn Fn(T)> = use_notion_applier::<T>();
    
    opened by futursolo 0
Owner
Kaede Hoshikawa
Kaede Hoshikawa
🐱‍👤 Cross-language static library for accessing the Lua state in Garry's Mod server plugins

gmserverplugin This is a utility library for making Server Plugins that access the Lua state in Garry's Mod. Currently, accessing the Lua state from a

William 4 Nov 1, 2021
Rust library to interface with Lua

hlua This library is a high-level binding for Lua 5.2. You don't have access to the Lua stack, all you can do is read/write variables (including callb

Pierre Krieger 468 Nov 26, 2021
A minimalist and safe ECS library for rust!

The full ECS (Entity-Component-System) library. Support an Open Source Developer! ♥️ Composed of two smaller libraries: world_dispatcher: the System p

Joël Lupien 114 Sep 24, 2021
A library for functional programming in Rust

It contains purely functional data structures to supplement the functional programming needs alongside with the Rust Standard Library.

Jason Shin 916 Nov 19, 2021
A rust library containing typings and utility functions dealing with the Public specification of the Internet Computer.

IC Types Contributing Please follow the guidelines in the CONTRIBUTING.md document. Goal This library contains typings and utility functions dealing w

DFINITY 3 Oct 21, 2021
Lineiform is a meta-JIT library for Rust interpreters

Lineiform Lineiform is a meta-JIT library for Rust interpreters. Given an interpreter that uses closure generation, it allows an author to add minimal

null 10 Sep 23, 2021
libnotcurses-sys is a low-level Rust wrapper for the notcurses C library

libnotcurses-sys is a low-level Rust wrapper for the notcurses C library This library is built with several layers of zero-overhead abstractions over

nick black 9 Nov 24, 2021
A minimal library for building compiled Node.js add-ons in Rust via Node-API

A minimal library for building compiled Node.js add-ons in Rust via Node-API

Node-API (N-API) for Rust 1.4k Nov 25, 2021
Stack unwinding library in Rust

Unwinding library in Rust and for Rust This library serves two purposes: Provide a pure Rust alternative to libgcc_eh or libunwind. Provide easier unw

Gary Guo 22 Nov 19, 2021
Node.js bindings to the ripgrep library, for fast file searching in JavaScript without child processes!

ripgrepjs ripgrepjs: Node.js bindings to the ripgrep library, for direct integration with JS programs without spawning an extra subprocess! This proje

Annika 0 Nov 7, 2021
Build a python wheel from a dynamic library

build_wheel Small utility to create a Python wheel given a pre-built dynamic library (.so, .dylib, .dll). If you are just trying to produce a wheel fr

Tangram 1 Nov 22, 2021
The polyglot bindings generator for your library (C#, C, Python, …) 🐙

Interoptopus ?? The polyglot bindings generator for your library. Interoptopus allows you to deliver high-quality system libraries to your users, and

Ralf Biedert 33 Nov 30, 2021
Fastest lz4 compression library in Node.js, powered by napi-rs and lz4-flex.

Lz4 Fastest lz4 compression library in Node.js, powered by napi-rs and lz4-flex. Install this package yarn add lz4-napi API export function compress:

Antonio Musolino 1 Nov 28, 2021
yew-material is a material-ui framework for yew

About yew-material is a material-ui framework for yew create-yew-material-app Create Yew Material App is a way to create a single page Yew application

jie.wang 309 Nov 20, 2021
VR Lighthouse power state management in Rust

Lighthouse VR Lighthouse power state management in Rust Windows and Linux binaries available here Usage SteamVR v1: lighthouse [on|off] [BSID] lightho

Shayne Hartford 18 Oct 22, 2021
RevonsOs is a new OS written from scratch in Rust to experiment with novel OS structure, better state management

RevonsOs is a new OS written from scratch in Rust to experiment with novel OS structure, better state management, and how to leverage intralingual design principles to shift OS responsibilities like resource management into the compiler.

Revons Community 2 Oct 12, 2021
A notebook app integrated with todo lists utility. Developed with Rust, WebAssembly, Yew and Trunk.

Flow.er A notebook app integrated with todo-list utility. Project flow.er is a Rust WASM app running in browser. Taking advantage of Yew and Trunk, it

null 31 Sep 13, 2021
Yew Tauri Desktop App Template

Yew Tauri Desktop App Template This is Template App for Desktop App. Run Develop $ cargo tauri dev Build App $ cargo tauri build Setup yew setup Using

usa 12 Nov 16, 2021
The lightest distributed consensus library. Run your own replicated state machine! ❤️

Little Raft The lightest distributed consensus library. Run your own replicated state machine! ❤️ Installing Simply import the crate. In your Cargo.to

Ilya Andreev 71 Nov 22, 2021
🐱‍👤 Cross-language static library for accessing the Lua state in Garry's Mod server plugins

gmserverplugin This is a utility library for making Server Plugins that access the Lua state in Garry's Mod. Currently, accessing the Lua state from a

William 4 Nov 1, 2021
An opinionated, practical color management library for games and graphics.

colstodian An opinionated color management library built on top of kolor. Introduction colstodian is a practical color management library for games an

Gray Olson 18 Nov 8, 2021
The new, performant, and simplified version of Holochain on Rust (sometimes called Holochain RSM for Refactored State Model)

Holochain License: This repository contains the core Holochain libraries and binaries. This is the most recent and well maintained version of Holochai

Holochain 489 Nov 21, 2021
💥 Fast State-of-the-Art Tokenizers optimized for Research and Production

Provides an implementation of today's most used tokenizers, with a focus on performance and versatility. Main features: Train new vocabularies and tok

Hugging Face 5k Nov 27, 2021
Represent large sets and maps compactly with finite state transducers.

fst This crate provides a fast implementation of ordered sets and maps using finite state machines. In particular, it makes use of finite state transd

Andrew Gallant 1.3k Nov 29, 2021
Synchronized state machines for Rust over WebSockets.

Aper is a framework for real-time sharing of application state over WebSockets.

null 90 Nov 20, 2021
💥 Fast State-of-the-Art Tokenizers optimized for Research and Production

Provides an implementation of today's most used tokenizers, with a focus on performance and versatility. Main features: Train new vocabularies and tok

Hugging Face 5k Nov 26, 2021
Cassette A simple, single-future, non-blocking executor intended for building state machines.

Cassette A simple, single-future, non-blocking executor intended for building state machines. Designed to be no-std and embedded friendly. This execut

James Munns 41 Oct 5, 2021
The new, performant, and simplified version of Holochain on Rust (sometimes called Holochain RSM for Refactored State Model)

Holochain License: This repository contains the core Holochain libraries and binaries. This is the most recent and well maintained version of Holochai

Holochain 493 Nov 25, 2021