github.com/MontFerret/ferret@v0.18.0/pkg/runtime/events/iterator.go (about)

     1  package events
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  )
     9  
    10  type Iterator struct {
    11  	messages <-chan Message
    12  }
    13  
    14  func NewIterator(ch <-chan Message) core.Iterator {
    15  	return &Iterator{ch}
    16  }
    17  
    18  func (e *Iterator) Next(ctx context.Context) (value core.Value, key core.Value, err error) {
    19  	select {
    20  	case evt, ok := <-e.messages:
    21  		if !ok {
    22  			return values.None, values.None, core.ErrNoMoreData
    23  		}
    24  
    25  		if err := evt.Err(); err != nil {
    26  			return values.None, values.None, err
    27  		}
    28  
    29  		return evt.Value(), values.None, nil
    30  	case <-ctx.Done():
    31  		return values.None, values.None, ctx.Err()
    32  	}
    33  }