refactor!(core): add in environment bundler of service (#19041)

Co-authored-by: Facundo <facundomedica@gmail.com>
This commit is contained in:
Marko
2024-01-30 12:22:35 +00:00
committed by GitHub
co-authored by Facundo
parent 7e4d12209b
commit ca04e1144c
28 changed files with 212 additions and 135 deletions
+19
View File
@@ -0,0 +1,19 @@
package appmodule
import (
"cosmossdk.io/core/branch"
"cosmossdk.io/core/event"
"cosmossdk.io/core/gas"
"cosmossdk.io/core/header"
"cosmossdk.io/core/store"
)
// Environment is used to get all services to their respective module
type Environment struct {
BranchService branch.Service
EventService event.Service
GasService gas.Service
HeaderService header.Service
KVStoreService store.KVStoreService
MemStoreService store.MemoryStoreService
}
-32
View File
@@ -1,32 +0,0 @@
package appmodule
import (
"context"
"google.golang.org/protobuf/runtime/protoiface"
)
// HasEventListeners is the extension interface that modules should implement to register
// event listeners.
type HasEventListeners interface {
AppModule
// RegisterEventListeners registers the module's events listeners.
RegisterEventListeners(registrar *EventListenerRegistrar)
}
// EventListenerRegistrar allows registering event listeners.
type EventListenerRegistrar struct {
listeners []any
}
// GetListeners gets the event listeners that have been registered
func (e *EventListenerRegistrar) GetListeners() []any {
return e.listeners
}
// RegisterEventListener registers an event listener for event type E. If a non-nil error is returned by the listener,
// it will cause the process which emitted the event to fail.
func RegisterEventListener[E protoiface.MessageV1](registrar *EventListenerRegistrar, listener func(context.Context, E) error) {
registrar.listeners = append(registrar.listeners, listener)
}
-17
View File
@@ -1,17 +0,0 @@
package appmodule
import (
"context"
"reflect"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestEventListenerRegistrar(t *testing.T) {
registrar := &EventListenerRegistrar{}
RegisterEventListener(registrar, func(ctx context.Context, dummy *timestamppb.Timestamp) error { return nil })
require.Len(t, registrar.listeners, 1)
require.Equal(t, reflect.Func, reflect.TypeOf(registrar.listeners[0]).Kind())
}