{"id":34,"date":"2007-12-22T19:26:46","date_gmt":"2007-12-22T18:26:46","guid":{"rendered":"http:\/\/schuerig.de\/michael\/blog\/index.php\/2007\/12\/22\/javascript-fsm\/"},"modified":"2021-11-24T09:20:32","modified_gmt":"2021-11-24T08:20:32","slug":"javascript-fsm","status":"publish","type":"post","link":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2007\/12\/22\/javascript-fsm\/","title":{"rendered":"Finite State Machines in JavaScript"},"content":{"rendered":"<p>I like finite state machines and I wanted to try metaprogramming in JavaScript ever since I&#8217;d seen Adam McCrea <a href=\"http:\/\/www.adamlogic.com\/2007\/03\/20\/3_metaprogramming-javascript-presentation\">presentation<\/a> on the topic.<\/p>\n<p>The result is an FSM builder in JavaScript. Machine descriptions look like my test machine here:<\/p>\n<p><code><\/p>\n<pre>\r\nvar Machine = FSM.build(function(fsm) { with (fsm) {\r\n  onUnexpectedEvent(function() { ... });\r\n\r\n  state('start', 'initial')\r\n    .event('go')\r\n      .goesTo('middle')\r\n      .doing(function() { ... })\r\n      .doing('phew')\r\n    .event('run')\r\n      .goesTo('finish')\r\n    .onExiting(function() { ... });\r\n\r\n  state('middle')\r\n    .onUnexpectedEvent(function() { ... })\r\n    .onEntering(function() { ... })\r\n    .event('back')\r\n      .goesTo('start')\r\n      .onlyIf(function() { return true_or_false })\r\n    .event('go')\r\n      .goesTo('finish');\r\n\r\n  state('finish', 'final');\r\n}});\r\n\r\nfunction TestMachine() {}\r\n\/\/ amend TestMachine.prototype here all the way you want\r\nTestMachine.prototype.phew = function() { ... };\r\nTestMachine.prototype = new Machine(TestMachine.prototype);\r\n<\/pre>\n<p><\/code><\/p>\n<p>There&#8217;s no documentation yet, but thorough <del>unit tests<\/del><ins>specs<\/ins><br \/>\nusing Nicol\u00c3\u00a1s Sanguinetti&#8217;s very nice <a href=\"http:\/\/code.google.com\/p\/js-spec\/\">js-spec<\/a> framework.<\/p>\n<p>Something to look at:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.schuerig.de\/michael\/javascript\/jsfsm\/fsm.js\">Just the script<\/a><\/li>\n<li><a href=\"http:\/\/www.schuerig.de\/michael\/javascript\/jsfsm\/spec\/fsm.html\">The specs<\/a> (Look at the source)<\/li>\n<li><a href=\"http:\/\/www.schuerig.de\/michael\/javascript\/jsfsm\/jsfsm-latest.tar.gz\">The complete package<\/a><\/li>\n<\/ul>\n<p>The script is meant to be usable independently of and play nicely with any other libraries.<\/p>\n<p><del>The manner in which an FSM is attached to classes\/objects is still rather convoluted. The general idea is that it (a) should be possible to insert the FSM as the first link in the prototype chain and (b) should be possible to directly attach it to an existing object. I&#8217;d appreciate suggestions how to make this nice and shiny.<\/del><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I like finite state machines and I wanted to try metaprogramming in JavaScript ever since I&#8217;d seen Adam McCrea presentation on the topic. The result is an FSM builder in JavaScript. Machine descriptions look like my test machine here: var &hellip; <a href=\"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2007\/12\/22\/javascript-fsm\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":1,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/34\/revisions\/123"}],"wp:attachment":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}