From b7b62fc47c8dbdac1080346b7341ea32a427c0b8 Mon Sep 17 00:00:00 2001 From: Federico Pinna Date: Fri, 17 Feb 2017 15:12:12 +0100 Subject: [PATCH] feat(MachineX): Updated to support latest fseh Machine interface changes --- package.json | 16 ++++++++-------- src/index.test.js | 30 +++++++++++++++++------------- src/index.ts | 28 ++++++++++++++++------------ tsconfig.json | 2 +- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index ddb7fca..1ae3a79 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -63,6 +63,6 @@ } }, "dependencies": { - "fseh": "1.8.0" + "fseh": "1.9.0" } } diff --git a/src/index.test.js b/src/index.test.js index 5510003..66461fd 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -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: {} @@ -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'); + }); + }); }); }); diff --git a/src/index.ts b/src/index.ts index 2c345a6..e5acef9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { + 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 diff --git a/tsconfig.json b/tsconfig.json index 9f17fc5..d33f9f1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "module": "commonjs", - "target": "es5", + "target": "es6", "noImplicitAny": false, "sourceMap": true, "declaration": true,