@@ -242,69 +242,3 @@ local fsm = machine.create({
242242}})
243243print (fsm .current ) -- "green"
244244```
245-
246- If your object already has a ` startup ` method you can use a different name for the initial event
247-
248- ``` lua
249- local machine = require (' statemachine' )
250-
251- local fsm = machine .create ({
252- inital = { state = ' green' , event = ' init' },
253- events = {
254- { name = ' panic' , from = ' green' , to = ' red' },
255- { name = ' calm' , from = ' red' , to = ' green' },
256- }})
257- print (fsm .current ) -- "green"
258- ```
259-
260- Finally, if you want to wait to call the initial state transition event until a later date you
261- can ` defer ` it:
262-
263- ``` lua
264- local machine = require (' statemachine' )
265-
266- local fsm = machine .create ({
267- inital = { state = ' green' , event = ' init' , defer = true },
268- events = {
269- { name = ' panic' , from = ' green' , to = ' red' },
270- { name = ' calm' , from = ' red' , to = ' green' },
271- }})
272-
273- print (fsm .current ) -- "none"
274- fsm .init ()
275- print (fsm .current ) -- "green"
276- ```
277-
278- Of course, we have now come full circle, this last example is pretty much functionally the
279- same as the first example in this section where you simply define your own startup event.
280-
281- So you have a number of choices available to you when initializing your state machine.
282-
283- >> _ IMPORTANT NOTE: if you are using the pattern described in the previous section "State Machine
284- Classes", and wish to declare an ` initial ` state in this manner, you MUST use the ` defer: true `
285- attribute and manually call the starting event in your constructor function. This will ensure
286- that each instance gets its own unique ` current ` state, rather than an (unwanted) shared
287- ` current ` state on the prototype object itself._
288-
289- Handling Failures
290- ======================
291-
292- By default, if you try to call an event method that is not allowed in the current state, the
293- state machine will throw an exception. If you prefer to handle the problem yourself, you can
294- define a custom ` error ` handler:
295-
296- ``` lua
297- local machine = require (' statemachine' )
298-
299- local fsm = machine .create ({
300- inital = { state = ' green' , event = ' init' , defer = true },
301- error = function (eventName , from , to , args , errorCode , errorMessage )
302- return ' event ' .. eventName .. ' was naughty :- ' .. errorMessage
303- end ,
304- events = {
305- { name = ' panic' , from = ' green' , to = ' red' },
306- { name = ' calm' , from = ' red' , to = ' green' },
307- }})
308-
309- print (fsm :calm ()); -- "event calm was naughty :- event not allowed in current state green"
310- ```
0 commit comments