diff --git a/context.go b/context.go index 2fb9b8f..c4ca473 100644 --- a/context.go +++ b/context.go @@ -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 } diff --git a/fsm/fsm.go b/fsm/fsm.go index 9168316..6aa2edd 100644 --- a/fsm/fsm.go +++ b/fsm/fsm.go @@ -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 { diff --git a/fsm/types.go b/fsm/types.go index 91e3ceb..e725047 100644 --- a/fsm/types.go +++ b/fsm/types.go @@ -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 diff --git a/machine.go b/machine.go index 104e7b8..83c296a 100644 --- a/machine.go +++ b/machine.go @@ -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}) },