Package rx is a reactive programming library for Go, inspired by
https://reactivex.io/ (mostly RxJS).
An Observer is a consumer of notifications delivered by an Observable.
An Observer is usually created and passed to Observable.Subscribe method
when subscribing to an Observable.
An Observable is a collection of future values, waiting to become a flow
of data. Subscribing an Observer to an Observable makes it happen.
When an Observable is subscribed, its values, when available, are emitted
to a given Observer.
There are four kinds of notifications:
Both Error notifications and Stop notifications carry an error value.
The main differences between them are as follows:
An Operator is an operation on an Observable. When applied, they do not
change the existing Observable value. Instead, they return a new one,
whose subscription logic is based on the first Observable.
There are many kinds of Operators in this library.
Here is a list of what Operators can do:
Previously, Operator was also a function type like Observable and
Observer. It was changed to be an interface type for one reason:
implementations can carry additional methods for setting extra options.
For example, MergeMap has two extra options:
MergeMapOperator.WithBuffering and MergeMapOperator.WithConcurrency,
and this is how they are specified when using a MergeMap:
To chain multiple Operators, do either this:
or this:
There are 9 Pipe functions in this library, from Pipe1 to Pipe9.
For different number of Operators, use different Pipe function.
When there are really too many Operators to chain, do either this:
or this:
There are 8 Compose functions in this library, from Compose2 to
Compose9.
Notifications emitted by an Observable may come from any started
goroutine, but they are guaranteed to be in sequence, one after another.
Operators in a chain may run in different goroutines.
In the following code:
Race conditions could happen for any two of ob, op1, op2, op3 and o.
Race conditions could also happen for any two Observables, however, not
every Operator or Observable has concurrent behavior.
The following operations may cause concurrent behavior:
The following operations may cause concurrent behavior due to Context
cancellation:
Since Context cancellations are very common in this library, and that
a Context cancellation usually results in a Stop notification, emitted
in a goroutine started by Context.AfterFunc or Context.Go, handling
Stop notifications must take extra precaution. The problem is that,
Stop notifications are not deterministic. They may just come from random
goroutines. If that happens, one would have to deal with race conditions.
It's very common that an Observable, when subscribed, also subscribes to
other Observables.
In this library, inner Observables are usually subscribed in the same
goroutine where the outer one is being subscribed. However,
When in doubt, read the code.
Multicasts and Unicasts are special Observables that developers can decide
what values they produce or when they complete, later after they are subscribed.
Multicasts can be subscribed multiple times, whereas Unicasts can only be
successfully subscribed once.
Both Multicasts and Unicasts are safe for concurrent use.
Here is an example demonstrates how to use a Unicast.
Ecosystem
proxy.golang.org
Latest Release
v0.10.0
over 1 year ago
Versions
21