Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit01565a8

Browse files
aman-saiAman Sai Ankam
and
Aman Sai Ankam
authored
Metadata (#63)
* 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 NoDataErrorCo-authored-by: Aman Sai Ankam <aman.sai@PP-C02CP5SYMD6M.local>
1 parent7b255ef commit01565a8

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

‎examples/data.go‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/looplab/fsm"
9+
)
10+
11+
funcmain() {
12+
fsm:=fsm.NewFSM(
13+
"idle",
14+
fsm.Events{
15+
{Name:"produce",Src: []string{"idle"},Dst:"idle"},
16+
{Name:"consume",Src: []string{"idle"},Dst:"idle"},
17+
},
18+
fsm.Callbacks{
19+
"produce":func(e*fsm.Event) {
20+
e.FSM.SetMetadata("message","hii")
21+
fmt.Println("produced data")
22+
},
23+
"consume":func(e*fsm.Event) {
24+
message,ok:=e.FSM.Metadata("message")
25+
ifok {
26+
fmt.Println("message = "+message.(string))
27+
}
28+
29+
},
30+
},
31+
)
32+
33+
fmt.Println(fsm.Current())
34+
35+
err:=fsm.Event("produce")
36+
iferr!=nil {
37+
fmt.Println(err)
38+
}
39+
40+
fmt.Println(fsm.Current())
41+
42+
err=fsm.Event("consume")
43+
iferr!=nil {
44+
fmt.Println(err)
45+
}
46+
47+
fmt.Println(fsm.Current())
48+
49+
}

‎fsm.go‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type FSM struct {
4444
// transitions maps events and source states to destination states.
4545
transitionsmap[eKey]string
4646

47-
// callbacks maps events andtargers to callback functions.
47+
// callbacks maps events andtargets to callback functions.
4848
callbacksmap[cKey]Callback
4949

5050
// transition is the internal transition functions used either directly
@@ -57,6 +57,11 @@ type FSM struct {
5757
stateMu sync.RWMutex
5858
// eventMu guards access to Event() and Transition().
5959
eventMu sync.Mutex
60+
// metadata can be used to store and load data that maybe used across events
61+
// use methods SetMetadata() and Metadata() to store and load data
62+
metadatamap[string]interface{}
63+
64+
metadataMu sync.RWMutex
6065
}
6166

6267
// EventDesc represents an event when initializing the FSM.
@@ -129,6 +134,7 @@ func NewFSM(initial string, events []EventDesc, callbacks map[string]Callback) *
129134
current:initial,
130135
transitions:make(map[eKey]string),
131136
callbacks:make(map[cKey]Callback),
137+
metadata:make(map[string]interface{}),
132138
}
133139

134140
// Build transition map and store sets of all events and states.
@@ -229,7 +235,7 @@ func (f *FSM) Can(event string) bool {
229235
returnok&& (f.transition==nil)
230236
}
231237

232-
// AvailableTransitions returns a list of transitionsavilable in the
238+
// AvailableTransitions returns a list of transitionsavailable in the
233239
// current state.
234240
func (f*FSM)AvailableTransitions() []string {
235241
f.stateMu.RLock()
@@ -249,6 +255,21 @@ func (f *FSM) Cannot(event string) bool {
249255
return!f.Can(event)
250256
}
251257

258+
// Metadata returns the value stored in metadata
259+
func (f*FSM)Metadata(keystring) (interface{},bool) {
260+
f.metadataMu.RLock()
261+
deferf.metadataMu.RUnlock()
262+
dataElement,ok:=f.metadata[key]
263+
returndataElement,ok
264+
}
265+
266+
// SetMetadata stores the dataValue in metadata indexing it with key
267+
func (f*FSM)SetMetadata(keystring,dataValueinterface{}) {
268+
f.metadataMu.Lock()
269+
deferf.metadataMu.Unlock()
270+
f.metadata[key]=dataValue
271+
}
272+
252273
// Event initiates a state transition with the named event.
253274
//
254275
// The call takes a variable number of arguments that will be passed to the

‎visualizer.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const (
1313
GRAPHVIZVisualizeType="graphviz"
1414
// MERMAID the type for mermaid output (https://mermaid-js.github.io/mermaid/#/stateDiagram) in the stateDiagram form
1515
MERMAIDVisualizeType="mermaid"
16-
//MERMAID the type for mermaid output (https://mermaid-js.github.io/mermaid/#/stateDiagram) in the stateDiagram form
16+
//MermaidStateDiagram the type for mermaid output (https://mermaid-js.github.io/mermaid/#/stateDiagram) in the stateDiagram form
1717
MermaidStateDiagramVisualizeType="mermaid-state-diagram"
18-
//MERMAID the type for mermaid output (https://mermaid-js.github.io/mermaid/#/flowchart) in the flow chart form
18+
//MermaidFlowChart the type for mermaid output (https://mermaid-js.github.io/mermaid/#/flowchart) in the flow chart form
1919
MermaidFlowChartVisualizeType="mermaid-flow-chart"
2020
)
2121

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp