docs: demonstrate how to define hooks using depinject (backport #21892) (#22132)

Co-authored-by: Eric Mokaya <4112301+ziscky@users.noreply.github.com>
This commit is contained in:
mergify[bot]
2024-10-04 17:42:25 +02:00
committed by GitHub
co-authored by Eric Mokaya
parent 0768b9b0bd
commit 10c19a2592
7 changed files with 169 additions and 5 deletions
+57
View File
@@ -0,0 +1,57 @@
---
sidebar_position: 1
---
# Hooks
Hooks are functions that are called before and/or after certain events in the module's lifecycle.
## Defining Hooks
1. Define the hook interface and a wrapper implementing `depinject.OnePerModuleType`:
```go reference
https://github.com/cosmos/cosmos-sdk/blob/71c603a2a5a103df00f216d78ec8b108ed64ae28/testutil/x/counter/types/expected_keepers.go#L5-L12
```
2. Add a `CounterHooks` field to the keeper:
```go reference
https://github.com/cosmos/cosmos-sdk/blob/71c603a2a5a103df00f216d78ec8b108ed64ae28/testutil/x/counter/keeper/keeper.go#L25
```
3. Create a `depinject` invoker function
```go reference
https://github.com/cosmos/cosmos-sdk/blob/71c603a2a5a103df00f216d78ec8b108ed64ae28/testutil/x/counter/depinject.go#L53-L75
```
4. Inject the hooks during app initialization:
```go
appConfig = appconfig.Compose(&appv1alpha1.Config{
Modules: []*appv1alpha1.ModuleConfig{
// ....
{
Name: types.ModuleName,
Config: appconfig.WrapAny(&types.Module{}),
},
}
})
appConfig = depinject.Configs(
AppConfig(),
runtime.DefaultServiceBindings(),
depinject.Supply(
logger,
viper,
map[string]types.CounterHooksWrapper{
"counter": types.CounterHooksWrapper{&types.Hooks{}},
},
))
```
## Examples in the SDK
For examples of hooks implementation in the Cosmos SDK, refer to the [Epochs Hooks documentation](https://docs.cosmos.network/main/build/modules/epochs#hooks) and [Distribution Hooks Documentation](https://docs.cosmos.network/main/build/modules/distribution#hooks).