|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from collections.abc import Callable, Iterable |
5 | 6 | from typing import TYPE_CHECKING, Any, Generic, TypeVar, cast |
6 | 7 |
|
7 | 8 | if TYPE_CHECKING: |
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | _T = TypeVar("_T", covariant=True) |
| 15 | +_T2 = TypeVar("_T2") |
14 | 16 |
|
15 | 17 |
|
16 | 18 | class CombinationMixin(Generic[_T]): |
@@ -311,3 +313,171 @@ def amb(self, right_source: Observable[_T]) -> Observable[_T]: |
311 | 313 | from reactivex import operators as ops |
312 | 314 |
|
313 | 315 | return self._as_observable().pipe(ops.amb(right_source)) |
| 316 | + |
| 317 | + def merge_all(self) -> Observable[Any]: |
| 318 | + """Merge all inner observables. |
| 319 | +
|
| 320 | + Merges an observable sequence of observable sequences into an observable |
| 321 | + sequence. |
| 322 | +
|
| 323 | + Examples: |
| 324 | + Fluent style: |
| 325 | + >>> result = source_of_sources.merge_all() |
| 326 | +
|
| 327 | + Equivalent pipe style: |
| 328 | + >>> from reactivex import operators as ops |
| 329 | + >>> result = source_of_sources.pipe(ops.merge_all()) |
| 330 | +
|
| 331 | + Returns: |
| 332 | + An observable sequence that merges the elements of the inner sequences. |
| 333 | +
|
| 334 | + See Also: |
| 335 | + - :func:`merge_all <reactivex.operators.merge_all>` |
| 336 | + - :meth:`merge` |
| 337 | + - :meth:`concat_all` |
| 338 | + """ |
| 339 | + # Cast is safe: merge_all is meant to be called on Observable of Observables. |
| 340 | + # The fluent API allows chaining this on nested observable sequences where |
| 341 | + # _T is Observable[_T2]. We return Observable[Any] as the inner type cannot |
| 342 | + # be statically inferred from _T without higher-kinded types. |
| 343 | + from reactivex import operators as ops |
| 344 | + |
| 345 | + op: Callable[[Observable[Any]], Observable[Any]] = cast( |
| 346 | + "Callable[[Observable[Any]], Observable[Any]]", ops.merge_all() |
| 347 | + ) |
| 348 | + return self._as_observable().pipe(op) |
| 349 | + |
| 350 | + def zip_with_iterable( |
| 351 | + self, second: Iterable[_T2] |
| 352 | + ) -> Observable[tuple[_T, _T2]]: |
| 353 | + """Zip with iterable. |
| 354 | +
|
| 355 | + Merges the specified observable sequence and iterable into one observable |
| 356 | + sequence by creating a tuple whenever both sequences have produced an element |
| 357 | + at a corresponding index. |
| 358 | +
|
| 359 | + Examples: |
| 360 | + Fluent style: |
| 361 | + >>> result = source.zip_with_iterable([1, 2, 3]) |
| 362 | +
|
| 363 | + Equivalent pipe style: |
| 364 | + >>> from reactivex import operators as ops |
| 365 | + >>> result = source.pipe(ops.zip_with_iterable([1, 2, 3])) |
| 366 | +
|
| 367 | + Args: |
| 368 | + second: Iterable to zip with the source observable. |
| 369 | +
|
| 370 | + Returns: |
| 371 | + An observable sequence containing the result of combining elements of the |
| 372 | + sources as a tuple. |
| 373 | +
|
| 374 | + See Also: |
| 375 | + - :func:`zip_with_iterable <reactivex.operators.zip_with_iterable>` |
| 376 | + - :meth:`zip` |
| 377 | + """ |
| 378 | + from reactivex import operators as ops |
| 379 | + |
| 380 | + return self._as_observable().pipe(ops.zip_with_iterable(second)) |
| 381 | + |
| 382 | + def join( |
| 383 | + self, |
| 384 | + right: Observable[_T2], |
| 385 | + left_duration_mapper: Callable[[_T], Observable[Any]], |
| 386 | + right_duration_mapper: Callable[[_T2], Observable[Any]], |
| 387 | + ) -> Observable[tuple[_T, _T2]]: |
| 388 | + """Join based on overlapping durations. |
| 389 | +
|
| 390 | + Correlates the elements of two sequences based on overlapping durations. |
| 391 | +
|
| 392 | + Examples: |
| 393 | + Fluent style: |
| 394 | + >>> result = left.join( |
| 395 | + ... right, |
| 396 | + ... lambda x: rx.timer(0.5), |
| 397 | + ... lambda x: rx.timer(0.5) |
| 398 | + ... ) |
| 399 | +
|
| 400 | + Equivalent pipe style: |
| 401 | + >>> from reactivex import operators as ops |
| 402 | + >>> result = left.pipe( |
| 403 | + ... ops.join( |
| 404 | + ... right, |
| 405 | + ... lambda x: rx.timer(0.5), |
| 406 | + ... lambda x: rx.timer(0.5) |
| 407 | + ... ) |
| 408 | + ... ) |
| 409 | +
|
| 410 | + Args: |
| 411 | + right: The right observable sequence to join elements for. |
| 412 | + left_duration_mapper: A function to select the duration (expressed as an |
| 413 | + observable sequence) of each element of the left observable sequence, |
| 414 | + used to determine overlap. |
| 415 | + right_duration_mapper: A function to select the duration (expressed as an |
| 416 | + observable sequence) of each element of the right observable sequence, |
| 417 | + used to determine overlap. |
| 418 | +
|
| 419 | + Returns: |
| 420 | + An observable sequence that contains elements combined into a tuple from |
| 421 | + source elements that have an overlapping duration. |
| 422 | +
|
| 423 | + See Also: |
| 424 | + - :func:`join <reactivex.operators.join>` |
| 425 | + - :meth:`group_join` |
| 426 | + """ |
| 427 | + from reactivex import operators as ops |
| 428 | + |
| 429 | + return self._as_observable().pipe( |
| 430 | + ops.join(right, left_duration_mapper, right_duration_mapper) |
| 431 | + ) |
| 432 | + |
| 433 | + def group_join( |
| 434 | + self, |
| 435 | + right: Observable[_T2], |
| 436 | + left_duration_mapper: Callable[[_T], Observable[Any]], |
| 437 | + right_duration_mapper: Callable[[_T2], Observable[Any]], |
| 438 | + ) -> Observable[tuple[_T, Observable[_T2]]]: |
| 439 | + """Group join based on overlapping durations. |
| 440 | +
|
| 441 | + Correlates the elements of two sequences based on overlapping durations, and |
| 442 | + groups the results. |
| 443 | +
|
| 444 | + Examples: |
| 445 | + Fluent style: |
| 446 | + >>> result = left.group_join( |
| 447 | + ... right, |
| 448 | + ... lambda x: rx.timer(0.5), |
| 449 | + ... lambda x: rx.timer(0.5) |
| 450 | + ... ) |
| 451 | +
|
| 452 | + Equivalent pipe style: |
| 453 | + >>> from reactivex import operators as ops |
| 454 | + >>> result = left.pipe( |
| 455 | + ... ops.group_join( |
| 456 | + ... right, |
| 457 | + ... lambda x: rx.timer(0.5), |
| 458 | + ... lambda x: rx.timer(0.5) |
| 459 | + ... ) |
| 460 | + ... ) |
| 461 | +
|
| 462 | + Args: |
| 463 | + right: The right observable sequence to join elements for. |
| 464 | + left_duration_mapper: A function to select the duration (expressed as an |
| 465 | + observable sequence) of each element of the left observable sequence, |
| 466 | + used to determine overlap. |
| 467 | + right_duration_mapper: A function to select the duration (expressed as an |
| 468 | + observable sequence) of each element of the right observable sequence, |
| 469 | + used to determine overlap. |
| 470 | +
|
| 471 | + Returns: |
| 472 | + An observable sequence that contains elements combined into a tuple from |
| 473 | + source elements that have an overlapping duration. |
| 474 | +
|
| 475 | + See Also: |
| 476 | + - :func:`group_join <reactivex.operators.group_join>` |
| 477 | + - :meth:`join` |
| 478 | + """ |
| 479 | + from reactivex import operators as ops |
| 480 | + |
| 481 | + return self._as_observable().pipe( |
| 482 | + ops.group_join(right, left_duration_mapper, right_duration_mapper) |
| 483 | + ) |
0 commit comments