Last active
August 29, 2015 14:25
-
-
Save gitseitanaka/53e936f6d415da56f79e to your computer and use it in GitHub Desktop.
node.js : EventEmitter, nextTick and setImmediate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('msg', function(message) { | |
console.log(message); | |
}); | |
console.log('--- before emit msg'); | |
process.emit('msg', 'foo'); | |
console.log('--- after emit msg'); | |
// $ node process_emit.js | |
// --- before called emit msg | |
// foo | |
// --- after called emit msg | |
// $ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('msg', function(message) { | |
console.log(message); | |
}); | |
console.log('--- before called emit msg'); | |
process.nextTick(function(){ | |
process.emit('msg', 'foo'); | |
}); | |
console.log('--- after called emit msg'); | |
// $ node process_emit_nextTick.js | |
// --- before called emit msg | |
// --- after called emit msg | |
// foo | |
// $ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* initial */ | |
var fs = require('fs'); | |
var largefile = ''; | |
if (process.platform === 'win32') { | |
largefile = 'c:/Program Files/Windows Photo Viewer/ImagingEngine.dll'; | |
} else { | |
largefile = '/var/log/syslog.2.gz'; | |
} | |
var reader = fs.createReadStream(largefile); | |
var times = 2; | |
/* io access */ | |
var count_on = 0; | |
reader.on('data', function (chunk) { | |
console.log('--- [', count_on, ']', 'on_chuck'); | |
count_on++; | |
if (count_on > times) { | |
reader.pause(); | |
} | |
}); | |
/* nextTick loop */ | |
process.on('msg', function (message, count) { | |
console.log('[', count, ']', message); | |
if (count > times) { | |
} else { | |
console.log('--- before called(registed) emit msg'); | |
process.nextTick(function () { | |
process.emit('msg', 'foo', ++count); | |
}); | |
console.log('--- after called(registed) emit msg'); | |
} | |
}); | |
console.log('--- before called(registed) emit msg'); | |
process.nextTick(function () { | |
process.emit('msg', 'foo', 0/* initial count */); | |
}); | |
console.log('--- after called(registed) emit msg'); | |
// $ node process_emit_nextTick_large.js | |
// --- before called(registed) emit msg | |
// --- after called(registed) emit msg | |
// [ 0 ] foo | |
// --- before called(registed) emit msg | |
// --- after called(registed) emit msg | |
// [ 1 ] foo | |
// --- before called(registed) emit msg | |
// --- after called(registed) emit msg | |
// [ 2 ] foo | |
// --- before called(registed) emit msg | |
// --- after called(registed) emit msg | |
// [ 3 ] foo | |
// --- [ 0 ] on_chuck | |
// --- [ 1 ] on_chuck | |
// --- [ 2 ] on_chuck | |
// $ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('msg', function(message) { | |
console.log(message); | |
}); | |
console.log('--- before called emit msg'); | |
setImmediate(function(){ | |
process.emit('msg', 'foo'); | |
}); | |
console.log('--- after called emit msg'); | |
// $ node process_emit_setImmeiate.js | |
// --- before called emit msg | |
// --- after called emit msg | |
// foo | |
// $ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* initial */ | |
var fs = require('fs'); | |
var largefile = ''; | |
if (process.platform === 'win32') { | |
largefile = 'c:/Program Files/Windows Photo Viewer/ImagingEngine.dll'; | |
} else { | |
largefile = '/var/log/syslog.2.gz'; | |
} | |
var reader = fs.createReadStream(largefile); | |
var times = 2; | |
/* io access */ | |
var count_on = 0; | |
reader.on('data', function (chunk) { | |
console.log('--- [', count_on, ']', 'on_chuck'); | |
count_on++; | |
if (count_on > times) { | |
reader.pause(); | |
} | |
}); | |
/* nextTick loop */ | |
process.on('msg', function (message, count) { | |
console.log('[', count, ']', message); | |
if (count > times) { | |
} else { | |
console.log('--- before called(registed) emit msg'); | |
setImmediate(function () { | |
process.emit('msg', 'foo', ++count); | |
}); | |
console.log('--- after called(registed) emit msg'); | |
} | |
}); | |
console.log('--- before called(registed) emit msg'); | |
setImmediate(function () { | |
process.emit('msg', 'foo', 0/* initial count */); | |
}); | |
console.log('--- after called(registed) emit msg'); | |
// $ node process_emit_setImmediate_large.js | |
// --- before called(registed) emit msg | |
// --- after called(registed) emit msg | |
// [ 0 ] foo | |
// --- before called(registed) emit msg | |
// --- after called(registed) emit msg | |
// [ 1 ] foo | |
// --- before called(registed) emit msg | |
// --- after called(registed) emit msg | |
// --- [ 0 ] on_chuck | |
// [ 2 ] foo | |
// --- before called(registed) emit msg | |
// --- after called(registed) emit msg | |
// --- [ 1 ] on_chuck | |
// [ 3 ] foo | |
// --- [ 2 ] on_chuck | |
// $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment