<s> from rx . observable import Observable <EOL> from rx . anonymousobservable import AnonymousObservable <EOL> from rx . disposables import CompositeDisposable , SingleAssignmentDisposable , SerialDisposable <EOL> from rx . concurrency import timeout_scheduler <EOL> from rx . internal import extensionmethod <EOL> @ extensionmethod ( Observable , alias = "<STR_LIT>" ) <EOL> def debounce ( self , duetime , scheduler = None ) : <EOL> """<STR_LIT>""" <EOL> scheduler = scheduler or timeout_scheduler <EOL> source = self <EOL> def subscribe ( observer ) : <EOL> cancelable = SerialDisposable ( ) <EOL> has_value = [ False ] <EOL> value = [ None ] <EOL> _id = [ <NUM_LIT:0> ] <EOL> def on_next ( x ) : <EOL> has_value [ <NUM_LIT:0> ] = True <EOL> value [ <NUM_LIT:0> ] = x <EOL> _id [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> current_id = _id [ <NUM_LIT:0> ] <EOL> d = SingleAssignmentDisposable ( ) <EOL> cancelable . disposable = d <EOL> def action ( scheduler , state = None ) : <EOL> if has_value [ <NUM_LIT:0> ] and _id [ <NUM_LIT:0> ] == current_id : <EOL> observer . on_next ( value [ <NUM_LIT:0> ] ) <EOL> has_value [ <NUM_LIT:0> ] = False <EOL> d . disposable = scheduler . schedule_relative ( duetime , action ) <EOL> def on_error ( exception ) : <EOL> cancelable . dispose ( ) <EOL> observer . on_error ( exception ) <EOL> has_value [ <NUM_LIT:0> ] = False <EOL> _id [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> def on_completed ( ) : <EOL> cancelable . dispose ( ) <EOL> if has_value [ <NUM_LIT:0> ] : <EOL> observer . on_next ( value [ <NUM_LIT:0> ] ) <EOL> observer . on_completed ( ) <EOL> has_value [ <NUM_LIT:0> ] = False <EOL> _id [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> subscription = source . subscribe ( on_next , on_error , on_completed ) <EOL> return CompositeDisposable ( subscription , cancelable ) <EOL> return AnonymousObservable ( subscribe ) <EOL> @ extensionmethod ( Observable ) <EOL> def throttle_with_selector ( self , throttle_duration_selector ) : <EOL> """<STR_LIT>""" <EOL> source = self <EOL> def subscribe ( observer ) : <EOL> cancelable = SerialDisposable ( ) <EOL> has_value = [ False ] <EOL> value = [ None ] <EOL> _id = [ <NUM_LIT:0> ] <EOL> def on_next ( x ) : <EOL> throttle = None <EOL> try : <EOL> throttle = throttle_duration_selector ( x ) <EOL> except Exception as e : <EOL> observer . on_error ( e ) <EOL> return <EOL> has_value [ <NUM_LIT:0> ] = True <EOL> value [ <NUM_LIT:0> ] = x <EOL> _id [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> current_id = _id [ <NUM_LIT:0> ] <EOL> d = SingleAssignmentDisposable ( ) <EOL> cancelable . disposable = d <EOL> def on_next ( x ) : <EOL> if has_value [ <NUM_LIT:0> ] and _id [ <NUM_LIT:0> ] == current_id : <EOL> observer . on_next ( value [ <NUM_LIT:0> ] ) <EOL> has_value [ <NUM_LIT:0> ] = False <EOL> d . dispose ( ) <EOL> def on_completed ( ) : <EOL> if has_value [ <NUM_LIT:0> ] and _id [ <NUM_LIT:0> ] == current_id : <EOL> observer . on_next ( value [ <NUM_LIT:0> ] ) <EOL> has_value [ <NUM_LIT:0> ] = False <EOL> d . dispose ( ) <EOL> d . disposable = throttle . subscribe ( on_next , observer . on_error , <EOL> on_completed ) <EOL> def on_error ( e ) : <EOL> cancelable . dispose ( ) <EOL> observer . on_error ( e ) <EOL> has_value [ <NUM_LIT:0> ] = False <EOL> _id [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> def on_completed ( ) : <EOL> cancelable . dispose ( ) <EOL> if has_value [ <NUM_LIT:0> ] : <EOL> observer . on_next ( value [ <NUM_LIT:0> ] ) <EOL> observer . on_completed ( ) <EOL> has_value [ <NUM_LIT:0> ] = False <EOL> _id [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> subscription = source . subscribe ( on_next , on_error , on_completed ) <EOL> return CompositeDisposable ( subscription , cancelable ) <EOL> return AnonymousObservable ( subscribe ) </s>
