github.com/MontFerret/ferret@v0.18.0/pkg/runtime/events/builtin.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 ( 11 strm chan Message 12 13 msg struct { 14 value core.Value 15 err error 16 } 17 ) 18 19 func New(source chan Message) Stream { 20 return strm(source) 21 } 22 23 func (s strm) Close(_ context.Context) error { 24 close(s) 25 26 return nil 27 } 28 29 func (s strm) Read(ctx context.Context) <-chan Message { 30 proxy := make(chan Message) 31 32 go func() { 33 defer close(proxy) 34 35 for { 36 select { 37 case <-ctx.Done(): 38 return 39 case evt := <-s: 40 if ctx.Err() != nil { 41 return 42 } 43 44 proxy <- evt 45 } 46 } 47 }() 48 49 return s 50 } 51 52 func (n *msg) Value() core.Value { 53 return n.value 54 } 55 56 func (n *msg) Err() error { 57 return n.err 58 } 59 60 func WithValue(val core.Value) Message { 61 return &msg{value: val} 62 } 63 64 func WithErr(err error) Message { 65 return &msg{err: err, value: values.None} 66 }