use hydroxy_leptos::diff_viewer::DiffViewer;
use leptos::prelude::*;
const OLD: &str = r"use std::time::Duration;
/// Retry policy for outbound webhook deliveries.
pub struct Retry {
pub attempts: u32,
pub delay: Duration,
}
impl Retry {
pub fn new(attempts: u32) -> Self {
Self { attempts, delay: Duration::from_millis(500) }
}
/// How long to wait before attempt `n`.
pub fn backoff(&self, n: u32) -> Duration {
self.delay * n
}
pub fn exhausted(&self, n: u32) -> bool {
n >= self.attempts
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn grows_linearly() {
let r = Retry::new(3);
assert_eq!(r.backoff(2), Duration::from_millis(1000));
}
}
";
const NEW: &str = r"use std::time::Duration;
/// Retry policy for outbound webhook deliveries.
pub struct Retry {
pub attempts: u32,
pub delay: Duration,
pub max_delay: Duration,
}
impl Retry {
pub fn new(attempts: u32) -> Self {
Self { attempts, delay: Duration::from_millis(250), max_delay: Duration::from_secs(30) }
}
/// How long to wait before attempt `n`: doubles each time, capped.
pub fn backoff(&self, n: u32) -> Duration {
(self.delay * 2u32.saturating_pow(n)).min(self.max_delay)
}
pub fn exhausted(&self, n: u32) -> bool {
n >= self.attempts
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn grows_exponentially_up_to_the_cap() {
let r = Retry::new(8);
assert_eq!(r.backoff(2), Duration::from_millis(1000));
assert_eq!(r.backoff(12), Duration::from_secs(30));
}
}
";
pub fn demo() -> AnyView {
view! {
<div style="max-width: 900px; max-height: 520px; display: flex; flex-direction: column;">
<DiffViewer before=OLD after=NEW file_name="src/webhooks/retry.rs" lang="rust" />
</div>
}
.into_any()
}
use dioxus::prelude::*;
use hydroxy_dioxus::diff_viewer::DiffViewer;
const OLD: &str = r"use std::time::Duration;
/// Retry policy for outbound webhook deliveries.
pub struct Retry {
pub attempts: u32,
pub delay: Duration,
}
impl Retry {
pub fn new(attempts: u32) -> Self {
Self { attempts, delay: Duration::from_millis(500) }
}
/// How long to wait before attempt `n`.
pub fn backoff(&self, n: u32) -> Duration {
self.delay * n
}
pub fn exhausted(&self, n: u32) -> bool {
n >= self.attempts
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn grows_linearly() {
let r = Retry::new(3);
assert_eq!(r.backoff(2), Duration::from_millis(1000));
}
}
";
const NEW: &str = r"use std::time::Duration;
/// Retry policy for outbound webhook deliveries.
pub struct Retry {
pub attempts: u32,
pub delay: Duration,
pub max_delay: Duration,
}
impl Retry {
pub fn new(attempts: u32) -> Self {
Self { attempts, delay: Duration::from_millis(250), max_delay: Duration::from_secs(30) }
}
/// How long to wait before attempt `n`: doubles each time, capped.
pub fn backoff(&self, n: u32) -> Duration {
(self.delay * 2u32.saturating_pow(n)).min(self.max_delay)
}
pub fn exhausted(&self, n: u32) -> bool {
n >= self.attempts
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn grows_exponentially_up_to_the_cap() {
let r = Retry::new(8);
assert_eq!(r.backoff(2), Duration::from_millis(1000));
assert_eq!(r.backoff(12), Duration::from_secs(30));
}
}
";
#[component]
pub fn DiffViewerDemo() -> Element {
rsx! {
div { style: "max-width: 900px; max-height: 520px; display: flex; flex-direction: column;",
DiffViewer {
before: OLD.to_string(),
after: NEW.to_string(),
file_name: "src/webhooks/retry.rs",
lang: "rust",
}
}
}
}