#NEWLINE# @ extensionclassmethod ( Observable ) #NEWLINE# def when( cls , * args ) : #NEWLINE# #INDENT# 'Joins together the results from several patterns.\n\n    :param Observable cls: Observable class.\n    :param list[Plan] args: A series of plans (specified as a list of as a\n        series of arguments) created by use of the Then operator on patterns.\n    :returns: Observable sequence with the results form matching several\n        patterns.\n    :rtype: Observable\n    ' #NEWLINE# plans = ( args [ 0 ] if ( len ( args ) and isinstance ( args [ 0 ] , list ) ) else list ( args ) ) #NEWLINE# def subscribe( observer ) : #NEWLINE# #INDENT# active_plans = [ ] #NEWLINE# external_subscriptions = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# def on_error( err ) : #NEWLINE# #INDENT# for v in external_subscriptions . values ( ) : #NEWLINE# #INDENT# v . on_error ( err ) #NEWLINE# #UNINDENT# observer . on_error ( err ) #NEWLINE# #UNINDENT# out_observer = Observer ( observer . on_next , on_error , observer . on_completed ) #NEWLINE# def deactivate( active_plan ) : #NEWLINE# #INDENT# active_plans . remove ( active_plan ) #NEWLINE# if ( not len ( active_plans ) ) : #NEWLINE# #INDENT# observer . on_completed ( ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# for plan in plans : #NEWLINE# #INDENT# active_plans . append ( plan . activate ( external_subscriptions , out_observer , deactivate ) ) #NEWLINE# #UNINDENT# #UNINDENT# except Exception as ex : #NEWLINE# #INDENT# Observable . throw ( ex ) . subscribe ( observer ) #NEWLINE# #UNINDENT# group = CompositeDisposable ( ) #NEWLINE# for join_observer in external_subscriptions . values ( ) : #NEWLINE# #INDENT# join_observer . subscribe ( ) #NEWLINE# group . add ( join_observer ) #NEWLINE# #UNINDENT# return group #NEWLINE# #UNINDENT# return AnonymousObservable ( subscribe )