Skip to content

Commit

Permalink
feat(fsm): add identifier getter
Browse files Browse the repository at this point in the history
Add ability to get identifier for this instance of the state machine
  • Loading branch information
hannahhoward committed Feb 19, 2020
1 parent c670342 commit 206d210
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import "context"

type Context struct {
ctx context.Context
name interface{}
send func(evt interface{}) error
}

func (ctx *Context) Name() interface{} {
return ctx.name
}

func (ctx *Context) Context() context.Context {
return ctx.ctx
}
Expand Down
4 changes: 4 additions & 0 deletions fsm/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ func (dc fsmContext) Context() context.Context {
return dc.ctx.Context()
}

func (dc fsmContext) Identifier() Identifier {
return dc.ctx.Name()
}

func (dc fsmContext) Event(event EventName, args ...interface{}) error {
evt, err := dc.d.event(dc.ctx.Context(), event, nil, args...)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions fsm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import (
// EventName is the name of an event
type EventName string

// Identifier is a unique identifier for a given statemachine instance
type Identifier interface{}

// Context provides access to the statemachine inside of a state handler
type Context interface {
// Context returns the golang context for this context
Context() context.Context

// Identifier returns a unique identifier for this instance of a the state machine
Identifier() Identifier

// 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
3 changes: 2 additions & 1 deletion machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (fsm *StateMachine) run() {
}

ctx := Context{
ctx: context.TODO(),
ctx: context.TODO(),
name: fsm.name,
send: func(evt interface{}) error {
return fsm.send(Event{User: evt})
},
Expand Down

0 comments on commit 206d210

Please sign in to comment.