Skip to main content

RouteLifecycle

Trait RouteLifecycle 

Source
pub trait RouteLifecycle:
    Send
    + Sync
    + 'static {
    type Future: Future<Output = LifecycleResult> + Send + 'static;

    // Required methods
    fn on_enter(&self, cx: &App, request: &NavigationRequest) -> Self::Future;
    fn on_exit(&self, cx: &App) -> Self::Future;
    fn can_deactivate(&self, cx: &App) -> Self::Future;
}
Expand description

Route lifecycle hooks

Lifecycle hooks allow you to run code at key points in the navigation process:

  • on_enter: Called when entering a route (for data loading, setup)
  • on_exit: Called when leaving a route (for cleanup, saving state)
  • can_deactivate: Called to check if user can leave (for unsaved changes warning)

§Example

use gpui_navigator::{RouteLifecycle, LifecycleResult, NavigationRequest};
use std::future::Future;
use std::pin::Pin;

struct FormLifecycle;

impl RouteLifecycle for FormLifecycle {
    type Future = Pin<Box<dyn Future<Output = LifecycleResult> + Send>>;

    fn on_enter(&self, _cx: &gpui::App, _request: &NavigationRequest) -> Self::Future {
        // Load form data
        Box::pin(async { LifecycleResult::Continue })
    }

    fn on_exit(&self, _cx: &gpui::App) -> Self::Future {
        Box::pin(async { LifecycleResult::Continue })
    }

    fn can_deactivate(&self, _cx: &gpui::App) -> Self::Future {
        // Check for unsaved changes
        Box::pin(async { LifecycleResult::Continue })
    }
}

Required Associated Types§

Source

type Future: Future<Output = LifecycleResult> + Send + 'static

The future returned by lifecycle methods

Required Methods§

Source

fn on_enter(&self, cx: &App, request: &NavigationRequest) -> Self::Future

Called when entering the route

Use this to:

  • Load data for the route
  • Set up subscriptions
  • Initialize state
  • Validate navigation parameters

Return LifecycleResult::Abort to prevent navigation. Return LifecycleResult::Redirect to navigate elsewhere.

Source

fn on_exit(&self, cx: &App) -> Self::Future

Called when exiting the route

Use this to:

  • Save state
  • Clean up subscriptions
  • Cancel pending operations

Return LifecycleResult::Abort to prevent navigation.

Source

fn can_deactivate(&self, cx: &App) -> Self::Future

Check if the route can be deactivated

Use this to:

  • Check for unsaved changes
  • Confirm navigation away
  • Validate state before leaving

Return LifecycleResult::Abort to prevent navigation.

Implementors§