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/resource.go
Dr. Carsten Leue 84c3e3ff88 fix: add support for context sensitive readers
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-07-14 23:52:14 +02:00

16 lines
681 B
Go

package readerioeither
import (
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[R, A any](onCreate ReaderIOEither[R], onRelease func(R) ReaderIOEither[any]) func(func(R) ReaderIOEither[A]) ReaderIOEither[A] {
// wraps the callback functions with a context check
return F.Flow2(
F.Bind2nd(F.Flow2[func(R) ReaderIOEither[A], func(ReaderIOEither[A]) ReaderIOEither[A], R, ReaderIOEither[A], ReaderIOEither[A]], WithContext[A]),
RIE.WithResource[ReaderIOEither[A]](WithContext(onCreate), onRelease),
)
}