Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 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 | |
| 15 | package fsm |
| 16 | |
| 17 | // Event is the info that get passed as a reference in the callbacks. |
| 18 | type Event struct { |
| 19 | // FSM is a reference to the current FSM. |
| 20 | FSM *FSM |
| 21 | |
| 22 | // Event is the event name. |
| 23 | Event string |
| 24 | |
| 25 | // Src is the state before the transition. |
| 26 | Src string |
| 27 | |
| 28 | // Dst is the state after the transition. |
| 29 | Dst string |
| 30 | |
| 31 | // Err is an optional error that can be returned from a callback. |
| 32 | Err error |
| 33 | |
| 34 | // Args is a optinal list of arguments passed to the callback. |
| 35 | Args []interface{} |
| 36 | |
| 37 | // canceled is an internal flag set if the transition is canceled. |
| 38 | canceled bool |
| 39 | |
| 40 | // async is an internal flag set if the transition should be asynchronous |
| 41 | async bool |
| 42 | } |
| 43 | |
| 44 | // Cancel can be called in before_<EVENT> or leave_<STATE> to cancel the |
| 45 | // current transition before it happens. It takes an opitonal error, which will |
| 46 | // overwrite e.Err if set before. |
| 47 | func (e *Event) Cancel(err ...error) { |
| 48 | e.canceled = true |
| 49 | |
| 50 | if len(err) > 0 { |
| 51 | e.Err = err[0] |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Async can be called in leave_<STATE> to do an asynchronous state transition. |
| 56 | // |
| 57 | // The current state transition will be on hold in the old state until a final |
| 58 | // call to Transition is made. This will comlete the transition and possibly |
| 59 | // call the other callbacks. |
| 60 | func (e *Event) Async() { |
| 61 | e.async = true |
| 62 | } |