func_actor.go
1 package actor 2 3 import ( 4 "context" 5 6 "github.com/lightningnetwork/lnd/fn/v2" 7 ) 8 9 // ActorFunc is a function type that represents an actor which functions purely 10 // based on a simple function processor. 11 type ActorFunc[M Message, R any] func(context.Context, M) fn.Result[R] 12 13 // FunctionBehavior adapts a function to the ActorBehavior interface. 14 type FunctionBehavior[M Message, R any] struct { 15 fn ActorFunc[M, R] 16 } 17 18 // NewFunctionBehavior creates a behavior from a function. 19 func NewFunctionBehavior[M Message, R any]( 20 fn ActorFunc[M, R]) *FunctionBehavior[M, R] { 21 22 return &FunctionBehavior[M, R]{fn: fn} 23 } 24 25 // Receive implements ActorBehavior interface for the function. 26 // 27 // TODO(roasbeef): just base it off the function direct instead? 28 func (b *FunctionBehavior[M, R]) Receive(ctx context.Context, 29 msg M) fn.Result[R] { 30 31 return b.fn(ctx, msg) 32 } 33 34 // FunctionBehaviorFromSimple adapts a simpler function to the ActorBehavior 35 // interface. 36 func FunctionBehaviorFromSimple[M Message, R any]( 37 sFunc func(M) (R, error)) *FunctionBehavior[M, R] { 38 39 return NewFunctionBehavior( 40 func(ctx context.Context, msg M) fn.Result[R] { 41 val, err := sFunc(msg) 42 return fn.NewResult(val, err) 43 }, 44 ) 45 }