github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/output/render_ctx.go (about)

     1  package output
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
     7  )
     8  
     9  type RenderCtx struct {
    10  	Ctx       context.Context    `json:"-"`
    11  	Cancel    context.CancelFunc `json:"-"`
    12  	ModelChan chan types.Modeler `json:"-"`
    13  	ErrorChan chan error         `json:"-"`
    14  }
    15  
    16  func NewRenderContext() *RenderCtx {
    17  	ctx, cancel := context.WithCancel(context.Background())
    18  	return &RenderCtx{
    19  		Ctx:    ctx,
    20  		Cancel: cancel,
    21  	}
    22  }
    23  
    24  func NewStreamingContext() *RenderCtx {
    25  	rCtx := NewRenderContext()
    26  	// TODO: Should these be buffered channels? Issue #3821
    27  	rCtx.ModelChan = make(chan types.Modeler)
    28  	rCtx.ErrorChan = make(chan error)
    29  	return rCtx
    30  }
    31  
    32  func (r *RenderCtx) WasCanceled() bool {
    33  	select {
    34  	case <-r.Ctx.Done():
    35  		return true
    36  	default:
    37  		return false
    38  	}
    39  }