Comparing version 1.0.0 to 1.0.1
@@ -8,2 +8,3 @@ const EventEmitter = require('events').EventEmitter; | ||
constructor(states) { | ||
super(); | ||
this.states = {}; | ||
@@ -47,5 +48,7 @@ this.history = []; | ||
if (err) { return cb(err); } | ||
this.history.push({ type: 'event', name: eventName, args, results }); | ||
this.history.push({ stateName: nextStateName, eventName, args, results }); | ||
this.emit('transition', { stateName: nextStateName, eventName, args, results }); | ||
this.history.length = Math.min(this.history.length, 100); | ||
this.setState(nextStateName); | ||
cb(null, ...results) | ||
cb(null, ...results); | ||
}); | ||
@@ -64,3 +67,2 @@ } | ||
} | ||
this.history.push({ type: 'state', name: stateName }); | ||
this.currentState = nextState; | ||
@@ -67,0 +69,0 @@ } |
@@ -19,4 +19,5 @@ | ||
handle(eventName, ...args) { | ||
const cb = args.pop(); | ||
const eventHandler = this.events[eventName] || this.events.default; | ||
const cb = args.pop(); | ||
const stateInitializer = this.events.initialize || ((eventName, next) => next(null)); | ||
const eventHandler = this.events[eventName] || this.events.default; | ||
@@ -38,3 +39,16 @@ if (eventName === 'error') { | ||
} | ||
if (typeof stateInitializer !== 'function') { | ||
throw new Error( | ||
`Cannot handle event ${eventName}. initialize must be a function` | ||
); | ||
} | ||
const next = (err, nextStateName, ...args) => { | ||
if (err) { return handleError(err); } // eslint-disable-line no-use-before-define | ||
if (typeof nextStateName !== 'string') { | ||
throw new Error('nextStateName must be a string'); | ||
} | ||
cb(null, nextStateName, ...args); | ||
}; | ||
const handleError = (err, nextStateName, ...args) => { | ||
@@ -46,12 +60,13 @@ const errorHandler = this.events.error; | ||
const next = (err, nextStateName, ...args) => { | ||
if (err) { return handleError(err); } | ||
if (typeof nextStateName !== 'string') { | ||
throw new Error('nextStateName must be a string'); | ||
const initializerNext = (err) => { | ||
if (err) { return cb(err); } | ||
try { | ||
eventHandler.call(this.context, next, ...args); | ||
} catch (err) { | ||
handleError(err); | ||
} | ||
cb(null, nextStateName, ...args); | ||
}; | ||
try { | ||
eventHandler.call(this.context, next, ...args); | ||
stateInitializer.call(this.context, eventName, initializerNext, ...args); | ||
} catch (err) { | ||
@@ -58,0 +73,0 @@ handleError(err); |
{ | ||
"name": "avto", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A very simple state machine", | ||
@@ -13,2 +13,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"eslint": "^2.12.0", | ||
"eslint-config-airbnb": "^9.0.1", | ||
"eslint-plugin-import": "^1.8.1", | ||
"eslint-plugin-jsx-a11y": "^1.4.2", | ||
"eslint-plugin-react": "^5.1.1", | ||
"istanbul": "^0.4.3", | ||
@@ -15,0 +20,0 @@ "mocha": "^2.5.3", |
@@ -0,1 +1,3 @@ | ||
/* globals describe it */ | ||
const assert = require('assert'); | ||
@@ -2,0 +4,0 @@ const sinon = require('sinon'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10879
9
293
8