Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Some Hooks Aren't Reactive #36

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions sdk/src/utils/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ pub struct WindowSize {
/// let size = use_window_size();
///
/// rsx! {
/// p { "Width: {size.width}" }
/// p { "Height: {size.height}" }
/// p { "Width: {size().width}" }
/// p { "Height: {size().height}" }
/// }
/// }
/// ```
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
Loading