event.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) 2013 - Max Persson <max@looplab.se>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fsm
  15. // Event is the info that get passed as a reference in the callbacks.
  16. type Event struct {
  17. // FSM is a reference to the current FSM.
  18. FSM *FSM
  19. // Event is the event name.
  20. Event string
  21. // Src is the state before the transition.
  22. Src string
  23. // Dst is the state after the transition.
  24. Dst string
  25. // Err is an optional error that can be returned from a callback.
  26. Err error
  27. // Args is a optinal list of arguments passed to the callback.
  28. Args []interface{}
  29. // canceled is an internal flag set if the transition is canceled.
  30. canceled bool
  31. // async is an internal flag set if the transition should be asynchronous
  32. async bool
  33. }
  34. // Cancel can be called in before_<EVENT> or leave_<STATE> to cancel the
  35. // current transition before it happens. It takes an opitonal error, which will
  36. // overwrite e.Err if set before.
  37. func (e *Event) Cancel(err ...error) {
  38. e.canceled = true
  39. if len(err) > 0 {
  40. e.Err = err[0]
  41. }
  42. }
  43. // Async can be called in leave_<STATE> to do an asynchronous state transition.
  44. //
  45. // The current state transition will be on hold in the old state until a final
  46. // call to Transition is made. This will comlete the transition and possibly
  47. // call the other callbacks.
  48. func (e *Event) Async() {
  49. e.async = true
  50. }