hookable

Crates.iohookable
lib.rshookable
version0.1.1
created_at2025-06-06 12:17:33.987452+00
updated_at2025-06-06 12:18:47.853086+00
descriptionA thread-safe hook system that allows registering and executing sync and async hooks.
homepage
repositoryhttps://github.com/malezjaa/hookable
max_upload_size
id1702947
size28,727
Krzysztof (malezjaa)

documentation

README

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

Commit count: 2

cargo fmt