Thanks to visit codestin.com
Credit goes to lib.rs

#executing #hook-system #registering #thread-safe #events #async-sync #tokio-time #pin #millis

hookable

A thread-safe hook system that allows registering and executing sync and async hooks

2 releases

Uses new Rust 2024

0.1.1 Jun 6, 2025
0.1.0 Jun 6, 2025

#1558 in Asynchronous

22 downloads per month

MIT license

17KB
343 lines

Hookable

A thread-safe hook system that allows registering and executing sync and async hooks.

Hooks are functions that can be attached to named events and executed when those events are triggered.

Usage

You can assign both async and sync to the same name. If you do that you will always have to call it using call_async.

use std::time::Duration;
use tokio::time::sleep;
use hookable::Hookable;

fn main() {
    let hookable = Hookable::new();

    // Register a sync hook
    hookable.hook("event", || println!("Sync hook executed"));

    // Register an async hook
    hookable.hook_async("event", || Box::pin(async {
        sleep(Duration::from_millis(200)).await;
    }));

    // ❌ This won't work because one of the events has to be run using await. 
    // `HookableError::AsyncHookCalledSync` will be returned.
    hookable.call("event").unwrap();

    // ✅ This will work.
    hookable.call_async("event").await.unwrap();
}

Credits

The JS version both inspires this crate and its name

Dependencies

~3–6.5MB
~117K SLoC