Expand description
Lifecycle event hooks for modules Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
This file is part of Ri. The Ri project belongs to the Dunimd Team.
Licensed under the Apache License, Version 2.0 (the “License”); You may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
§Hooks System
This module provides an event bus system for Ri, enabling communication between components during various lifecycle events. It supports both synchronous and asynchronous module lifecycle phases, and allows for custom event handlers to be registered.
§Key Components
- RiHookKind: Enum defining the different types of hooks
- RiModulePhase: Enum defining the different module lifecycle phases
- RiHookEvent: Struct representing a hook event
- RiHookBus: Event bus for registering and emitting hooks
§Design Principles
- Event-Driven Architecture: Uses an event bus pattern for loose coupling between components
- Lifecycle Support: Covers all stages of module lifecycle, both synchronous and asynchronous
- Type Safety: Uses enums for hook kinds and phases to ensure type safety
- Flexibility: Allows registering multiple handlers for the same hook
- Contextual Information: Events carry contextual information about the module and phase
- Error Propagation: Hook handlers can return errors that propagate up the call stack
§Usage
use ri::prelude::*;
fn example() -> RiResult<()> {
// Create a hook bus
let mut hook_bus = RiHookBus::new();
// Register a hook handler
hook_bus.register(RiHookKind::Startup, "example.startup".to_string(), |ctx, event| {
// Handle startup event
Ok(())
});
// Create a service context (usually provided by the runtime)
let ctx = RiServiceContext::new();
// Emit a hook event
hook_bus.emit(&RiHookKind::Startup, &ctx)?;
Ok(())
}Structs§
- RiHook
Bus - Hook bus for registering and emitting hooks.
- RiHook
Event - Hook event structure.
Enums§
- RiHook
Kind - Hook kind definition.
- RiModule
Phase - Module lifecycle phase definition.
Type Aliases§
- RiHook
Handler - Type alias for a hook handler function
- RiHook
Handler Entry - Type alias for a hook handler entry (ID + handler)
- RiHook
Handlers Map - Type alias for a collection of hook handlers grouped by hook kind
- RiHook
Id - Type alias for hook IDs.