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