-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathstream.go
More file actions
25 lines (21 loc) · 846 Bytes
/
stream.go
File metadata and controls
25 lines (21 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package coderdtest
import "github.com/coder/coder/v2/codersdk/wsjson"
// SynchronousStream returns a function that assumes the stream is synchronous.
// Meaning each request sent assumes exactly one response will be received.
// The function will block until the response is received or an error occurs.
//
// This should not be used in production code, as it does not handle edge cases.
// The second function `pop` can be used to retrieve the next response from the
// stream without sending a new request. This is useful for dynamic parameters
func SynchronousStream[R any, W any](stream *wsjson.Stream[R, W]) (do func(W) (R, error), pop func() R) {
rec := stream.Chan()
return func(req W) (R, error) {
err := stream.Send(req)
if err != nil {
return *new(R), err
}
return <-rec, nil
}, func() R {
return <-rec
}
}