1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::{use_ref, LocalContext, Ref, UseRef};
use std::{
    any::{Any, TypeId},
    marker::PhantomData,
    rc::Rc,
};

pub fn use_context<T: 'static>() -> Option<UseContext<T>> {
    let cell =
        use_ref(|| {
            LocalContext::current()
                .scope
                .borrow()
                .contexts
                .get(&TypeId::of::<T>())
                .cloned()
        });

    if cell.get().is_some() {
        Some(UseContext {
            use_ref: cell,
            _marker: PhantomData,
        })
    } else {
        None
    }
}

pub struct UseContext<T> {
    use_ref: UseRef<Option<Rc<dyn Any>>>,
    _marker: PhantomData<T>,
}

impl<T: 'static> UseContext<T> {
    pub fn get(&self) -> Ref<T> {
        let guard = self.use_ref.get();
        let value = std::cell::Ref::map(guard.value, |value| {
            value.as_ref().unwrap().downcast_ref().unwrap()
        });

        Ref {
            value,
            _rc: guard._rc,
        }
    }
}

pub fn use_provider<T: 'static>(make_context: impl FnOnce() -> T) {
    use_ref(|| {
        let value = make_context();
        LocalContext::current()
            .scope
            .borrow_mut()
            .contexts
            .insert(value.type_id(), Rc::new(value));
    });
}