From 1aa51ce32c25510939d5025e6bf96c1b45c21a26 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 21 Sep 2024 20:10:17 +0700 Subject: [PATCH] refactor: remove context not use --- example/fibo/fibo.go | 2 +- example/http/http.go | 4 ++-- example/main.go | 6 ++--- promise.go | 12 ++++------ promise_test.go | 54 ++++++++++++++++++++++---------------------- 5 files changed, 37 insertions(+), 41 deletions(-) diff --git a/example/fibo/fibo.go b/example/fibo/fibo.go index 244ec87..f96d300 100644 --- a/example/fibo/fibo.go +++ b/example/fibo/fibo.go @@ -26,7 +26,7 @@ func main() { // Create promises for Fibonacci numbers for _, n := range numbers { - tmp := promise4g.New(ctx, func(resolve func(int), reject func(error)) { + tmp := promise4g.New(func(resolve func(int), reject func(error)) { time.Sleep(100 * time.Millisecond) resolve(fibo(n)) }) diff --git a/example/http/http.go b/example/http/http.go index 496509b..eaefb7f 100644 --- a/example/http/http.go +++ b/example/http/http.go @@ -37,7 +37,7 @@ func fakeHttp2(url string) (httpResponse2, error) { func main() { ctx := context.Background() - p1 := promise4g.New(ctx, func(resolve func(any), reject func(error)) { + p1 := promise4g.New(func(resolve func(any), reject func(error)) { resp1, err := fakeHttp1("fakeHttp1") if err != nil { reject(err) @@ -46,7 +46,7 @@ func main() { } }) - p2 := promise4g.New(ctx, func(resolve func(any), reject func(error)) { + p2 := promise4g.New(func(resolve func(any), reject func(error)) { resp1, err := fakeHttp2("fakeHttp2") if err != nil { reject(err) diff --git a/example/main.go b/example/main.go index 6a526bf..c1ee756 100644 --- a/example/main.go +++ b/example/main.go @@ -10,17 +10,17 @@ import ( func main() { ctx := context.Background() - p1 := promise4g.New(ctx, func(resolve func(string), reject func(error)) { + p1 := promise4g.New(func(resolve func(string), reject func(error)) { time.Sleep(100 * time.Millisecond) resolve("one") }) - p2 := promise4g.New(ctx, func(resolve func(string), reject func(error)) { + p2 := promise4g.New(func(resolve func(string), reject func(error)) { time.Sleep(200 * time.Millisecond) resolve("two") }) - p3 := promise4g.New(ctx, func(resolve func(string), reject func(error)) { + p3 := promise4g.New(func(resolve func(string), reject func(error)) { time.Sleep(300 * time.Millisecond) resolve("three") }) diff --git a/promise.go b/promise.go index 58b7432..de29e91 100644 --- a/promise.go +++ b/promise.go @@ -8,7 +8,6 @@ import ( // Promise represents a computation that will eventually be completed with a value of type T or an error. type Promise[T any] struct { - ctx context.Context value T err error ch chan struct{} @@ -16,13 +15,11 @@ type Promise[T any] struct { } func New[T any]( - ctx context.Context, task func(resolve func(T), reject func(error))) *Promise[T] { - return NewWithPool(ctx, task, defaultPool) + return NewWithPool(task, defaultPool) } func NewWithPool[T any]( - ctx context.Context, task func(resolve func(T), reject func(error)), pool Pool) *Promise[T] { if task == nil { @@ -33,7 +30,6 @@ func NewWithPool[T any]( } var t T p := &Promise[T]{ - ctx: ctx, value: t, err: nil, ch: make(chan struct{}), @@ -99,7 +95,7 @@ func AllWithPool[T any]( panic("missing promises") } - return NewWithPool(ctx, func(resolve func([]T), reject func(error)) { + return NewWithPool(func(resolve func([]T), reject func(error)) { resultsChan := make(chan tuple[T, int], len(promises)) errsChan := make(chan error, len(promises)) @@ -143,7 +139,7 @@ func ThenWithPool[A, B any]( resolve func(A) (B, error), pool Pool, ) *Promise[B] { - return NewWithPool(ctx, func(resolveB func(B), reject func(error)) { + return NewWithPool(func(resolveB func(B), reject func(error)) { result, err := p.Await(ctx) if err != nil { reject(err) @@ -174,7 +170,7 @@ func CatchWithPool[T any]( reject func(err error) error, pool Pool, ) *Promise[T] { - return NewWithPool(ctx, func(resolve func(T), internalReject func(error)) { + return NewWithPool(func(resolve func(T), internalReject func(error)) { result, err := p.Await(ctx) if err != nil { internalReject(reject(err)) diff --git a/promise_test.go b/promise_test.go index 166db31..476cdfa 100644 --- a/promise_test.go +++ b/promise_test.go @@ -16,7 +16,7 @@ import ( func TestPromise_One(t *testing.T) { t.Run("Happy", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(string), reject func(error)) { + p := New(func(resolve func(string), reject func(error)) { resolve("one") }) result, err := p.Await(ctx) @@ -26,7 +26,7 @@ func TestPromise_One(t *testing.T) { t.Run("Reject", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(string), reject func(error)) { + p := New(func(resolve func(string), reject func(error)) { reject(errors.New("error")) }) _, err := p.Await(ctx) @@ -35,7 +35,7 @@ func TestPromise_One(t *testing.T) { t.Run("Panic", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(string), reject func(error)) { + p := New(func(resolve func(string), reject func(error)) { panic(errors.New("panic")) }) _, err := p.Await(ctx) @@ -45,7 +45,7 @@ func TestPromise_One(t *testing.T) { t.Run("MultipleResolves", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(string), reject func(error)) { + p := New(func(resolve func(string), reject func(error)) { resolve("one") resolve("two") // This should be ignored }) @@ -56,7 +56,7 @@ func TestPromise_One(t *testing.T) { t.Run("MultipleRejects", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(string), reject func(error)) { + p := New(func(resolve func(string), reject func(error)) { reject(errors.New("first error")) reject(errors.New("second error")) // This should be ignored }) @@ -69,15 +69,15 @@ func TestPromise_One(t *testing.T) { func TestPromise_All(t *testing.T) { t.Run("AllHappy", func(t *testing.T) { ctx := context.Background() - p1 := New(ctx, func(resolve func(string), reject func(error)) { + p1 := New(func(resolve func(string), reject func(error)) { resolve("one") }) - p2 := New(ctx, func(resolve func(string), reject func(error)) { + p2 := New(func(resolve func(string), reject func(error)) { resolve("two") }) - p3 := New(ctx, func(resolve func(string), reject func(error)) { + p3 := New(func(resolve func(string), reject func(error)) { resolve("five") }) p := All(ctx, p1, p2, p3) @@ -88,15 +88,15 @@ func TestPromise_All(t *testing.T) { t.Run("AllContainReject", func(t *testing.T) { ctx := context.Background() - p1 := New(ctx, func(resolve func(string), reject func(error)) { + p1 := New(func(resolve func(string), reject func(error)) { resolve("one") }) - p2 := New(ctx, func(resolve func(string), reject func(error)) { + p2 := New(func(resolve func(string), reject func(error)) { resolve("two") }) - p3 := New(ctx, func(resolve func(string), reject func(error)) { + p3 := New(func(resolve func(string), reject func(error)) { reject(errors.New("error")) }) p := All(ctx, p1, p2, p3) @@ -109,12 +109,12 @@ func TestPromise_All(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - p1 := New(ctx, func(resolve func(int), reject func(error)) { + p1 := New(func(resolve func(int), reject func(error)) { time.Sleep(100 * time.Millisecond) resolve(1) }) - p2 := New(ctx, func(resolve func(int), reject func(error)) { + p2 := New(func(resolve func(int), reject func(error)) { time.Sleep(200 * time.Millisecond) resolve(2) }) @@ -132,11 +132,11 @@ func TestPromise_All(t *testing.T) { t.Run("AllMixedResolveReject", func(t *testing.T) { ctx := context.Background() - p1 := New(ctx, func(resolve func(string), reject func(error)) { + p1 := New(func(resolve func(string), reject func(error)) { resolve("one") }) - p2 := New(ctx, func(resolve func(string), reject func(error)) { + p2 := New(func(resolve func(string), reject func(error)) { reject(errors.New("error")) }) @@ -150,12 +150,12 @@ func TestPromise_All(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond) defer cancel() - p1 := New(ctx, func(resolve func(int), reject func(error)) { + p1 := New(func(resolve func(int), reject func(error)) { time.Sleep(100 * time.Millisecond) resolve(1) }) - p2 := New(ctx, func(resolve func(int), reject func(error)) { + p2 := New(func(resolve func(int), reject func(error)) { time.Sleep(200 * time.Millisecond) resolve(2) }) @@ -170,7 +170,7 @@ func TestPromise_All(t *testing.T) { func TestPromise_Then(t *testing.T) { t.Run("ThenSuccess", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(int), reject func(error)) { + p := New(func(resolve func(int), reject func(error)) { resolve(1) }) @@ -185,7 +185,7 @@ func TestPromise_Then(t *testing.T) { t.Run("ThenFailure", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(int), reject func(error)) { + p := New(func(resolve func(int), reject func(error)) { reject(errors.New("initial error")) }) @@ -200,7 +200,7 @@ func TestPromise_Then(t *testing.T) { t.Run("ThenSuccessButThenPromiseError", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(int), reject func(error)) { + p := New(func(resolve func(int), reject func(error)) { resolve(1) }) @@ -218,7 +218,7 @@ func TestPromise_Then(t *testing.T) { func TestPromise_Catch(t *testing.T) { t.Run("CatchNoError", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(int), reject func(error)) { + p := New(func(resolve func(int), reject func(error)) { resolve(1) }) @@ -233,7 +233,7 @@ func TestPromise_Catch(t *testing.T) { t.Run("CatchSuccess", func(t *testing.T) { ctx := context.Background() - p := New(ctx, func(resolve func(int), reject func(error)) { + p := New(func(resolve func(int), reject func(error)) { reject(errors.New("initial error")) }) @@ -277,7 +277,7 @@ func TestNewWithPool(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - p := NewWithPool(ctx, func(resolve func(string), reject func(error)) { + p := NewWithPool(func(resolve func(string), reject func(error)) { resolve(test.name) }, test.pool) @@ -294,17 +294,17 @@ func TestCheckAllConcurrent(t *testing.T) { ctx := context.Background() start := time.Now() - p1 := New(ctx, func(resolve func(string), reject func(error)) { + p1 := New(func(resolve func(string), reject func(error)) { time.Sleep(100 * time.Millisecond) resolve("one") }) - p2 := New(ctx, func(resolve func(string), reject func(error)) { + p2 := New(func(resolve func(string), reject func(error)) { time.Sleep(200 * time.Millisecond) resolve("two") }) - p3 := New(ctx, func(resolve func(string), reject func(error)) { + p3 := New(func(resolve func(string), reject func(error)) { time.Sleep(300 * time.Millisecond) resolve("three") }) @@ -349,7 +349,7 @@ func BenchmarkNewWithPool(b *testing.B) { for _, test := range tests { b.Run(test.name, func(b *testing.B) { for i := 0; i < b.N; i++ { - p := NewWithPool(ctx, func(resolve func(string), reject func(error)) { + p := NewWithPool(func(resolve func(string), reject func(error)) { resolve(test.name) }, test.pool)