Expand description
Lifecycle event hooks for modules Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
This file is part of DMSC. The DMSC 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 DMSC, 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
- DMSCHookKind: Enum defining the different types of hooks
- DMSCModulePhase: Enum defining the different module lifecycle phases
- DMSCHookEvent: Struct representing a hook event
- DMSCHookBus: 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 dmsc::prelude::*;
fn example() -> DMSCResult<()> {
// Create a hook bus
let mut hook_bus = DMSCHookBus::new();
// Register a hook handler
hook_bus.register(DMSCHookKind::Startup, "example.startup".to_string(), |ctx, event| {
// Handle startup event
Ok(())
});
// Create a service context (usually provided by the runtime)
let ctx = DMSCServiceContext::new();
// Emit a hook event
hook_bus.emit(&DMSCHookKind::Startup, &ctx)?;
Ok(())
}Structs§
- DMSC
Hook Bus - Hook bus for registering and emitting hooks.
- DMSC
Hook Event - Hook event structure.
Enums§
- DMSC
Hook Kind - Hook kind definition.
- DMSC
Module Phase - Module lifecycle phase definition.
Type Aliases§
- DMSC
Hook Handler - Type alias for a hook handler function
- DMSC
Hook Handler Entry - Type alias for a hook handler entry (ID + handler)
- DMSC
Hook Handlers Map - Type alias for a collection of hook handlers grouped by hook kind
- DMSC
Hook Id - Type alias for hook IDs.