зеркало из https://github.com/Azure/go-asynctask.git
more tweaks
This commit is contained in:
Родитель
2f94f1ac9c
Коммит
5164f48908
|
@ -21,3 +21,12 @@ func AfterBoth[T, S, R any](ctx context.Context, tskT *Task[T], tskS *Task[S], n
|
|||
return next(fCtx, t, s)
|
||||
})
|
||||
}
|
||||
|
||||
// ContinueActionToFunc convert a Action to Func (C# term), to satisfy the AfterBothFunc interface.
|
||||
// Action is function that runs without return anything
|
||||
// Func is function that runs and return something
|
||||
func AfterBothActionToFunc[T, S any](action func(context.Context, *T, *S) error) func(context.Context, *T, *S) (*interface{}, error) {
|
||||
return func(ctx context.Context, t *T, s *S) (*interface{}, error) {
|
||||
return nil, action(ctx, t, s)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,3 +14,12 @@ func ContinueWith[T any, S any](ctx context.Context, tsk *Task[T], next Continue
|
|||
return next(fCtx, result)
|
||||
})
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package asynctask_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -75,3 +76,18 @@ func TestContinueWithFailureCase(t *testing.T) {
|
|||
assert.Equal(t, asynctask.StateFailed, t3.State(), "Task3 should fail since preceeding task failed")
|
||||
assert.Equal(t, "devide by 0", err.Error())
|
||||
}
|
||||
|
||||
func TestContinueActionToFunc(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := newTestContext(t)
|
||||
|
||||
t1 := asynctask.Start(ctx, func(ctx context.Context) (*int, error) { i := 0; return &i, nil })
|
||||
t2 := asynctask.ContinueWith(ctx, t1, asynctask.ContinueActionToFunc(func(ctx context.Context, i *int) error {
|
||||
if *i != 0 {
|
||||
return fmt.Errorf("input should be 0, but got %d", i)
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
_, err := t2.Result(ctx)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
6
task.go
6
task.go
|
@ -8,6 +8,9 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// AsyncFunc is a function interface this asyncTask accepts.
|
||||
type AsyncFunc[T any] func(context.Context) (*T, error)
|
||||
|
||||
// ActionToFunc 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
|
||||
|
@ -17,9 +20,6 @@ func ActionToFunc(action func(context.Context) error) func(context.Context) (*in
|
|||
}
|
||||
}
|
||||
|
||||
// AsyncFunc is a function interface this asyncTask accepts.
|
||||
type AsyncFunc[T any] func(context.Context) (*T, error)
|
||||
|
||||
// Task is a handle to the running function.
|
||||
// which you can use to wait, cancel, get the result.
|
||||
type Task[T any] struct {
|
||||
|
|
Загрузка…
Ссылка в новой задаче