Răsfoiți Sursa

Add a passthrough option for Future

Matt Bolt 1 an în urmă
părinte
comite
4529a75471
1 a modificat fișierele cu 15 adăugiri și 0 ștergeri
  1. 15 0
      core/pkg/source/future.go

+ 15 - 0
core/pkg/source/future.go

@@ -9,6 +9,9 @@ type ResultDecoder[T any] func(*QueryResult) *T
 type Future[T any] struct {
 	decoder     ResultDecoder[T]
 	resultsChan QueryResultsChan
+
+	// results is set when we use a passthrough
+	results []*T
 }
 
 // NewFuture Creates a new `Future[T]` with the given `ResultDecoder[T]` and `QueryResultsChan`.
@@ -19,6 +22,13 @@ func NewFuture[T any](decoder ResultDecoder[T], resultsChan QueryResultsChan) *F
 	}
 }
 
+// NewFutureFrom accepts a result set to wrap in the a Future implementation for passthrough.
+func NewFutureFrom[T any](results []*T) *Future[T] {
+	return &Future[T]{
+		results: results,
+	}
+}
+
 // awaitWith allows internal callers to pass an error collector for grouping futures
 func (f *Future[T]) awaitWith(errorCollector *QueryErrorCollector) ([]*T, error) {
 	defer close(f.resultsChan)
@@ -39,6 +49,11 @@ func (f *Future[T]) awaitWith(errorCollector *QueryErrorCollector) ([]*T, error)
 // Await blocks and waits for the `Future` to resolve, and returns the results if successful, or an error
 // otherwise.
 func (f *Future[T]) Await() ([]*T, error) {
+	// in the event that we have a resolved future, we can return the results directly
+	if f.results != nil {
+		return f.results, nil
+	}
+
 	results, err := f.resultsChan.Await()
 	if err != nil {
 		return nil, err