Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mdw-fsm

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdw-fsm - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

.history/lib/application_20170517173513.js

105

lib/application.js
'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);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc