-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add component state
- Loading branch information
Showing
38 changed files
with
2,402 additions
and
1,123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
test: | ||
go test ./... | ||
|
||
lint: | ||
golangci-lint run ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package component | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
type ActivationFunc func(this *Component) error | ||
|
||
// WithActivationFunc sets activation function | ||
func (c *Component) WithActivationFunc(f ActivationFunc) *Component { | ||
if c.HasErr() { | ||
return c | ||
} | ||
|
||
c.f = f | ||
return c | ||
} | ||
|
||
// hasActivationFunction checks when activation function is set | ||
func (c *Component) hasActivationFunction() bool { | ||
if c.HasErr() { | ||
return false | ||
} | ||
|
||
return c.f != nil | ||
} | ||
|
||
// MaybeActivate tries to run the activation function if all required conditions are met | ||
func (c *Component) MaybeActivate() (activationResult *ActivationResult) { | ||
c.propagateChainErrors() | ||
|
||
if c.HasErr() { | ||
activationResult = NewActivationResult(c.Name()).WithErr(c.Err()) | ||
return | ||
} | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
activationResult = c.newActivationResultPanicked(fmt.Errorf("panicked with: %v", r)) | ||
} | ||
}() | ||
|
||
if !c.hasActivationFunction() { | ||
//Activation function is not set (maybe useful while the mesh is under development) | ||
activationResult = c.newActivationResultNoFunction() | ||
return | ||
} | ||
|
||
if !c.Inputs().AnyHasSignals() { | ||
//No inputs set, stop here | ||
activationResult = c.newActivationResultNoInput() | ||
return | ||
} | ||
|
||
//Invoke the activation func | ||
err := c.f(c) | ||
|
||
if errors.Is(err, errWaitingForInputs) { | ||
activationResult = c.newActivationResultWaitingForInputs(err) | ||
return | ||
} | ||
|
||
if err != nil { | ||
activationResult = c.newActivationResultReturnedError(err) | ||
return | ||
} | ||
|
||
activationResult = c.newActivationResultOK() | ||
return | ||
} |
Oops, something went wrong.