Skip to content

Commit

Permalink
fix: make hooks reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
DogeDark committed Apr 23, 2024
1 parent f214e4d commit 005fba4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/geolocation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
[dependencies]
dioxus-sdk = { workspace = true, features = ["geolocation"] }
# You can change from 'desktop' to 'web' as well
dioxus = { workspace = true, features = ["desktop"] }
dioxus = { workspace = true, features = ["web"] }

2 changes: 1 addition & 1 deletion examples/geolocation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn app() -> Element {
});
let latest_coords = use_geolocation();

let latest_coords = match latest_coords {
let latest_coords = match latest_coords() {
Ok(v) => v,
Err(e) => {
let e = format!("Initializing: {:?}", e);
Expand Down
9 changes: 7 additions & 2 deletions examples/use_window_size/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Learn how to use `use_window_size`.

Run:

```dioxus serve```
### Run

**Desktop**
```dioxus serve --platform desktop```

**Web**
```dioxus serve --platform web```
4 changes: 2 additions & 2 deletions examples/use_window_size/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fn app() -> Element {
p { "Height: {initial_size().height}" }

h3 { "Current Size" }
p { "Width: {window_size.width}" }
p { "Height: {window_size.height}" }
p { "Width: {window_size().width}" }
p { "Height: {window_size().height}" }
}
)
}
39 changes: 21 additions & 18 deletions sdk/src/geolocation/use_geolocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use super::core::{Error, Event, Geocoordinates, Geolocator, PowerMode, Status};
use dioxus::{
prelude::{
provide_context, try_consume_context, use_coroutine, use_hook, use_signal, Signal,
UnboundedReceiver,
provide_context, try_consume_context, use_coroutine, use_hook, use_signal, ReadOnlySignal,
Signal, UnboundedReceiver,
},
signals::{Readable, Writable},
};
Expand All @@ -14,19 +14,10 @@ use std::sync::Once;
static INIT: Once = Once::new();

/// Provides the latest geocoordinates. Good for navigation-type apps.
pub fn use_geolocation() -> Result<Geocoordinates, Error> {
pub fn use_geolocation() -> ReadOnlySignal<Result<Geocoordinates, Error>> {
// Store the coords
let mut coords: Signal<Result<Geocoordinates, Error>> =
use_signal(|| Err(Error::NotInitialized));
let Some(geolocator) = try_consume_context::<Signal<Result<Geolocator, Error>>>() else {
return Err(Error::NotInitialized);
};
let geolocator = geolocator.read();

// Get geolocator
let Ok(geolocator) = geolocator.as_ref() else {
return Err(Error::NotInitialized);
};

// Initialize the handler of events
let listener = use_coroutine(|mut rx: UnboundedReceiver<Event>| async move {
Expand All @@ -43,13 +34,25 @@ pub fn use_geolocation() -> Result<Geocoordinates, Error> {
}
});

// Start listening
INIT.call_once(|| {
geolocator.listen(listener).ok();
});
// Try getting the geolocator and starting the listener.
match try_consume_context::<Signal<Result<Geolocator, Error>>>() {
Some(geo) => {
let geo = geo.read();
match geo.as_ref() {
Ok(geolocator) => {
INIT.call_once(|| {
geolocator.listen(listener).ok();
});
}
Err(e) => coords.set(Err(e.clone())),
}
}
None => {
coords.set(Err(Error::NotInitialized));
}
}

// Get the result and return a clone
coords.read_unchecked().clone()
use_hook(|| ReadOnlySignal::new(coords))
}

/// Must be called before any use of the geolocation abstraction.
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/utils/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct WindowSize {
/// }
/// }
/// ```
pub fn use_window_size() -> WindowSize {
pub fn use_window_size() -> ReadOnlySignal<WindowSize> {
let mut window_size = use_signal(get_window_size);

// Initialize the handler
Expand All @@ -48,7 +48,7 @@ pub fn use_window_size() -> WindowSize {

listen(tx);

*window_size.read_unchecked()
use_hook(|| ReadOnlySignal::new(window_size))
}

// Listener for the web implementation.
Expand Down

0 comments on commit 005fba4

Please sign in to comment.