Observable¶
Subcription¶
The final target of an Observable, be it directly or after operations have been applied to it is that a subscription takes place. A subscription is performed by an Observer.
Ideally an Observer would be an instance of a class defining these 3 methods.
class Observer:
def on_next(self, value):
'''Called with each value generated by the Observable'''
def on_completed(self):
'''Called when the Observable can no longer produce values'''
def on_error(self, error):
'''Called when an error has occurred in the chain'''
Note
A class with the 3 methods is offered inside AnPyLar, where the needed methods can be overridden by the end user
In practice the following suffices
class Observer:
def on_next(self, value):
# Receive the next value generated by the Observable and do something
pass
or a simple callable
def observer(value):
# Receive the next value generated by the Observable and do something
pass
Subscribing:
ObservableSource.subscribe(on_next, on_completed=None, on_error=None, observer=None)
or
with chained operations:
ObservableSource \ .op1() \ .op2() \ .subscribe(on_next, on_completed=None, on_error=None, observer=None)
The parameters work as follows:
If
observeris notNone, it will be taken as the target for the values. It is assumed it will be an object with the following methods
on_next,on_completed,on_errorNote
In this case and because
on_nexthas no default value in the signature, anything can be passed, includingNoneIf
observerisNone
If
on_completedisNone, it will then be sought as an attribute ofon_nextIf
on_errorisNone, it will then be sought as an attribute ofon_next
on_nextwill be scanned for an attribute calledon_next. If found it will be taken, else the passed value will be used.
The pattern which one will usually apply:
observable.subscribe(callable)
A practical example:
Observable.from_(3).map(lambda x: x * 2).subscribe(print)
which prints:
6
Sources¶
The following methods create an Observable using the provided parameters.
from_¶
-
from_(iterable)¶ Generates an observable from
iterable, generating as many values as elements are present initerable
of¶
-
of(*args)¶ Generates an observable from
*args, generating as many values as arguments are provided
Operators¶
The following operators can be applied to Observables. They can be chained.
all¶
-
all(predicate)¶ Check if all items generated by the Observable meet the condition determined by
predicate
catch_exception¶
-
catch_exception(handler)¶ Swicth to another observable if an error has been produced and
on_errorwould be invoked in the subscriber.If
handleris an Observable, it will be forwarded to itElse,
handlermust be a callable accepting the value and returning the value to switch to for forwardingIf the forwarded value is an Observable it will be the result to switch to
Else, the forwarded value will be converted using
Observable.of
debounce¶
-
debounce(ms)¶ Delay the generated value by the amount of milliseconds
msand discard the delayed value if a new one is produced.
defval¶
-
defval(defval)¶ This is a particular implementation in AnPyLar. It provides a default value for the Observable + Chain of Operators, intended for elements that for example subscribe in the background (like html nodes do)
It is intended to be used as the last operator in a chain (although with care it can be used earlier in the chain)
delay¶
-
distinct(predicate=None)¶ Let the value through if it has not been seen before. If
predicateis provided it will be used to assess if a value is distinct from previous valuesNote
Use with care, because on long running observables, the buffer will grow with no limits.
distinct¶
-
distinct(predicate=None) Let the value through if it has not been seen before. If
predicateis provided it will be used to assess if a value is distinct from previous valuesNote
Use with care, because on long running observables, the buffer will grow with no limits.
distinct_until_changed¶
-
distinct_until_changed(predicate=None)¶ Let the value through if it is the same that was last seen. If
predicateis provided it will be used to assess if a value is distinct from previous values
do_action¶
-
do_action(action)¶ It forwards values after calling
actionwith the incoming value.The value is forwarded as is, regardless of the actions of
action
first¶
-
first(predicate=None)¶ Take only the first value.
If
predicateis provided, it will be used to determine which is the first value.
first_or_default¶
-
first_or_default(predicate=None, default_value=None)¶ Take only the 1st value. If
predicateis provided, it will be invoked with each value and the 1st value will be that for whichpredicatereturnsTrueIf no first value can be delivered, the operator will deliver
default_valueon completion.
publish¶
-
publish()¶ Freezes an observable source to make it multicast. Several subscriptions can take place without values being forwarded to them.
It supports the following additional operations:
auto_connect(count)Unfreeze the observable after
countsubscriptions have taken placeconnect()Unfreeze the observable
switch_map¶
-
switch_map(handler)¶ Swith to another observable based:
If
handleris an Observable, it will be switched to itElse,
handlermust be a callable accepting the value and returning the value to switch toIf the return value is an Observable it will be the result to switch to
Else, the return value will be converted using
Observable.of