Hello,

I have now used Node.js for some time and have encountered some typical 
problems like the code flow. It's easy do design the code that flows 
linearly, like make query to database or N times. Event the callbacks can 
be handled easily with great libraries called async, q and hopefully the 
yield and generators save us. But the problem a rises when you have 
callbacks and there are branches which create you different execution then 
linear, some business logic, error handling. So what would be best cases to 
handle these situations ? 

As a solution I provide example of event driven code flow, which is based 
on the events and executing small function units. There are some problems 
like namespacing the event names and how does the listeners affect the 
overall performance. 

Here is my example:

var util = require("util"),
events = require("events");
function MyStream() {
    events.EventEmitter.call(this);
}
util.inherits(MyStream, events.EventEmitter);
MyStream.prototype.init = function() {
// Init all the listeners
this.on('user:create', this.create);
this.on('user:exists', this.userExists);
this.on('user:delete:done', this.createAccess);
this.on('user:access:tags', this.accessTags);
this.on('user:end', this.end);
// Shoot out first event
this.emit('user:create');
};
MyStream.prototype.create = function() {
console.log('We need to create user');
this.emit('user:exists');
};
MyStream.prototype.userExists = function() {
console.log('Delete the old user and try to create new');

this.emit('user:delete:done', { name: 'Jukka', age: 24 });
};
MyStream.prototype.createAccess = function(user) {
// Based on the user information create accesses

if (user.name === 'Jukka') {
this.emit('user:access:tags', [ 'books', 'computers', 'programming' ]);
} else {
this.emit('user:access:tags', [ 'normal-people' ]);
}
};
MyStream.prototype.accessTags = function(tags) {
// Write tags to database
console.log('Write tags to database');
this.emit('user:end');
};
MyStream.prototype.end = function() {
console.log('This is the last call');
};


// Application starts here 
// Yes the error handling is missing 
var stream = new MyStream();
stream.on("user:exists", function(data) {
    console.log('Really the user exists we can all something else out');
})
stream.init();

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to