@@ -204,42 +204,6 @@ local fsm = machine.create({
204204
205205>> _ NOTE: If you decide to cancel the ASYNC event, you can call ` fsm.transition.cancel() `
206206
207- State Machine Classes
208- =====================
209-
210- You can also turn all instances of a _ class_ into an FSM by applying
211- the state machine functionality to the prototype, including your callbacks
212- in your prototype, and providing a ` startup ` event for use when constructing
213- instances:
214-
215- ``` lua
216- local machine = require (' statemachine' )
217- local MyFSM = {}
218-
219- function MyFSM :onpanic (event , from , to )
220- print (' panic' )
221- end
222-
223- function MyFSM :onclear (event , from , to )
224- print (' all is clear' )
225- end
226-
227- local fsm = machine .create ({
228- metatable = MyFSM ,
229- events = [
230- { name = ' startup' , from = ' none' , to = ' green' },
231- { name = ' warn' , from = ' green' , to = ' yellow' },
232- { name = ' panic' , from = ' yellow' , to = ' red' },
233- { name = ' calm' , from = ' red' , to = ' yellow' },
234- { name = ' clear' , from = ' yellow' , to = ' green' }
235- }})
236- ```
237-
238- This should be easy to adjust to fit your appropriate mechanism for object construction.
239-
240- >> _ NOTE: the ` startup ` event can be given any name, but it must be present in some form to
241- ensure that each instance constructed is initialized with its own unique ` current ` state._
242-
243207Initialization Options
244208======================
245209
@@ -278,69 +242,3 @@ local fsm = machine.create({
278242}})
279243print (fsm .current ) -- "green"
280244```
281-
282- If your object already has a ` startup ` method you can use a different name for the initial event
283-
284- ``` lua
285- local machine = require (' statemachine' )
286-
287- local fsm = machine .create ({
288- inital = { state = ' green' , event = ' init' },
289- events = {
290- { name = ' panic' , from = ' green' , to = ' red' },
291- { name = ' calm' , from = ' red' , to = ' green' },
292- }})
293- print (fsm .current ) -- "green"
294- ```
295-
296- Finally, if you want to wait to call the initial state transition event until a later date you
297- can ` defer ` it:
298-
299- ``` lua
300- local machine = require (' statemachine' )
301-
302- local fsm = machine .create ({
303- inital = { state = ' green' , event = ' init' , defer = true },
304- events = {
305- { name = ' panic' , from = ' green' , to = ' red' },
306- { name = ' calm' , from = ' red' , to = ' green' },
307- }})
308-
309- print (fsm .current ) -- "none"
310- fsm .init ()
311- print (fsm .current ) -- "green"
312- ```
313-
314- Of course, we have now come full circle, this last example is pretty much functionally the
315- same as the first example in this section where you simply define your own startup event.
316-
317- So you have a number of choices available to you when initializing your state machine.
318-
319- >> _ IMPORTANT NOTE: if you are using the pattern described in the previous section "State Machine
320- Classes", and wish to declare an ` initial ` state in this manner, you MUST use the ` defer: true `
321- attribute and manually call the starting event in your constructor function. This will ensure
322- that each instance gets its own unique ` current ` state, rather than an (unwanted) shared
323- ` current ` state on the prototype object itself._
324-
325- Handling Failures
326- ======================
327-
328- By default, if you try to call an event method that is not allowed in the current state, the
329- state machine will throw an exception. If you prefer to handle the problem yourself, you can
330- define a custom ` error ` handler:
331-
332- ``` lua
333- local machine = require (' statemachine' )
334-
335- local fsm = machine .create ({
336- inital = { state = ' green' , event = ' init' , defer = true },
337- error = function (eventName , from , to , args , errorCode , errorMessage )
338- return ' event ' .. eventName .. ' was naughty :- ' .. errorMessage
339- end ,
340- events = {
341- { name = ' panic' , from = ' green' , to = ' red' },
342- { name = ' calm' , from = ' red' , to = ' green' },
343- }})
344-
345- print (fsm :calm ()); -- "event calm was naughty :- event not allowed in current state green"
346- ```
0 commit comments