diff --git a/events/events.go b/events/events.go index 6f68c70..947372d 100644 --- a/events/events.go +++ b/events/events.go @@ -1,8 +1,13 @@ package events const ( - InvoiceCreated = "invoice.created" - InvoiceSettled = "invoice.settled" + PeerUpdated = "peer.updated" + BlockReceived = "block.received" + InvoiceCreated = "invoice.created" + InvoiceSettled = "invoice.settled" + ChannelPending = "channel.pending" + ChannelActive = "channel.active" + ChannelInactive = "channel.inactive" ) type Event struct { diff --git a/pubsub/pubsub.go b/pubsub/pubsub.go index 620847e..625e7a4 100644 --- a/pubsub/pubsub.go +++ b/pubsub/pubsub.go @@ -5,6 +5,7 @@ import ( "os" "os/signal" "sync" + "time" "github.com/edouardparis/lntop/app" "github.com/edouardparis/lntop/events" @@ -29,6 +30,49 @@ func newPubSub(logger logging.Logger, network *network.Network) *pubSub { } } +func (p *pubSub) ticker(ctx context.Context, sub chan *events.Event) { + p.wg.Add(1) + ticker := time.NewTicker(3 * time.Second) + go func() { + var old *models.Info + for { + select { + case <-p.stop: + ticker.Stop() + p.wg.Done() + return + case <-ticker.C: + info, err := p.network.Info(ctx) + if err != nil { + p.logger.Error("SubscribeInvoice returned an error", logging.Error(err)) + } + if old != nil { + if old.BlockHeight != info.BlockHeight { + sub <- events.New(events.BlockReceived) + } + + if old.NumPeers != info.NumPeers { + sub <- events.New(events.PeerUpdated) + } + + if old.NumPendingChannels < info.NumPendingChannels { + sub <- events.New(events.ChannelPending) + } + + if old.NumActiveChannels < info.NumActiveChannels { + sub <- events.New(events.ChannelActive) + } + + if old.NumInactiveChannels < info.NumInactiveChannels { + sub <- events.New(events.ChannelInactive) + } + } + old = info + } + } + }() +} + func (p *pubSub) invoices(ctx context.Context, sub chan *events.Event) { p.wg.Add(3) invoices := make(chan *models.Invoice) @@ -81,5 +125,6 @@ func Run(ctx context.Context, app *app.App, sub chan *events.Event) { pubSub.logger.Debug("Starting...") pubSub.invoices(ctx, sub) + pubSub.ticker(ctx, sub) pubSub.wait() } diff --git a/ui/controller.go b/ui/controller.go index ff3410e..2fbd17e 100644 --- a/ui/controller.go +++ b/ui/controller.go @@ -71,10 +71,24 @@ func (c *controller) SetModels(ctx context.Context) error { func (c *controller) Listen(ctx context.Context, g *gocui.Gui, sub chan *events.Event) { c.logger.Debug("Listening...") for event := range sub { + var err error switch event.Type { + case events.BlockReceived: + err = c.models.RefreshInfo(ctx) + case events.ChannelPending: + err = c.models.RefreshInfo(ctx) + case events.ChannelActive: + err = c.models.RefreshInfo(ctx) + case events.ChannelInactive: + err = c.models.RefreshInfo(ctx) + case events.PeerUpdated: + err = c.models.RefreshInfo(ctx) default: c.logger.Info("event received", logging.String("type", event.Type)) } + if err != nil { + c.logger.Error("failed", logging.String("event", event.Type)) + } } }