# 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`.
```rust
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](https://github.com/unjs/hookable) both inspires this crate and its name