2020-10-06 10:27:33 +03:00
|
|
|
package asynctask
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
// ContinueFunc is a function that can be connected to previous task with ContinueWith
|
2022-05-31 23:49:21 +03:00
|
|
|
type ContinueFunc[T any, S any] func(context.Context, *T) (*S, error)
|
2020-10-06 10:27:33 +03:00
|
|
|
|
2022-05-31 23:49:21 +03:00
|
|
|
func ContinueWith[T any, S any](ctx context.Context, tsk *Task[T], next ContinueFunc[T, S]) *Task[S] {
|
|
|
|
return Start(ctx, func(fCtx context.Context) (*S, error) {
|
|
|
|
result, err := tsk.Result(fCtx)
|
2020-10-06 10:27:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return next(fCtx, result)
|
|
|
|
})
|
|
|
|
}
|
2022-10-28 01:55:38 +03:00
|
|
|
|
|
|
|
// ContinueActionToFunc convert a Action to Func (C# term), to satisfy the AsyncFunc interface.
|
|
|
|
// Action is function that runs without return anything
|
|
|
|
// Func is function that runs and return something
|
|
|
|
func ContinueActionToFunc[T any](action func(context.Context, *T) error) func(context.Context, *T) (*interface{}, error) {
|
|
|
|
return func(ctx context.Context, t *T) (*interface{}, error) {
|
|
|
|
return nil, action(ctx, t)
|
|
|
|
}
|
|
|
|
}
|