from rx.core import Observable, AnonymousObservable
from rx.disposables import CompositeDisposable, SingleAssignmentDisposable
from rx.internal import extensionmethod, extensionclassmethod


def listify_args(*a):
    return list(a)


@extensionmethod(Observable, instancemethod=True)
def with_latest_from(self, *args):
    """Merges the specified observable sequences into one observable sequence
    by using the selector function only when the source observable sequence
    (the instance) produces an element. The other observables can be passed
    either as seperate arguments or as a list.

    1 - obs = observable.with_latest_from(obs1, obs2, obs3,
                                        lambda o1, o2, o3: o1 + o2 + o3)
    2 - obs = observable.with_latest_from([obs1, obs2, obs3],
                                        lambda o1, o2, o3: o1 + o2 + o3)

    Returns an observable sequence containing the result of combining
    elements of the sources using the specified result selector function.
    """

    args = list(args)
    if args and isinstance(args[0], list):
        args = args[0]

    args.insert(0, self)

    return Observable.with_latest_from(*args)


@extensionclassmethod(Observable)
def with_latest_from(cls, *args):

    """Merges the specified observable sequences into one observable sequence
    by using the selector function only when the first observable sequence
    produces an element. The observables can be passed either as seperate
    arguments or as a list.

    1 - obs = Observable.with_latest_from(obs1, obs2, obs3,
                                       lambda o1, o2, o3: o1 + o2 + o3)
    2 - obs = Observable.with_latest_from([obs1, obs2, obs3],
                                        lambda o1, o2, o3: o1 + o2 + o3)

    Returns an observable sequence containing the result of combining
    elements of the sources using the specified result selector function.
    """

    if args and isinstance(args[0], list):
        args = args[0]
    else:
        args = list(args)

    result_selector = args.pop()
    NO_VALUE = object()

    def subscribe(observer):

        def subscribe_all(parent, *children):

            values = [NO_VALUE for _ in children]

            def subscribe_child(i, child):
                subscription = SingleAssignmentDisposable()
                def on_next(value):
                    with parent.lock:
                        values[i] = value
                subscription.disposable = child.subscribe(
                    on_next, observer.on_error)
                return subscription

            parent_subscription = SingleAssignmentDisposable()
            def on_next(value):
                with parent.lock:
                    if NO_VALUE not in values:
                        try:
                            result = result_selector(value, *values)
                        except Exception as error:
                            observer.on_error(error)
                        else:
                            observer.on_next(result)
            parent_subscription.disposable = parent.subscribe(
                on_next, observer.on_error, observer.on_completed)

            return listify_args(
                parent_subscription,
                *(subscribe_child(*a) for a in enumerate(children))
            )

        return CompositeDisposable(subscribe_all(*args))
    return AnonymousObservable(subscribe)

@@ -1012,38 +1012,16 @@
    %22%22%22%0A%0A
-    args = list(args)%0A
     if a
@@ -1063,28 +1063,66 @@
         
-args
+children = args%5B0%5D%0A        result_selector
  = args%5B
 0%5D%0A%0A    
@@ -1117,35 +1117,109 @@
 rgs%5B
-0%5D%0A%0A
+1%5D%0A   
     
+ 
 args
-.insert(0, self
+_ = %5Bself%5D + children + %5Bresult_selector%5D%0A    else:%0A        args_ = %5Bself%5D + list(args
 )%0A%0A 
@@ -1261,16 +1261,17 @@
 om(*args
+_
 )%0A%0A%0A@ext
@@ -2067,20 +2067,61 @@
     
-args
+observables = args%5B0%5D%0A        result_selector
  = args%5B
 0%5D%0A 
@@ -2112,25 +2112,25 @@
 ctor = args%5B
-0
+1
 %5D%0A    else:%0A
@@ -2137,19 +2137,26 @@
         
-arg
+observable
 s = list
@@ -2156,27 +2156,35 @@
  = list(args
-)%0A%0A
+%5B:-1%5D)%0A    
     result_s
@@ -2201,14 +2201,13 @@
 args
-.pop()
+%5B-1%5D%0A
 %0A   
@@ -3514,19 +3514,26 @@
 be_all(*
-arg
+observable
 s))%0A    

Fix parameter interpretation of 'with_latest_from'