Skip to content

Commit

Permalink
Simplify UseForceUpdateHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Jun 20, 2022
1 parent c830d7a commit b0f0224
Showing 1 changed file with 4 additions and 61 deletions.
65 changes: 4 additions & 61 deletions packages/yew/src/functional/hooks/use_force_update.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,9 @@
use std::fmt;

use super::{Hook, HookContext};
use crate::functional::ReRender;

/// A handle which can be used to force a re-render of the associated
/// function component.
#[derive(Clone)]
pub struct UseForceUpdate {
trigger: ReRender,
}

impl fmt::Debug for UseForceUpdate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("UseForceUpdate").finish()
}
}

impl UseForceUpdate {
/// Trigger an unconditional re-render of the associated function component
pub fn force_update(&self) {
(self.trigger)()
}
}

#[cfg(feature = "nightly")]
mod feat_nightly {
use super::*;

impl FnOnce<()> for UseForceUpdate {
type Output = ();

extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
self.force_update()
}
}

impl FnMut<()> for UseForceUpdate {
extern "rust-call" fn call_mut(&mut self, _args: ()) -> Self::Output {
self.force_update()
}
}

impl Fn<()> for UseForceUpdate {
extern "rust-call" fn call(&self, _args: ()) -> Self::Output {
self.force_update()
}
}
}
type UseForceUpdateHandle = ReRender;

/// This hook is used to manually force a function component to re-render.
///
Expand Down Expand Up @@ -96,30 +53,16 @@ mod feat_nightly {
///
/// [`use_state`]: super::use_state()
/// [`use_reducer`]: super::use_reducer()
pub fn use_force_update() -> impl Hook<Output = UseForceUpdate> {
pub fn use_force_update() -> impl Hook<Output = UseForceUpdateHandle> {
struct UseRerenderHook;

impl Hook for UseRerenderHook {
type Output = UseForceUpdate;
type Output = UseForceUpdateHandle;

fn run(self, ctx: &mut HookContext) -> Self::Output {
UseForceUpdate {
trigger: ctx.re_render.clone(),
}
ctx.re_render.clone()
}
}

UseRerenderHook
}

#[cfg(all(test, feature = "nightly"))]
mod nightly_test {
use yew::prelude::*;

#[function_component]
fn ManuallyUpdatedDate() -> Html {
let trigger = use_force_update();
let _ = move || trigger();
html! {}
}
}

0 comments on commit b0f0224

Please sign in to comment.