Amb takes several Observables, emit all of the items from only the first of these Observables to emit an item or notification.
(observables []Observable, opts ...Option)
| 11 | // Amb takes several Observables, emit all of the items from only the first of these Observables |
| 12 | // to emit an item or notification. |
| 13 | func Amb(observables []Observable, opts ...Option) Observable { |
| 14 | option := parseOptions(opts...) |
| 15 | ctx := option.buildContext(emptyContext) |
| 16 | next := option.buildChannel() |
| 17 | once := sync.Once{} |
| 18 | |
| 19 | f := func(o Observable) { |
| 20 | it := o.Observe(opts...) |
| 21 | |
| 22 | select { |
| 23 | case <-ctx.Done(): |
| 24 | return |
| 25 | case item, ok := <-it: |
| 26 | if !ok { |
| 27 | return |
| 28 | } |
| 29 | once.Do(func() { |
| 30 | defer close(next) |
| 31 | if item.Error() { |
| 32 | next <- item |
| 33 | return |
| 34 | } |
| 35 | next <- item |
| 36 | for { |
| 37 | select { |
| 38 | case <-ctx.Done(): |
| 39 | return |
| 40 | case item, ok := <-it: |
| 41 | if !ok { |
| 42 | return |
| 43 | } |
| 44 | if item.Error() { |
| 45 | next <- item |
| 46 | return |
| 47 | } |
| 48 | next <- item |
| 49 | } |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | for _, o := range observables { |
| 56 | go f(o) |
| 57 | } |
| 58 | |
| 59 | return &ObservableImpl{ |
| 60 | iterable: newChannelIterable(next), |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // CombineLatest combines the latest item emitted by each Observable via a specified function |
| 65 | // and emit items based on the results of this function. |