AuthExchange
, LogExchange
), process an operation (e.g. FetchExchange
, WebSocketExchange
) or both (e.g. CacheExchange
).
Understanding the Client
When you create a client, it connects all exchanges into a single pipeline. The easiest way to imagine the pipeline is as a ladder with requests going down on the right side and coming up on the left side. We call the right side of the ladder downstream and the left side upstream. When a new opeartion request is created by the client, the operation starts going down the ladder stopping at each exchange. As mentioned, each exchange may either- modify the operation and push it further down the stream,
- process the operation and stop its way down,
- modify the operation and push it down as well as start processing it.
The Structure of an Exchange
Each exchange has to follow theExchange
protocol spec.
register
method. register
method lets the exchange hook itself to the stream of operations and call generic methods on client
. It should return the stream of operation results.
Do Nothing Exchange
The simplest exchange is an exchange that does nothing. It forwards the operations to the next exchange and returns the result stream of that exchange.Logging Exchange
A slightly more complex example of an exchange is a logging exchange that logs all operations and results that go up and down the stream.Operation Processing Exchange
Lastly, we are going to observe an example of a more complex exchange - an exchange that processes operations. Such an exchange should take care of- creating new streams,
- merging their results into the result upstream, and
- dismantling each pipeline when the application stops listening to events or the server has stopped sending them.
- create a shared stream,
- filter the operations it’s going to process and forward the rest downstream,
- create a new result stream for a new operation,
- emit events until the client sends a
teardown
event with the same operation ID, - manage the “dangling” stream appropriately.
FetchExchange
and WebSocketExchange
source codes to see an example.