Skip to content

Commit

Permalink
fixup! util: Add a Future[T] library
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevgeniy Miretskiy committed Mar 2, 2023
1 parent be291d0 commit 1834760
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions pkg/util/future/future.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ func MakeCompletedErrorFuture(err error) *ErrorFuture {
// MakeAwaitableFuture wraps underlying future so that the callers can wait
// for future to become available.
func MakeAwaitableFuture[T any](f *Future[T]) AwaitableFuture[T] {
if f.isReady() {
if ptr := f.value.Load(); ptr != nil && ptr.v != nil {
// Future already prepared; no need to wait for it to become ready.
return AwaitableFuture[T]{Future: f, done: closedChan}
}

done := make(chan struct{})
f.WhenReady(func(v T) {
close(done)
Expand Down Expand Up @@ -160,11 +162,6 @@ func (f *Future[T]) prepare(v T) {
}
}

func (f *Future[T]) isReady() bool {
ptr := f.value.Load()
return ptr != nil && ptr.v != nil
}

var closedChan = func() <-chan struct{} {
ch := make(chan struct{})
close(ch)
Expand Down

0 comments on commit 1834760

Please sign in to comment.