Answer the question
In order to leave comments, you need to log in
What's wrong with callbacks and architecture?
There is NodeJS,
I don’t understand how to make the architecture correct, many actions are asynchronous and how to keep track of everything so as not to fence a bunch of callbacks and run tests.
Suppose there is a class that observes something, this class performs the function of receiving data and transferring it to the desired action by the handler class, the handler class performs any checks and makes a record in the database. This whole bunch is complicated by asynchronous requests.
The problem is that sometimes a certain sequence is required, yes, you can use async from Node. But this is not a solution, the problem is clearly in the architecture.
PS Yes, I know that an additional layer is needed to work with the database, but in this example the question is not about that and I think you can do without it.
class Watcher{
get EventEmitter() { return this._eventEmitter; }
constructor(){
}
DataReceived(data){
...
this._eventEmitter.emit('dataReceived',data);
}
}
class Worker{
get Watcher() { return this._watcher; }
get Database() { return this._database; }
constructor(){
this._database = orm.connect(....);
this._watcher = new Watcher();
this._watcher.EventEmitter.on('dataReceived', function(data) {
this.ProcessReceivedData(data);
}
ProcessReceivedData(data) {
var self = this;
this._database.models.A.find(... , function(err,data){
if (data.length > 1) {
self._database.models.B.create({...}, function(err,data) { });
}else{
self._database.models.B.update({...}, function(err,data) { });
}
}
}
}
it('Should create record in DB', function (done) {
var Wkr = new Worker();
Wkr.Watcher.DataReceived("something"); //инсценируем отправку данных
//Проверяем есть ли запись, но она с вероятностью 99% не успеет создаться
//поскольку обращение к базе не дожидается ответа
Wkr.Database.models.Device.get(1, function (e, d) {
should.exist(d);
done();
});
});
});
self._database.models.B.create({...}, function(err,data) { self.emitter.emit('onProcessedData') });
...
self._database.models.B.update({...}, function(err,data) { self.emitter.emit('onProcessedData') });
Answer the question
In order to leave comments, you need to log in
The event model + promises usually provide everything you need to structure your asynchronous code. You have a problem with application. Actions such as create / update, for example, should generate their own events in the callback, to which the general code will be subscribed later (and for good, there should be an onProcessedData function that already generates the corresponding event inside itself, and not a nested emit):
create({}, function() {
object.emit("create");
// В триггеры подобных событий, к тому же, лучше передавать
// дополнительные аргументы, чтобы можно было отличить,
// что именно создано когда это потребуется
});
update({}, function() {
object.emit("update");
});
object.on("create update", onProcessedData);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question