Skip to content

Commit

Permalink
Doc hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 20, 2024
1 parent 61bfd53 commit 21ab792
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/concoct/src/hook/use_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::Context;
use std::{any::TypeId, rc::Rc};

/// Hook to provide a context.
pub fn use_provider<T: 'static>(value: T) {
let cx = Context::current();
let cx_ref = cx.inner.borrow();
Expand All @@ -9,6 +10,7 @@ pub fn use_provider<T: 'static>(value: T) {
scope.contexts.insert(TypeId::of::<T>(), Rc::new(value));
}

/// Hook to get a context from its type.
pub fn use_context<T: 'static>() -> Option<Rc<T>> {
Context::current()
.inner
Expand Down
1 change: 1 addition & 0 deletions crates/concoct/src/hook/use_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::use_ref;
use rustc_hash::FxHasher;
use std::hash::{Hash, Hasher};

/// Hook to cache a value and run an effect when it's changed.
pub fn use_effect(input: impl Hash, effect: impl FnOnce()) {
let mut hasher = FxHasher::default();
input.hash(&mut hasher);
Expand Down
1 change: 1 addition & 0 deletions crates/concoct/src/hook/use_on_drop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::use_ref;
use crate::Context;

/// Hook to store a function that's triggered on removal of the current `View`.
pub fn use_on_drop(on_drop: impl FnMut() + 'static) {
use_ref(|| {
Context::current()
Expand Down
4 changes: 4 additions & 0 deletions crates/concoct/src/hook/use_ref.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::Context;
use std::rc::Rc;

/// Hook to store a stateless value.
///
/// This function will only call `make_value` once, on the first render,
/// to create the initial value.
pub fn use_ref<T: 'static>(make_value: impl FnOnce() -> T) -> Rc<T> {
let cx = Context::current();
let cx_ref = cx.inner.borrow();
Expand Down
12 changes: 12 additions & 0 deletions crates/concoct/src/hook/use_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ use super::use_ref;
use crate::Context;
use std::{cell::RefCell, rc::Rc};

/// Hook to create render state.
///
/// This function will only call `make_value` once, on the first render,
/// to create the initial state.
/// The returned function can be called to set the state.
///
/// ```no_run
/// use concoct::hook::use_state;
///
/// let (count, set_count) = use_state(|| 0);
/// assert_eq!(count, 0);
/// ```
pub fn use_state<T: Clone + 'static>(make_value: impl FnOnce() -> T) -> (T, Rc<dyn Fn(T)>) {
let cell = use_ref(|| RefCell::new(make_value()));
let getter = cell.borrow().clone();
Expand Down
6 changes: 4 additions & 2 deletions crates/concoct/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use rustc_hash::FxHasher;

use crate::{body::Empty, Body};
use std::{rc::Rc, hash::{Hash, Hasher}};
use std::{
hash::{Hash, Hasher},
rc::Rc,
};

pub trait View: 'static {
fn body(&self) -> impl Body;
Expand Down Expand Up @@ -33,4 +36,3 @@ macro_rules! impl_string_view {

impl_string_view!(&'static str);
impl_string_view!(String);

0 comments on commit 21ab792

Please sign in to comment.