-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
builtin.go
66 lines (50 loc) · 888 Bytes
/
builtin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package events
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type (
strm chan Message
msg struct {
value core.Value
err error
}
)
func New(source chan Message) Stream {
return strm(source)
}
func (s strm) Close(_ context.Context) error {
close(s)
return nil
}
func (s strm) Read(ctx context.Context) <-chan Message {
proxy := make(chan Message)
go func() {
defer close(proxy)
for {
select {
case <-ctx.Done():
return
case evt := <-s:
if ctx.Err() != nil {
return
}
proxy <- evt
}
}
}()
return s
}
func (n *msg) Value() core.Value {
return n.value
}
func (n *msg) Err() error {
return n.err
}
func WithValue(val core.Value) Message {
return &msg{value: val}
}
func WithErr(err error) Message {
return &msg{err: err, value: values.None}
}