Feat: rename recoil to atoms
This commit is contained in:
1 parent
47e896038e
commit
36ea39ae30
29 files changed
+398
-249
No files matched your search
@@ -0,0 +1,45 @@
|
||||
//! RecoilAPI Pattern
|
||||
//! ----------------
|
||||
//! This example demonstrates how the use_recoil_callback hook can be used to build view controllers.
|
||||
//! These view controllers are cheap to make and can be easily shared across the app to provide global
|
||||
//! state logic to many components.
|
||||
//!
|
||||
//! This pattern is meant to replace the typical use_dispatch pattern used in Redux apps.
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
use recoil::*;
|
||||
|
||||
const TITLE: Atom<String> = |_| format!("Heading");
|
||||
const SUBTITLE: Atom<String> = |_| format!("Subheading");
|
||||
|
||||
struct TitleController(RecoilApi);
|
||||
impl TitleController {
|
||||
fn new(api: RecoilApi) -> Self {
|
||||
Self(api)
|
||||
}
|
||||
fn uppercase(&self) {
|
||||
self.0.modify(&TITLE, |f| *f = f.to_uppercase());
|
||||
self.0.modify(&SUBTITLE, |f| *f = f.to_uppercase());
|
||||
}
|
||||
fn lowercase(&self) {
|
||||
self.0.modify(&TITLE, |f| *f = f.to_lowercase());
|
||||
self.0.modify(&SUBTITLE, |f| *f = f.to_lowercase());
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(|ctx| {
|
||||
let title = use_read(&ctx, &TITLE);
|
||||
let subtitle = use_read(&ctx, &SUBTITLE);
|
||||
let controller = TitleController::new(use_recoil_api(&ctx));
|
||||
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
"{title}"
|
||||
"{subtitle}"
|
||||
button { onclick: move |_| controller.uppercase(), "Uppercase" }
|
||||
button { onclick: move |_| controller.lowercase(), "Lowercase" }
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//! Example: RecoilCallback
|
||||
//! ------------------------
|
||||
//! This example shows how use_recoil_callback can be used to abstract over sets/gets.
|
||||
//! This hook provides a way to capture the RecoilApi object. In this case, we capture
|
||||
//! it in a closure an abstract the set/get functionality behind the update_title function.
|
||||
//!
|
||||
//! It should be straightforward to build a complex app with recoil_callback.
|
||||
use dioxus_core::prelude::*;
|
||||
use recoil::*;
|
||||
|
||||
const TITLE: Atom<&str> = |_| "red";
|
||||
|
||||
fn update_title(api: &RecoilApi) {
|
||||
match *api.get(&TITLE) {
|
||||
"green" => api.set(&TITLE, "yellow"),
|
||||
"yellow" => api.set(&TITLE, "red"),
|
||||
"red" => api.set(&TITLE, "green"),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
static App: FC<()> = |ctx| {
|
||||
let title = use_read(&ctx, &TITLE);
|
||||
let next_light = use_recoil_api(&ctx, |api| move |_| update_title(&api));
|
||||
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
"{title}"
|
||||
button { onclick: {next_light}, "Next light" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//! Example: ECS Architecture for very list selectors
|
||||
//! --------------------------------------------------
|
||||
//! Sometimes, you need *peak* performance. Cloning and Rc might simply be too much overhead for your app.
|
||||
//! If you're building CPU intense apps like graphics editors, simulations, or advanced visualizations,
|
||||
//! slicing up your state beyond atoms might be desirable.
|
||||
//!
|
||||
//! Instead of storing groups of entities in a collection of structs, the ECS Architecture instead stores
|
||||
//! an array for each field in of a struct. This tends to improve performance for batch operations on
|
||||
//! individual fields at the cost of complexity. Fortunately, this ECS model is built right into Recoil,
|
||||
//! making it easier than ever to enable sharded datastructures in your app.
|
||||
//!
|
||||
//! Instead of defining a struct for our primary datastructure, we'll instead use a type tuple, and then later
|
||||
//! index that tuple to get the value we care about. Unfortunately, we lose name information wrt to each
|
||||
//! type in the type tuple. This can be solved with an associated module, the derive EcsMacro, or just
|
||||
//! by good documentation.
|
||||
//!
|
||||
//! This approach is best suited for applications where individual entries in families are very large
|
||||
//! and updates to neighbors are costly in terms of Clone or field comparisons for memoization.
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus_core as dioxus;
|
||||
use recoil::*;
|
||||
|
||||
type TodoModel = (
|
||||
bool, // checked
|
||||
String, // name
|
||||
String, // contents
|
||||
);
|
||||
|
||||
const TODOS: EcsModel<u32, TodoModel> = |builder| {};
|
||||
// const SELECT_TITLE: SelectorBorrowed<u32, &str> = |s, k| TODOS.field(0).select(k);
|
||||
// const SELECT_SUBTITLE: SelectorBorrowed<u32, &str> = |s, k| TODOS.field(1).select(k);
|
||||
|
||||
static App: FC<()> = |ctx| {
|
||||
use_init_recoil_root(ctx, |_| {});
|
||||
|
||||
// let title = use_recoil_value(ctx, &C_SELECTOR);
|
||||
|
||||
let title = "";
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
"{title}"
|
||||
// button { onclick: {next_light}, "Next light" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
use std::{collections::HashMap, rc::Rc};
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
use recoil::*;
|
||||
use uuid::Uuid;
|
||||
|
||||
const TODOS: AtomHashMap<Uuid, Rc<Todo>> = |map| {};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct Todo {
|
||||
checked: bool,
|
||||
title: String,
|
||||
content: String,
|
||||
}
|
||||
|
||||
static App: FC<()> = |ctx| {
|
||||
use_init_recoil_root(ctx, move |cfg| {});
|
||||
|
||||
let todos = use_read_family(ctx, &TODOS);
|
||||
|
||||
// rsx! { in ctx,
|
||||
// div {
|
||||
// h1 {"Basic Todolist with AtomFamilies in Recoil.rs"}
|
||||
// ul { { todos.keys().map(|id| rsx! { Child { id: *id } }) } }
|
||||
// }
|
||||
// }
|
||||
ctx.render(html! {
|
||||
<a href="#" class="">
|
||||
<img class="inline-block h-10 w-10 rounded-full object-cover ring-2 ring-white" src="/images/person/4.jpg" alt="Jade"/>
|
||||
</a>
|
||||
})
|
||||
};
|
||||
|
||||
#[derive(Props, PartialEq)]
|
||||
struct ChildProps {
|
||||
id: Uuid,
|
||||
}
|
||||
|
||||
static Child: FC<ChildProps> = |ctx| {
|
||||
let (todo, set_todo) = use_read_write(ctx, &TODOS.select(&ctx.id));
|
||||
|
||||
rsx! { in ctx,
|
||||
li {
|
||||
h1 {"{todo.title}"}
|
||||
input { type: "checkbox", name: "scales", checked: "{todo.checked}" }
|
||||
label { "{todo.content}", for: "scales" }
|
||||
p {"{todo.content}"}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
use dioxus_core::prelude::*;
|
||||
use im_rc::HashMap as ImMap;
|
||||
use recoil::*;
|
||||
use uuid::Uuid;
|
||||
|
||||
const TODOS: Atom<ImMap<Uuid, Todo>> = |_| ImMap::new();
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct Todo {
|
||||
checked: bool,
|
||||
title: String,
|
||||
contents: String,
|
||||
}
|
||||
|
||||
static App: FC<()> = |ctx| {
|
||||
use_init_recoil_root(ctx, |_| {});
|
||||
let todos = use_read(&ctx, &TODOS);
|
||||
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
"Basic Todolist with AtomFamilies in Recoil.rs"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#[derive(Props, PartialEq)]
|
||||
struct ChildProps {
|
||||
id: Uuid,
|
||||
}
|
||||
|
||||
static Child: FC<ChildProps> = |ctx| {
|
||||
let todo = use_read(ctx, &TODOS).get(&ctx.id).unwrap();
|
||||
// let (todo, set_todo) = use_read_write(ctx, &TODOS);
|
||||
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
h1 {"{todo.title}"}
|
||||
input { type: "checkbox", name: "scales", checked: "{todo.checked}" }
|
||||
label { "{todo.contents}", for: "scales" }
|
||||
p {"{todo.contents}"}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
use dioxus_core::prelude::*;
|
||||
use recoil::*;
|
||||
|
||||
const COUNT: Atom<i32> = |_| 0;
|
||||
|
||||
static App: FC<()> = |ctx| {
|
||||
use_init_recoil_root(ctx, |_| {});
|
||||
|
||||
let (count, set_count) = use_read_write(&ctx, &COUNT);
|
||||
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
"Count: {count}"
|
||||
br {}
|
||||
button { onclick: move |_| set_count(count + 1), "<Incr" }
|
||||
">___<"
|
||||
button { onclick: move |_| set_count(count - 1), "Decr>" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {
|
||||
// Setup logging
|
||||
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
log::debug!("asd");
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App));
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
use recoil::*;
|
||||
|
||||
const A_ITEMS: AtomHashMap<i32, i32> = |_| HashMap::new();
|
||||
const B_ITEMS: AtomHashMap<i32, i32> = |_| HashMap::new();
|
||||
|
||||
const C_SELECTOR: SelectorFamily<i32, i32> = |api, key| {
|
||||
let a = api.get(&A_ITEMS.select(&key));
|
||||
let b = api.get(&B_ITEMS.select(&key));
|
||||
a + b
|
||||
};
|
||||
|
||||
const D_SELECTOR: SelectorFamilyBorrowed<i32, i32> = |api, key| -> &i32 {
|
||||
let a = api.get(&A_ITEMS.select(&key));
|
||||
a
|
||||
};
|
||||
|
||||
static App: FC<()> = |ctx| {
|
||||
use_init_recoil_root(ctx, |_| {});
|
||||
|
||||
let title = use_read(ctx, &C_SELECTOR);
|
||||
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
"{title}"
|
||||
// button { onclick: {next_light}, "Next light" }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
use dioxus_core::prelude::*;
|
||||
use recoil::*;
|
||||
|
||||
const A: Atom<i32> = |_| 0;
|
||||
const B: Atom<i32> = |_| 0;
|
||||
const C: Selector<i32> = |api| api.get(&A) + api.get(&B);
|
||||
|
||||
static App: FC<()> = |ctx| {
|
||||
use_init_recoil_root(ctx, |_| {});
|
||||
rsx! { in ctx,
|
||||
div {
|
||||
Banner {}
|
||||
BtnA {}
|
||||
BtnB {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static Banner: FC<()> = |ctx| {
|
||||
let count = use_read(&ctx, &C);
|
||||
ctx.render(rsx! { h1 { "Count: {count}" } })
|
||||
};
|
||||
|
||||
static BtnA: FC<()> = |ctx| {
|
||||
let (a, set) = use_read_write(&ctx, &A);
|
||||
rsx! { in ctx,
|
||||
div { "a"
|
||||
button { "+", onclick: move |_| set(a + 1) }
|
||||
button { "-", onclick: move |_| set(a - 1) }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static BtnB: FC<()> = |ctx| {
|
||||
let (b, set) = use_read_write(&ctx, &B);
|
||||
rsx! { in ctx,
|
||||
div { "b"
|
||||
button { "+", onclick: move |_| set(b + 1) }
|
||||
button { "-", onclick: move |_| set(b - 1) }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// const Posts: AtomFamily = |family| {
|
||||
// family.on_get(|key| {
|
||||
|
||||
// //
|
||||
// })
|
||||
// };
|
||||
|
||||
fn main() {
|
||||
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
|
||||
}
|
||||
|
||||
use std::future::Future;
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
|
||||
static App: FC<()> = |ctx| {
|
||||
//
|
||||
|
||||
let title = use_async_atom();
|
||||
let title_card = suspend(&ctx, title, move |val| {
|
||||
//
|
||||
rsx!(in ctx, div {
|
||||
h3 { "{val}" }
|
||||
})
|
||||
});
|
||||
|
||||
// let fut = (use_async_atom(), use_async_atom());
|
||||
// let title_card2 = ctx.suspend(fut, move |(text, text2)| {
|
||||
// ctx.render(rsx!( h3 { "{text}" } ))
|
||||
// });
|
||||
|
||||
ctx.render(rsx! {
|
||||
div {
|
||||
{title_card}
|
||||
// {title_card2}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
async fn use_async_atom() -> String {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn suspend<'a, O>(
|
||||
c: &impl Scoped<'a>,
|
||||
f: impl Future<Output = O>,
|
||||
g: impl FnOnce(O) -> VNode<'a> + 'a,
|
||||
) -> VNode<'a> {
|
||||
todo!()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
println!("asd")
|
||||
Reference in new issue
Block a user