51 lines
1.3 KiB
Markdown
51 lines
1.3 KiB
Markdown
# uni-channel
|
|
|
|
`uni-channel` provides one simple wrapper over `async-channel`, `place-channel`
|
|
and `port-channel`.
|
|
|
|
```racket
|
|
(make-uni-channel channel)
|
|
```
|
|
|
|
The constructor detects the concrete channel kind and returns a `uni-channel?`.
|
|
No helper constructors are part of the public API; create the underlying channel
|
|
with the normal Racket or `port-channel` constructor and then wrap it.
|
|
|
|
A wrapped channel that can receive is itself synchronizable. `(sync ch)` returns
|
|
the value received from the underlying channel. For an output-only endpoint, such
|
|
as an output `port-channel`, the receive event is `never-evt` because no value can
|
|
be received from that endpoint.
|
|
|
|
|
|
```racket
|
|
(require racket/async-channel
|
|
uni-channel)
|
|
|
|
(define ch (make-uni-channel (make-async-channel)))
|
|
(uni-channel-send ch 'hello)
|
|
(uni-channel-recv ch)
|
|
```
|
|
|
|
For `port-channel`, wrap the input and output endpoints separately:
|
|
|
|
```racket
|
|
(require port-channel
|
|
uni-channel)
|
|
|
|
(define-values (in out) (make-pipe))
|
|
(define reader (make-uni-channel (make-port-channel in)))
|
|
(define writer (make-uni-channel (make-port-channel out)))
|
|
|
|
(uni-channel-send writer '(hello 1 2 3))
|
|
(uni-channel-recv reader)
|
|
```
|
|
|
|
Install and test:
|
|
|
|
```sh
|
|
raco pkg install --auto port-channel
|
|
raco pkg install --auto ./uni-channel
|
|
raco test -x -p uni-channel
|
|
raco setup --pkgs uni-channel
|
|
```
|