|
1 | 1 | from asyncio import Future
|
2 |
| -from typing import Callable, List, Optional, TypeVar, Union |
| 2 | +from typing import List, Optional, TypeVar, Union |
3 | 3 |
|
4 | 4 | from reactivex import Observable, abc, from_future
|
| 5 | +from reactivex.curry import curry_flip |
5 | 6 | from reactivex.disposable import CompositeDisposable, SingleAssignmentDisposable
|
6 | 7 |
|
7 | 8 | _T = TypeVar("_T")
|
8 | 9 |
|
9 | 10 |
|
| 11 | +@curry_flip(1) |
10 | 12 | def amb_(
|
11 |
| - right_source: Union[Observable[_T], "Future[_T]"] |
12 |
| -) -> Callable[[Observable[_T]], Observable[_T]]: |
| 13 | + left_source: Observable[_T], right_source: Union[Observable[_T], "Future[_T]"] |
| 14 | +) -> Observable[_T]: |
13 | 15 |
|
14 | 16 | if isinstance(right_source, Future):
|
15 | 17 | obs: Observable[_T] = from_future(right_source)
|
16 | 18 | else:
|
17 | 19 | obs = right_source
|
18 | 20 |
|
19 |
| - def amb(left_source: Observable[_T]) -> Observable[_T]: |
20 |
| - def subscribe( |
21 |
| - observer: abc.ObserverBase[_T], |
22 |
| - scheduler: Optional[abc.SchedulerBase] = None, |
23 |
| - ) -> abc.DisposableBase: |
24 |
| - choice: List[Optional[str]] = [None] |
25 |
| - left_choice = "L" |
26 |
| - right_choice = "R" |
27 |
| - left_subscription = SingleAssignmentDisposable() |
28 |
| - right_subscription = SingleAssignmentDisposable() |
29 |
| - |
30 |
| - def choice_left(): |
31 |
| - if not choice[0]: |
32 |
| - choice[0] = left_choice |
33 |
| - right_subscription.dispose() |
34 |
| - |
35 |
| - def choice_right(): |
36 |
| - if not choice[0]: |
37 |
| - choice[0] = right_choice |
38 |
| - left_subscription.dispose() |
39 |
| - |
40 |
| - def on_next_left(value: _T) -> None: |
41 |
| - with left_source.lock: |
42 |
| - choice_left() |
43 |
| - if choice[0] == left_choice: |
44 |
| - observer.on_next(value) |
45 |
| - |
46 |
| - def on_error_left(err: Exception) -> None: |
47 |
| - with left_source.lock: |
48 |
| - choice_left() |
49 |
| - if choice[0] == left_choice: |
50 |
| - observer.on_error(err) |
51 |
| - |
52 |
| - def on_completed_left() -> None: |
53 |
| - with left_source.lock: |
54 |
| - choice_left() |
55 |
| - if choice[0] == left_choice: |
56 |
| - observer.on_completed() |
57 |
| - |
58 |
| - left_d = left_source.subscribe( |
59 |
| - on_next_left, on_error_left, on_completed_left, scheduler=scheduler |
60 |
| - ) |
61 |
| - left_subscription.disposable = left_d |
62 |
| - |
63 |
| - def send_right(value: _T) -> None: |
64 |
| - with left_source.lock: |
65 |
| - choice_right() |
66 |
| - if choice[0] == right_choice: |
67 |
| - observer.on_next(value) |
68 |
| - |
69 |
| - def on_error_right(err: Exception) -> None: |
70 |
| - with left_source.lock: |
71 |
| - choice_right() |
72 |
| - if choice[0] == right_choice: |
73 |
| - observer.on_error(err) |
74 |
| - |
75 |
| - def on_completed_right() -> None: |
76 |
| - with left_source.lock: |
77 |
| - choice_right() |
78 |
| - if choice[0] == right_choice: |
79 |
| - observer.on_completed() |
80 |
| - |
81 |
| - right_d = obs.subscribe( |
82 |
| - send_right, on_error_right, on_completed_right, scheduler=scheduler |
83 |
| - ) |
84 |
| - right_subscription.disposable = right_d |
85 |
| - return CompositeDisposable(left_subscription, right_subscription) |
86 |
| - |
87 |
| - return Observable(subscribe) |
88 |
| - |
89 |
| - return amb |
| 21 | + def subscribe( |
| 22 | + observer: abc.ObserverBase[_T], |
| 23 | + scheduler: Optional[abc.SchedulerBase] = None, |
| 24 | + ) -> abc.DisposableBase: |
| 25 | + choice: List[Optional[str]] = [None] |
| 26 | + left_choice = "L" |
| 27 | + right_choice = "R" |
| 28 | + left_subscription = SingleAssignmentDisposable() |
| 29 | + right_subscription = SingleAssignmentDisposable() |
| 30 | + |
| 31 | + def choice_left(): |
| 32 | + if not choice[0]: |
| 33 | + choice[0] = left_choice |
| 34 | + right_subscription.dispose() |
| 35 | + |
| 36 | + def choice_right(): |
| 37 | + if not choice[0]: |
| 38 | + choice[0] = right_choice |
| 39 | + left_subscription.dispose() |
| 40 | + |
| 41 | + def on_next_left(value: _T) -> None: |
| 42 | + with left_source.lock: |
| 43 | + choice_left() |
| 44 | + if choice[0] == left_choice: |
| 45 | + observer.on_next(value) |
| 46 | + |
| 47 | + def on_error_left(err: Exception) -> None: |
| 48 | + with left_source.lock: |
| 49 | + choice_left() |
| 50 | + if choice[0] == left_choice: |
| 51 | + observer.on_error(err) |
| 52 | + |
| 53 | + def on_completed_left() -> None: |
| 54 | + with left_source.lock: |
| 55 | + choice_left() |
| 56 | + if choice[0] == left_choice: |
| 57 | + observer.on_completed() |
| 58 | + |
| 59 | + left_d = left_source.subscribe( |
| 60 | + on_next_left, on_error_left, on_completed_left, scheduler=scheduler |
| 61 | + ) |
| 62 | + left_subscription.disposable = left_d |
| 63 | + |
| 64 | + def send_right(value: _T) -> None: |
| 65 | + with left_source.lock: |
| 66 | + choice_right() |
| 67 | + if choice[0] == right_choice: |
| 68 | + observer.on_next(value) |
| 69 | + |
| 70 | + def on_error_right(err: Exception) -> None: |
| 71 | + with left_source.lock: |
| 72 | + choice_right() |
| 73 | + if choice[0] == right_choice: |
| 74 | + observer.on_error(err) |
| 75 | + |
| 76 | + def on_completed_right() -> None: |
| 77 | + with left_source.lock: |
| 78 | + choice_right() |
| 79 | + if choice[0] == right_choice: |
| 80 | + observer.on_completed() |
| 81 | + |
| 82 | + right_d = obs.subscribe( |
| 83 | + send_right, on_error_right, on_completed_right, scheduler=scheduler |
| 84 | + ) |
| 85 | + right_subscription.disposable = right_d |
| 86 | + return CompositeDisposable(left_subscription, right_subscription) |
| 87 | + |
| 88 | + return Observable(subscribe) |
90 | 89 |
|
91 | 90 |
|
92 | 91 | __all__ = ["amb_"]
|
0 commit comments