Skip to content

Commit 525a1dd

Browse files
authored
Add ability to delete from fsm metadata by key (#93)
1 parent 3637340 commit 525a1dd

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

examples/data.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func main() {
1616
fsm.Events{
1717
{Name: "produce", Src: []string{"idle"}, Dst: "idle"},
1818
{Name: "consume", Src: []string{"idle"}, Dst: "idle"},
19+
{Name: "remove", Src: []string{"idle"}, Dst: "idle"},
1920
},
2021
fsm.Callbacks{
2122
"produce": func(_ context.Context, e *fsm.Event) {
@@ -27,7 +28,12 @@ func main() {
2728
if ok {
2829
fmt.Println("message = " + message.(string))
2930
}
30-
31+
},
32+
"remove": func(_ context.Context, e *fsm.Event) {
33+
e.FSM.DeleteMetadata("message")
34+
if _, ok := e.FSM.Metadata("message"); !ok {
35+
fmt.Println("message removed")
36+
}
3137
},
3238
},
3339
)
@@ -48,4 +54,11 @@ func main() {
4854

4955
fmt.Println(fsm.Current())
5056

57+
err = fsm.Event(context.Background(), "remove")
58+
if err != nil {
59+
fmt.Println(err)
60+
}
61+
62+
fmt.Println(fsm.Current())
63+
5164
}

fsm.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
//
2222
// Fysom for Python
2323
// https://github.com/oxplot/fysom (forked at https://github.com/mriehl/fysom)
24-
//
2524
package fsm
2625

2726
import (
@@ -270,6 +269,13 @@ func (f *FSM) SetMetadata(key string, dataValue interface{}) {
270269
f.metadata[key] = dataValue
271270
}
272271

272+
// DeleteMetadata deletes the dataValue in metadata by key
273+
func (f *FSM) DeleteMetadata(key string) {
274+
f.metadataMu.Lock()
275+
delete(f.metadata, key)
276+
f.metadataMu.Unlock()
277+
}
278+
273279
// Event initiates a state transition with the named event.
274280
//
275281
// The call takes a variable number of arguments that will be passed to the

0 commit comments

Comments
 (0)