chore: rename ctx to cx

This commit is contained in:
Jonathan Kelley committed 2021-06-25 21:15:33 -04:00
1 parent 23c14078bb
commit 81382e7044
99 files changed
+789 -664

No files matched your search

+14 -14
View File
@@ -53,23 +53,23 @@ This atom of state is initialized with a value of `"Green"`. The atom that is re
This is then later used in components like so:
```rust
fn App(ctx: Context<()>) -> VNode {
fn App(cx: Context<()>) -> VNode {
// The Atom root must be initialized at the top of the application before any use_Atom hooks
dirac::intialize_root(&ctx, |_| {});
dirac::intialize_root(&cx, |_| {});
let color = dirac::use_read(&ctx, &LightColor);
let color = dirac::use_read(&cx, &LightColor);
ctx.render(rsx!( h1 {"Color of light: {color}"} ))
cx.render(rsx!( h1 {"Color of light: {color}"} ))
}
```
Atoms are considered "Writable" objects since any consumer may also set the Atom's value with their own:
```rust
fn App(ctx: Context<()>) -> VNode {
let color = dirac::use_read(&ctx, &LightColor);
let set_color = dirac::use_write(&ctx, &LightColor);
ctx.render(rsx!(
fn App(cx: Context<()>) -> VNode {
let color = dirac::use_read(&cx, &LightColor);
let set_color = dirac::use_write(&cx, &LightColor);
cx.render(rsx!(
h1 { "Color: {color}" }
button { onclick: move |_| set_color("red"), "red" }
button { onclick: move |_| set_color("yellow"), "yellow" }
@@ -81,9 +81,9 @@ fn App(ctx: Context<()>) -> VNode {
"Reading" a value with use_read subscribes that component to any changes to that value while "Writing" a value does not. It's a good idea to use `write-only` whenever it makes sense to prevent unnecessary evaluations. Both `read` and `write` may be combined together to provide a `use_state` style hook.
```rust
fn App(ctx: Context<()>) -> VNode {
let (color, set_color) = dirac::use_read_write(&ctx, &Light);
ctx.render(rsx!(
fn App(cx: Context<()>) -> VNode {
let (color, set_color) = dirac::use_read_write(&cx, &Light);
cx.render(rsx!(
h1 { "Color: {color}" }
button { onclick: move |_| set_color("red"), "red" }
))
@@ -154,9 +154,9 @@ const CloudRatings: AtomFamily<&str, i32> = |api| {
Whenever you `select` on a `Family`, the ID of the entry is tracked. Other subscribers will only be updated if they too select the same family with the same key and that value is updated. Otherwise, these subscribers will never re-render on an "insert", "remove", or "update" of the collection. You could easily implement this yourself with Atoms, immutable datastructures, and selectors, but our implementation is more efficient and first-class.
```rust
fn App(ctx: Context<()>) -> VNode {
let (rating, set_rating) = dirac::use_read_write(ctx, CloudRatings.select("AWS"));
ctx.render(rsx!(
fn App(cx: Context<()>) -> VNode {
let (rating, set_rating) = dirac::use_read_write(cx, CloudRatings.select("AWS"));
cx.render(rsx!(
h1 { "AWS rating: {rating}" }
button { onclick: move |_| set_rating((rating + 1) % 5), "incr" }
))
+5 -5
View File
@@ -28,12 +28,12 @@ impl TitleController {
}
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));
wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(|cx| {
let title = use_read(&cx, &TITLE);
let subtitle = use_read(&cx, &SUBTITLE);
let controller = TitleController::new(use_recoil_api(&cx));
rsx! { in ctx,
rsx! { in cx,
div {
"{title}"
"{subtitle}"
+4 -4
View File
@@ -19,11 +19,11 @@ fn update_title(api: &RecoilApi) {
}
}
static App: FC<()> = |ctx| {
let title = use_read(&ctx, &TITLE);
let next_light = use_recoil_api(&ctx, |api| move |_| update_title(&api));
static App: FC<()> = |cx| {
let title = use_read(&cx, &TITLE);
let next_light = use_recoil_api(&cx, |api| move |_| update_title(&api));
rsx! { in ctx,
rsx! { in cx,
div {
"{title}"
button { onclick: {next_light}, "Next light" }
+4 -4
View File
@@ -31,13 +31,13 @@ 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, |_| {});
static App: FC<()> = |cx| {
use_init_recoil_root(cx, |_| {});
// let title = use_recoil_value(ctx, &C_SELECTOR);
// let title = use_recoil_value(cx, &C_SELECTOR);
let title = "";
rsx! { in ctx,
rsx! { in cx,
div {
"{title}"
// button { onclick: {next_light}, "Next light" }
+8 -8
View File
@@ -13,18 +13,18 @@ struct Todo {
content: String,
}
static App: FC<()> = |ctx| {
use_init_recoil_root(ctx, move |cfg| {});
static App: FC<()> = |cx| {
use_init_recoil_root(cx, move |cfg| {});
let todos = use_read_family(ctx, &TODOS);
let todos = use_read_family(cx, &TODOS);
// rsx! { in ctx,
// rsx! { in cx,
// div {
// h1 {"Basic Todolist with AtomFamilies in Recoil.rs"}
// ul { { todos.keys().map(|id| rsx! { Child { id: *id } }) } }
// }
// }
ctx.render(html! {
cx.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>
@@ -36,10 +36,10 @@ struct ChildProps {
id: Uuid,
}
static Child: FC<ChildProps> = |ctx| {
let (todo, set_todo) = use_read_write(ctx, &TODOS.select(&ctx.id));
static Child: FC<ChildProps> = |cx| {
let (todo, set_todo) = use_read_write(cx, &TODOS.select(&cx.id));
rsx! { in ctx,
rsx! { in cx,
li {
h1 {"{todo.title}"}
input { type: "checkbox", name: "scales", checked: "{todo.checked}" }
+8 -8
View File
@@ -12,11 +12,11 @@ struct Todo {
contents: String,
}
static App: FC<()> = |ctx| {
use_init_recoil_root(ctx, |_| {});
let todos = use_read(&ctx, &TODOS);
static App: FC<()> = |cx| {
use_init_recoil_root(cx, |_| {});
let todos = use_read(&cx, &TODOS);
rsx! { in ctx,
rsx! { in cx,
div {
"Basic Todolist with AtomFamilies in Recoil.rs"
}
@@ -28,11 +28,11 @@ 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);
static Child: FC<ChildProps> = |cx| {
let todo = use_read(cx, &TODOS).get(&cx.id).unwrap();
// let (todo, set_todo) = use_read_write(cx, &TODOS);
rsx! { in ctx,
rsx! { in cx,
div {
h1 {"{todo.title}"}
input { type: "checkbox", name: "scales", checked: "{todo.checked}" }
+4 -4
View File
@@ -3,12 +3,12 @@ use recoil::*;
const COUNT: Atom<i32> = |_| 0;
static App: FC<()> = |ctx| {
use_init_recoil_root(ctx, |_| {});
static App: FC<()> = |cx| {
use_init_recoil_root(cx, |_| {});
let (count, set_count) = use_read_write(&ctx, &COUNT);
let (count, set_count) = use_read_write(&cx, &COUNT);
rsx! { in ctx,
rsx! { in cx,
div {
"Count: {count}"
br {}
+4 -4
View File
@@ -17,12 +17,12 @@ const D_SELECTOR: SelectorFamilyBorrowed<i32, i32> = |api, key| -> &i32 {
a
};
static App: FC<()> = |ctx| {
use_init_recoil_root(ctx, |_| {});
static App: FC<()> = |cx| {
use_init_recoil_root(cx, |_| {});
let title = use_read(ctx, &C_SELECTOR);
let title = use_read(cx, &C_SELECTOR);
rsx! { in ctx,
rsx! { in cx,
div {
"{title}"
// button { onclick: {next_light}, "Next light" }
+12 -12
View File
@@ -5,9 +5,9 @@ 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,
static App: FC<()> = |cx| {
use_init_recoil_root(cx, |_| {});
rsx! { in cx,
div {
Banner {}
BtnA {}
@@ -16,14 +16,14 @@ static App: FC<()> = |ctx| {
}
};
static Banner: FC<()> = |ctx| {
let count = use_read(&ctx, &C);
ctx.render(rsx! { h1 { "Count: {count}" } })
static Banner: FC<()> = |cx| {
let count = use_read(&cx, &C);
cx.render(rsx! { h1 { "Count: {count}" } })
};
static BtnA: FC<()> = |ctx| {
let (a, set) = use_read_write(&ctx, &A);
rsx! { in ctx,
static BtnA: FC<()> = |cx| {
let (a, set) = use_read_write(&cx, &A);
rsx! { in cx,
div { "a"
button { "+", onclick: move |_| set(a + 1) }
button { "-", onclick: move |_| set(a - 1) }
@@ -31,9 +31,9 @@ static BtnA: FC<()> = |ctx| {
}
};
static BtnB: FC<()> = |ctx| {
let (b, set) = use_read_write(&ctx, &B);
rsx! { in ctx,
static BtnB: FC<()> = |cx| {
let (b, set) = use_read_write(&cx, &B);
rsx! { in cx,
div { "b"
button { "+", onclick: move |_| set(b + 1) }
button { "-", onclick: move |_| set(b - 1) }
+1 -1
View File
@@ -2,7 +2,7 @@ fn main() {}
use dioxus_core::prelude::*;
static App: FC<()> = |ctx| {
static App: FC<()> = |cx| {
//
use_initialize_app()
};
@@ -13,23 +13,23 @@ use std::future::Future;
use dioxus_core::prelude::*;
static App: FC<()> = |ctx| {
static App: FC<()> = |cx| {
//
let title = use_async_atom();
let title_card = suspend(&ctx, title, move |val| {
let title_card = suspend(&cx, title, move |val| {
//
rsx!(in ctx, div {
rsx!(in cx, 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}" } ))
// let title_card2 = cx.suspend(fut, move |(text, text2)| {
// cx.render(rsx!( h3 { "{text}" } ))
// });
ctx.render(rsx! {
cx.render(rsx! {
div {
{title_card}
// {title_card2}
+5 -5
View File
@@ -64,7 +64,7 @@ Also, we want selectors to have internal dependencies.
## Async atoms
```rust
let (atom, set_atom, modify_atom) = (ATOM.use_read(ctx), ATOM.use_write(ctx), ATOM.use_modify(ctx));
let (atom, set_atom, modify_atom) = (ATOM.use_read(cx), ATOM.use_write(cx), ATOM.use_modify(cx));
const Title: AsyncAtom<String> = |builder| {
builder.on_set(|api, new_val| async {
@@ -93,10 +93,10 @@ const ContentCards: SelectorFamily<Uuid, ContentCard> = |api, key| api.on_get_as
data
})
static ContentCard: FC<()> = |ctx| {
let body = async match use_recoil_value()(ctx.id).await {
Ok(content) => rsx!{in ctx, p {"{content}"} }
Err(e) => rsx!{in ctx, p {"Failed to load"}}
static ContentCard: FC<()> = |cx| {
let body = async match use_recoil_value()(cx.id).await {
Ok(content) => rsx!{in cx, p {"{content}"} }
Err(e) => rsx!{in cx, p {"Failed to load"}}
};
rsx!{
+35 -35
View File
@@ -31,15 +31,15 @@ mod traits {
// Atoms, selectors, and their family variants are readable
pub trait Readable<T: AtomValue>: Sized + Copy {
fn use_read<'a, P: 'static>(self, ctx: Context<'a, P>) -> &'a T {
hooks::use_read(&ctx, self)
fn use_read<'a, P: 'static>(self, cx: Context<'a, P>) -> &'a T {
hooks::use_read(&cx, self)
}
// This returns a future of the value
// If the atom is currently pending, that future will resolve to pending
// If the atom is currently ready, the future will immediately resolve
// if the atom switches from ready to pending, the component will re-run, returning a pending future
fn use_read_async<'a, P>(self, ctx: Context<'a, P>) -> &'a T {
fn use_read_async<'a, P>(self, cx: Context<'a, P>) -> &'a T {
todo!()
}
@@ -53,11 +53,11 @@ mod traits {
// Only atoms and atom families are writable
// Selectors and selector families are not
pub trait Writable<T: AtomValue>: Readable<T> + Sized {
fn use_read_write<'a, P>(self, ctx: Context<'a, P>) -> (&'a T, &'a Rc<dyn Fn(T)>) {
fn use_read_write<'a, P>(self, cx: Context<'a, P>) -> (&'a T, &'a Rc<dyn Fn(T)>) {
todo!()
}
fn use_write<'a, P>(self, ctx: Context<'a, P>) -> &'a Rc<dyn Fn(T)> {
fn use_write<'a, P>(self, cx: Context<'a, P>) -> &'a Rc<dyn Fn(T)> {
todo!()
}
}
@@ -91,13 +91,13 @@ mod atoms {
use super::*;
use dioxus_core::prelude::Context;
fn _test(ctx: Context<()>) {
fn _test(cx: Context<()>) {
const EXAMPLE_ATOM: Atom<i32> = |_| 10;
// ensure that atoms are both read and write
let _ = use_read(&ctx, &EXAMPLE_ATOM);
let _ = use_read_write(&ctx, &EXAMPLE_ATOM);
let _ = use_write(&ctx, &EXAMPLE_ATOM);
let _ = use_read(&cx, &EXAMPLE_ATOM);
let _ = use_read_write(&cx, &EXAMPLE_ATOM);
let _ = use_write(&cx, &EXAMPLE_ATOM);
}
}
}
@@ -159,9 +159,9 @@ mod atomfamily {
use super::*;
const Titles: AtomHashMap<u32, &str> = |map| {};
fn test(ctx: Context<()>) {
let title = Titles.select(&10).use_read(ctx);
let t2 = use_read(&ctx, &Titles.select(&10));
fn test(cx: Context<()>) {
let title = Titles.select(&10).use_read(cx);
let t2 = use_read(&cx, &Titles.select(&10));
}
}
}
@@ -370,8 +370,8 @@ mod hooks {
use super::*;
use dioxus_core::{hooks::use_ref, prelude::Context, virtual_dom::Scoped};
pub fn use_init_recoil_root<P>(ctx: Context<P>, cfg: impl Fn(())) {
ctx.use_create_context(move || RefCell::new(RecoilRoot::new()))
pub fn use_init_recoil_root<P>(cx: Context<P>, cfg: impl Fn(())) {
cx.use_create_context(move || RefCell::new(RecoilRoot::new()))
}
/// Gain access to the recoil API directly - set, get, modify, everything
@@ -381,17 +381,17 @@ mod hooks {
///
/// You can use this method to create controllers that perform much more complex actions than set/get
/// However, be aware that "getting" values through this hook will not subscribe the component to any updates.
pub fn use_recoil_api<'a>(ctx: &impl Scoped<'a>) -> &'a Rc<RecoilContext> {
ctx.use_context::<RecoilContext>()
pub fn use_recoil_api<'a>(cx: &impl Scoped<'a>) -> &'a Rc<RecoilContext> {
cx.use_context::<RecoilContext>()
}
pub fn use_write<'a, T: AtomValue>(
ctx: &impl Scoped<'a>,
cx: &impl Scoped<'a>,
// todo: this shouldn't need to be static
writable: impl Writable<T>,
) -> &'a Rc<dyn Fn(T)> {
let api = use_recoil_api(ctx);
ctx.use_hook(
let api = use_recoil_api(cx);
cx.use_hook(
move || {
let api = api.clone();
let raw_id = writable.static_id();
@@ -413,7 +413,7 @@ mod hooks {
/// This is useful if you need the memoized Atom value. However, Rc<T> is not as easy to
/// work with as
pub fn use_read_raw<'a, T: AtomValue>(
ctx: &impl Scoped<'a>,
cx: &impl Scoped<'a>,
readable: impl Readable<T>,
) -> &'a Rc<T> {
struct ReadHook<T> {
@@ -421,12 +421,12 @@ mod hooks {
consumer_id: u32,
}
let api = use_recoil_api(ctx);
ctx.use_hook(
let api = use_recoil_api(cx);
cx.use_hook(
move || {
let mut api = api.as_ref().borrow_mut();
let update = ctx.schedule_update();
let update = cx.schedule_update();
let val = api.try_get_raw(readable).unwrap();
let id = api.subscribe(readable, update);
ReadHook {
@@ -449,8 +449,8 @@ mod hooks {
}
///
pub fn use_read<'a, T: AtomValue>(ctx: &impl Scoped<'a>, readable: impl Readable<T>) -> &'a T {
use_read_raw(ctx, readable).as_ref()
pub fn use_read<'a, T: AtomValue>(cx: &impl Scoped<'a>, readable: impl Readable<T>) -> &'a T {
use_read_raw(cx, readable).as_ref()
}
/// # Use an atom in both read and write
@@ -463,16 +463,16 @@ mod hooks {
///
/// ```
/// const Title: Atom<&str> = |_| "hello";
/// let (title, set_title) = use_read_write(ctx, &Title);
/// let (title, set_title) = use_read_write(cx, &Title);
///
/// // equivalent to:
/// let (title, set_title) = (use_read(ctx, &Title), use_write(ctx, &Title));
/// let (title, set_title) = (use_read(cx, &Title), use_write(cx, &Title));
/// ```
pub fn use_read_write<'a, T: AtomValue + 'static>(
ctx: &impl Scoped<'a>,
cx: &impl Scoped<'a>,
writable: impl Writable<T>,
) -> (&'a T, &'a Rc<dyn Fn(T)>) {
(use_read(ctx, writable), use_write(ctx, writable))
(use_read(cx, writable), use_write(cx, writable))
}
/// # Modify an atom without using `use_read`.
@@ -491,12 +491,12 @@ mod hooks {
/// ## Example
///
/// ```ignore
/// let modify_atom = use_modify(ctx, Atom);
/// let modify_atom = use_modify(cx, Atom);
///
/// modify_atom(&|a| *a += 1)
/// ```
pub fn use_modify<'a, T: AtomValue + 'static + Clone, P>(
ctx: Context<'a, P>,
cx: Context<'a, P>,
writable: impl Writable<T>,
) -> impl Fn(&dyn Fn()) {
|_| {}
@@ -506,7 +506,7 @@ mod hooks {
/// !! Any changes to the family will cause this subscriber to update
/// Try not to put this at the very top-level of your app.
pub fn use_read_family<'a, K, V, P>(
ctx: Context<'a, P>,
cx: Context<'a, P>,
t: &AtomHashMap<K, V>,
) -> &'a im_rc::HashMap<K, V> {
todo!()
@@ -532,9 +532,9 @@ mod utils {
pub fn RecoilApp<T: 'static>(
root: impl for<'a> Fn(Context<'a, T>) -> VNode,
) -> impl for<'a> Fn(Context<'a, T>) -> VNode {
move |ctx| {
use_init_recoil_root(ctx, |_| {});
root(ctx)
move |cx| {
use_init_recoil_root(cx, |_| {});
root(cx)
}
}
}