Skip to content

Commit

Permalink
Merge pull request #4 from reugn/develop
Browse files Browse the repository at this point in the history
Make the generic package exported
  • Loading branch information
reugn authored Apr 30, 2022
2 parents b672a53 + 419d2ab commit ee40c65
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.17.x]
go-version: [1.18.x]
steps:
- name: Setup Go
uses: actions/setup-go@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The implemented patterns were taken from Scala and Java.
* **Reentrant Lock** - Mutex that allows goroutines to enter into the lock on a resource more than once.
* **Optimistic Lock** - Mutex that allows optimistic reading. Could be retried or switched to RLock in case of failure. Significantly improves performance in case of frequent reads and short writes. See [benchmarks](./benchmarks/README.md).

### [Go 1.18 Generic prototypes](./generic)
### [Generic types](./generic)
* **Task** - A data type for controlling possibly lazy and asynchronous computations.

## Examples
Expand Down
22 changes: 0 additions & 22 deletions generic/README.md

This file was deleted.

12 changes: 7 additions & 5 deletions generic/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions generic/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/reugn/async

go 1.12
go 1.18

0 comments on commit ee40c65

Please sign in to comment.