mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 09:09:21 -05:00
Remove expect's; Rephrase comments
This commit is contained in:
parent
e038f9664a
commit
be05687ad2
1 changed files with 41 additions and 45 deletions
|
@ -35,22 +35,23 @@ pub fn use_service_worker() -> UseServiceWorkerReturn {
|
||||||
|
|
||||||
/// Version of [`use_service_worker`] that takes a `UseServiceWorkerOptions`. See [`use_service_worker`] for how to use.
|
/// Version of [`use_service_worker`] that takes a `UseServiceWorkerOptions`. See [`use_service_worker`] for how to use.
|
||||||
pub fn use_service_worker_with_options(options: UseServiceWorkerOptions) -> UseServiceWorkerReturn {
|
pub fn use_service_worker_with_options(options: UseServiceWorkerOptions) -> UseServiceWorkerReturn {
|
||||||
// Reload the page whenever a new ServiceWorker is installed.
|
// Trigger the user-defined action (page-reload by default)
|
||||||
|
// whenever a new ServiceWorker is installed.
|
||||||
if let Some(navigator) = use_window().navigator() {
|
if let Some(navigator) = use_window().navigator() {
|
||||||
let on_controller_change = options.on_controller_change.clone();
|
let on_controller_change = options.on_controller_change.clone();
|
||||||
let reload = Closure::wrap(Box::new(move |_event: JsValue| {
|
let js_closure = Closure::wrap(Box::new(move |_event: JsValue| {
|
||||||
on_controller_change.call(());
|
on_controller_change.call(());
|
||||||
}) as Box<dyn FnMut(JsValue)>)
|
}) as Box<dyn FnMut(JsValue)>)
|
||||||
.into_js_value();
|
.into_js_value();
|
||||||
navigator
|
navigator
|
||||||
.service_worker()
|
.service_worker()
|
||||||
.set_oncontrollerchange(Some(reload.as_ref().unchecked_ref()));
|
.set_oncontrollerchange(Some(js_closure.as_ref().unchecked_ref()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create async actions.
|
// Create async actions.
|
||||||
let create_or_update_registration = create_action_create_or_update_sw_registration();
|
let create_or_update_registration = create_action_create_or_update_registration();
|
||||||
let get_registration = create_action_get_sw_registration();
|
let get_registration = create_action_get_registration();
|
||||||
let update_action = create_action_update_sw();
|
let update_sw = create_action_update();
|
||||||
|
|
||||||
// Immediately create or update the SW registration.
|
// Immediately create or update the SW registration.
|
||||||
create_or_update_registration.dispatch(ServiceWorkerScriptUrl(options.script_url.to_string()));
|
create_or_update_registration.dispatch(ServiceWorkerScriptUrl(options.script_url.to_string()));
|
||||||
|
@ -60,7 +61,8 @@ pub fn use_service_worker_with_options(options: UseServiceWorkerOptions) -> UseS
|
||||||
Signal::derive(move || {
|
Signal::derive(move || {
|
||||||
let a = get_registration.value().get();
|
let a = get_registration.value().get();
|
||||||
let b = create_or_update_registration.value().get();
|
let b = create_or_update_registration.value().get();
|
||||||
// We only dispatch create_or_update_registration once. Whenever we manually re-fetched the registration, the result of that has precedence!
|
// We only dispatch create_or_update_registration once.
|
||||||
|
// Whenever we manually re-fetched the registration, the result of that has precedence!
|
||||||
match a {
|
match a {
|
||||||
Some(res) => res.map_err(ServiceWorkerRegistrationError::Js),
|
Some(res) => res.map_err(ServiceWorkerRegistrationError::Js),
|
||||||
None => match b {
|
None => match b {
|
||||||
|
@ -84,7 +86,7 @@ pub fn use_service_worker_with_options(options: UseServiceWorkerOptions) -> UseS
|
||||||
registration.set_onupdatefound(Some(fetch_registration.as_ref().unchecked_ref()));
|
registration.set_onupdatefound(Some(fetch_registration.as_ref().unchecked_ref()));
|
||||||
|
|
||||||
// Trigger a check to see IF an updated SW is available.
|
// Trigger a check to see IF an updated SW is available.
|
||||||
update_action.dispatch(registration.clone());
|
update_sw.dispatch(registration.clone());
|
||||||
|
|
||||||
// If a SW is installing, we must be notified if its state changes!
|
// If a SW is installing, we must be notified if its state changes!
|
||||||
if let Some(sw) = registration.installing() {
|
if let Some(sw) = registration.installing() {
|
||||||
|
@ -126,7 +128,7 @@ pub fn use_service_worker_with_options(options: UseServiceWorkerOptions) -> UseS
|
||||||
check_for_update: Callback::new(move |()| {
|
check_for_update: Callback::new(move |()| {
|
||||||
registration.with(|reg| {
|
registration.with(|reg| {
|
||||||
if let Ok(reg) = reg {
|
if let Ok(reg) = reg {
|
||||||
update_action.dispatch(reg.clone())
|
update_sw.dispatch(reg.clone())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
|
@ -135,7 +137,9 @@ pub fn use_service_worker_with_options(options: UseServiceWorkerOptions) -> UseS
|
||||||
match reg.waiting() {
|
match reg.waiting() {
|
||||||
Some(sw) => {
|
Some(sw) => {
|
||||||
tracing::info!("Updating to newly installed SW...");
|
tracing::info!("Updating to newly installed SW...");
|
||||||
sw.post_message(&JsValue::from_str(&options.skip_waiting_message)).expect("post message");
|
if let Err(err) = sw.post_message(&JsValue::from_str(&options.skip_waiting_message)) {
|
||||||
|
tracing::warn!("Could not send message to active SW: Error: {err:?}");
|
||||||
|
}
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
tracing::warn!("You tried to update the SW while no new SW was waiting. This is probably a bug.");
|
tracing::warn!("You tried to update the SW while no new SW was waiting. This is probably a bug.");
|
||||||
|
@ -149,12 +153,13 @@ pub fn use_service_worker_with_options(options: UseServiceWorkerOptions) -> UseS
|
||||||
/// Options for [`use_service_worker_with_options`].
|
/// Options for [`use_service_worker_with_options`].
|
||||||
#[derive(DefaultBuilder)]
|
#[derive(DefaultBuilder)]
|
||||||
pub struct UseServiceWorkerOptions {
|
pub struct UseServiceWorkerOptions {
|
||||||
/// The name of your service-worker.
|
/// The name of your service-worker file. Must be deployed alongside your app.
|
||||||
/// You will most likely deploy the service-worker JS fiel alongside your app.
|
/// The default name is 'service-worker.js'.
|
||||||
/// A typical name is 'service-worker.js'.
|
|
||||||
pub script_url: Cow<'static, str>,
|
pub script_url: Cow<'static, str>,
|
||||||
|
|
||||||
/// The message sent to a waiting ServiceWorker when you call the `skip_waiting` callback.
|
/// The message sent to a waiting ServiceWorker when you call the `skip_waiting` callback.
|
||||||
|
/// The callback is part of the return type of [`use_service_worker`]!
|
||||||
|
/// The default message is 'skipWaiting'.
|
||||||
pub skip_waiting_message: Cow<'static, str>,
|
pub skip_waiting_message: Cow<'static, str>,
|
||||||
|
|
||||||
/// What should happen when a new service worker was activated?
|
/// What should happen when a new service worker was activated?
|
||||||
|
@ -170,11 +175,10 @@ impl Default for UseServiceWorkerOptions {
|
||||||
on_controller_change: Callback::new(move |()| {
|
on_controller_change: Callback::new(move |()| {
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
if let Some(window) = use_window().deref() {
|
if let Some(window) = use_window().deref() {
|
||||||
match window.location().reload() {
|
if let Err(err) = window.location().reload() {
|
||||||
Ok(()) => {}
|
tracing::warn!(
|
||||||
Err(err) => tracing::warn!(
|
|
||||||
"Detected a ServiceWorkerController change but the page reload failed! Error: {err:?}"
|
"Detected a ServiceWorkerController change but the page reload failed! Error: {err:?}"
|
||||||
),
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
@ -196,10 +200,11 @@ pub struct UseServiceWorkerReturn {
|
||||||
/// Whether a SW is active.
|
/// Whether a SW is active.
|
||||||
pub active: Signal<bool>,
|
pub active: Signal<bool>,
|
||||||
|
|
||||||
/// Check for ServiceWorker update.
|
/// Check for a ServiceWorker update.
|
||||||
pub check_for_update: Callback<()>,
|
pub check_for_update: Callback<()>,
|
||||||
|
|
||||||
/// Call this to activate a new ("waiting") SW if one is available.
|
/// Call this to activate a new ("waiting") SW if one is available.
|
||||||
|
/// Calling this while the [`UseServiceWorkerReturn::waiting`] signal resolves to false has no effect.
|
||||||
pub skip_waiting: Callback<()>,
|
pub skip_waiting: Callback<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -212,24 +217,23 @@ pub enum ServiceWorkerRegistrationError {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A leptos action which asynchronously checks for ServiceWorker updates, given an existing ServiceWorkerRegistration.
|
/// A leptos action which asynchronously checks for ServiceWorker updates, given an existing ServiceWorkerRegistration.
|
||||||
fn create_action_update_sw(
|
fn create_action_update(
|
||||||
) -> Action<ServiceWorkerRegistration, Result<ServiceWorkerRegistration, JsValue>> {
|
) -> Action<ServiceWorkerRegistration, Result<ServiceWorkerRegistration, JsValue>> {
|
||||||
create_action(move |registration: &ServiceWorkerRegistration| {
|
create_action(move |registration: &ServiceWorkerRegistration| {
|
||||||
let registration = registration.clone();
|
let registration = registration.clone();
|
||||||
async move {
|
async move {
|
||||||
let update_promise = registration.update().expect("update to not fail");
|
match registration.update() {
|
||||||
wasm_bindgen_futures::JsFuture::from(update_promise)
|
Ok(promise) => wasm_bindgen_futures::JsFuture::from(promise)
|
||||||
.await
|
.await
|
||||||
.map(|ok| {
|
.and_then(|ok| ok.dyn_into::<ServiceWorkerRegistration>()),
|
||||||
ok.dyn_into::<ServiceWorkerRegistration>()
|
Err(err) => Err(err),
|
||||||
.expect("conversion into ServiceWorkerRegistration")
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A leptos action which asynchronously creates or updates and than retrieves the ServiceWorkerRegistration.
|
/// A leptos action which asynchronously creates or updates and than retrieves the ServiceWorkerRegistration.
|
||||||
fn create_action_create_or_update_sw_registration(
|
fn create_action_create_or_update_registration(
|
||||||
) -> Action<ServiceWorkerScriptUrl, Result<ServiceWorkerRegistration, JsValue>> {
|
) -> Action<ServiceWorkerScriptUrl, Result<ServiceWorkerRegistration, JsValue>> {
|
||||||
create_action(move |script_url: &ServiceWorkerScriptUrl| {
|
create_action(move |script_url: &ServiceWorkerScriptUrl| {
|
||||||
let script_url = script_url.0.to_owned();
|
let script_url = script_url.0.to_owned();
|
||||||
|
@ -238,10 +242,7 @@ fn create_action_create_or_update_sw_registration(
|
||||||
let promise = navigator.service_worker().register(script_url.as_str());
|
let promise = navigator.service_worker().register(script_url.as_str());
|
||||||
wasm_bindgen_futures::JsFuture::from(promise)
|
wasm_bindgen_futures::JsFuture::from(promise)
|
||||||
.await
|
.await
|
||||||
.map(|ok| {
|
.and_then(|ok| ok.dyn_into::<ServiceWorkerRegistration>())
|
||||||
ok.dyn_into::<ServiceWorkerRegistration>()
|
|
||||||
.expect("conversion into ServiceWorkerRegistration")
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
Err(JsValue::from_str("no navigator"))
|
Err(JsValue::from_str("no navigator"))
|
||||||
}
|
}
|
||||||
|
@ -250,20 +251,15 @@ fn create_action_create_or_update_sw_registration(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A leptos action which asynchronously fetches the current ServiceWorkerRegistration.
|
/// A leptos action which asynchronously fetches the current ServiceWorkerRegistration.
|
||||||
fn create_action_get_sw_registration() -> Action<(), Result<ServiceWorkerRegistration, JsValue>> {
|
fn create_action_get_registration() -> Action<(), Result<ServiceWorkerRegistration, JsValue>> {
|
||||||
create_action(move |(): &()| {
|
create_action(move |(): &()| async move {
|
||||||
async move {
|
if let Some(navigator) = use_window().navigator() {
|
||||||
if let Some(navigator) = use_window().navigator() {
|
let promise = navigator.service_worker().get_registration();
|
||||||
let promise = navigator.service_worker().get_registration(); // Could take a scope like "/app"...
|
wasm_bindgen_futures::JsFuture::from(promise)
|
||||||
wasm_bindgen_futures::JsFuture::from(promise)
|
.await
|
||||||
.await
|
.and_then(|ok| ok.dyn_into::<ServiceWorkerRegistration>())
|
||||||
.map(|ok| {
|
} else {
|
||||||
ok.dyn_into::<ServiceWorkerRegistration>()
|
Err(JsValue::from_str("no navigator"))
|
||||||
.expect("conversion into ServiceWorkerRegistration")
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Err(JsValue::from_str("no navigator"))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue