github.com/cs3org/reva/v2@v2.27.7/pkg/events/stream/stream.go (about) 1 // Package stream provides streaming clients used by `Consume` and `Publish` methods 2 package stream 3 4 import ( 5 "encoding/json" 6 "reflect" 7 8 "go-micro.dev/v4/events" 9 ) 10 11 // Chan is a channel based streaming clients 12 // Useful for tests or in memory applications 13 type Chan [2]chan interface{} 14 15 // Publish implementation 16 func (ch Chan) Publish(_ string, msg interface{}, _ ...events.PublishOption) error { 17 go func() { 18 ch[0] <- msg 19 }() 20 return nil 21 } 22 23 // Consume implementation 24 func (ch Chan) Consume(_ string, _ ...events.ConsumeOption) (<-chan events.Event, error) { 25 evch := make(chan events.Event) 26 go func() { 27 for { 28 e := <-ch[1] 29 if e == nil { 30 // channel closed 31 return 32 } 33 b, _ := json.Marshal(e) 34 evname := reflect.TypeOf(e).String() 35 evch <- events.Event{ 36 Payload: b, 37 Metadata: map[string]string{"eventtype": evname}, 38 } 39 } 40 }() 41 return evch, nil 42 }