Hook Decorator
This file defines the Hook
decorator, which is used to mark class methods to be executed at specific application lifecycle events in the @sodacore/core
library. By specifying an IHookType
, you can trigger methods automatically during setup, teardown, or other lifecycle hooks.
Features
- Lifecycle Integration: Allows methods to be registered for execution at specific lifecycle hooks.
- Metadata Management: Stores hooked methods and their types using metadata utilities.
Usage
typescript
import Hook from './decorator/hook';
class MyService {
@Hook('onInit')
initialize() {
// This method will be called during the 'onInit' lifecycle hook
}
}
- Use
@Hook(type)
on any method you want to be triggered by a lifecycle event. - The
type
parameter should be a validIHookType
.
API
Hook(type: IHookType): MethodDecorator
- type: The lifecycle hook type at which the method should be executed.
- Returns: A method decorator that registers the method for the specified hook.
How It Works
- Retrieves the current list of hooked methods from the class metadata.
- Adds the decorated method's property key to the list.
- Sets the hook type metadata for the specific method.
Notes
- Designed for use in classes that need to respond to application lifecycle events.
- Multiple methods can be registered for different hooks within the same class.