protofsm: add SpendMapper to craft custom spend events

In this commit, we add the SpendMapper which allows callers to create
custom spent events. Before this commit, the caller would be able to
have an event sent to them in the case a spend happens, but that event
wouldn't have any of the relevant spend details.

With this new addition, the caller can specify how to take a generic
spend event, and transform it into the state machine specific spend
event.
This commit is contained in:
Olaoluwa Osuntokun committed 2024-11-18 20:49:00 -08:00
1 parent d805c0fc3c
commit 847c1a789d
2 files changed
+13 -8

No files matched your search

+4 -3
View File
@@ -451,13 +451,14 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent( //nolint:funlen
defer s.wg.Done()
for {
select {
case <-spendEvent.Spend:
case spend := <-spendEvent.Spend:
// If there's a post-send event, then
// we'll send that into the current
// state now.
postSpend := daemonEvent.PostSpendEvent
postSpend.WhenSome(func(e Event) {
s.SendEvent(e)
postSpend.WhenSome(func(f SpendMapper[Event]) { //nolint:lll
customEvent := f(spend)
s.SendEvent(customEvent)
})
return