1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-06-23 00:27:49 +02:00
Files
fp-go/context/readerioeither/generic/resource.go
Dr. Carsten Leue b25de3c7c3 doc: fix case
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-07-18 15:57:54 +02:00

26 lines
800 B
Go

package generic
import (
"context"
E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function"
RIE "github.com/IBM/fp-go/readerioeither/generic"
)
// 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),
)
}