From 26dd3303c41543bccc189df27c4f621d5f7f92e8 Mon Sep 17 00:00:00 2001 From: reugn Date: Sat, 30 Apr 2022 16:26:54 +0300 Subject: [PATCH] make the generic package exported --- generic/task.go | 12 +++++++----- generic/task_test.go | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/generic/task.go b/generic/task.go index dcbbdba..878b1ba 100644 --- a/generic/task.go +++ b/generic/task.go @@ -7,18 +7,20 @@ import ( "github.com/reugn/async" ) -// asyncTask is a data type for controlling possibly lazy & asynchronous computations. -type asyncTask[T any] struct { +// AsyncTask is a data type for controlling possibly lazy & asynchronous computations. +type AsyncTask[T any] struct { taskFunc func() (T, error) } -func newAsyncTask[T any](taskFunc func() (T, error)) *asyncTask[T] { - return &asyncTask[T]{ +// NewAsyncTask returns a new AsyncTask. +func NewAsyncTask[T any](taskFunc func() (T, error)) *AsyncTask[T] { + return &AsyncTask[T]{ taskFunc: taskFunc, } } -func (task *asyncTask[T]) call() async.Future { +// Call executes the AsyncTask and returns a Future. +func (task *AsyncTask[T]) Call() async.Future { promise := async.NewPromise() go func() { res, err := task.taskFunc() diff --git a/generic/task_test.go b/generic/task_test.go index d7fd727..99d4de8 100644 --- a/generic/task_test.go +++ b/generic/task_test.go @@ -11,11 +11,11 @@ import ( ) func TestAsyncTask(t *testing.T) { - task := newAsyncTask[string](func() (string, error) { + task := NewAsyncTask(func() (string, error) { time.Sleep(1 * time.Second) return "ok", nil }) - res, err := task.call().Get() + res, err := task.Call().Get() internal.AssertEqual(t, "ok", res.(string)) internal.AssertEqual(t, err, nil)