lotus/paychmgr/msglistener.go

55 lines
1.2 KiB
Go
Raw Permalink Normal View History

2020-07-28 23:16:47 +00:00
package paychmgr
import (
"github.com/hannahhoward/go-pubsub"
2020-07-28 23:16:47 +00:00
"github.com/ipfs/go-cid"
2022-06-14 15:00:51 +00:00
"golang.org/x/xerrors"
2020-07-28 23:16:47 +00:00
)
type msgListeners struct {
ps *pubsub.PubSub
2020-07-28 23:16:47 +00:00
}
type msgCompleteEvt struct {
mcid cid.Cid
err error
2020-07-28 23:16:47 +00:00
}
type subscriberFn func(msgCompleteEvt)
2020-07-28 23:16:47 +00:00
func newMsgListeners() msgListeners {
ps := pubsub.New(func(event pubsub.Event, subFn pubsub.SubscriberFn) error {
evt, ok := event.(msgCompleteEvt)
if !ok {
return xerrors.Errorf("wrong type of event")
}
sub, ok := subFn.(subscriberFn)
if !ok {
return xerrors.Errorf("wrong type of subscriber")
}
sub(evt)
return nil
})
return msgListeners{ps: ps}
2020-07-28 23:16:47 +00:00
}
// onMsgComplete registers a callback for when the message with the given cid
// completes
func (ml *msgListeners) onMsgComplete(mcid cid.Cid, cb func(error)) pubsub.Unsubscribe {
var fn subscriberFn = func(evt msgCompleteEvt) {
if mcid.Equals(evt.mcid) {
cb(evt.err)
2020-07-28 23:16:47 +00:00
}
}
return ml.ps.Subscribe(fn)
2020-07-28 23:16:47 +00:00
}
// fireMsgComplete is called when a message completes
func (ml *msgListeners) fireMsgComplete(mcid cid.Cid, err error) {
e := ml.ps.Publish(msgCompleteEvt{mcid: mcid, err: err})
if e != nil {
// In theory we shouldn't ever get an error here
log.Errorf("unexpected error publishing message complete: %s", e)
}
2020-07-28 23:16:47 +00:00
}