Skip to content

Commit

Permalink
Metadata (looplab#63)
Browse files Browse the repository at this point in the history
* added data feature

* added commit for NoDataError

* added NoDataError

* added data.go to demonstrate usage of data feature

* renamed data to metadata

* removed key level locks from metadata

* removed NoDataError

Co-authored-by: Aman Sai Ankam <aman.sai@PP-C02CP5SYMD6M.local>
  • Loading branch information
2 people authored and wahyurudiyan committed Mar 15, 2021
1 parent 882719a commit 5a52348
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
49 changes: 49 additions & 0 deletions examples/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// +build ignore

package main

import (
"fmt"

"github.com/looplab/fsm"
)

func main() {
fsm := fsm.NewFSM(
"idle",
fsm.Events{
{Name: "produce", Src: []string{"idle"}, Dst: "idle"},
{Name: "consume", Src: []string{"idle"}, Dst: "idle"},
},
fsm.Callbacks{
"produce": func(e *fsm.Event) {
e.FSM.SetMetadata("message", "hii")
fmt.Println("produced data")
},
"consume": func(e *fsm.Event) {
message, ok := e.FSM.Metadata("message")
if ok {
fmt.Println("message = " + message.(string))
}

},
},
)

fmt.Println(fsm.Current())

err := fsm.Event("produce")
if err != nil {
fmt.Println(err)
}

fmt.Println(fsm.Current())

err = fsm.Event("consume")
if err != nil {
fmt.Println(err)
}

fmt.Println(fsm.Current())

}
34 changes: 26 additions & 8 deletions fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ type FSM struct {
// transitions maps events and source states to destination states.
transitions map[EventKey]string

// callbacks maps events and targers to callback functions.
callbacks map[CallbackKey]Callback

message map[EventKey]string
// callbacks maps events and targets to callback functions.
callbacks map[cKey]Callback

// transition is the internal transition functions used either directly
// or when Transition is called in an asynchronous state transition.
Expand All @@ -61,6 +59,11 @@ type FSM struct {
stateMu sync.RWMutex
// eventMu guards access to Event() and Transition().
eventMu sync.Mutex
// metadata can be used to store and load data that maybe used across events
// use methods SetMetadata() and Metadata() to store and load data
metadata map[string]interface{}

metadataMu sync.RWMutex
}

// EventDesc represents an event when initializing the FSM.
Expand Down Expand Up @@ -133,9 +136,9 @@ func NewFSM(initial string, events []*EventDesc, callbacks map[string]Callback)
f := &FSM{
transitionerObj: &transitionerStruct{},
current: initial,
transitions: make(map[EventKey]string),
callbacks: make(map[CallbackKey]Callback),
message: make(map[EventKey]string),
transitions: make(map[eKey]string),
callbacks: make(map[cKey]Callback),
metadata: make(map[string]interface{}),
}

// Build transition map and store sets of all events and states.
Expand Down Expand Up @@ -253,7 +256,7 @@ func (f *FSM) Can(event string) bool {
return ok && (f.transition == nil)
}

// AvailableTransitions returns a list of transitions avilable in the
// AvailableTransitions returns a list of transitions available in the
// current state.
func (f *FSM) AvailableTransitions() []string {
f.stateMu.RLock()
Expand All @@ -273,6 +276,21 @@ func (f *FSM) Cannot(event string) bool {
return !f.Can(event)
}

// Metadata returns the value stored in metadata
func (f *FSM) Metadata(key string) (interface{}, bool) {
f.metadataMu.RLock()
defer f.metadataMu.RUnlock()
dataElement, ok := f.metadata[key]
return dataElement, ok
}

// SetMetadata stores the dataValue in metadata indexing it with key
func (f *FSM) SetMetadata(key string, dataValue interface{}) {
f.metadataMu.Lock()
defer f.metadataMu.Unlock()
f.metadata[key] = dataValue
}

// 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
4 changes: 2 additions & 2 deletions visualizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const (
GRAPHVIZ VisualizeType = "graphviz"
// MERMAID the type for mermaid output (https://mermaid-js.github.io/mermaid/#/stateDiagram) in the stateDiagram form
MERMAID VisualizeType = "mermaid"
// MERMAID the type for mermaid output (https://mermaid-js.github.io/mermaid/#/stateDiagram) in the stateDiagram form
// MermaidStateDiagram the type for mermaid output (https://mermaid-js.github.io/mermaid/#/stateDiagram) in the stateDiagram form
MermaidStateDiagram VisualizeType = "mermaid-state-diagram"
// MERMAID the type for mermaid output (https://mermaid-js.github.io/mermaid/#/flowchart) in the flow chart form
// MermaidFlowChart the type for mermaid output (https://mermaid-js.github.io/mermaid/#/flowchart) in the flow chart form
MermaidFlowChart VisualizeType = "mermaid-flow-chart"
)

Expand Down

0 comments on commit 5a52348

Please sign in to comment.