use std::fmt;
use super::{Hook, HookContext};
use crate::functional::ReRender;
#[derive(Clone)]
pub struct UseForceUpdateHandle {
trigger: ReRender,
}
impl fmt::Debug for UseForceUpdateHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("UseForceUpdate").finish()
}
}
impl UseForceUpdateHandle {
pub fn force_update(&self) {
(self.trigger)()
}
}
#[cfg(nightly_yew)]
mod feat_nightly {
use super::*;
impl FnOnce<()> for UseForceUpdateHandle {
type Output = ();
extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
self.force_update()
}
}
impl FnMut<()> for UseForceUpdateHandle {
extern "rust-call" fn call_mut(&mut self, _args: ()) -> Self::Output {
self.force_update()
}
}
impl Fn<()> for UseForceUpdateHandle {
extern "rust-call" fn call(&self, _args: ()) -> Self::Output {
self.force_update()
}
}
}
pub fn use_force_update() -> impl Hook<Output = UseForceUpdateHandle> {
struct UseRerenderHook;
impl Hook for UseRerenderHook {
type Output = UseForceUpdateHandle;
fn run(self, ctx: &mut HookContext) -> Self::Output {
UseForceUpdateHandle {
trigger: ctx.re_render.clone(),
}
}
}
UseRerenderHook
}
#[cfg(all(test, nightly_yew))]
mod nightly_test {
use yew::prelude::*;
#[component]
fn ManuallyUpdatedDate() -> Html {
let trigger = use_force_update();
let _ = move || trigger();
html! {}
}
}