mirror of
https://github.com/tribufu/node-gamedig
synced 2026-06-01 09:42:41 +00:00
Add support for promises
This commit is contained in:
parent
42df8ed96b
commit
7b9fe2161c
4 changed files with 99 additions and 71 deletions
97
lib/index.js
97
lib/index.js
|
|
@ -29,50 +29,67 @@ udpSocket.on('error', function(e) {
|
|||
Gamedig = {
|
||||
|
||||
query: function(options,callback) {
|
||||
if(callback) options.callback = callback;
|
||||
const promise = new Promise((resolve,reject) => {
|
||||
options.callback = (state) => {
|
||||
if (state.error) reject(state.error);
|
||||
else resolve(state);
|
||||
};
|
||||
|
||||
var query;
|
||||
try {
|
||||
query = TypeResolver.lookup(options.type);
|
||||
} catch(e) {
|
||||
process.nextTick(function() {
|
||||
callback({error:e.message});
|
||||
});
|
||||
return;
|
||||
}
|
||||
query.debug = Gamedig.debug;
|
||||
query.udpSocket = udpSocket;
|
||||
query.type = options.type;
|
||||
|
||||
if(!('port' in query.options) && ('port_query' in query.options)) {
|
||||
if(Gamedig.isCommandLine) {
|
||||
process.stderr.write(
|
||||
"Warning! This game is so old, that we don't know"
|
||||
+" what the server's connection port is. We've guessed that"
|
||||
+" the query port for "+query.type+" is "+query.options.port_query+"."
|
||||
+" If you know the connection port for this type of server, please let"
|
||||
+" us know on the GameDig issue tracker, thanks!\n"
|
||||
);
|
||||
var query;
|
||||
try {
|
||||
query = TypeResolver.lookup(options.type);
|
||||
} catch(e) {
|
||||
process.nextTick(function() {
|
||||
options.callback({error:e.message});
|
||||
});
|
||||
return;
|
||||
}
|
||||
query.debug = Gamedig.debug;
|
||||
query.udpSocket = udpSocket;
|
||||
query.type = options.type;
|
||||
|
||||
if(!('port' in query.options) && ('port_query' in query.options)) {
|
||||
if(Gamedig.isCommandLine) {
|
||||
process.stderr.write(
|
||||
"Warning! This game is so old, that we don't know"
|
||||
+" what the server's connection port is. We've guessed that"
|
||||
+" the query port for "+query.type+" is "+query.options.port_query+"."
|
||||
+" If you know the connection port for this type of server, please let"
|
||||
+" us know on the GameDig issue tracker, thanks!\n"
|
||||
);
|
||||
}
|
||||
query.options.port = query.options.port_query;
|
||||
delete query.options.port_query;
|
||||
}
|
||||
|
||||
// copy over options
|
||||
for(var i in options) query.options[i] = options[i];
|
||||
|
||||
activeQueries.push(query);
|
||||
|
||||
query.on('finished',function(state) {
|
||||
var i = activeQueries.indexOf(query);
|
||||
if(i >= 0) activeQueries.splice(i, 1);
|
||||
});
|
||||
|
||||
process.nextTick(function() {
|
||||
query.start();
|
||||
});
|
||||
});
|
||||
|
||||
if (callback && callback instanceof Function) {
|
||||
if(callback.length == 2) {
|
||||
promise
|
||||
.then((state) => callback(null,state))
|
||||
.catch((error) => callback(error));
|
||||
} else if (callback.length == 1) {
|
||||
promise
|
||||
.then((state) => callback(state))
|
||||
.catch((error) => callback({error:error}));
|
||||
}
|
||||
query.options.port = query.options.port_query;
|
||||
delete query.options.port_query;
|
||||
}
|
||||
|
||||
// copy over options
|
||||
for(var i in options) query.options[i] = options[i];
|
||||
|
||||
activeQueries.push(query);
|
||||
|
||||
query.on('finished',function(state) {
|
||||
var i = activeQueries.indexOf(query);
|
||||
if(i >= 0) activeQueries.splice(i, 1);
|
||||
});
|
||||
|
||||
process.nextTick(function() {
|
||||
query.start();
|
||||
});
|
||||
|
||||
return query;
|
||||
return promise;
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue