You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

45 lines
1.1 KiB

var Emitter = require('./Emitter');
var each = require('./each');
var some = require('./some');
var toArr = require('./toArr');
exports = Emitter.extend({
className: 'State',
initialize: function(initial, events) {
this.callSuper(Emitter, 'initialize');
this.current = initial;
var self = this;
each(events, function(event, key) {
self[key] = buildEvent(key, event);
});
},
is: function(state) {
return this.current === state;
}
});
function buildEvent(name, event) {
var from = toArr(event.from);
var to = event.to;
return function() {
var args = toArr(arguments);
args.unshift(name);
var hasEvent = some(
from,
function(val) {
return this.current === val;
},
this
);
if (hasEvent) {
this.current = to;
this.emit.apply(this, args);
} else {
this.emit(
'error',
new Error(this.current + ' => ' + to + ' error'),
name
);
}
};
}
module.exports = exports;