GroupByDynamic divides an Observable into a dynamic set of Observables that each emit GroupedObservable from the original Observable, organized by key.
(distribution func(Item) string, opts ...Option)
| 1359 | |
| 1360 | // GroupByDynamic divides an Observable into a dynamic set of Observables that each emit GroupedObservable from the original Observable, organized by key. |
| 1361 | func (o *ObservableImpl) GroupByDynamic(distribution func(Item) string, opts ...Option) Observable { |
| 1362 | option := parseOptions(opts...) |
| 1363 | next := option.buildChannel() |
| 1364 | ctx := option.buildContext(o.parent) |
| 1365 | chs := make(map[string]chan Item) |
| 1366 | |
| 1367 | go func() { |
| 1368 | observe := o.Observe(opts...) |
| 1369 | loop: |
| 1370 | for { |
| 1371 | select { |
| 1372 | case <-ctx.Done(): |
| 1373 | break loop |
| 1374 | case i, ok := <-observe: |
| 1375 | if !ok { |
| 1376 | break loop |
| 1377 | } |
| 1378 | idx := distribution(i) |
| 1379 | ch, contains := chs[idx] |
| 1380 | if !contains { |
| 1381 | ch = option.buildChannel() |
| 1382 | chs[idx] = ch |
| 1383 | Of(GroupedObservable{ |
| 1384 | Observable: &ObservableImpl{ |
| 1385 | iterable: newChannelIterable(ch), |
| 1386 | }, |
| 1387 | Key: idx, |
| 1388 | }).SendContext(ctx, next) |
| 1389 | } |
| 1390 | i.SendContext(ctx, ch) |
| 1391 | } |
| 1392 | } |
| 1393 | for _, ch := range chs { |
| 1394 | close(ch) |
| 1395 | } |
| 1396 | close(next) |
| 1397 | }() |
| 1398 | |
| 1399 | return &ObservableImpl{ |
| 1400 | iterable: newChannelIterable(next), |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | // Last returns a new Observable which emit only last item. |
| 1405 | // Cannot be run in parallel. |
nothing calls this directly
no test coverage detected