Skip to content

Commit

Permalink
feat(MachineX): Updated to support latest fseh
Browse files Browse the repository at this point in the history
Machine interface changes
  • Loading branch information
0xfede committed Feb 17, 2017
1 parent 8e55166 commit b7b62fc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@
},
"homepage": "https://github.com/0xfede/fsehx#readme",
"devDependencies": {
"@types/node": "0.0.2",
"@types/node": "7.0.5",
"chai": "3.5.0",
"chai-spies": "0.7.1",
"codecov.io": "0.1.6",
"commitizen": "2.8.6",
"commitizen": "2.9.6",
"cz-conventional-changelog": "1.2.0",
"ghooks": "1.3.2",
"mocha": "3.0.2",
"nyc": "8.3.0",
"ghooks": "2.0.0",
"mocha": "3.2.0",
"nyc": "10.1.2",
"rimraf": "2.5.4",
"semantic-release": "^4.3.5",
"typescript": "2.0.3"
"semantic-release": "6.3.2",
"typescript": "2.1.6"
},
"config": {
"commitizen": {
Expand All @@ -63,6 +63,6 @@
}
},
"dependencies": {
"fseh": "1.8.0"
"fseh": "1.9.0"
}
}
30 changes: 17 additions & 13 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ describe('fsehx', function() {
m.on('start', named);

should.not.exist(m.state);
m.enter('start', 'aaa');
should.exist(m.state);
m.state.should.be.a('string');
m.state.should.equal('start');
named_pre_entry.should.have.been.called.once();
pre_entry.should.have.been.called.once.with('start', 'aaa');
named_entry.should.have.been.called.once();
entry.should.have.been.called.once.with('start', 'aaa');
named.should.have.been.called.once();
return m.enter('start', 'aaa').then(function() {
should.exist(m.state);
m.state.should.be.a('string');
m.state.should.equal('start');
named_pre_entry.should.have.been.called.once();
pre_entry.should.have.been.called.once.with('start', 'aaa');
named_entry.should.have.been.called.once();
entry.should.have.been.called.once.with('start', 'aaa');
named.should.have.been.called.once();
});
});

it('should emit events when entering a state', function() {
it('should emit events when exiting a state', function() {
var m = new MachineX({
start: {},
end: {}
Expand All @@ -49,9 +50,12 @@ describe('fsehx', function() {
m.on('start:exit', named_exit);
m.on('exit', exit);

m.enter('end', 'aaa');
named_exit.should.have.been.called.once();
exit.should.have.been.called.once.with('start', 'aaa');
m.ready.then(function() {
return m.enter('end', 'aaa').then(function() {
named_exit.should.have.been.called.once();
exit.should.have.been.called.once.with('start', 'aaa');
});
});
});

});
Expand Down
28 changes: 16 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { EventEmitter } from 'events';
import { Machine } from 'fseh';
export { Handlers, State, StateList, StateTable } from 'fseh';
export { Handlers, StateTable } from 'fseh';

export class MachineX extends Machine implements EventEmitter {

// Machine overrides
enter(state: string, ...args:any[]): void {
if (this.state) {
this.emit(`${this.state}:exit`, state, ...args);
this.emit('exit', this.state, state, ...args);
}
this.emit(`${state}:pre-entry`, ...args);
this.emit('pre-entry', state, ...args);
super.enter(state, ...args);
this.emit(`${state}:entry`, ...args);
this.emit('entry', state, ...args);
this.emit(state, ...args);
enter(state: string, ...args:any[]):Promise<any> {
return this.ready.then(() => {
if (this.state) {
this.emit(`${this.state}:exit`, state, ...args);
this.emit('exit', this.state, state, ...args);
}
this.emit(`${state}:pre-entry`, ...args);
this.emit('pre-entry', state, ...args);
return super.enter(state, ...args).then(data => {
this.emit(`${state}:entry`, ...args);
this.emit('entry', state, ...args);
this.emit(state, ...args);
return data;
});
});
}

// EventEmitter interface
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"target": "es6",
"noImplicitAny": false,
"sourceMap": true,
"declaration": true,
Expand Down

0 comments on commit b7b62fc

Please sign in to comment.