Skip to content

Commit

Permalink
feat: resolve error
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsmejkal committed May 2, 2024
1 parent c54bd7b commit 9554895
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions plumber.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ package plumber

import (
"context"
"errors"
"fmt"
"reflect"
"sync"
)

var (
ErrResolving = errors.New("Just resolving, possible circular dependency")
)

// Future represents a struct that will help with dependency evaluation
type Future[T any] struct {
deps []Dependency
Expand All @@ -20,12 +25,17 @@ type Future[T any] struct {

// Then evaluates a dependencies and trigger callback when all good
func (f *Future[T]) Then(callback func()) {
var errs []error
for _, d := range f.deps {
if !d.Resolved() {
f.d.err = fmt.Errorf("dependency not resolved, %s requires %s", f.d, d)
return
err := d.Resolved()
if err != nil {
errs = append(errs, fmt.Errorf("dependency not resolved, %s requires %s, %w", f.d, d, err))
}
}
if len(errs) != 0 {
f.d.err = errors.Join(errs...)
return
}
callback()
}

Expand Down Expand Up @@ -132,12 +142,12 @@ func (d *D[T]) Error() error {

// Resolved return true if current value was resolved and is valid
// In case that current value is just being resolved it return false to not trigger infinite loop during cyclic dependency
func (d *D[T]) Resolved() bool {
func (d *D[T]) Resolved() error {
if d.resolving {
return false
return ErrResolving
}
_, err := d.InstanceError()
return err == nil
return err
}

// Resolve returns a callback providing a resolution orchestrator
Expand Down Expand Up @@ -259,5 +269,5 @@ func (rr *ResolutionR[T]) Require(deps ...Dependency) *Future[T] {

// Dependency represent a dependency that can be supplied into Require method
type Dependency interface {
Resolved() bool
Resolved() error
}

0 comments on commit 9554895

Please sign in to comment.