Application scenarios
The previous blog introduced several EventEmitter APIs, which are simple and efficient and facilitate decoupling of business logic.
Use the once () method to solve the avalanche problem
In event subscription/release mode, the listener registered using the once () method is executed only once, and its association with the event is removed after execution. this can be used in actual development to filter repetitive event responses.
For example, in the case of high traffic volume and high concurrency, the following direct writing may cause the database to be unable to withstand at the same time, thus affecting the overall response call of the website:
Var select = function (callback ){
Db. select ("SQL", function (results ){
Callback (results );
});
}
Of course, adding a status lock can solve the concurrency problem. However, except for the first request to receive a response, other requests will not get the corresponding result:
Var status = 'ready ';
Var select = function (callback ){
If (status = 'ready '){
Status = 'panding ';
Db. select ('SQL', function (results ){
Status = 'ready ';
Callback (results );
});
}
}
At this time, you can introduce the event queue:
Var event = require ('event ');
Var ee = new event. EventEmitter ();
Var select = function (callback ){
Ee. once ('selected', callback); // register multiple listeners that only run once or store these listeners in one queue
If (status = 'ready '){
Status = 'pending ';
Db. select ('SQL', function (results ){
Ee. emit ('selected', results); // query only once, and send the result to multiple previously registered listeners that will only run once
});
}
}
Query only once, and send the result to multiple previously registered listeners that will only run once. With EventEmitter, the result is sent to all queues, the EventEmitter will help us solve various recycling problems in the future. of course, as mentioned earlier, this must be used with setMaxListeners (0) to remove the limit on the number of listeners.
Use EventEmitter to solve multi-asynchronous collaboration
The event Publishing/subscription mode isolates the business logic and maintains a single responsibility of the business logic unit. Generally, the relationship between the event and the listener is one-to-many, but in asynchronous programming, the events and listeners are many-to-one. That is to say, a business logic may depend on three results passed through callback or events.
This section describes how to read two pieces of data on the rendering page without using libraries such as Promise async Q:
Var count = 0;
Var results = {};
Var done = function (key, value ){
Results [key] = value;
Count ++;
If (count = 3) {// The three dependencies are successfully completed.
Render (); // render the page
}
}
Db. select ('sql1', function (results ){
Done ('data1', results );
});
Db. select ('sql2', function (results ){
Done ('data2 ', results );
});
User. get (function (err, user )){
Done ('user', user );
});
If the publishing/subscription mode is used:
Var ee = new require ('event'). EventEmitter ();
Var after = function (times, callback ){
Var count = 0;
Var results = {};
Return function (key, value ){
Results [key] = value;
Count ++;
If (count = times ){
Callback (results );
}
}
}
Ee. on ('done', after );
Db. select ('sql1', function (results ){
Ee. emit ('done', 3, results );
});
Db. select ('sql2', function (results ){
Ee. emit ('done', 3, results );
});
User. get (function (err, user )){
Ee. emit ('done', 3, user );
});