@@ -69,13 +69,13 @@ type StateMachine[D any] struct {
69
69
// Type alias for MessageHandler callbacks.
70
70
// D is the type of the data associated with the StateMachine.
71
71
// M is the type of the message this handler accepts.
72
- type StateMessageHandler [D any , M any ] func (* StateMachine [ D ] , M ) error
72
+ type StateMessageHandler [D any , M any ] func (gen. Atom , D , M , gen. Process ) (gen. Atom , D , error )
73
73
74
74
// Type alias for CallHandler callbacks.
75
75
// D is the type of the data associated with the StateMachine.
76
76
// M is the type of the message this handler accepts.
77
77
// R is the type of the result value.
78
- type StateCallHandler [D any , M any , R any ] func (* StateMachine [ D ] , M ) (R , error )
78
+ type StateCallHandler [D any , M any , R any ] func (gen. Atom , D , M , gen. Process ) (gen. Atom , D , R , error )
79
79
80
80
type StateMachineSpec [D any ] struct {
81
81
initialState gen.Atom
@@ -347,14 +347,26 @@ func (sm *StateMachine[D]) lookupMessageHandler(messageType string) (any, bool)
347
347
348
348
func (sm * StateMachine [D ]) invokeMessageHandler (handler any , message * gen.MailboxMessage ) error {
349
349
callbackValue := reflect .ValueOf (handler )
350
- smValue := reflect .ValueOf (sm )
350
+ stateValue := reflect .ValueOf (sm .currentState )
351
+ dataValue := reflect .ValueOf (sm .Data ())
351
352
msgValue := reflect .ValueOf (message .Message )
353
+ procValue := reflect .ValueOf (sm )
352
354
353
- results := callbackValue .Call ([]reflect.Value {smValue , msgValue })
355
+ results := callbackValue .Call ([]reflect.Value {stateValue , dataValue , msgValue , procValue })
354
356
355
- if len (results ) > 0 && ! results [0 ].IsNil () {
356
- return results [0 ].Interface ().(error )
357
+ if len (results ) != 3 {
358
+ sm .Log ().Panic ("StateMachine terminated. Panic reason: unexpected " +
359
+ "error when invoking call handler for %v" , typeName (message ))
357
360
}
361
+ if ! results [2 ].IsNil () {
362
+ return results [2 ].Interface ().(error )
363
+ }
364
+ //TODO: panic if new state or new data is not provided
365
+ setCurrentStateMethod := reflect .ValueOf (sm ).MethodByName ("SetCurrentState" )
366
+ setCurrentStateMethod .Call ([]reflect.Value {results [0 ]})
367
+ setDataMethod := reflect .ValueOf (sm ).MethodByName ("SetData" )
368
+ setDataMethod .Call ([]reflect.Value {results [1 ]})
369
+
358
370
return nil
359
371
}
360
372
@@ -369,21 +381,28 @@ func (sm *StateMachine[D]) lookupCallHandler(messageType string) (any, bool) {
369
381
370
382
func (sm * StateMachine [D ]) invokeCallHandler (handler any , message * gen.MailboxMessage ) (any , error ) {
371
383
callbackValue := reflect .ValueOf (handler )
372
- smValue := reflect .ValueOf (sm )
384
+ stateValue := reflect .ValueOf (sm .currentState )
385
+ dataValue := reflect .ValueOf (sm .Data ())
373
386
msgValue := reflect .ValueOf (message .Message )
387
+ procValue := reflect .ValueOf (sm )
374
388
375
- results := callbackValue .Call ([]reflect.Value {smValue , msgValue })
389
+ results := callbackValue .Call ([]reflect.Value {stateValue , dataValue , msgValue , procValue })
376
390
377
- if len (results ) != 2 {
391
+ if len (results ) != 4 {
378
392
sm .Log ().Panic ("StateMachine terminated. Panic reason: unexpected " +
379
393
"error when invoking call handler for %v" , typeName (message ))
380
394
}
381
395
382
- if ! results [1 ].IsNil () {
396
+ if ! results [3 ].IsNil () {
383
397
err := results [1 ].Interface ().(error )
384
398
return nil , err
385
399
}
400
+ //TODO: panic if new state or new data is not provided
401
+ setCurrentStateMethod := reflect .ValueOf (sm ).MethodByName ("SetCurrentState" )
402
+ setCurrentStateMethod .Call ([]reflect.Value {results [0 ]})
403
+ setDataMethod := reflect .ValueOf (sm ).MethodByName ("SetData" )
404
+ setDataMethod .Call ([]reflect.Value {results [1 ]})
386
405
387
- result := results [0 ].Interface ()
406
+ result := results [2 ].Interface ()
388
407
return result , nil
389
408
}
0 commit comments