mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.
## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
67 lines
2.3 KiB
Go
67 lines
2.3 KiB
Go
package hsm
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
)
|
|
|
|
// ErrInvalidTransition is returned from [Transition.Apply] on an invalid state transition.
|
|
var ErrInvalidTransition = errors.New("invalid transition")
|
|
|
|
// A TaskRegenerator is invoked to regenerate tasks post state-based replication or when refreshing all tasks for a
|
|
// workflow.
|
|
type TaskRegenerator interface {
|
|
RegenerateTasks(*Node) ([]Task, error)
|
|
}
|
|
|
|
// A StateMachine is anything that can get and set a comparable state S and re-generate tasks based on current state.
|
|
// It is meant to be used with [Transition] objects to safely transition their state on a given event.
|
|
type StateMachine[S comparable] interface {
|
|
TaskRegenerator
|
|
State() S
|
|
SetState(S)
|
|
}
|
|
|
|
// TransitionOutput is output produced for a single transition.
|
|
type TransitionOutput struct {
|
|
Tasks []Task
|
|
}
|
|
|
|
// Transition represents a state machine transition for a machine of type SM with state S and event E.
|
|
type Transition[S comparable, SM StateMachine[S], E any] struct {
|
|
// Source states that are valid for this transition.
|
|
Sources []S
|
|
// Destination state to transition to.
|
|
Destination S
|
|
// Function to apply the transition. Mutate the state machine object here and return tasks.
|
|
apply func(SM, E) (TransitionOutput, error)
|
|
}
|
|
|
|
// NewTransition creates a new [Transition] from the given source states to a destination state for a given event.
|
|
// The apply function is called after verifying the transition is possible and setting the destination state.
|
|
func NewTransition[S comparable, SM StateMachine[S], E any](src []S, dst S, apply func(SM, E) (TransitionOutput, error)) Transition[S, SM, E] {
|
|
return Transition[S, SM, E]{
|
|
Sources: src,
|
|
Destination: dst,
|
|
apply: apply,
|
|
}
|
|
}
|
|
|
|
// Possible returns a boolean indicating whether the transition is possible for the current state.
|
|
func (t Transition[S, SM, E]) Possible(sm SM) bool {
|
|
return slices.Contains(t.Sources, sm.State())
|
|
}
|
|
|
|
// Apply applies a transition event to the given state machine changing the state machine's state to the transition's
|
|
// Destination on success.
|
|
func (t Transition[S, SM, E]) Apply(sm SM, event E) (TransitionOutput, error) {
|
|
prevState := sm.State()
|
|
if !t.Possible(sm) {
|
|
return TransitionOutput{}, fmt.Errorf("%w from %v: %v", ErrInvalidTransition, prevState, event)
|
|
}
|
|
|
|
sm.SetState(t.Destination)
|
|
return t.apply(sm, event)
|
|
}
|