statetransition-mixin
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -38,3 +38,3 @@ { | ||
"homepage": "https://github.com/arlac77/statetransition-mixin#readme", | ||
"version": "1.0.1" | ||
"version": "1.1.0" | ||
} |
@@ -35,2 +35,16 @@ /* jslint node: true, esnext: true */ | ||
/** | ||
* Called when the state transtinio implementation Promise rejects. | ||
* Resets the transition | ||
* @return {Promise} rejecting promise | ||
*/ | ||
stateTransitionRejection(rejected) { | ||
//this.error(level => `Executing ${this._transition.name} transition leads to ${rejected}`); | ||
this.state = 'failed'; | ||
this._transitionPromise = undefined; | ||
this._transition = undefined; | ||
return Promise.reject(rejected); | ||
} | ||
/** | ||
* To be overwritten | ||
@@ -75,2 +89,21 @@ * Called when the state changes | ||
/** | ||
* Defines methods to perfom the state transitions. | ||
* States are traversed in the following way: | ||
* current -> during -> final | ||
* If the step is not in one of the transitions current | ||
* states and also not already in the transitions final | ||
* state a rejecting promise will be delivered from the | ||
* generated function. In the 'during' state a function | ||
* named '_' + <transitions name> (sample: '_start()') | ||
* will be called first. | ||
* It is expected that this function delivers a promise. | ||
* Special handling of consequent transitions: | ||
* While in a during state the former delivered primise will be | ||
* delivered again. This enshures that several consequent | ||
* transitions in a row will be fullfiled by the same promise. | ||
* There can only be one transition in place at a given point in time. | ||
* @param {Object} object where we define the metods | ||
* @param {Object} actions object describing the state transitions | ||
*/ | ||
module.exports.defineActionMethods = function (object, actions) { | ||
@@ -92,8 +125,8 @@ //console.log(`${JSON.stringify(actions,undefined,1)}`); | ||
if (this._transition) { | ||
if (this.state === this._transition.during) { | ||
return this._transitionPromise; | ||
switch (this.state) { | ||
case this._transition.during: | ||
return this._transitionPromise; | ||
case this._transition.target: | ||
return Promise.resolve(this); | ||
} | ||
if (this.state === this._transition.target) { | ||
return Promise.resolve(this); | ||
} | ||
} | ||
@@ -110,11 +143,4 @@ if (action.transitions[this.state]) { | ||
return this; | ||
}, rejected => { | ||
this.error(level => `Executing ${this._transition.name} transition leads to ${rejected}`); | ||
}, rejected => this.stateTransitionRejection(rejected)); | ||
this.state = 'failed'; | ||
this._transitionPromise = undefined; | ||
this._transition = undefined; | ||
return Promise.reject(reject); | ||
}); | ||
return this._transitionPromise; | ||
@@ -121,0 +147,0 @@ } else { |
@@ -36,2 +36,4 @@ /* global describe, it, xit */ | ||
var shouldReject = false; | ||
class StatefullClass extends stm.StateTransitionMixin(BaseClass, actions, 'stopped') { | ||
@@ -41,3 +43,7 @@ _start() { | ||
setTimeout(() => { | ||
f(this) | ||
if (shouldReject) { | ||
r(new Error("always reject")); | ||
} else { | ||
f(this); | ||
} | ||
}, 10); | ||
@@ -106,2 +112,12 @@ }); | ||
it('handle failure while starting', function (done) { | ||
o1.stop().then(() => { | ||
shouldReject = true; | ||
assert.equal(o1.state, 'stopped'); | ||
o1.start().then(() => {}).catch(e => { | ||
assert.equal(o1.state, 'failed'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
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
19340
237