1use std::ops::{Deref, DerefMut};
2
3use dioxus_lib::prelude::*;
4
5mod macros;
6pub mod re_exports;
8
9#[allow(async_fn_in_trait)]
10pub trait DataKey {
11 type Value: Clone + 'static;
12 async fn init();
13 async fn on_change();
14 async fn on_drop();
15 fn read() -> impl Deref<Target = Self::Value> {
16 Self::try_read().unwrap()
17 }
18 fn peek() -> impl Deref<Target = Self::Value> {
19 Self::try_read().unwrap()
20 }
21 fn write() -> impl DerefMut<Target = Self::Value> {
22 Self::try_write().unwrap()
23 }
24 fn try_read() -> Option<impl Deref<Target = Self::Value>>;
25 fn try_peek() -> Option<impl Deref<Target = Self::Value>>;
26 fn try_write() -> Option<impl DerefMut<Target = Self::Value>>;
27}
28
29pub fn use_init_key<K: DataKey<Value = V>, V>(_key: K) -> Resource<()> {
30 use_effect(move || {
31 let val = K::try_read();
32 if val.is_some() {
33 spawn(async { K::on_change().await });
34 }
35 });
36
37 use_drop(|| {
38 spawn(async { K::on_drop().await });
39 });
40
41 use_resource(|| async { K::init().await })
42}