Skip to content

Commit

Permalink
feat: add support for state groups for the Remove relation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta authored Feb 22, 2024
1 parent e4608e7 commit 88ef244
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,19 @@ type Machine struct {
// New creates a new Machine instance, bound to context and modified with
// optional Opts
func New(ctx context.Context, states States, opts *Opts) *Machine {
// parse relations
parsedStates := cloneStates(states)
for name, state := range states {
// avoid self removal
if lo.Contains(state.Remove, name) {
state.Remove = lo.Without(state.Remove, name)
parsedStates[name] = state
}
}
m := &Machine{
ID: uuid.New().String(),
HandlerTimeout: time.Second,
States: states,
States: parsedStates,
clock: map[string]uint64{},
emitters: []*emitter{},
PrintExceptions: true,
Expand Down
29 changes: 29 additions & 0 deletions pkg/machine/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,32 @@ func padString(str string, length int, pad string) string {
}
}
}

// cloneStates deep clones the states struct and returns a copy.
func cloneStates(states States) States {
ret := make(States)
for name, state := range states {
stateCopy := State{
Auto: state.Auto,
Multi: state.Multi,
}
if state.Require != nil {
stateCopy.Require = make(S, len(state.Require))
copy(stateCopy.Require, state.Require)
}
if state.Add != nil {
stateCopy.Add = make(S, len(state.Add))
copy(stateCopy.Add, state.Add)
}
if state.Remove != nil {
stateCopy.Remove = make(S, len(state.Remove))
copy(stateCopy.Remove, state.Remove)
}
if state.After != nil {
stateCopy.After = make(S, len(state.After))
copy(stateCopy.After, state.After)
}
ret[name] = stateCopy
}
return ret
}

0 comments on commit 88ef244

Please sign in to comment.