2023-07-18 15:24:00 +02:00
|
|
|
package generic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-07-18 15:57:54 +02:00
|
|
|
E "github.com/IBM/fp-go/either"
|
|
|
|
F "github.com/IBM/fp-go/function"
|
|
|
|
RIE "github.com/IBM/fp-go/readerioeither/generic"
|
2023-07-18 15:24:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
|
|
|
|
func WithResource[
|
|
|
|
GRA ~func(context.Context) GIOA,
|
|
|
|
GRR ~func(context.Context) GIOR,
|
|
|
|
GRANY ~func(context.Context) GIOANY,
|
|
|
|
GIOR ~func() E.Either[error, R],
|
|
|
|
GIOA ~func() E.Either[error, A],
|
|
|
|
GIOANY ~func() E.Either[error, ANY],
|
|
|
|
R, A, ANY any](onCreate GRR, onRelease func(R) GRANY) func(func(R) GRA) GRA {
|
|
|
|
// wraps the callback functions with a context check
|
|
|
|
return F.Flow2(
|
|
|
|
F.Bind2nd(F.Flow2[func(R) GRA, func(GRA) GRA, R, GRA, GRA], WithContext[GRA]),
|
|
|
|
RIE.WithResource[GRA](WithContext(onCreate), onRelease),
|
|
|
|
)
|
|
|
|
}
|