More performant way of defining event listeners

This commit is contained in:
AlexKvazos
2015-05-01 12:11:02 -05:00
parent 6ee4de3920
commit 39531b1f99
9 changed files with 36 additions and 26 deletions
+4 -2
View File
@@ -1,8 +1,10 @@
module.exports = (socket) => {
socket.mcbot.on('end', () => {
function onEnd() {
socket.emit('bot:disconnect');
delete socket.mcbot;
});
}
socket.mcbot.on('end', onEnd);
};
+4 -2
View File
@@ -1,6 +1,6 @@
module.exports = (socket) => {
socket.mcbot.on('error', (error) => {
function onError(error) {
// this is okay because the connection was ended
if (error.toString() === 'Error: write after end') return;
@@ -14,6 +14,8 @@ module.exports = (socket) => {
// delete the bot
delete socket.mcbot;
});
}
socket.mcbot.on('error', onError);
};
+4 -2
View File
@@ -1,6 +1,6 @@
module.exports = (socket) => {
socket.mcbot.on('login', () => {
function onLogin() {
socket.emit('buffer:success', `Successfully logged in as ${socket.mcbot.username} with entity id ${socket.mcbot.entity.id}`);
socket.emit('bot:connect', {
host: socket.connectionParams.hostname,
@@ -8,6 +8,8 @@ module.exports = (socket) => {
username: socket.mcbot.username
});
console.log(`logged in > ${socket.connectionParams.hostname}:${socket.connectionParams.port} - Username: ${socket.mcbot.username}`);
});
}
socket.mcbot.on('login', onLogin);
};
+4 -2
View File
@@ -5,7 +5,7 @@ import {escapeHtml} from '../../utils';
module.exports = (socket) => {
socket.mcbot.on('message', (message) => {
function onMessage(message) {
// empty buffer
var buffer = '';
@@ -44,6 +44,8 @@ module.exports = (socket) => {
// send line back to the client
socket.emit('bot:message', buffer);
});
}
socket.mcbot.on('message', onMessage);
};
+4 -2
View File
@@ -1,9 +1,11 @@
module.exports = (socket) => {
socket.mcbot.on('spawn', () => {
function onSpawn() {
var pos = socket.mcbot.entity.position;
socket.emit('buffer:info', `Spawned at X:${pos.x}, Y:${pos.y}, Z:${pos.z}`);
socket.emit('bot:players', socket.mcbot.players);
});
}
socket.mcbot.on('spawn', onSpawn);
};