mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
chore: refactor dynamic parameters into dedicated package (#18420)
This PR extracts dynamic parameter rendering logic from coderd/parameters.go into a new coderd/dynamicparameters package. Partly for organization and maintainability, but primarily to be reused in `wsbuilder` to be leveraged as validation.
This commit is contained in:
25
coderd/coderdtest/stream.go
Normal file
25
coderd/coderdtest/stream.go
Normal file
@ -0,0 +1,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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user