Skip to content

Commit

Permalink
stream.go: refactor some code in Run() out to routeBlock() function
Browse files Browse the repository at this point in the history
  • Loading branch information
pnx committed May 10, 2023
1 parent c27da9b commit 3f4bbff
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ func (s *Stream) SendStatusRequest() error {
})
}

func (s *Stream) routeBlock(block *ship.GetBlocksResultV0) {
if s.BlockHandler != nil {
s.BlockHandler(block)
}

if block.Traces != nil && len(block.Traces.Elem) > 0 && s.TraceHandler != nil {
s.TraceHandler(block.Traces.AsTransactionTracesV0())
}

if block.Deltas != nil && len(block.Deltas.Elem) > 0 && s.TableDeltaHandler != nil {
s.TableDeltaHandler(block.Deltas.AsTableDeltasV0())
}
}

// Run starts the stream.
// Messages from the server is read and forwarded to the appropriate callback function.
// This function will block until an error occur or the stream is closed.
Expand All @@ -266,17 +280,7 @@ func (s *Stream) Run() error {
continue
}

if s.BlockHandler != nil {
s.BlockHandler(block)
}

if block.Traces != nil && len(block.Traces.Elem) > 0 && s.TraceHandler != nil {
s.TraceHandler(block.Traces.AsTransactionTracesV0())
}

if block.Deltas != nil && len(block.Deltas.Elem) > 0 && s.TableDeltaHandler != nil {
s.TableDeltaHandler(block.Deltas.AsTableDeltasV0())
}
s.routeBlock(block)

if block.ThisBlock.BlockNum+1 >= s.EndBlock {
// Send Close message, ignore errors here as we
Expand Down

0 comments on commit 3f4bbff

Please sign in to comment.