Skip to content

Instantly share code, notes, and snippets.

@omerucel
Created May 6, 2012 20:22
Show Gist options
  • Save omerucel/2624208 to your computer and use it in GitHub Desktop.
Save omerucel/2624208 to your computer and use it in GitHub Desktop.
Yeniden kullanılabilirlik.
var util = require('util');
var async = require('async');
var EventEmitter = require('events').EventEmitter;
// İşlem denizi dersek yeridir.
var Ocean = {
__start : function(params){
return function(callback){
callback(null, {
params : params
})
};
},
process1 : function(response, callback){
console.log(response.params);
response.process1 = true;
callback(null, response);
},
process2 : function(response, callback){
response.process2 = true;
callback(null, response);
},
process3 : function(response, callback){
response.process3 = true;
callback(null, response);
}
};
// Birinci modül.
var One = function(){
EventEmitter.call(this);
};
util.inherits(One, EventEmitter);
One.prototype.do = function(params){
var self = this;
var processes = [
Ocean.__start(params),
Ocean.process1,
Ocean.process2
];
async.waterfall(processes, function(err, response){
if (err)
{
self.emit('error', err);
}else{
self.emit('success', response);
}
});
};
// İkinci modül.
var Two = function(){
EventEmitter.call(this);
};
util.inherits(Two, EventEmitter);
Two.prototype.do = function(params){
var self = this;
var processes = [
Ocean.__start(params),
Ocean.process1,
Ocean.process3
];
async.waterfall(processes, function(err, response){
if (err)
{
self.emit('error', err);
}else{
self.emit('success', response);
}
});
};
// Yönetici sınıf.
var Lib = {
one : function(){
return new One();
},
two : function(){
return new Two();
}
};
// Test
Lib.one()
.on('success', function(data){
console.log('DATA : ', data);
})
.on('error', function(err){
console.log('ERROR : ', err);
})
.do({data : 'one'});
Lib.two()
.on('success', function(data){
console.log('DATA : ', data);
})
.on('error', function(err){
console.log('ERROR : ', err);
})
.do({data : 'two'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment