Comparing version 1.0.3 to 1.0.4
'use strict'; | ||
var fs = require('fs'); | ||
var Layer = require('./layer'); | ||
var util = require('./util/util'); | ||
let fs = require('fs'); | ||
let Layer = require('./layer'); | ||
let util = require('./util/util'); | ||
var app = module.exports; | ||
let app = module.exports; | ||
@@ -12,44 +12,92 @@ app.init = function(opts) { | ||
this.base = opts.base; | ||
this.runningJobNum = 0; | ||
this.waitings = []; | ||
this.settings = {}; | ||
this.layers = {}; | ||
// this.defaultConfiguration(); | ||
}; | ||
// app.defaultConfiguration = function() { | ||
// }; | ||
app.start = function() { | ||
var app = this; | ||
this.loadHandles(); | ||
// event driven | ||
this.on('running', this.running.bind(this)); | ||
this.on('waiting', this.waiting.bind(this)); | ||
}; | ||
app.stop = function() { | ||
this.setStepMode(true); | ||
}; | ||
app.setStepMode = function(stepMode) { | ||
this.stepMode = !!stepMode; | ||
}; | ||
app.dispatch = function(job) { | ||
var app = this; | ||
app.getStepMode = function() { | ||
return this.stepMode; | ||
}; | ||
function next() { | ||
var state = job.getState(); | ||
var layer = app.layers[state]; | ||
if (!layer) { | ||
app.emit('error', 'state ' + state + ' donot exist'); | ||
return; | ||
app.running = function(jobs) { | ||
let self = this; | ||
jobs.forEach(function(job) { | ||
self.runningJobNum++; | ||
// attach job.done | ||
job.done = function() { | ||
self.runningJobNum--; | ||
if (self.runningJobNum === 0) { | ||
self.emit('stop'); | ||
} | ||
}; | ||
function next() { | ||
let state = job.getState(); | ||
let layer = self.layers[state]; | ||
if (!layer) { | ||
self.emit('error', 'state ' + state + ' donot exist'); | ||
return; | ||
} | ||
layer.handleEntry(job, function() { | ||
layer.handleExit(job, function() { | ||
if (self.getStepMode()) { | ||
self.runningJobNum--; | ||
self.emit('waiting', job); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
}); | ||
} | ||
layer.handleEntry(job, function() { | ||
layer.handleExit(job, next); | ||
}); | ||
} | ||
next(); | ||
}); | ||
next(); | ||
}; | ||
app.waiting = function(job) { | ||
this.waitings.push(job); | ||
}; | ||
/** | ||
* in stepMode, let all waiting job exec one step | ||
*/ | ||
app.next = function() { | ||
let waitings = this.waitings; | ||
this.waitings = []; | ||
this.emit('running', waitings); | ||
}; | ||
/** | ||
* @param {Array|Object} job instance of job.js | ||
*/ | ||
app.dispatch = function(job) { | ||
if (!Array.isArray(job)) job = [job]; | ||
// -> event('dispatch') | ||
this.emit('running', job); | ||
}; | ||
app.loadHandles = function() { | ||
var fsmPath = util.getHandlePath(this.base, this.name); | ||
var app = this; | ||
let fsmPath = util.getHandlePath(this.base, this.name); | ||
let app = this; | ||
if (fsmPath) { | ||
@@ -61,4 +109,4 @@ fs.readdirSync(fsmPath).forEach(function(filename) { | ||
var state = util.getState(filename); | ||
var stateHandle = require(fsmPath + '/' + filename); | ||
let state = util.getState(filename); | ||
let stateHandle = require(fsmPath + '/' + filename); | ||
if (!stateHandle.entry && !stateHandle.exit) { | ||
@@ -69,3 +117,3 @@ app.emit('error', 'state ' + state + ' both entry/exit not defined'); | ||
var layer = new Layer(state, stateHandle); | ||
let layer = new Layer(state, stateHandle); | ||
app.layers[state] = layer; | ||
@@ -88,2 +136,1 @@ }); | ||
}; | ||
{ | ||
"name": "mdw-fsm", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "middleware finite state machine", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,2 +20,9 @@ 'use strict'; | ||
var job = Job.create('green'); | ||
trafficLight.dispatch(job); | ||
trafficLight.dispatch(job); | ||
trafficLight.setStepMode(true); | ||
setInterval(function() { | ||
console.log(trafficLight.runningJobNum, trafficLight.waitings); | ||
trafficLight.next(); | ||
}, 10000); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
133408
93
5320
92