I am using machina.js library in my nodejs server. I run the State Machine when a GET request is done and I use the State Machine to determine the response to provide to the client.
I have not clear how to manage my FSM if in a certain state I have to: 1) wait to get some information from a third party web-server; 2) transit to a new state which will get the data returned by third party web-server.
Look at following code:
const soap = require('./soap-interface');
const machina = require('machina');
var express = require('express');
var app = express();
app.get('/', determine);
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
function determine(req, res) {
const jsonData = req.body;
const FSM = new machina.BehavioralFsm({
namespace: 'action',
initialState: 'uninitialized',
states: {
uninitialized: {
'*': function (email) {
this.deferUntilTransition(email);
this.transition(email, 'checkStatus');
}
},
checkStatus: {
_onEnter(email) {
const that = this;
const action = 'GetStatus';
const params = [{ paramName: 'invoiceNumber', paramValue: email.InvoiceNumber }];
// Make request to Soap service and await for callback with results
soap.request(action, params, (output) => {
console.log(output);
// TODO: here I want to transit to a specific state
if(output.isOverdue)
that.transition(output, 'isOverdue');
else
that.transition(output, 'notOverdue');
});
}
},
isOverdue: {
_onEnter(output) {
const response = { status : 'overdue' };
res.json(response);
}
},
notOverdue: {
_onEnter(output) {
const response = { status : 'not overdue' };
res.json(response);
}
}
},
runFSM(email) {
console.log('============= START FSM =============');
this.handle(email, '*');
console.log('============= END FSM =============');
}
});
FSM.runFSM(jsonData);
}
The error that I have got is - of course - the timeout. I mean, I terminate the function 'determine' before sending the json response.
via dventi3
No comments:
Post a Comment