node-state
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -0,1 +1,2 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
(function() { | ||
@@ -10,29 +11,41 @@ var EventEmitter2, NodeState, | ||
function NodeState(config) { | ||
var event, events, fn, from_state, state, state_name, to, to_states, _base, _base2, _base3, _ref, _ref2, _ref3; | ||
var event, events, fn, from_state, state, state_name, states, to, to_states, transitions, _base, _base1, _base2, _base3, _ref, _ref1, _ref2; | ||
this.config = config != null ? config : {}; | ||
this.stop = __bind(this.stop, this); | ||
this.start = __bind(this.start, this); | ||
this.unwait = __bind(this.unwait, this); | ||
this.wait = __bind(this.wait, this); | ||
this.raise = __bind(this.raise, this); | ||
this.goto = __bind(this.goto, this); | ||
this._notifier = new EventEmitter2({ | ||
wildcard: true | ||
}); | ||
states = {}; | ||
_ref = this.states; | ||
for (state in _ref) { | ||
events = _ref[state]; | ||
states[state] = {}; | ||
for (event in events) { | ||
fn = events[event]; | ||
this.states[state][event] = fn.bind(this); | ||
states[state][event] = fn.bind(this); | ||
} | ||
} | ||
_ref2 = this.transitions; | ||
for (from_state in _ref2) { | ||
to_states = _ref2[from_state]; | ||
this.states = states; | ||
transitions = {}; | ||
_ref1 = this.transitions; | ||
for (from_state in _ref1) { | ||
to_states = _ref1[from_state]; | ||
transitions[from_state] = {}; | ||
for (to in to_states) { | ||
fn = to_states[to]; | ||
this.transitions[from_state][to] = fn.bind(this); | ||
transitions[from_state][to] = fn.bind(this); | ||
} | ||
} | ||
this.transitions = transitions; | ||
(_base = this.config).initial_state || (_base.initial_state = ((function() { | ||
@@ -50,6 +63,7 @@ var _results; | ||
this._current_timeout = null; | ||
(_base2 = this.config).autostart || (_base2.autostart = false); | ||
_ref3 = this.states; | ||
for (state_name in _ref3) { | ||
events = _ref3[state_name]; | ||
(_base1 = this.config).autostart || (_base1.autostart = false); | ||
(_base2 = this.config).sync_goto || (_base2.sync_goto = false); | ||
_ref2 = this.states; | ||
for (state_name in _ref2) { | ||
events = _ref2[state_name]; | ||
(_base3 = this.states[state_name])['Enter'] || (_base3['Enter'] = function(data) { | ||
@@ -59,11 +73,15 @@ return this.current_data = data; | ||
} | ||
if (this.config.autostart) this.goto(this.current_state_name); | ||
if (this.config.autostart) { | ||
this.goto(this.current_state_name); | ||
} | ||
} | ||
NodeState.prototype.goto = function(state_name, data) { | ||
var callback, event_name, previous_state_name, transition, _ref, _ref2, | ||
var callback, event_name, previous_state_name, transition, _ref, _ref1, | ||
_this = this; | ||
this.current_data = data || this.current_data; | ||
previous_state_name = this.current_state_name; | ||
if (this._current_timeout) clearTimeout(this._current_timeout); | ||
if (this._current_timeout) { | ||
clearTimeout(this._current_timeout); | ||
} | ||
_ref = this.current_state; | ||
@@ -76,5 +94,5 @@ for (event_name in _ref) { | ||
this.current_state = this.states[this.current_state_name]; | ||
_ref2 = this.current_state; | ||
for (event_name in _ref2) { | ||
callback = _ref2[event_name]; | ||
_ref1 = this.current_state; | ||
for (event_name in _ref1) { | ||
callback = _ref1[event_name]; | ||
this._notifier.on(event_name, callback); | ||
@@ -98,5 +116,9 @@ } | ||
} | ||
return process.nextTick(function() { | ||
return transition(_this.current_data, callback); | ||
}); | ||
if (this.config.sync_goto) { | ||
return transition(this.current_data, callback); | ||
} else { | ||
return process.nextTick(function() { | ||
return transition(_this.current_data, callback); | ||
}); | ||
} | ||
}; | ||
@@ -120,3 +142,5 @@ | ||
NodeState.prototype.unwait = function() { | ||
if (this._current_timeout) return clearTimeout(this._current_timeout); | ||
if (this._current_timeout) { | ||
return clearTimeout(this._current_timeout); | ||
} | ||
}; | ||
@@ -123,0 +147,0 @@ |
{ | ||
"author": "Nick Fisher", | ||
"author": "Iskren Chernev <iskren.chernev@gmail.com>", | ||
"contributors": [ | ||
"Nick Fisher" | ||
], | ||
"name": "node-state", | ||
"description": "A finite state machine (FSM) implementation for node.js", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"main": "index", | ||
"homepage": "https://github.com/nrf110/node-state", | ||
"homepage": "https://github.com/ichernev/node-state", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/nrf110/node-state.git" | ||
"url": "git://github.com/ichernev/node-state.git" | ||
}, | ||
@@ -12,0 +15,0 @@ "engines": { |
@@ -18,3 +18,3 @@ # Node-State: A finite state machine (FSM) implementation for Node.js and CoffeeScript | ||
### adding states and events | ||
```javascript | ||
```coffeescript | ||
class MyStateMachine extends NodeState | ||
@@ -35,3 +35,3 @@ states: | ||
### adding transitions | ||
```javascript | ||
```coffeescript | ||
class MyStateMachine extends NodeState | ||
@@ -50,3 +50,3 @@ states: | ||
Enter: (data) -> | ||
transitions: | ||
@@ -84,3 +84,3 @@ A: | ||
```javascript | ||
```coffeescript | ||
fsm = new MyStateMachine | ||
@@ -95,3 +95,3 @@ autostart: true | ||
```javascript | ||
```coffeescript | ||
class MyStateMachine extends NodeState | ||
@@ -118,4 +118,4 @@ states: | ||
```javascript | ||
class MyStateMachine extends NodeState | ||
```coffeescript | ||
class MyStateMachine extends NodeState | ||
states: | ||
@@ -132,5 +132,11 @@ A: | ||
+ `@unwait` - Cancels the current wait operation. Usually, the combination of @wait/@unwait is used if you are waiting a specified time period for other events to come in. @unwait would be used once you've received an event of interest and no longer want to respond to the timer. | ||
+ `@unwait` - Cancels the current wait operation. Usually, the combination of @wait/@unwait is used if you are waiting a specified time period for other events to come in. `@unwait` would be used once you've received an event of interest and no longer want to respond to the timer. | ||
+ `@start` - Kicks off the transition to the initial state. | ||
+ `@stop` - Unregisters all event handlers for the state machine, effectively turning it off. In the future, pre- and post-stop event hooks may be added to allow for additional cleanup during shutdown. | ||
+ `@stop` - Unregisters all event handlers for the state machine, effectively turning it off. In the future, pre- and post-stop event hooks may be added to allow for additional cleanup during shutdown. | ||
## History | ||
+ v1.3.0 - bugfixes and `sync_goto` option to constructor | ||
## Credits | ||
+ originally published by [Nick Fisher]("https://github.com/nrf110") |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
25172
137
0
136