Skip to content

Commit

Permalink
refactor: remove context not use
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanguyenkh committed Sep 21, 2024
1 parent 9e89b86 commit 1aa51ce
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion example/fibo/fibo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
4 changes: 2 additions & 2 deletions example/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down
12 changes: 4 additions & 8 deletions promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ 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{}
once sync.Once
}

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 {
Expand All @@ -33,7 +30,6 @@ func NewWithPool[T any](
}
var t T
p := &Promise[T]{
ctx: ctx,
value: t,
err: nil,
ch: make(chan struct{}),
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
54 changes: 27 additions & 27 deletions promise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
})
Expand All @@ -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
})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
})
Expand All @@ -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"))
})

Expand All @@ -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)
})
Expand All @@ -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)
})

Expand All @@ -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"))
})

Expand All @@ -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)
})

Expand All @@ -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)
})

Expand All @@ -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"))
})

Expand Down Expand Up @@ -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)

Expand All @@ -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")
})
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 1aa51ce

Please sign in to comment.