Module hooks

Module hooks 

Source
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.0

Unless 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

  1. Event-Driven Architecture: Uses an event bus pattern for loose coupling between components
  2. Lifecycle Support: Covers all stages of module lifecycle, both synchronous and asynchronous
  3. Type Safety: Uses enums for hook kinds and phases to ensure type safety
  4. Flexibility: Allows registering multiple handlers for the same hook
  5. Contextual Information: Events carry contextual information about the module and phase
  6. 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§

DMSCHookBus
Hook bus for registering and emitting hooks.
DMSCHookEvent
Hook event structure.

Enums§

DMSCHookKind
Hook kind definition.
DMSCModulePhase
Module lifecycle phase definition.

Type Aliases§

DMSCHookHandler
Type alias for a hook handler function
DMSCHookHandlerEntry
Type alias for a hook handler entry (ID + handler)
DMSCHookHandlersMap
Type alias for a collection of hook handlers grouped by hook kind
DMSCHookId
Type alias for hook IDs.