Skip to content

Commit

Permalink
Add ability to delete from fsm metadata by key
Browse files Browse the repository at this point in the history
  • Loading branch information
abramlab committed Dec 2, 2022
1 parent 3637340 commit 10b91cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 14 additions & 1 deletion examples/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
fsm.Events{
{Name: "produce", Src: []string{"idle"}, Dst: "idle"},
{Name: "consume", Src: []string{"idle"}, Dst: "idle"},
{Name: "remove", Src: []string{"idle"}, Dst: "idle"},
},
fsm.Callbacks{
"produce": func(_ context.Context, e *fsm.Event) {
Expand All @@ -27,7 +28,12 @@ func main() {
if ok {
fmt.Println("message = " + message.(string))
}

},
"remove": func(_ context.Context, e *fsm.Event) {
e.FSM.DeleteMetadata("message")
if _, ok := e.FSM.Metadata("message"); !ok {
fmt.Println("message removed")
}
},
},
)
Expand All @@ -48,4 +54,11 @@ func main() {

fmt.Println(fsm.Current())

err = fsm.Event(context.Background(), "remove")
if err != nil {
fmt.Println(err)
}

fmt.Println(fsm.Current())

}
8 changes: 7 additions & 1 deletion fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
//
// Fysom for Python
// https://github.com/oxplot/fysom (forked at https://github.com/mriehl/fysom)
//
package fsm

import (
Expand Down Expand Up @@ -270,6 +269,13 @@ func (f *FSM) SetMetadata(key string, dataValue interface{}) {
f.metadata[key] = dataValue
}

// DeleteMetadata deletes the dataValue in metadata by key
func (f *FSM) DeleteMetadata(key string) {
f.metadataMu.Lock()
delete(f.metadata, key)
f.metadataMu.Unlock()
}

// Event initiates a state transition with the named event.
//
// The call takes a variable number of arguments that will be passed to the
Expand Down

0 comments on commit 10b91cc

Please sign in to comment.